Allow setting a global mute that does not overwrite the other settings

This commit is contained in:
Andre Medeiros 2022-03-26 12:39:28 -04:00
parent 84b5959aa2
commit 9cc60972dc
4 changed files with 23 additions and 18 deletions

View file

@ -17,6 +17,7 @@ import (
type accountNotificationsRequest struct { type accountNotificationsRequest struct {
InboxNotifications bool `json:"inbox_notifications"` InboxNotifications bool `json:"inbox_notifications"`
WatcherNotifications bool `json:"watcher_notifications"` WatcherNotifications bool `json:"watcher_notifications"`
GlobalMute bool `json:"global_mute"`
} }
func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) { func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) {
@ -44,7 +45,7 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request
return return
} }
if err := a.deviceRepo.SetNotifiable(ctx, &dev, &acct, anr.InboxNotifications, anr.WatcherNotifications); err != nil { if err := a.deviceRepo.SetNotifiable(ctx, &dev, &acct, anr.InboxNotifications, anr.WatcherNotifications, anr.GlobalMute); err != nil {
a.errorResponse(w, r, 500, err.Error()) a.errorResponse(w, r, 500, err.Error())
return return
} }
@ -71,7 +72,7 @@ func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Requ
return return
} }
inbox, watchers, err := a.deviceRepo.GetNotifiable(ctx, &dev, &acct) inbox, watchers, global, err := a.deviceRepo.GetNotifiable(ctx, &dev, &acct)
if err != nil { if err != nil {
a.errorResponse(w, r, 500, err.Error()) a.errorResponse(w, r, 500, err.Error())
return return
@ -79,7 +80,7 @@ func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Requ
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
an := &accountNotificationsRequest{InboxNotifications: inbox, WatcherNotifications: watchers} an := &accountNotificationsRequest{InboxNotifications: inbox, WatcherNotifications: watchers, GlobalMute: global}
_ = json.NewEncoder(w).Encode(an) _ = json.NewEncoder(w).Encode(an)
} }

View file

@ -35,8 +35,8 @@ type DeviceRepository interface {
Update(ctx context.Context, dev *Device) error Update(ctx context.Context, dev *Device) error
Create(ctx context.Context, dev *Device) error Create(ctx context.Context, dev *Device) error
Delete(ctx context.Context, token string) error Delete(ctx context.Context, token string) error
SetNotifiable(ctx context.Context, dev *Device, acct *Account, inbox, watcher bool) error SetNotifiable(ctx context.Context, dev *Device, acct *Account, inbox, watcher, global bool) error
GetNotifiable(ctx context.Context, dev *Device, acct *Account) (bool, bool, error) GetNotifiable(ctx context.Context, dev *Device, acct *Account) (bool, bool, bool, error)
PruneStale(ctx context.Context, before int64) (int64, error) PruneStale(ctx context.Context, before int64) (int64, error)
} }

View file

@ -170,15 +170,16 @@ func (p *postgresDeviceRepository) Delete(ctx context.Context, token string) err
return err return err
} }
func (p *postgresDeviceRepository) SetNotifiable(ctx context.Context, dev *domain.Device, acct *domain.Account, inbox, watcher bool) error { func (p *postgresDeviceRepository) SetNotifiable(ctx context.Context, dev *domain.Device, acct *domain.Account, inbox, watcher, global bool) error {
query := ` query := `
UPDATE devices_accounts UPDATE devices_accounts
SET SET
inbox_notifiable = $1, inbox_notifiable = $1,
watcher_notifiable = $2 watcher_notifiable = $2,
WHERE device_id = $3 AND account_id = $4` global_mute = $3
WHERE device_id = $4 AND account_id = $5`
res, err := p.pool.Exec(ctx, query, inbox, watcher, dev.ID, acct.ID) res, err := p.pool.Exec(ctx, query, inbox, watcher, global, dev.ID, acct.ID)
if res.RowsAffected() != 1 { if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected()) return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
@ -187,26 +188,26 @@ func (p *postgresDeviceRepository) SetNotifiable(ctx context.Context, dev *domai
} }
func (p *postgresDeviceRepository) GetNotifiable(ctx context.Context, dev *domain.Device, acct *domain.Account) (bool, bool, error) { func (p *postgresDeviceRepository) GetNotifiable(ctx context.Context, dev *domain.Device, acct *domain.Account) (bool, bool, bool, error) {
query := ` query := `
SELECT inbox_notifiable, watcher_notifiable SELECT inbox_notifiable, watcher_notifiable, global_mute
FROM devices_accounts FROM devices_accounts
WHERE device_id = $1 AND account_id = $2` WHERE device_id = $1 AND account_id = $2`
rows, err := p.pool.Query(ctx, query, dev.ID, acct.ID) rows, err := p.pool.Query(ctx, query, dev.ID, acct.ID)
if err != nil { if err != nil {
return false, false, err return false, false, false, err
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var inbox, watcher bool var inbox, watcher, global bool
if err := rows.Scan(&inbox, &watcher); err != nil { if err := rows.Scan(&inbox, &watcher, &global); err != nil {
return false, false, err return false, false, false, err
} }
return inbox, watcher, nil return inbox, watcher, global, nil
} }
return false, false, domain.ErrNotFound return false, false, false, domain.ErrNotFound
} }
func (p *postgresDeviceRepository) PruneStale(ctx context.Context, before int64) (int64, error) { func (p *postgresDeviceRepository) PruneStale(ctx context.Context, before int64) (int64, error) {

View file

@ -149,7 +149,10 @@ func (p *postgresWatcherRepository) GetByTypeAndWatcheeID(ctx context.Context, t
INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id AND accounts.id = devices_accounts.account_id INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id AND accounts.id = devices_accounts.account_id
LEFT JOIN subreddits ON watchers.type IN(0,2) AND watchers.watchee_id = subreddits.id LEFT JOIN subreddits ON watchers.type IN(0,2) AND watchers.watchee_id = subreddits.id
LEFT JOIN users ON watchers.type = 1 AND watchers.watchee_id = users.id LEFT JOIN users ON watchers.type = 1 AND watchers.watchee_id = users.id
WHERE watchers.type = $1 AND watchers.watchee_id = $2 AND devices_accounts.watcher_notifiable = TRUE` WHERE watchers.type = $1 AND
watchers.watchee_id = $2 AND
devices_accounts.watcher_notifiable = TRUE AND
devices_accounts.global_mute = FALSE`
return p.fetch(ctx, query, typ, id) return p.fetch(ctx, query, typ, id)
} }