From 092edee210b74bb9b18dff9fe89cff1bcfeb5558 Mon Sep 17 00:00:00 2001 From: Dan Peleg Date: Wed, 5 May 2021 10:03:01 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Test=201=20Can=20fetch=20a=20user's?= =?UTF-8?q?=20pushers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clientapi/routing/pusher.go | 33 +++++++------- userapi/api/api.go | 13 ++---- userapi/internal/api.go | 12 ++++- userapi/storage/pushers/interface.go | 4 +- .../storage/pushers/postgres/pushers_table.go | 44 ++++++++----------- userapi/storage/pushers/postgres/storage.go | 8 ++-- .../storage/pushers/sqlite3/pushers_table.go | 43 ++++++++---------- userapi/storage/pushers/sqlite3/storage.go | 8 ++-- 8 files changed, 74 insertions(+), 91 deletions(-) diff --git a/clientapi/routing/pusher.go b/clientapi/routing/pusher.go index 1739103a..3b20989b 100644 --- a/clientapi/routing/pusher.go +++ b/clientapi/routing/pusher.go @@ -15,6 +15,7 @@ package routing import ( + "encoding/json" "net/http" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -27,25 +28,20 @@ import ( // https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-pushers type pusherJSON struct { - PushKey string `json:"pushkey"` - Kind string `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 pusherDataJSON `json:"data"` + PushKey string `json:"pushkey"` + Kind string `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]json.RawMessage `json:"data"` } type pushersJSON struct { Pushers []pusherJSON `json:"pushers"` } -type pusherDataJSON struct { - URL string `json:"url"` - Format string `json:"format"` -} - // GetPushersByLocalpart handles /_matrix/client/r0/pushers func GetPushersByLocalpart( req *http.Request, userAPI userapi.UserInternalAPI, device *api.Device, @@ -63,7 +59,10 @@ func GetPushersByLocalpart( Pushers: []pusherJSON{}, } + //data := map[string]json.RawMessage; + var data map[string]json.RawMessage for _, pusher := range queryRes.Pushers { + json.Unmarshal([]byte(pusher.Data), &data) res.Pushers = append(res.Pushers, pusherJSON{ PushKey: pusher.PushKey, Kind: pusher.Kind, @@ -72,7 +71,7 @@ func GetPushersByLocalpart( DeviceDisplayName: pusher.DeviceDisplayName, ProfileTag: pusher.ProfileTag, Language: pusher.Language, - Data: pusherDataJSON(pusher.Data), + Data: data, }) } @@ -127,8 +126,7 @@ func SetPusherByLocalpart( DeviceDisplayName: body.DeviceDisplayName, ProfileTag: body.ProfileTag, Language: body.Language, - URL: body.Data.URL, - Format: body.Data.Format, + Data: body.Data, }, &pusherResponse) if err != nil { @@ -164,8 +162,7 @@ func SetPusherByLocalpart( DeviceDisplayName: body.DeviceDisplayName, ProfileTag: body.ProfileTag, Language: body.Language, - URL: body.Data.URL, - Format: body.Data.Format, + Data: body.Data, }, &pusherResponse) if err != nil { diff --git a/userapi/api/api.go b/userapi/api/api.go index 74e690d0..7896d0fa 100644 --- a/userapi/api/api.go +++ b/userapi/api/api.go @@ -252,8 +252,7 @@ type PerformPusherCreationRequest struct { DeviceDisplayName string ProfileTag string Language string - URL string - Format string + Data map[string]json.RawMessage } // PerformPusherCreationResponse is the response for PerformPusherCreation @@ -270,8 +269,7 @@ type PerformPusherUpdateRequest struct { DeviceDisplayName string ProfileTag string Language string - URL string - Format string + Data map[string]json.RawMessage } // PerformPusherUpdateResponse is the response for PerformPusherUpdate @@ -340,12 +338,7 @@ type Pusher struct { DeviceDisplayName string ProfileTag string Language string - Data PusherData -} - -type PusherData struct { - URL string - Format string + Data string } // Account represents a Matrix account on this home server. diff --git a/userapi/internal/api.go b/userapi/internal/api.go index 59c38269..c25135d6 100644 --- a/userapi/internal/api.go +++ b/userapi/internal/api.go @@ -161,7 +161,11 @@ func (a *UserInternalAPI) PerformPusherCreation(ctx context.Context, req *api.Pe if err != nil { return err } - err = a.PusherDB.CreatePusher(ctx, req.Device.SessionID, req.PushKey, req.Kind, req.AppID, req.AppDisplayName, req.DeviceDisplayName, req.ProfileTag, req.Language, req.URL, req.Format, local) + jsonData, err := json.Marshal(req.Data) + if err != nil { + return err + } + err = a.PusherDB.CreatePusher(ctx, req.Device.SessionID, req.PushKey, req.Kind, req.AppID, req.AppDisplayName, req.DeviceDisplayName, req.ProfileTag, req.Language, string(jsonData), local) return err } @@ -175,7 +179,11 @@ func (a *UserInternalAPI) PerformPusherUpdate(ctx context.Context, req *api.Perf if err != nil { return err } - err = a.PusherDB.UpdatePusher(ctx, req.PushKey, req.Kind, req.AppID, req.AppDisplayName, req.DeviceDisplayName, req.ProfileTag, req.Language, req.URL, req.Format, local) + jsonData, err := json.Marshal(req.Data) + if err != nil { + return err + } + err = a.PusherDB.UpdatePusher(ctx, req.PushKey, req.Kind, req.AppID, req.AppDisplayName, req.DeviceDisplayName, req.ProfileTag, req.Language, string(jsonData), local) return err } diff --git a/userapi/storage/pushers/interface.go b/userapi/storage/pushers/interface.go index 842699d3..a9f49bf9 100644 --- a/userapi/storage/pushers/interface.go +++ b/userapi/storage/pushers/interface.go @@ -21,9 +21,9 @@ import ( ) type Database interface { - CreatePusher(ctx context.Context, sessionId int64, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string) error + CreatePusher(ctx context.Context, sessionId int64, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string) error GetPushersByLocalpart(ctx context.Context, localpart string) ([]api.Pusher, error) GetPusherByPushkey(ctx context.Context, pushkey, localpart string) (*api.Pusher, error) - UpdatePusher(ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string) error + UpdatePusher(ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string) error RemovePusher(ctx context.Context, appId, pushkey, localpart string) error } diff --git a/userapi/storage/pushers/postgres/pushers_table.go b/userapi/storage/pushers/postgres/pushers_table.go index 574658ac..c7e8148a 100644 --- a/userapi/storage/pushers/postgres/pushers_table.go +++ b/userapi/storage/pushers/postgres/pushers_table.go @@ -52,10 +52,8 @@ CREATE TABLE IF NOT EXISTS pusher_pushers ( pushkey VARCHAR(512) NOT NULL, -- The preferred language for receiving notifications (e.g. 'en' or 'en-US') lang TEXT, - -- Required if kind is http. The URL to use to send notifications to. - url TEXT, - -- The format to use when sending notifications to the Push Gateway. - format TEXT + -- A dictionary of information for the pusher implementation itself. + data TEXT ); -- Pushkey must be unique for a given user. @@ -63,16 +61,16 @@ CREATE UNIQUE INDEX IF NOT EXISTS pusher_app_id_pushkey_localpart_idx ON pusher_ ` const insertPusherSQL = "" + - "INSERT INTO pusher_pushers(localpart, session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)" + "INSERT INTO pusher_pushers(localpart, session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)" const selectPushersByLocalpartSQL = "" + - "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1" + "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data FROM pusher_pushers WHERE localpart = $1" const selectPusherByPushkeySQL = "" + - "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2" + "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2" const updatePusherSQL = "" + - "UPDATE pusher_pushers SET kind = $1, app_id = $2, app_display_name = $3, device_display_name = $4, profile_tag = $5, lang = $6, url = $7, format = $8 WHERE localpart = $9 AND pushkey = $10" + "UPDATE pusher_pushers SET kind = $1, app_id = $2, app_display_name = $3, device_display_name = $4, profile_tag = $5, lang = $6, data = $7 WHERE localpart = $8 AND pushkey = $9" const deletePusherSQL = "" + "DELETE FROM pusher_pushers WHERE app_id = $1 AND pushkey = $2 AND localpart = $3" @@ -116,10 +114,10 @@ func (s *pushersStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerN // Returns nil error success. func (s *pushersStatements) insertPusher( ctx context.Context, txn *sql.Tx, session_id int64, - pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { stmt := sqlutil.TxStmt(txn, s.insertPusherStmt) - _, err := stmt.ExecContext(ctx, localpart, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format) + _, err := stmt.ExecContext(ctx, localpart, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data) logrus.Debugf("🥳 Created pusher %d", session_id) return err } @@ -138,8 +136,8 @@ func (s *pushersStatements) selectPushersByLocalpart( for rows.Next() { var pusher api.Pusher var sessionid sql.NullInt64 - var pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format sql.NullString - err = rows.Scan(&sessionid, &pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format) + var pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data sql.NullString + err = rows.Scan(&sessionid, &pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &data) if err != nil { return pushers, err } @@ -167,11 +165,8 @@ func (s *pushersStatements) selectPushersByLocalpart( if lang.Valid { pusher.Language = lang.String } - if url.Valid && format.Valid { - pusher.Data = api.PusherData{ - URL: url.String, - Format: format.String, - } + if data.Valid { + pusher.Data = data.String } pusher.UserID = userutil.MakeUserID(localpart, s.serverName) @@ -187,10 +182,10 @@ func (s *pushersStatements) selectPusherByPushkey( ) (*api.Pusher, error) { var pusher api.Pusher var sessionid sql.NullInt64 - var key, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format sql.NullString + var key, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data sql.NullString stmt := s.selectPusherByPushkeyStmt - err := stmt.QueryRowContext(ctx, localpart, pushkey).Scan(&sessionid, &key, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format) + err := stmt.QueryRowContext(ctx, localpart, pushkey).Scan(&sessionid, &key, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &data) if err == nil { if sessionid.Valid { @@ -217,11 +212,8 @@ func (s *pushersStatements) selectPusherByPushkey( if lang.Valid { pusher.Language = lang.String } - if url.Valid && format.Valid { - pusher.Data = api.PusherData{ - URL: url.String, - Format: format.String, - } + if data.Valid { + pusher.Data = data.String } pusher.UserID = userutil.MakeUserID(localpart, s.serverName) @@ -231,10 +223,10 @@ func (s *pushersStatements) selectPusherByPushkey( } func (s *pushersStatements) updatePusher( - ctx context.Context, txn *sql.Tx, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + ctx context.Context, txn *sql.Tx, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { stmt := sqlutil.TxStmt(txn, s.updatePusherStmt) - _, err := stmt.ExecContext(ctx, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart, pushkey) + _, err := stmt.ExecContext(ctx, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart, pushkey) return err } diff --git a/userapi/storage/pushers/postgres/storage.go b/userapi/storage/pushers/postgres/storage.go index 84bf7438..dd9ad8a2 100644 --- a/userapi/storage/pushers/postgres/storage.go +++ b/userapi/storage/pushers/postgres/storage.go @@ -58,9 +58,9 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver func (d *Database) CreatePusher( ctx context.Context, session_id int64, - pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { - return d.pushers.insertPusher(ctx, nil, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart) + return d.pushers.insertPusher(ctx, nil, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart) } // GetPushersByLocalpart returns the pushers matching the given localpart. @@ -80,10 +80,10 @@ func (d *Database) GetPusherByPushkey( // UpdatePusher updates the given pusher with the display name. // Returns SQL error if there are problems and nil on success. func (d *Database) UpdatePusher( - ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error { - return d.pushers.updatePusher(ctx, txn, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart) + return d.pushers.updatePusher(ctx, txn, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart) }) } diff --git a/userapi/storage/pushers/sqlite3/pushers_table.go b/userapi/storage/pushers/sqlite3/pushers_table.go index 7d709b3f..1d708dfc 100644 --- a/userapi/storage/pushers/sqlite3/pushers_table.go +++ b/userapi/storage/pushers/sqlite3/pushers_table.go @@ -38,24 +38,23 @@ CREATE TABLE IF NOT EXISTS pusher_pushers ( device_display_name TEXT, pushkey VARCHAR(512), lang TEXT, - url TEXT, - format TEXT, + data TEXT, UNIQUE (app_id, pushkey, localpart) ); ` const insertPusherSQL = "" + - "INSERT INTO pusher_pushers (localpart, session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format)" + - " VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)" + "INSERT INTO pusher_pushers (localpart, session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data)" + + " VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)" const selectPushersByLocalpartSQL = "" + - "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1" + "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data FROM pusher_pushers WHERE localpart = $1" const selectPusherByPushkeySQL = "" + - "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2" + "SELECT session_id, pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2" const updatePusherSQL = "" + - "UPDATE pusher_pushers SET kind = $1, app_id = $2, app_display_name = $3, device_display_name = $4, profile_tag = $5, lang = $6, url = $7, format = $8 WHERE localpart = $9 AND pushkey = $10" + "UPDATE pusher_pushers SET kind = $1, app_id = $2, app_display_name = $3, device_display_name = $4, profile_tag = $5, lang = $6, data = $7 WHERE localpart = $8 AND pushkey = $9" const deletePusherSQL = "" + "DELETE FROM pusher_pushers WHERE app_id = $1 AND pushkey = $2 AND localpart = $3" @@ -103,10 +102,10 @@ func (s *pushersStatements) prepare(db *sql.DB, writer sqlutil.Writer, server go // Returns nil error success. func (s *pushersStatements) insertPusher( ctx context.Context, txn *sql.Tx, session_id int64, - pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { stmt := sqlutil.TxStmt(txn, s.insertPusherStmt) - _, err := stmt.ExecContext(ctx, localpart, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format) + _, err := stmt.ExecContext(ctx, localpart, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data) logrus.Debugf("🥳 Created pusher %d", session_id) return err } @@ -125,8 +124,8 @@ func (s *pushersStatements) selectPushersByLocalpart( logrus.Debug("Next pusher row...") var pusher api.Pusher var sessionid sql.NullInt64 - var pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format sql.NullString - err = rows.Scan(&sessionid, &pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format) + var pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data sql.NullString + err = rows.Scan(&sessionid, &pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &data) if err != nil { return pushers, err } @@ -154,11 +153,8 @@ func (s *pushersStatements) selectPushersByLocalpart( if lang.Valid { pusher.Language = lang.String } - if url.Valid && format.Valid { - pusher.Data = api.PusherData{ - URL: url.String, - Format: format.String, - } + if data.Valid { + pusher.Data = data.String } pusher.UserID = userutil.MakeUserID(localpart, s.serverName) @@ -175,9 +171,9 @@ func (s *pushersStatements) selectPusherByPushkey( ctx context.Context, localpart, pushkey string, ) (*api.Pusher, error) { var pusher api.Pusher - var id, key, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format sql.NullString + var id, key, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data sql.NullString stmt := s.selectPusherByPushkeyStmt - err := stmt.QueryRowContext(ctx, localpart, pushkey).Scan(&id, &key, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &url, &format) + err := stmt.QueryRowContext(ctx, localpart, pushkey).Scan(&id, &key, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &lang, &data) if err == nil { pusher.UserID = userutil.MakeUserID(localpart, s.serverName) if key.Valid { @@ -201,21 +197,18 @@ func (s *pushersStatements) selectPusherByPushkey( if lang.Valid { pusher.Language = lang.String } - if url.Valid && format.Valid { - pusher.Data = api.PusherData{ - URL: url.String, - Format: format.String, - } + if data.Valid { + pusher.Data = data.String } } return &pusher, err } func (s *pushersStatements) updatePusher( - ctx context.Context, txn *sql.Tx, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + ctx context.Context, txn *sql.Tx, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { stmt := sqlutil.TxStmt(txn, s.updatePusherStmt) - _, err := stmt.ExecContext(ctx, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart, pushkey) + _, err := stmt.ExecContext(ctx, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart, pushkey) return err } diff --git a/userapi/storage/pushers/sqlite3/storage.go b/userapi/storage/pushers/sqlite3/storage.go index 034b1424..2b613ba3 100644 --- a/userapi/storage/pushers/sqlite3/storage.go +++ b/userapi/storage/pushers/sqlite3/storage.go @@ -60,9 +60,9 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver func (d *Database) CreatePusher( ctx context.Context, session_id int64, - pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { - return d.pushers.insertPusher(ctx, nil, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart) + return d.pushers.insertPusher(ctx, nil, session_id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart) } // GetPushersByLocalpart returns the pushers matching the given localpart. @@ -82,10 +82,10 @@ func (d *Database) GetPusherByPushkey( // UpdatePusher updates the given device with the display name. // Returns SQL error if there are problems and nil on success. func (d *Database) UpdatePusher( - ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart string, + ctx context.Context, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart string, ) error { return d.writer.Do(d.db, nil, func(txn *sql.Tx) error { - return d.pushers.updatePusher(ctx, txn, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, url, format, localpart) + return d.pushers.updatePusher(ctx, txn, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, lang, data, localpart) }) }