增加群聊置顶、免打扰和应用内提醒
This commit is contained in:
1 parent
ced6e9030d
commit
f54077ce46
17 files changed
+228
-42
No files matched your search
@@ -0,0 +1,4 @@
|
||||
ALTER TABLE group_members
|
||||
ADD COLUMN pinned boolean NOT NULL DEFAULT false,
|
||||
ADD COLUMN muted boolean NOT NULL DEFAULT false,
|
||||
ADD CHECK (state = 'active' OR (NOT pinned AND NOT muted));
|
||||
@@ -34,6 +34,9 @@ var groupTransportMigration string
|
||||
//go:embed 009_group_attachments.sql
|
||||
var groupAttachmentsMigration string
|
||||
|
||||
//go:embed 010_group_preferences.sql
|
||||
var groupPreferencesMigration string
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -48,7 +51,7 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
}
|
||||
for index, migration := range []string{
|
||||
identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration,
|
||||
attachmentsMigration, groupsMigration, groupTransportMigration, groupAttachmentsMigration,
|
||||
attachmentsMigration, groupsMigration, groupTransportMigration, groupAttachmentsMigration, groupPreferencesMigration,
|
||||
} {
|
||||
version := index + 1
|
||||
var applied bool
|
||||
|
||||
@@ -18,6 +18,12 @@ type Group struct {
|
||||
State string `json:"state"`
|
||||
Role string `json:"role"`
|
||||
Version string `json:"membershipVersion"`
|
||||
Preferences
|
||||
}
|
||||
|
||||
type Preferences struct {
|
||||
Pinned bool `json:"pinned"`
|
||||
Muted bool `json:"muted"`
|
||||
}
|
||||
|
||||
type Member struct {
|
||||
@@ -53,12 +59,12 @@ func (s *Store) Create(ctx context.Context, owner, name string) (Group, error) {
|
||||
}
|
||||
|
||||
const groupRows = `
|
||||
SELECT g.id,g.name,m.state,CASE WHEN g.owner_id=m.user_id THEN 'owner' ELSE m.role END,g.membership_version::text
|
||||
SELECT g.id,g.name,m.state,CASE WHEN g.owner_id=m.user_id THEN 'owner' ELSE m.role END,g.membership_version::text,m.pinned,m.muted
|
||||
FROM chat_groups g JOIN group_members m ON m.group_id=g.id WHERE m.user_id=$1
|
||||
`
|
||||
|
||||
func (s *Store) List(ctx context.Context, user string) ([]Group, error) {
|
||||
rows, err := s.db.Query(ctx, groupRows+" ORDER BY g.created_at DESC, g.id LIMIT 200", user)
|
||||
rows, err := s.db.Query(ctx, groupRows+" ORDER BY m.pinned DESC, g.created_at DESC, g.id LIMIT 200", user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -66,7 +72,7 @@ func (s *Store) List(ctx context.Context, user string) ([]Group, error) {
|
||||
result := make([]Group, 0)
|
||||
for rows.Next() {
|
||||
var g Group
|
||||
if err := rows.Scan(&g.ID, &g.Name, &g.State, &g.Role, &g.Version); err != nil {
|
||||
if err := rows.Scan(&g.ID, &g.Name, &g.State, &g.Role, &g.Version, &g.Pinned, &g.Muted); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, g)
|
||||
@@ -82,7 +88,7 @@ func (s *Store) Detail(ctx context.Context, user, id string) (Detail, error) {
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
err = tx.QueryRow(ctx, groupRows+" AND g.id=$2", user, id).
|
||||
Scan(&result.ID, &result.Name, &result.State, &result.Role, &result.Version)
|
||||
Scan(&result.ID, &result.Name, &result.State, &result.Role, &result.Version, &result.Pinned, &result.Muted)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return result, ErrUnavailable
|
||||
}
|
||||
@@ -115,6 +121,26 @@ func (s *Store) Detail(ctx context.Context, user, id string) (Detail, error) {
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) SetPreferences(ctx context.Context, id, user, device string, pinned, muted *bool) (Preferences, error) {
|
||||
var result Preferences
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, _, err := lockTransport(ctx, tx, id, user, device); err != nil {
|
||||
return result, err
|
||||
}
|
||||
err = tx.QueryRow(ctx, `
|
||||
UPDATE group_members SET pinned=COALESCE($3,pinned), muted=COALESCE($4,muted)
|
||||
WHERE group_id=$1 AND user_id=$2 AND state='active' RETURNING pinned,muted
|
||||
`, id, user, pinned, muted).Scan(&result.Pinned, &result.Muted)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Change(ctx context.Context, user, id string, change Change) ([]string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
|
||||
@@ -150,7 +150,12 @@ func retryEpoch(ctx context.Context, tx pgx.Tx, group, sender string, input Epoc
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) ([]string, error) {
|
||||
type EpochDevice struct {
|
||||
ID string
|
||||
Muted bool
|
||||
}
|
||||
|
||||
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) ([]EpochDevice, error) {
|
||||
version, _, err := lockTransport(ctx, tx, group, user, device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -171,37 +176,38 @@ func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch
|
||||
// Match the device revocation transaction's row lock. Once a bound device is
|
||||
// revoked the generation cannot accept any further application ciphertext.
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT d.id,d.revoked_at IS NULL FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
SELECT d.id,d.revoked_at IS NULL,m.muted FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
JOIN group_members m ON m.group_id=e.group_id AND m.user_id=d.user_id AND m.state='active'
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 ORDER BY d.id FOR UPDATE OF d
|
||||
`, group, epoch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
devices := make([]string, 0, 32)
|
||||
devices := make([]EpochDevice, 0, 32)
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var member EpochDevice
|
||||
var active bool
|
||||
if err = rows.Scan(&id, &active); err != nil {
|
||||
if err = rows.Scan(&member.ID, &active, &member.Muted); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !active {
|
||||
return nil, ErrVersion
|
||||
}
|
||||
devices = append(devices, id)
|
||||
devices = append(devices, member)
|
||||
}
|
||||
return devices, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, []string, error) {
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, []EpochDevice, bool, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
devices, err := lockSendingEpoch(ctx, tx, group, user, device, message.Epoch)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
if message.AttachmentID != nil {
|
||||
var found string
|
||||
@@ -209,10 +215,10 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
|
||||
SELECT id FROM group_attachments WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND id=$4 FOR KEY SHARE
|
||||
`, group, message.Epoch, device, message.AttachmentID).Scan(&found)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil, ErrAttachment
|
||||
return "", nil, false, ErrAttachment
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +227,7 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
|
||||
INSERT INTO group_messages(group_id,epoch_id,sender_device,message_id,ciphertext,attachment_id)
|
||||
VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING sequence::text
|
||||
`, group, message.Epoch, device, message.ID, message.Wire, message.AttachmentID).Scan(&sequence)
|
||||
created := err == nil
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
var same bool
|
||||
err = tx.QueryRow(ctx, `
|
||||
@@ -228,13 +235,13 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
|
||||
WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND message_id=$4
|
||||
`, group, message.Epoch, device, message.ID, message.Wire, message.AttachmentID).Scan(&sequence, &same)
|
||||
if errors.Is(err, pgx.ErrNoRows) || err == nil && !same {
|
||||
return "", nil, ErrVersion
|
||||
return "", nil, false, ErrVersion
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
return sequence, devices, tx.Commit(ctx)
|
||||
return sequence, devices, created, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Inbox(ctx context.Context, group, user, device string, after int64) ([]Message, error) {
|
||||
|
||||
@@ -64,6 +64,7 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/groups", a.authorized(a.createGroup))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}", a.authorized(a.groupDetail))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}", a.authorized(a.changeGroup))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/preferences", a.authorized(a.setGroupPreferences))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/epochs", a.authorized(a.groupEpochs))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/epochs", a.authorized(a.publishGroupEpoch))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/messages", a.authorized(a.groupInbox))
|
||||
|
||||
@@ -97,11 +97,11 @@ func (a *API) publishGroupEpoch(w http.ResponseWriter, r *http.Request, _ string
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
devices := make([]string, 0, len(input.Devices))
|
||||
devices := make([]groups.EpochDevice, 0, len(input.Devices))
|
||||
for _, device := range input.Devices {
|
||||
devices = append(devices, device.Device)
|
||||
devices = append(devices, groups.EpochDevice{ID: device.Device})
|
||||
}
|
||||
a.hintGroup(r.Context(), id, devices)
|
||||
a.hintGroup(r.Context(), id, devices, "", false)
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
@@ -129,21 +129,26 @@ func (a *API) sendGroupMessage(w http.ResponseWriter, r *http.Request, _ string,
|
||||
return
|
||||
}
|
||||
message := groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire, AttachmentID: input.AttachmentID}
|
||||
sequence, devices, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
|
||||
sequence, devices, created, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
a.hintGroup(r.Context(), id, devices)
|
||||
a.hintGroup(r.Context(), id, devices, actor.Device.ID, created)
|
||||
respond(w, 200, map[string]string{"sequence": sequence})
|
||||
}
|
||||
|
||||
func (a *API) hintGroup(ctx context.Context, group string, devices []string) {
|
||||
func (a *API) hintGroup(ctx context.Context, group string, devices []groups.EpochDevice, sender string, newMessage bool) {
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 250*time.Millisecond)
|
||||
defer cancel()
|
||||
pipe := a.redis.Pipeline()
|
||||
group = strings.ToLower(group)
|
||||
for _, device := range devices {
|
||||
pipe.Publish(ctx, "xhchat:inbox:"+device, "group:"+strings.ToLower(group))
|
||||
prefix := "group:"
|
||||
if newMessage && !device.Muted && sender != device.ID {
|
||||
prefix = "group-notify:"
|
||||
}
|
||||
pipe.Publish(ctx, "xhchat:inbox:"+device.ID, prefix+group)
|
||||
}
|
||||
// Hints are best effort. The durable cursor is also polled by clients.
|
||||
_, _ = pipe.Exec(ctx)
|
||||
|
||||
@@ -134,3 +134,32 @@ func (a *API) hintGroupList(ctx context.Context, users []string) {
|
||||
}
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
func (a *API) setGroupPreferences(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
group := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(group) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Pinned *bool `json:"pinned"`
|
||||
Muted *bool `json:"muted"`
|
||||
}
|
||||
if !parse(w, r, &input) {
|
||||
return
|
||||
}
|
||||
if input.Pinned == nil && input.Muted == nil {
|
||||
problem(w, 400, "invalid_group_preferences", "请选择要修改的群设置")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-change:"+actor.User.ID, 60) {
|
||||
return
|
||||
}
|
||||
result, err := a.groups.SetPreferences(r.Context(), group, actor.User.ID, actor.Device.ID, input.Pinned, input.Muted)
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
a.hintGroupList(r.Context(), []string{actor.User.ID})
|
||||
respond(w, 200, result)
|
||||
}
|
||||
@@ -103,15 +103,19 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
event := map[string]string{"type": "contacts_changed"}
|
||||
event := map[string]any{"type": "contacts_changed"}
|
||||
switch change.Channel {
|
||||
case groupsChannel:
|
||||
event["type"] = "group_list_changed"
|
||||
case inboxChannel:
|
||||
event["type"] = "inbox_changed"
|
||||
if group, found := strings.CutPrefix(change.Payload, "group:"); found && uuidPattern.MatchString(group) {
|
||||
kind, group, _ := strings.Cut(change.Payload, ":")
|
||||
if (kind == "group" || kind == "group-notify") && uuidPattern.MatchString(group) {
|
||||
event["type"] = "group_changed"
|
||||
event["groupId"] = group
|
||||
if kind == "group-notify" {
|
||||
event["notify"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
err := wsjson.Write(writeCtx, conn, event)
|
||||
|
||||
Reference in new issue
Block a user