接入群消息实时提醒和重连补拉
This commit is contained in:
1 parent
5ace82b0d7
commit
b2e04e8174
10 files changed
+122
-33
No files matched your search
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user