增加按设备统计的群消息未读角标
This commit is contained in:
1 parent
f54077ce46
commit
187923ca3c
16 files changed
+189
-23
No files matched your search
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE group_seen (
|
||||
group_id uuid NOT NULL,
|
||||
user_id uuid NOT NULL,
|
||||
device_id uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
sequence bigint NOT NULL CHECK (sequence > 0),
|
||||
PRIMARY KEY (group_id,device_id),
|
||||
FOREIGN KEY (group_id,user_id) REFERENCES group_members(group_id,user_id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX group_seen_membership ON group_seen(group_id,user_id);
|
||||
@@ -37,6 +37,9 @@ var groupAttachmentsMigration string
|
||||
//go:embed 010_group_preferences.sql
|
||||
var groupPreferencesMigration string
|
||||
|
||||
//go:embed 011_group_seen.sql
|
||||
var groupSeenMigration string
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -52,6 +55,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,
|
||||
} {
|
||||
version := index + 1
|
||||
var applied bool
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *Store) MarkSeen(ctx context.Context, group, user, device string, sequence int64) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, _, err := lockTransport(ctx, tx, group, user, device); err != nil {
|
||||
return err
|
||||
}
|
||||
var seen int64
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO group_seen(group_id,user_id,device_id,sequence)
|
||||
SELECT m.group_id,m.user_id,d.id,x.sequence FROM group_messages x
|
||||
JOIN group_epoch_devices e ON (e.group_id,e.epoch_id)=(x.group_id,x.epoch_id) AND e.device_id=$3
|
||||
JOIN group_epochs epoch ON (epoch.group_id,epoch.id)=(x.group_id,x.epoch_id)
|
||||
JOIN group_members m ON m.group_id=x.group_id AND m.user_id=$2 AND m.state='active'
|
||||
JOIN devices d ON d.id=e.device_id AND d.user_id=m.user_id AND d.revoked_at IS NULL
|
||||
WHERE x.group_id=$1 AND x.sequence=$4 AND epoch.membership_version>=m.joined_version
|
||||
ON CONFLICT (group_id,device_id) DO UPDATE SET sequence=GREATEST(group_seen.sequence,EXCLUDED.sequence)
|
||||
RETURNING sequence
|
||||
`, group, user, device, sequence).Scan(&seen)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrUnavailable
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
@@ -21,6 +21,11 @@ type Group struct {
|
||||
Preferences
|
||||
}
|
||||
|
||||
type ListedGroup struct {
|
||||
Group
|
||||
Unread int `json:"unread"`
|
||||
}
|
||||
|
||||
type Preferences struct {
|
||||
Pinned bool `json:"pinned"`
|
||||
Muted bool `json:"muted"`
|
||||
@@ -58,21 +63,31 @@ func (s *Store) Create(ctx context.Context, owner, name string) (Group, error) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
const groupColumns = `g.id,g.name,m.state,CASE WHEN g.owner_id=m.user_id THEN 'owner' ELSE m.role END,g.membership_version::text,m.pinned,m.muted`
|
||||
const groupRows = `
|
||||
SELECT g.id,g.name,m.state,CASE WHEN g.owner_id=m.user_id THEN 'owner' ELSE m.role END,g.membership_version::text,m.pinned,m.muted
|
||||
FROM chat_groups g JOIN group_members m ON m.group_id=g.id WHERE m.user_id=$1
|
||||
`
|
||||
|
||||
func (s *Store) List(ctx context.Context, user string) ([]Group, error) {
|
||||
rows, err := s.db.Query(ctx, groupRows+" ORDER BY m.pinned DESC, g.created_at DESC, g.id LIMIT 200", user)
|
||||
func (s *Store) List(ctx context.Context, user, device string) ([]ListedGroup, error) {
|
||||
rows, err := s.db.Query(ctx, "SELECT "+groupColumns+`, (
|
||||
SELECT count(*) FROM (
|
||||
SELECT 1 FROM group_messages x
|
||||
JOIN group_epoch_devices enrolled ON (enrolled.group_id,enrolled.epoch_id)=(x.group_id,x.epoch_id) AND enrolled.device_id=$2
|
||||
JOIN group_epochs epoch ON (epoch.group_id,epoch.id)=(x.group_id,x.epoch_id)
|
||||
JOIN devices d ON d.id=enrolled.device_id AND d.revoked_at IS NULL
|
||||
WHERE x.group_id=g.id AND epoch.membership_version>=m.joined_version AND x.sender_device<>$2
|
||||
AND x.sequence>COALESCE((SELECT seen.sequence FROM group_seen seen WHERE seen.group_id=g.id AND seen.device_id=$2),0)
|
||||
ORDER BY x.sequence LIMIT 100
|
||||
) AS unread
|
||||
)`+groupRows+" ORDER BY m.pinned DESC, g.created_at DESC, g.id LIMIT 200", user, device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]Group, 0)
|
||||
result := make([]ListedGroup, 0)
|
||||
for rows.Next() {
|
||||
var g Group
|
||||
if err := rows.Scan(&g.ID, &g.Name, &g.State, &g.Role, &g.Version, &g.Pinned, &g.Muted); err != nil {
|
||||
var g ListedGroup
|
||||
if err := rows.Scan(&g.ID, &g.Name, &g.State, &g.Role, &g.Version, &g.Pinned, &g.Muted, &g.Unread); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, g)
|
||||
@@ -87,7 +102,7 @@ func (s *Store) Detail(ctx context.Context, user, id string) (Detail, error) {
|
||||
return result, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
err = tx.QueryRow(ctx, groupRows+" AND g.id=$2", user, id).
|
||||
err = tx.QueryRow(ctx, "SELECT "+groupColumns+" "+groupRows+" AND g.id=$2", user, id).
|
||||
Scan(&result.ID, &result.Name, &result.State, &result.Role, &result.Version, &result.Pinned, &result.Muted)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return result, ErrUnavailable
|
||||
|
||||
@@ -65,6 +65,7 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}", a.authorized(a.groupDetail))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}", a.authorized(a.changeGroup))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/preferences", a.authorized(a.setGroupPreferences))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/seen", a.authorized(a.markGroupSeen))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/epochs", a.authorized(a.groupEpochs))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/epochs", a.authorized(a.publishGroupEpoch))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/messages", a.authorized(a.groupInbox))
|
||||
|
||||
@@ -183,3 +183,30 @@ func (a *API) groupInbox(w http.ResponseWriter, r *http.Request, _ string, actor
|
||||
}
|
||||
respond(w, 200, items)
|
||||
}
|
||||
|
||||
func (a *API) markGroupSeen(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
group := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(group) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Sequence string `json:"sequence"`
|
||||
}
|
||||
if !parse(w, r, &input) {
|
||||
return
|
||||
}
|
||||
sequence, err := strconv.ParseInt(input.Sequence, 10, 64)
|
||||
if err != nil || sequence <= 0 || strconv.FormatInt(sequence, 10) != input.Sequence {
|
||||
problem(w, 400, "invalid_cursor", "无效的已查看游标")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-seen:"+actor.Device.ID, 240) {
|
||||
return
|
||||
}
|
||||
if err = a.groups.MarkSeen(r.Context(), group, actor.User.ID, actor.Device.ID, sequence); err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func (a *API) createGroup(w http.ResponseWriter, r *http.Request, _ string, id a
|
||||
}
|
||||
|
||||
func (a *API) listGroups(w http.ResponseWriter, r *http.Request, _ string, id account.Identity) {
|
||||
result, err := a.groups.List(r.Context(), id.User.ID)
|
||||
result, err := a.groups.List(r.Context(), id.User.ID, id.Device.ID)
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user