Test 1 Can fetch a user's pushers

This commit is contained in:
Dan Peleg 2021-05-05 10:03:01 +03:00
parent c5247d390f
commit 092edee210
8 changed files with 74 additions and 91 deletions

View file

@ -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 {

View file

@ -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.

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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)
})
}

View file

@ -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
}

View file

@ -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)
})
}