From 5d1df3ce96f0897110dfe219090fa9257019253f Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Sun, 10 Oct 2021 11:50:08 +0200 Subject: [PATCH] Fixes for Pushserver API types. * 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. --- clientapi/routing/pusher.go | 8 ++++---- pushserver/api/api.go | 13 ++++++++++--- pushserver/internal/api.go | 10 +++++----- pushserver/storage/shared/pusher_table.go | 2 +- pushserver/storage/tables/interface.go | 2 +- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/clientapi/routing/pusher.go b/clientapi/routing/pusher.go index 5bebfb0f2f..34c61dc2f5 100644 --- a/clientapi/routing/pusher.go +++ b/clientapi/routing/pusher.go @@ -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 { @@ -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") diff --git a/pushserver/api/api.go b/pushserver/api/api.go index 39ea093014..8624734b38 100644 --- a/pushserver/api/api.go +++ b/pushserver/api/api.go @@ -19,7 +19,7 @@ type QueryPushersResponse struct { } type PerformPusherSetRequest struct { - Pusher + Pusher Pusher Localpart string Append bool `json:"append"` } @@ -31,9 +31,9 @@ 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"` @@ -41,3 +41,10 @@ type Pusher struct { Language string `json:"lang"` Data map[string]interface{} `json:"data"` } + +type PusherKind string + +const ( + EmailKind PusherKind = "email" + HTTPKind PusherKind = "http" +) diff --git a/pushserver/internal/api.go b/pushserver/internal/api.go index 2709dcef66..71c3462a3e 100644 --- a/pushserver/internal/api.go +++ b/pushserver/internal/api.go @@ -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) diff --git a/pushserver/storage/shared/pusher_table.go b/pushserver/storage/shared/pusher_table.go index 0922794504..1467aff25d 100644 --- a/pushserver/storage/shared/pusher_table.go +++ b/pushserver/storage/shared/pusher_table.go @@ -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) diff --git a/pushserver/storage/tables/interface.go b/pushserver/storage/tables/interface.go index 7c03f36891..3b72cef297 100644 --- a/pushserver/storage/tables/interface.go +++ b/pushserver/storage/tables/interface.go @@ -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,