GET /_matrix/client/r0/pushers (🐘postgres + sqlite³)

This commit is contained in:
Dan Peleg 2021-04-24 00:02:00 +03:00
parent 2c9ec8c1ab
commit 40baacf020
7 changed files with 80 additions and 48 deletions

View file

@ -17,6 +17,7 @@ package routing
import ( import (
"net/http" "net/http"
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/api"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
@ -25,7 +26,6 @@ import (
// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-pushers // https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-pushers
type pusherJSON struct { type pusherJSON struct {
PusherID string `json:"pusher_id"`
PushKey string `json:"pushkey"` PushKey string `json:"pushkey"`
Kind string `json:"kind"` Kind string `json:"kind"`
AppID string `json:"app_id"` AppID string `json:"app_id"`
@ -45,13 +45,13 @@ type pusherDataJSON struct {
Format string `json:"format"` Format string `json:"format"`
} }
// GetPushersByLocalpart handles /pushers // GetPushersByLocalpart handles /_matrix/client/r0/pushers
func GetPushersByLocalpart( func GetPushersByLocalpart(
req *http.Request, userAPI userapi.UserInternalAPI, pusher *api.Pusher, req *http.Request, userAPI userapi.UserInternalAPI, device *api.Device,
) util.JSONResponse { ) util.JSONResponse {
var queryRes userapi.QueryPushersResponse var queryRes userapi.QueryPushersResponse
err := userAPI.QueryPushers(req.Context(), &userapi.QueryPushersRequest{ err := userAPI.QueryPushers(req.Context(), &userapi.QueryPushersRequest{
UserID: pusher.UserID, UserID: device.UserID,
}, &queryRes) }, &queryRes)
if err != nil { if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("QueryPushers failed") util.GetLogger(req.Context()).WithError(err).Error("QueryPushers failed")
@ -62,7 +62,6 @@ func GetPushersByLocalpart(
for _, pusher := range queryRes.Pushers { for _, pusher := range queryRes.Pushers {
res.Pushers = append(res.Pushers, pusherJSON{ res.Pushers = append(res.Pushers, pusherJSON{
PusherID: pusher.ID,
PushKey: pusher.PushKey, PushKey: pusher.PushKey,
Kind: pusher.Kind, Kind: pusher.Kind,
AppID: pusher.AppID, AppID: pusher.AppID,

View file

@ -803,6 +803,11 @@ func Setup(
}), }),
).Methods(http.MethodPost, http.MethodOptions) ).Methods(http.MethodPost, http.MethodOptions)
r0mux.Handle("/pushers",
httputil.MakeAuthAPI("get_pushers", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
return GetPushersByLocalpart(req, userAPI, device)
}),
).Methods(http.MethodGet, http.MethodOptions)
// Stub implementations for sytest // Stub implementations for sytest
r0mux.Handle("/events", r0mux.Handle("/events",
httputil.MakeExternalAPI("events", func(req *http.Request) util.JSONResponse { httputil.MakeExternalAPI("events", func(req *http.Request) util.JSONResponse {

View file

@ -283,7 +283,6 @@ type Device struct {
// Pusher represents a push notification subscriber // Pusher represents a push notification subscriber
type Pusher struct { type Pusher struct {
ID string
UserID string UserID string
PushKey string PushKey string
Kind string Kind string

View file

@ -30,28 +30,32 @@ const pushersSchema = `
CREATE TABLE IF NOT EXISTS pusher_pushers ( CREATE TABLE IF NOT EXISTS pusher_pushers (
-- The Matrix user ID localpart for this pusher -- The Matrix user ID localpart for this pusher
localpart TEXT NOT NULL PRIMARY KEY, localpart TEXT NOT NULL PRIMARY KEY,
-- The push key for this pusher -- This is a unique identifier for this pusher. See /set for more detail. Max length, 512 bytes.
pushkey TEXT, pushkey VARCHAR(512) NOT NULL,
-- The pusher kind -- The kind of pusher. "http" is a pusher that sends HTTP pokes.
kind TEXT, kind TEXT,
-- The pusher Application ID -- This is a reverse-DNS style identifier for the application. Max length, 64 chars.
app_id TEXT, app_id VARCHAR(64),
-- The pusher application display name, human friendlier than app_id and updatable -- A string that will allow the user to identify what application owns this pusher.
app_display_name TEXT, app_display_name TEXT,
-- The pusher device display name, -- A string that will allow the user to identify what device owns this pusher.
device_display_name TEXT, device_display_name TEXT,
-- The pusher profile tag, -- This string determines which set of device specific rules this pusher executes.
profile_tag TEXT, profile_tag TEXT,
-- The pusher preferred language, -- The preferred language for receiving notifications (e.g. 'en' or 'en-US')
language TEXT, 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,
); );
-- Pusher IDs must be unique for a given user. -- Pusher IDs must be unique for a given user.
CREATE UNIQUE INDEX IF NOT EXISTS pusher_localpart_id_idx ON pusher_pushers(localpart, pusher_id); CREATE UNIQUE INDEX IF NOT EXISTS pusher_localpart_pushkey_idx ON pusher_pushers(localpart, pushkey);
` `
const selectPushersByLocalpartSQL = "" + const selectPushersByLocalpartSQL = "" +
"SELECT pusher_id, display_name, last_seen_ts, ip, user_agent FROM pusher_pushers WHERE localpart = $1 AND pusher_id != $2" "SELECT pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, language FROM pusher_pushers WHERE localpart = $1"
type pushersStatements struct { type pushersStatements struct {
selectPushersByLocalpartStmt *sql.Stmt selectPushersByLocalpartStmt *sql.Stmt
@ -67,15 +71,18 @@ func (s *pushersStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerN
if s.selectPushersByLocalpartStmt, err = db.Prepare(selectPushersByLocalpartSQL); err != nil { if s.selectPushersByLocalpartStmt, err = db.Prepare(selectPushersByLocalpartSQL); err != nil {
return return
} }
if s.selectPushersByPushkeyStmt, err = db.Prepare(selectPushersByPushkeySQL); err != nil {
return
}
s.serverName = server s.serverName = server
return return
} }
func (s *pushersStatements) selectPushersByLocalpart( func (s *pushersStatements) selectPushersByLocalpart(
ctx context.Context, txn *sql.Tx, localpart, exceptPusherID string, ctx context.Context, txn *sql.Tx, localpart string,
) ([]api.Pusher, error) { ) ([]api.Pusher, error) {
pushers := []api.Pusher{} pushers := []api.Pusher{}
rows, err := sqlutil.TxStmt(txn, s.selectPushersByLocalpartStmt).QueryContext(ctx, localpart, exceptPusherID) rows, err := sqlutil.TxStmt(txn, s.selectPushersByLocalpartStmt).QueryContext(ctx, localpart)
if err != nil { if err != nil {
return pushers, err return pushers, err
@ -84,14 +91,11 @@ func (s *pushersStatements) selectPushersByLocalpart(
for rows.Next() { for rows.Next() {
var pusher api.Pusher var pusher api.Pusher
var id, pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, language, url, format sql.NullString var pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, language, url, format sql.NullString
err = rows.Scan(&id, &pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &language, &url, &format) err = rows.Scan(&pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &language, &url, &format)
if err != nil { if err != nil {
return pushers, err return pushers, err
} }
if id.Valid {
pusher.ID = id.String
}
if pushkey.Valid { if pushkey.Valid {
pusher.PushKey = pushkey.String pusher.PushKey = pushkey.String
} }

View file

@ -59,5 +59,6 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
func (d *Database) GetPushersByLocalpart( func (d *Database) GetPushersByLocalpart(
ctx context.Context, localpart string, ctx context.Context, localpart string,
) ([]api.Pusher, error) { ) ([]api.Pusher, error) {
return d.pushers.selectPushersByLocalpart(ctx, nil, localpart, "") return d.pushers.selectPushersByLocalpart(ctx, nil, localpart)
}
} }

View file

@ -31,21 +31,21 @@ const pushersSchema = `
-- Stores data about pushers. -- Stores data about pushers.
CREATE TABLE IF NOT EXISTS pusher_pushers ( CREATE TABLE IF NOT EXISTS pusher_pushers (
access_token TEXT PRIMARY KEY, pushkey VARCHAR(512) PRIMARY KEY,
session_id INTEGER, kind TEXT ,
pusher_id TEXT , app_id VARCHAR(64) ,
localpart TEXT , app_display_name TEXT,
created_ts BIGINT, device_display_name TEXT,
display_name TEXT, profile_tag TEXT,
last_seen_ts BIGINT, language TEXT,
ip TEXT, url TEXT,
user_agent TEXT, format TEXT,
UNIQUE (localpart, pusher_id) UNIQUE (localpart, pushkey)
); );
` `
const selectPushersByLocalpartSQL = "" + const selectPushersByLocalpartSQL = "" +
"SELECT pusher_id, display_name, last_seen_ts, ip, user_agent FROM pusher_pushers WHERE localpart = $1 AND pusher_id != $2" "SELECT pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, language FROM pusher_pushers WHERE localpart = $1"
type pushersStatements struct { type pushersStatements struct {
db *sql.DB db *sql.DB
@ -70,29 +70,52 @@ func (s *pushersStatements) prepare(db *sql.DB, writer sqlutil.Writer, server go
} }
func (s *pushersStatements) selectPushersByLocalpart( func (s *pushersStatements) selectPushersByLocalpart(
ctx context.Context, txn *sql.Tx, localpart, exceptPusherID string, ctx context.Context, txn *sql.Tx, localpart string,
) ([]api.Pusher, error) { ) ([]api.Pusher, error) {
pushers := []api.Pusher{} pushers := []api.Pusher{}
rows, err := sqlutil.TxStmt(txn, s.selectPushersByLocalpartStmt).QueryContext(ctx, localpart, exceptPusherID) rows, err := sqlutil.TxStmt(txn, s.selectPushersByLocalpartStmt).QueryContext(ctx, localpart)
if err != nil { if err != nil {
return pushers, err return pushers, err
} }
for rows.Next() { for rows.Next() {
var dev api.Pusher var pusher api.Pusher
var lastseents sql.NullInt64 var pushkey, kind, appid, appdisplayname, devicedisplayname, profiletag, language, url, format sql.NullString
var id, displayname, ip, useragent sql.NullString err = rows.Scan(&pushkey, &kind, &appid, &appdisplayname, &devicedisplayname, &profiletag, &language, &url, &format)
err = rows.Scan(&id, &displayname, &lastseents, &ip, &useragent)
if err != nil { if err != nil {
return pushers, err return pushers, err
} }
if id.Valid { if pushkey.Valid {
dev.ID = id.String pusher.PushKey = pushkey.String
}
if kind.Valid {
pusher.Kind = kind.String
}
if appid.Valid {
pusher.AppID = appid.String
}
if appdisplayname.Valid {
pusher.AppDisplayName = appdisplayname.String
}
if devicedisplayname.Valid {
pusher.DeviceDisplayName = devicedisplayname.String
}
if profiletag.Valid {
pusher.ProfileTag = profiletag.String
}
if language.Valid {
pusher.Language = language.String
}
if url.Valid && format.Valid {
pusher.Data = api.PusherData{
URL: url.String,
Format: format.String,
}
} }
dev.UserID = userutil.MakeUserID(localpart, s.serverName) pusher.UserID = userutil.MakeUserID(localpart, s.serverName)
pushers = append(pushers, dev) pushers = append(pushers, pusher)
} }
return pushers, nil return pushers, nil

View file

@ -61,5 +61,6 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
func (d *Database) GetPushersByLocalpart( func (d *Database) GetPushersByLocalpart(
ctx context.Context, localpart string, ctx context.Context, localpart string,
) ([]api.Pusher, error) { ) ([]api.Pusher, error) {
return d.pushers.selectPushersByLocalpart(ctx, nil, localpart, "") return d.pushers.selectPushersByLocalpart(ctx, nil, localpart)
}
} }