Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Fixes for Pushserver API types.
Browse files Browse the repository at this point in the history
* It makes no sense to have Pusher be anonymous. It's a part of the
  request, it's not the base of the request.
* The pusher kind is static based on the spec. We can type-check that.
  • Loading branch information
tommie committed Oct 10, 2021
1 parent abce199 commit 5d1df3c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
8 changes: 4 additions & 4 deletions clientapi/routing/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func SetPusher(
if resErr := httputil.UnmarshalJSONRequest(req, &body); resErr != nil {
return *resErr
}
if len(body.AppID) > 64 {
if len(body.Pusher.AppID) > 64 {
return invalidParam("length of app_id must be no more than 64 characters")
}
if len(body.PushKey) > 512 {
if len(body.Pusher.PushKey) > 512 {
return invalidParam("length of pushkey must be no more than 512 bytes")
}
uInt := body.Data["url"]
uInt := body.Pusher.Data["url"]
if uInt != nil {
u, ok := uInt.(string)
if !ok {
Expand All @@ -94,7 +94,7 @@ func SetPusher(

}
body.Localpart = localpart
body.SessionID = device.SessionID
body.Pusher.SessionID = device.SessionID
err = psAPI.PerformPusherSet(req.Context(), &body, &struct{}{})
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("PerformPusherSet failed")
Expand Down
13 changes: 10 additions & 3 deletions pushserver/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type QueryPushersResponse struct {
}

type PerformPusherSetRequest struct {
Pusher
Pusher Pusher
Localpart string
Append bool `json:"append"`
}
Expand All @@ -31,13 +31,20 @@ type PerformPusherDeletionRequest struct {

// Pusher represents a push notification subscriber
type Pusher struct {
SessionID int64 `json:"omitempty"`
SessionID int64 `json:"session_id,omitempty"`
PushKey string `json:"pushkey"`
Kind string `json:"kind"`
Kind PusherKind `json:"kind"`
AppID string `json:"app_id"`
AppDisplayName string `json:"app_display_name"`
DeviceDisplayName string `json:"device_display_name"`
ProfileTag string `json:"profile_tag"`
Language string `json:"lang"`
Data map[string]interface{} `json:"data"`
}

type PusherKind string

const (
EmailKind PusherKind = "email"
HTTPKind PusherKind = "http"
)
10 changes: 5 additions & 5 deletions pushserver/internal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ func NewPushserverAPI(
func (a *PushserverInternalAPI) PerformPusherSet(ctx context.Context, req *api.PerformPusherSetRequest, res *struct{}) error {
util.GetLogger(ctx).WithFields(logrus.Fields{
"localpart": req.Localpart,
"pushkey": req.PushKey,
"display_name": req.AppDisplayName,
"pushkey": req.Pusher.PushKey,
"display_name": req.Pusher.AppDisplayName,
}).Info("PerformPusherCreation")
if !req.Append {
err := a.DB.RemovePushers(ctx, req.AppID, req.AppDisplayName)
err := a.DB.RemovePushers(ctx, req.Pusher.AppID, req.Pusher.AppDisplayName)
if err != nil {
return err
}
} else {
if req.Kind == "" {
return a.DB.RemovePusher(ctx, req.AppID, req.AppDisplayName, req.Localpart)
if req.Pusher.Kind == "" {
return a.DB.RemovePusher(ctx, req.Pusher.AppID, req.Pusher.AppDisplayName, req.Localpart)
}
}
return a.DB.CreatePusher(ctx, req.Pusher, req.Localpart)
Expand Down
2 changes: 1 addition & 1 deletion pushserver/storage/shared/pusher_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func preparePushersTable(db *sql.DB) (tables.Pusher, error) {
// Returns nil error success.
func (s *pushersStatements) InsertPusher(
ctx context.Context, session_id int64,
pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string,
pushkey string, kind api.PusherKind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string,
) error {
_, err := s.insertPusherStmt.ExecContext(ctx, localpart, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data)
logrus.Debugf("Created pusher %d", session_id)
Expand Down
2 changes: 1 addition & 1 deletion pushserver/storage/tables/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Pusher interface {
InsertPusher(
ctx context.Context, session_id int64,
pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string,
pushkey string, kind api.PusherKind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string,
) error
SelectPushers(
ctx context.Context, localpart string,
Expand Down

0 comments on commit 5d1df3c

Please sign in to comment.