🗑 Implemented Delete Pusher

This commit is contained in:
Dan Peleg 2021-05-02 17:10:40 +03:00
parent 784aba53b2
commit 024afaba7c
10 changed files with 122 additions and 9 deletions

View file

@ -22,4 +22,6 @@ import (
type Database interface {
GetPushersByLocalpart(ctx context.Context, localpart string) ([]api.Pusher, error)
GetPusherByPushkey(ctx context.Context, pushkey, localpart string) (*api.Pusher, error)
RemovePusher(ctx context.Context, pushkey, localpart string) error
}

View file

@ -64,9 +64,12 @@ const selectPushersByLocalpartSQL = "" +
const selectPusherByPushkeySQL = "" +
"SELECT pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2"
const deletePusherSQL = "" +
"DELETE FROM pusher_pushers WHERE pushkey = $1 AND localpart = $2"
type pushersStatements struct {
selectPushersByLocalpartStmt *sql.Stmt
selectPusherByPushkeyStmt *sql.Stmt
deletePusherStmt *sql.Stmt
serverName gomatrixserverlib.ServerName
}
@ -82,10 +85,22 @@ func (s *pushersStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerN
if s.selectPusherByPushkeyStmt, err = db.Prepare(selectPusherByPushkeySQL); err != nil {
return
}
if s.deletePusherStmt, err = db.Prepare(deletePusherSQL); err != nil {
return
}
s.serverName = server
return
}
// deletePusher removes a single pusher by pushkey and user localpart.
func (s *pushersStatements) deletePusher(
ctx context.Context, txn *sql.Tx, pushkey, localpart string,
) error {
stmt := sqlutil.TxStmt(txn, s.deletePusherStmt)
_, err := stmt.ExecContext(ctx, pushkey, localpart)
return err
}
func (s *pushersStatements) selectPushersByLocalpart(
ctx context.Context, txn *sql.Tx, localpart string,
) ([]api.Pusher, error) {

View file

@ -55,16 +55,30 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
return &Database{db, d}, nil
}
// GetPushersByLocalpart returns the pusers matching the given localpart.
func (d *Database) GetPushersByLocalpart(
ctx context.Context, localpart string,
) ([]api.Pusher, error) {
return d.pushers.selectPushersByLocalpart(ctx, nil, localpart)
}
// GetPushersByPushkey returns the pusers matching the given localpart.
func (d *Database) GetPushersByPushkey(
ctx context.Context, localpart, pushkey string,
// GetPusherByPushkey returns the pusher matching the given localpart.
func (d *Database) GetPusherByPushkey(
ctx context.Context, pushkey, localpart string,
) (*api.Pusher, error) {
return d.pushers.selectPushersByPushkey(ctx, localpart, pushkey)
return d.pushers.selectPusherByPushkey(ctx, localpart, pushkey)
}
// RemovePusher revokes a pusher by deleting the entry in the database
// matching with the given pushkey and user ID localpart.
// If the pusher doesn't exist, it will not return an error
// If something went wrong during the deletion, it will return the SQL error.
func (d *Database) RemovePusher(
ctx context.Context, pushkey, localpart string,
) error {
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
if err := d.pushers.deletePusher(ctx, txn, pushkey, localpart); err != sql.ErrNoRows {
return err
}
return nil
})
}

View file

@ -48,11 +48,15 @@ const selectPushersByLocalpartSQL = "" +
const selectPusherByPushkeySQL = "" +
"SELECT pushkey, kind, app_id, app_display_name, device_display_name, profile_tag, lang, url, format FROM pusher_pushers WHERE localpart = $1 AND pushkey = $2"
const deletePusherSQL = "" +
"DELETE FROM pusher_pushers WHERE pushkey = $1 AND localpart = $2"
type pushersStatements struct {
db *sql.DB
writer sqlutil.Writer
selectPushersByLocalpartStmt *sql.Stmt
selectPusherByPushkeyStmt *sql.Stmt
deletePusherStmt *sql.Stmt
serverName gomatrixserverlib.ServerName
}
@ -70,6 +74,9 @@ func (s *pushersStatements) prepare(db *sql.DB, writer sqlutil.Writer, server go
if s.selectPusherByPushkeyStmt, err = db.Prepare(selectPusherByPushkeySQL); err != nil {
return
}
if s.deletePusherStmt, err = db.Prepare(deletePusherSQL); err != nil {
return
}
s.serverName = server
return
}
@ -167,3 +174,11 @@ func (s *pushersStatements) selectPusherByPushkey(
}
return &pusher, err
}
func (s *pushersStatements) deletePusher(
ctx context.Context, txn *sql.Tx, id, localpart string,
) error {
stmt := sqlutil.TxStmt(txn, s.deletePusherStmt)
_, err := stmt.ExecContext(ctx, id, localpart)
return err
}

View file

@ -65,8 +65,23 @@ func (d *Database) GetPushersByLocalpart(
}
// GetPushersByLocalpart returns the pushers matching the given localpart.
func (d *Database) GetPushersByPushkey(
ctx context.Context, localpart, pushkey string,
func (d *Database) GetPusherByPushkey(
ctx context.Context, pushkey, localpart string,
) (*api.Pusher, error) {
return d.pushers.selectPusherByPushkey(ctx, localpart, pushkey)
return d.pushers.selectPusherByPushkey(ctx, pushkey, localpart)
}
// RemovePusher revokes a pusher by deleting the entry in the database
// matching with the given pusher pushkey and user ID localpart.
// If the pusher doesn't exist, it will not return an error
// If something went wrong during the deletion, it will return the SQL error.
func (d *Database) RemovePusher(
ctx context.Context, pushkey, localpart string,
) error {
return d.writer.Do(d.db, nil, func(txn *sql.Tx) error {
if err := d.pushers.deletePusher(ctx, txn, pushkey, localpart); err != sql.ErrNoRows {
return err
}
return nil
})
}