接入私聊消息通知和联系人免打扰

This commit is contained in:
xhgit committed 2026-11-15 22:27:58 +08:00
1 parent 5c882ac855
commit 7e165ba9b4
23 files changed
+286 -97

No files matched your search

+21 -6
View File
@@ -27,15 +27,15 @@ type Store struct{ db *pgxpool.Pool }
func New(db *pgxpool.Pool) *Store { return &Store{db: db} }
func (s *Store) Send(ctx context.Context, sender, recipient, message string, ciphertext []byte) (string, error) {
func (s *Store) Send(ctx context.Context, sender, recipient, message string, ciphertext []byte, requestNotice bool) (string, bool, error) {
tx, err := s.db.Begin(ctx)
if err != nil {
return "", err
return "", false, err
}
defer tx.Rollback(ctx)
if err = authorizeDelivery(ctx, tx, sender, recipient); err != nil {
return "", err
return "", false, err
}
var seq string
@@ -46,19 +46,34 @@ func (s *Store) Send(ctx context.Context, sender, recipient, message string, cip
RETURNING sequence::text
`, message, sender, recipient, ciphertext).Scan(&seq)
if errors.Is(err, pgx.ErrNoRows) {
requestNotice = false
var previous []byte
err = tx.QueryRow(ctx, `
SELECT sequence::text,ciphertext FROM device_envelopes
WHERE message_id=$1 AND sender_device=$2 AND recipient_device=$3
`, message, sender, recipient).Scan(&seq, &previous)
if err == nil && !bytes.Equal(previous, ciphertext) {
return "", ErrConflict
return "", false, ErrConflict
}
}
if err != nil {
return "", err
return "", false, err
}
return seq, tx.Commit(ctx)
var notify bool
if requestNotice {
err = tx.QueryRow(ctx, `
SELECT NOT EXISTS (
SELECT 1 FROM contact_preferences p
JOIN devices sender ON sender.user_id=p.contact_id
JOIN devices recipient ON recipient.user_id=p.owner_id
WHERE sender.id=$1 AND recipient.id=$2 AND p.muted
)
`, sender, recipient).Scan(&notify)
if err != nil {
return "", false, err
}
}
return seq, notify, tx.Commit(ctx)
}
func (s *Store) Inbox(ctx context.Context, recipient string, after int64) ([]Envelope, error) {