增加私聊置顶和按设备统计未读

This commit is contained in:
xhgit committed 2026-11-15 22:35:45 +08:00
1 parent 7e165ba9b4
commit ec2679b991
19 files changed
+312 -52

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
package delivery
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
func (s *Store) Unread(ctx context.Context, recipient string) (map[string]int64, error) {
rows, err := s.db.Query(ctx, `
SELECT sender.user_id, count(*)
FROM device_envelopes e
JOIN devices sender ON sender.id=e.sender_device AND sender.revoked_at IS NULL
JOIN devices recipient ON recipient.id=e.recipient_device AND recipient.revoked_at IS NULL
JOIN contact_relationships r ON r.first_user=LEAST(sender.user_id,recipient.user_id)
AND r.second_user=GREATEST(sender.user_id,recipient.user_id) AND r.status='accepted'
LEFT JOIN device_contact_seen seen ON seen.recipient_device=e.recipient_device AND seen.sender_device=e.sender_device
WHERE e.recipient_device=$1 AND e.counts_as_unread AND e.sequence>COALESCE(seen.sequence,0)
AND e.created_at>=r.updated_at
GROUP BY sender.user_id
`, recipient)
if err != nil {
return nil, err
}
defer rows.Close()
counts := make(map[string]int64)
for rows.Next() {
var user string
var count int64
if err := rows.Scan(&user, &count); err != nil {
return nil, err
}
counts[user] = count
}
return counts, rows.Err()
}
func (s *Store) MarkSeen(ctx context.Context, recipient, sender string, sequence int64) error {
tx, err := s.db.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
if err := authorizeDelivery(ctx, tx, sender, recipient); err != nil {
return err
}
var stored int64
err = tx.QueryRow(ctx, `
INSERT INTO device_contact_seen(recipient_device,sender_device,sequence)
SELECT recipient_device,sender_device,sequence FROM device_envelopes
WHERE recipient_device=$1 AND sender_device=$2 AND sequence=$3
ON CONFLICT (recipient_device,sender_device) DO UPDATE SET sequence=GREATEST(device_contact_seen.sequence,EXCLUDED.sequence)
RETURNING sequence
`, recipient, sender, sequence).Scan(&stored)
if errors.Is(err, pgx.ErrNoRows) {
return ErrUnavailable
}
if err != nil {
return err
}
return tx.Commit(ctx)
}
+6 -6
View File
@@ -27,7 +27,7 @@ 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, requestNotice bool) (string, bool, error) {
func (s *Store) Send(ctx context.Context, sender, recipient, message string, ciphertext []byte, countAsUnread bool) (string, bool, error) {
tx, err := s.db.Begin(ctx)
if err != nil {
return "", false, err
@@ -40,13 +40,13 @@ func (s *Store) Send(ctx context.Context, sender, recipient, message string, cip
var seq string
err = tx.QueryRow(ctx, `
INSERT INTO device_envelopes(message_id, sender_device, recipient_device, ciphertext)
VALUES ($1,$2,$3,$4)
INSERT INTO device_envelopes(message_id, sender_device, recipient_device, ciphertext, counts_as_unread)
VALUES ($1,$2,$3,$4,$5)
ON CONFLICT (sender_device, recipient_device, message_id) DO NOTHING
RETURNING sequence::text
`, message, sender, recipient, ciphertext).Scan(&seq)
`, message, sender, recipient, ciphertext, countAsUnread).Scan(&seq)
if errors.Is(err, pgx.ErrNoRows) {
requestNotice = false
countAsUnread = false
var previous []byte
err = tx.QueryRow(ctx, `
SELECT sequence::text,ciphertext FROM device_envelopes
@@ -60,7 +60,7 @@ func (s *Store) Send(ctx context.Context, sender, recipient, message string, cip
return "", false, err
}
var notify bool
if requestNotice {
if countAsUnread {
err = tx.QueryRow(ctx, `
SELECT NOT EXISTS (
SELECT 1 FROM contact_preferences p