mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-31 10:43:10 -06:00
Rename to UpsertPusher for clarity and handle pusher update
This commit is contained in:
parent
bbf4b7066d
commit
1dbcd842b0
|
|
@ -33,7 +33,7 @@ func TestOutputRoomEventConsumer(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("NewDatabase failed: %v", err)
|
t.Fatalf("NewDatabase failed: %v", err)
|
||||||
}
|
}
|
||||||
err = db.CreatePusher(ctx,
|
err = db.UpsertPusher(ctx,
|
||||||
api.Pusher{
|
api.Pusher{
|
||||||
PushKey: "apushkey",
|
PushKey: "apushkey",
|
||||||
Kind: api.HTTPKind,
|
Kind: api.HTTPKind,
|
||||||
|
|
@ -45,7 +45,7 @@ func TestOutputRoomEventConsumer(t *testing.T) {
|
||||||
},
|
},
|
||||||
"alice")
|
"alice")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("CreatePusher failed: %v", err)
|
t.Fatalf("UpsertPusher failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var rsAPI fakeRoomServerInternalAPI
|
var rsAPI fakeRoomServerInternalAPI
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ func (a *PushserverInternalAPI) PerformPusherSet(ctx context.Context, req *api.P
|
||||||
if req.Pusher.PushKeyTS == 0 {
|
if req.Pusher.PushKeyTS == 0 {
|
||||||
req.Pusher.PushKeyTS = gomatrixserverlib.AsTimestamp(time.Now())
|
req.Pusher.PushKeyTS = gomatrixserverlib.AsTimestamp(time.Now())
|
||||||
}
|
}
|
||||||
return a.DB.CreatePusher(ctx, req.Pusher, req.Localpart)
|
return a.DB.UpsertPusher(ctx, req.Pusher, req.Localpart)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *PushserverInternalAPI) PerformPusherDeletion(ctx context.Context, req *api.PerformPusherDeletionRequest, res *struct{}) error {
|
func (a *PushserverInternalAPI) PerformPusherDeletion(ctx context.Context, req *api.PerformPusherDeletionRequest, res *struct{}) error {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
|
|
||||||
type Database interface {
|
type Database interface {
|
||||||
internal.PartitionStorer
|
internal.PartitionStorer
|
||||||
CreatePusher(ctx context.Context, pusher api.Pusher, localpart string) error
|
UpsertPusher(ctx context.Context, pusher api.Pusher, localpart string) error
|
||||||
GetPushers(ctx context.Context, localpart string) ([]api.Pusher, error)
|
GetPushers(ctx context.Context, localpart string) ([]api.Pusher, error)
|
||||||
RemovePusher(ctx context.Context, appId, pushkey, localpart string) error
|
RemovePusher(ctx context.Context, appId, pushkey, localpart string) error
|
||||||
RemovePushers(ctx context.Context, appId, pushkey string) error
|
RemovePushers(ctx context.Context, appId, pushkey string) error
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,9 @@ CREATE UNIQUE INDEX IF NOT EXISTS pusher_app_id_pushkey_localpart_idx ON pushser
|
||||||
`
|
`
|
||||||
|
|
||||||
const insertPusherSQL = "" +
|
const insertPusherSQL = "" +
|
||||||
"INSERT INTO pushserver_pushers (localpart, session_id, pushkey, pushkey_ts_ms, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)"
|
"INSERT INTO pushserver_pushers (localpart, session_id, pushkey, pushkey_ts_ms, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data)" +
|
||||||
|
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)" +
|
||||||
|
"ON CONFLICT (app_id, pushkey, localpart) DO UPDATE SET session_id = $2, pushkey_ts_ms = $4, kind = $5, app_display_name = $7, device_display_name = $8, profile_tag = $9, lang = $10, data = $11"
|
||||||
|
|
||||||
const selectPushersSQL = "" +
|
const selectPushersSQL = "" +
|
||||||
"SELECT session_id, pushkey, pushkey_ts_ms, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data FROM pushserver_pushers WHERE localpart = $1"
|
"SELECT session_id, pushkey, pushkey_ts_ms, kind, app_id, app_display_name, device_display_name, profile_tag, lang, data FROM pushserver_pushers WHERE localpart = $1"
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ func (d *Database) GetRoomNotificationCounts(ctx context.Context, localpart, roo
|
||||||
return d.notifications.SelectRoomCounts(ctx, localpart, roomID)
|
return d.notifications.SelectRoomCounts(ctx, localpart, roomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) CreatePusher(
|
func (d *Database) UpsertPusher(
|
||||||
ctx context.Context, p api.Pusher, localpart string,
|
ctx context.Context, p api.Pusher, localpart string,
|
||||||
) error {
|
) error {
|
||||||
data, err := json.Marshal(p.Data)
|
data, err := json.Marshal(p.Data)
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ import (
|
||||||
|
|
||||||
var testCtx = context.Background()
|
var testCtx = context.Background()
|
||||||
|
|
||||||
var testPushers = []api.Pusher{
|
var (
|
||||||
|
testPushers = []api.Pusher{
|
||||||
{
|
{
|
||||||
SessionID: 42984798792,
|
SessionID: 42984798792,
|
||||||
PushKey: "dc_GxbDa8El0pWKkDIM-rQ:APA91bHflmL6ycJMbLKX8VYLD-Ebft3t-SLQwIap-pDWP-evu1AWxsXxzyl1pgSZxDMn6OeznZsjXhTU0m5xz05dyJ4syX86S89uwxBwtbK-k0PHQt9wF8CgOcibm-OYZodpY5TtmknZ",
|
PushKey: "dc_GxbDa8El0pWKkDIM-rQ:APA91bHflmL6ycJMbLKX8VYLD-Ebft3t-SLQwIap-pDWP-evu1AWxsXxzyl1pgSZxDMn6OeznZsjXhTU0m5xz05dyJ4syX86S89uwxBwtbK-k0PHQt9wF8CgOcibm-OYZodpY5TtmknZ",
|
||||||
|
|
@ -59,27 +60,49 @@ var testPushers = []api.Pusher{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updatePusher = api.Pusher{
|
||||||
|
AppID: "com.example.app.ios",
|
||||||
|
PushKey: "dc_GxbDa8El0pWKkDIM-rQ:APA91bHflmL6ycJMbLKX8VYLD-Ebft3t-SLQwIap-pDWP-evu1AWxsXxzyl1pgSZxDMn6OeznZsjXhTU0m5xz05dyJ4syX86S89uwxBwtbK-k0PHQt9wF8CgOcibm-OYZodpY5TtmknZ",
|
||||||
|
SessionID: 429847987,
|
||||||
|
Kind: "http",
|
||||||
|
AppDisplayName: "Mat Rix 2",
|
||||||
|
DeviceDisplayName: "iPhone 9a",
|
||||||
|
ProfileTag: "xxyyzzaa",
|
||||||
|
Language: "en",
|
||||||
|
Data: map[string]interface{}{
|
||||||
|
"format": "event_id_only",
|
||||||
|
"url": "https://push-gateway.location.here/_matrix/push/v1/notify",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
var testUsers = []string{
|
var testUsers = []string{
|
||||||
"admin",
|
"admin",
|
||||||
"admin",
|
"admin",
|
||||||
"admin0",
|
"admin0",
|
||||||
|
"admin",
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustNewDatabaseWithTestPushers(is *is.I) Database {
|
func mustNewDatabaseWithTestPushers(is *is.I) Database {
|
||||||
|
dut := mustNewDatabase(is)
|
||||||
|
for i, testPusher := range testPushers {
|
||||||
|
err := dut.UpsertPusher(testCtx, testPusher, testUsers[i])
|
||||||
|
is.NoErr(err)
|
||||||
|
}
|
||||||
|
return dut
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustNewDatabase(is *is.I) Database {
|
||||||
randPostfix := strconv.Itoa(rand.Int())
|
randPostfix := strconv.Itoa(rand.Int())
|
||||||
dbPath := os.TempDir() + "/dendrite-" + randPostfix
|
dbPath := os.TempDir() + "/dendrite-" + randPostfix
|
||||||
dut, err := Open(&config.DatabaseOptions{
|
dut, err := Open(&config.DatabaseOptions{
|
||||||
ConnectionString: config.DataSource("file:" + dbPath),
|
ConnectionString: config.DataSource("file:" + dbPath),
|
||||||
})
|
})
|
||||||
is.NoErr(err)
|
is.NoErr(err)
|
||||||
for i, testPusher := range testPushers {
|
|
||||||
err = dut.CreatePusher(testCtx, testPusher, testUsers[i])
|
|
||||||
is.NoErr(err)
|
|
||||||
}
|
|
||||||
return dut
|
return dut
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreatePusher(t *testing.T) {
|
func TestInsertPusher(t *testing.T) {
|
||||||
is := is.New(t)
|
is := is.New(t)
|
||||||
mustNewDatabaseWithTestPushers(is)
|
mustNewDatabaseWithTestPushers(is)
|
||||||
}
|
}
|
||||||
|
|
@ -131,3 +154,18 @@ func TestDeletePushers(t *testing.T) {
|
||||||
is.NoErr(err)
|
is.NoErr(err)
|
||||||
is.Equal(len(pushers), 0)
|
is.Equal(len(pushers), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdatePusher(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
dut := mustNewDatabase(is)
|
||||||
|
err := dut.UpsertPusher(testCtx, testPushers[0], "admin")
|
||||||
|
is.NoErr(err)
|
||||||
|
err = dut.UpsertPusher(testCtx, updatePusher, "admin")
|
||||||
|
is.NoErr(err)
|
||||||
|
pushers, err := dut.GetPushers(testCtx, "admin")
|
||||||
|
is.NoErr(err)
|
||||||
|
is.Equal(len(pushers), 1)
|
||||||
|
t.Log(pushers[0])
|
||||||
|
t.Log(updatePusher)
|
||||||
|
is.Equal(pushers[0], updatePusher)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue