接入群消息实时提醒和重连补拉
This commit is contained in:
1 parent
5ace82b0d7
commit
b2e04e8174
10 files changed
+122
-33
No files matched your search
@@ -15,7 +15,7 @@ func (s *Store) UploadAttachment(ctx context.Context, group, user, device, epoch
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = lockSendingEpoch(ctx, tx, group, user, device, epoch); err != nil {
|
||||
if _, err = lockSendingEpoch(ctx, tx, group, user, device, epoch); err != nil {
|
||||
return err
|
||||
}
|
||||
inserted, err := tx.Exec(ctx, `
|
||||
|
||||
@@ -150,10 +150,10 @@ 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) error {
|
||||
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) ([]string, error) {
|
||||
version, _, err := lockTransport(ctx, tx, group, user, device)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
var enrolled bool
|
||||
err = tx.QueryRow(ctx, `
|
||||
@@ -162,42 +162,46 @@ func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 AND e.device_id=$3 AND g.membership_version=$4::bigint AND g.retired_at IS NULL)
|
||||
`, group, epoch, device, version).Scan(&enrolled)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if !enrolled {
|
||||
return ErrVersion
|
||||
return nil, ErrVersion
|
||||
}
|
||||
|
||||
// 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.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 FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 ORDER BY d.id FOR UPDATE OF d
|
||||
`, group, epoch)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
devices := make([]string, 0, 32)
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var active bool
|
||||
if err = rows.Scan(&active); err != nil {
|
||||
return err
|
||||
if err = rows.Scan(&id, &active); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !active {
|
||||
return ErrVersion
|
||||
return nil, ErrVersion
|
||||
}
|
||||
devices = append(devices, id)
|
||||
}
|
||||
return rows.Err()
|
||||
return devices, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, error) {
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, []string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = lockSendingEpoch(ctx, tx, group, user, device, message.Epoch); err != nil {
|
||||
return "", err
|
||||
devices, err := lockSendingEpoch(ctx, tx, group, user, device, message.Epoch)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if message.AttachmentID != nil {
|
||||
var found string
|
||||
@@ -205,10 +209,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 "", ErrAttachment
|
||||
return "", nil, ErrAttachment
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", nil, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,13 +228,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 "", ErrVersion
|
||||
return "", nil, ErrVersion
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", nil, err
|
||||
}
|
||||
return sequence, tx.Commit(ctx)
|
||||
return sequence, devices, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Inbox(ctx context.Context, group, user, device string, after int64) ([]Message, error) {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"xhchat/server/internal/account"
|
||||
"xhchat/server/internal/groups"
|
||||
@@ -95,6 +97,11 @@ func (a *API) publishGroupEpoch(w http.ResponseWriter, r *http.Request, _ string
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
devices := make([]string, 0, len(input.Devices))
|
||||
for _, device := range input.Devices {
|
||||
devices = append(devices, device.Device)
|
||||
}
|
||||
a.hintGroup(r.Context(), id, devices)
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
@@ -122,21 +129,33 @@ 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, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
|
||||
sequence, devices, 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)
|
||||
respond(w, 200, map[string]string{"sequence": sequence})
|
||||
}
|
||||
|
||||
func (a *API) hintGroup(ctx context.Context, group string, devices []string) {
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 250*time.Millisecond)
|
||||
defer cancel()
|
||||
pipe := a.redis.Pipeline()
|
||||
for _, device := range devices {
|
||||
pipe.Publish(ctx, "xhchat:inbox:"+device, "group:"+strings.ToLower(group))
|
||||
}
|
||||
// Hints are best effort. The durable cursor is also polled by clients.
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
func (a *API) groupInbox(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 180) {
|
||||
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 240) {
|
||||
return
|
||||
}
|
||||
after := int64(0)
|
||||
|
||||
@@ -100,11 +100,15 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
event := "contacts_changed"
|
||||
event := map[string]string{"type": "contacts_changed"}
|
||||
if change.Channel == inboxChannel {
|
||||
event = "inbox_changed"
|
||||
event["type"] = "inbox_changed"
|
||||
if group, found := strings.CutPrefix(change.Payload, "group:"); found && uuidPattern.MatchString(group) {
|
||||
event["type"] = "group_changed"
|
||||
event["groupId"] = group
|
||||
}
|
||||
}
|
||||
err := wsjson.Write(writeCtx, conn, map[string]string{"type": event})
|
||||
err := wsjson.Write(writeCtx, conn, event)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user