增加私聊置顶和按设备统计未读
This commit is contained in:
1 parent
7e165ba9b4
commit
ec2679b991
19 files changed
+312
-52
No files matched your search
@@ -17,6 +17,7 @@ type Contact struct {
|
||||
Alias string `json:"alias"`
|
||||
Category string `json:"category"`
|
||||
Muted bool `json:"muted"`
|
||||
Pinned bool `json:"pinned"`
|
||||
}
|
||||
|
||||
type Store struct{ db *pgxpool.Pool }
|
||||
@@ -30,7 +31,7 @@ const selectContact = `
|
||||
WHEN r.requested_by = $1 THEN 'outgoing'
|
||||
WHEN r.status = 'pending' THEN 'incoming'
|
||||
ELSE 'none' END,
|
||||
COALESCE(p.alias,''), COALESCE(p.category,''), COALESCE(p.muted,false)
|
||||
COALESCE(p.alias,''), COALESCE(p.category,''), COALESCE(p.muted,false), COALESCE(p.pinned,false)
|
||||
FROM users u
|
||||
LEFT JOIN contact_relationships r ON r.first_user = LEAST($1::uuid, u.id)
|
||||
AND r.second_user = GREATEST($1::uuid, u.id)
|
||||
@@ -43,7 +44,7 @@ const selectContact = `
|
||||
func (s *Store) Search(ctx context.Context, owner, username string) (Contact, error) {
|
||||
var c Contact
|
||||
err := s.db.QueryRow(ctx, selectContact+" AND u.username=$2", owner, username).
|
||||
Scan(&c.ID, &c.Username, &c.State, &c.Alias, &c.Category, &c.Muted)
|
||||
Scan(&c.ID, &c.Username, &c.State, &c.Alias, &c.Category, &c.Muted, &c.Pinned)
|
||||
return c, err
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ func (s *Store) List(ctx context.Context, owner string) ([]Contact, error) {
|
||||
result := make([]Contact, 0)
|
||||
for rows.Next() {
|
||||
var c Contact
|
||||
if err := rows.Scan(&c.ID, &c.Username, &c.State, &c.Alias, &c.Category, &c.Muted); err != nil {
|
||||
if err := rows.Scan(&c.ID, &c.Username, &c.State, &c.Alias, &c.Category, &c.Muted, &c.Pinned); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, c)
|
||||
@@ -148,12 +149,16 @@ func (s *Store) Change(ctx context.Context, owner, target, action, alias, catego
|
||||
if err == nil && tag.RowsAffected() == 0 {
|
||||
return ErrUnavailable
|
||||
}
|
||||
case "mute", "unmute":
|
||||
case "mute", "unmute", "pin", "unpin":
|
||||
column, enabled := "muted", action == "mute"
|
||||
if action == "pin" || action == "unpin" {
|
||||
column, enabled = "pinned", action == "pin"
|
||||
}
|
||||
tag, writeErr := tx.Exec(ctx, `
|
||||
INSERT INTO contact_preferences(owner_id,contact_id,alias,category,muted)
|
||||
INSERT INTO contact_preferences(owner_id,contact_id,alias,category,`+column+`)
|
||||
SELECT $1::uuid,$2::uuid,'','',$3::boolean FROM contact_relationships WHERE `+pair+` AND status='accepted'
|
||||
ON CONFLICT (owner_id,contact_id) DO UPDATE SET muted=EXCLUDED.muted
|
||||
`, owner, target, action == "mute")
|
||||
ON CONFLICT (owner_id,contact_id) DO UPDATE SET `+column+`=EXCLUDED.`+column,
|
||||
owner, target, enabled)
|
||||
err = writeErr
|
||||
if err == nil && tag.RowsAffected() == 0 {
|
||||
return ErrUnavailable
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
ALTER TABLE contact_preferences ADD COLUMN pinned boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE device_envelopes ADD COLUMN counts_as_unread boolean NOT NULL DEFAULT false;
|
||||
|
||||
CREATE INDEX device_envelopes_unread ON device_envelopes(recipient_device, sender_device, sequence)
|
||||
WHERE counts_as_unread;
|
||||
|
||||
CREATE TABLE device_contact_seen (
|
||||
recipient_device uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
sender_device uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
sequence bigint NOT NULL CHECK (sequence > 0),
|
||||
PRIMARY KEY (recipient_device, sender_device),
|
||||
CHECK (recipient_device <> sender_device)
|
||||
);
|
||||
@@ -46,6 +46,9 @@ var groupInviteLinkMigration string
|
||||
//go:embed 013_contact_mute.sql
|
||||
var contactMuteMigration string
|
||||
|
||||
//go:embed 014_private_conversations.sql
|
||||
var privateConversationsMigration string
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -61,7 +64,7 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
for index, migration := range []string{
|
||||
identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration,
|
||||
attachmentsMigration, groupsMigration, groupTransportMigration, groupAttachmentsMigration, groupPreferencesMigration,
|
||||
groupSeenMigration, groupInviteLinkMigration, contactMuteMigration,
|
||||
groupSeenMigration, groupInviteLinkMigration, contactMuteMigration, privateConversationsMigration,
|
||||
} {
|
||||
version := index + 1
|
||||
var applied bool
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -52,6 +52,8 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/contacts/{id}", a.authorized(a.changeContact))
|
||||
mux.HandleFunc("GET /api/v1/inbox", a.authorized(a.inbox))
|
||||
mux.HandleFunc("POST /api/v1/inbox", a.authorized(a.sendEnvelope))
|
||||
mux.HandleFunc("GET /api/v1/inbox/unread", a.authorized(a.privateUnread))
|
||||
mux.HandleFunc("POST /api/v1/inbox/seen", a.authorized(a.markPrivateSeen))
|
||||
mux.HandleFunc("GET /api/v1/contacts/{id}/devices", a.authorized(a.contactDevices))
|
||||
mux.HandleFunc("POST /api/v1/contacts/{id}/devices/{device}/claim", a.authorized(a.claimPackage))
|
||||
mux.HandleFunc("POST /api/v1/key-packages", a.authorized(a.publishPackage))
|
||||
|
||||
@@ -78,7 +78,9 @@ func (a *API) changeContact(w http.ResponseWriter, r *http.Request, _ string, id
|
||||
}
|
||||
pipe := a.redis.Pipeline()
|
||||
pipe.Publish(r.Context(), "xhchat:contacts:"+identity.User.ID, "changed")
|
||||
if input.Action != "preferences" && input.Action != "mute" && input.Action != "unmute" {
|
||||
switch input.Action {
|
||||
case "preferences", "mute", "unmute", "pin", "unpin":
|
||||
default:
|
||||
pipe.Publish(r.Context(), "xhchat:contacts:"+strings.ToLower(target), "changed")
|
||||
}
|
||||
_, _ = pipe.Exec(r.Context())
|
||||
|
||||
@@ -74,3 +74,43 @@ func (a *API) inbox(w http.ResponseWriter, r *http.Request, _ string, identity a
|
||||
}
|
||||
respond(w, 200, items)
|
||||
}
|
||||
|
||||
func (a *API) privateUnread(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
if !a.limit(w, r, "private-unread:"+identity.Device.ID, 180) {
|
||||
return
|
||||
}
|
||||
counts, err := a.delivery.Unread(r.Context(), identity.Device.ID)
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, counts)
|
||||
}
|
||||
|
||||
func (a *API) markPrivateSeen(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
var input struct {
|
||||
SenderDevice string `json:"senderDevice"`
|
||||
Sequence string `json:"sequence"`
|
||||
}
|
||||
if !parse(w, r, &input) {
|
||||
return
|
||||
}
|
||||
sequence, err := strconv.ParseInt(input.Sequence, 10, 64)
|
||||
if !uuidPattern.MatchString(input.SenderDevice) || err != nil || sequence <= 0 || strconv.FormatInt(sequence, 10) != input.Sequence {
|
||||
problem(w, 400, "invalid_cursor", "无效的设备或已查看游标")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "private-seen:"+identity.Device.ID, 180) {
|
||||
return
|
||||
}
|
||||
err = a.delivery.MarkSeen(r.Context(), identity.Device.ID, strings.ToLower(input.SenderDevice), sequence)
|
||||
if errors.Is(err, delivery.ErrUnavailable) {
|
||||
problem(w, 404, "delivery_unavailable", "找不到当前设备可见的投递记录")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
Reference in new issue
Block a user