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

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

+14 -3
View File
@@ -16,6 +16,7 @@ type Contact struct {
State string `json:"state"`
Alias string `json:"alias"`
Category string `json:"category"`
Muted bool `json:"muted"`
}
type Store struct{ db *pgxpool.Pool }
@@ -29,7 +30,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.alias,''), COALESCE(p.category,''), COALESCE(p.muted,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)
@@ -42,7 +43,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)
Scan(&c.ID, &c.Username, &c.State, &c.Alias, &c.Category, &c.Muted)
return c, err
}
@@ -57,7 +58,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); err != nil {
if err := rows.Scan(&c.ID, &c.Username, &c.State, &c.Alias, &c.Category, &c.Muted); err != nil {
return nil, err
}
result = append(result, c)
@@ -147,6 +148,16 @@ func (s *Store) Change(ctx context.Context, owner, target, action, alias, catego
if err == nil && tag.RowsAffected() == 0 {
return ErrUnavailable
}
case "mute", "unmute":
tag, writeErr := tx.Exec(ctx, `
INSERT INTO contact_preferences(owner_id,contact_id,alias,category,muted)
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")
err = writeErr
if err == nil && tag.RowsAffected() == 0 {
return ErrUnavailable
}
default:
return ErrUnavailable
}
@@ -0,0 +1 @@
ALTER TABLE contact_preferences ADD COLUMN muted boolean NOT NULL DEFAULT false;
+4 -1
View File
@@ -43,6 +43,9 @@ var groupSeenMigration string
//go:embed 012_group_invite_link.sql
var groupInviteLinkMigration string
//go:embed 013_contact_mute.sql
var contactMuteMigration string
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
tx, err := pool.Begin(ctx)
if err != nil {
@@ -58,7 +61,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,
groupSeenMigration, groupInviteLinkMigration, contactMuteMigration,
} {
version := index + 1
var applied bool
+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) {
+1 -1
View File
@@ -78,7 +78,7 @@ 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" {
if input.Action != "preferences" && input.Action != "mute" && input.Action != "unmute" {
pipe.Publish(r.Context(), "xhchat:contacts:"+strings.ToLower(target), "changed")
}
_, _ = pipe.Exec(r.Context())
+8 -2
View File
@@ -15,6 +15,7 @@ func (a *API) sendEnvelope(w http.ResponseWriter, r *http.Request, _ string, ide
MessageID string `json:"messageId"`
RecipientDevice string `json:"recipientDevice"`
Ciphertext []byte `json:"ciphertext"`
Silent bool `json:"silent"`
}
if !parseLimit(w, r, &input, 96<<10) {
return
@@ -27,7 +28,8 @@ func (a *API) sendEnvelope(w http.ResponseWriter, r *http.Request, _ string, ide
return
}
recipient := strings.ToLower(input.RecipientDevice)
seq, err := a.delivery.Send(r.Context(), identity.Device.ID, recipient, input.MessageID, input.Ciphertext)
seq, notify, err := a.delivery.Send(r.Context(), identity.Device.ID, recipient, input.MessageID, input.Ciphertext,
input.Ciphertext[3] == 2 && !input.Silent)
switch {
case errors.Is(err, delivery.ErrUnavailable):
problem(w, 404, "delivery_unavailable", "无法向指定设备投递")
@@ -36,7 +38,11 @@ func (a *API) sendEnvelope(w http.ResponseWriter, r *http.Request, _ string, ide
case err != nil:
unavailable(w, err)
default:
_ = a.redis.Publish(r.Context(), "xhchat:inbox:"+recipient, "changed").Err()
hint := "changed"
if notify {
hint = "private-notify:" + identity.User.ID
}
_ = a.redis.Publish(r.Context(), "xhchat:inbox:"+recipient, hint).Err()
respond(w, 200, map[string]string{"sequence": seq})
}
}
+11 -5
View File
@@ -109,12 +109,18 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
event["type"] = "group_list_changed"
case inboxChannel:
event["type"] = "inbox_changed"
kind, group, _ := strings.Cut(change.Payload, ":")
if (kind == "group" || kind == "group-notify") && uuidPattern.MatchString(group) {
event["type"] = "group_changed"
event["groupId"] = group
if kind == "group-notify" {
kind, id, _ := strings.Cut(change.Payload, ":")
if uuidPattern.MatchString(id) {
switch kind {
case "private-notify":
event["contactId"] = id
event["notify"] = true
case "group", "group-notify":
event["type"] = "group_changed"
event["groupId"] = id
if kind == "group-notify" {
event["notify"] = true
}
}
}
}