增加群聊置顶、免打扰和应用内提醒
This commit is contained in:
1 parent
ced6e9030d
commit
f54077ce46
17 files changed
+228
-42
No files matched your search
@@ -18,6 +18,12 @@ type Group struct {
|
||||
State string `json:"state"`
|
||||
Role string `json:"role"`
|
||||
Version string `json:"membershipVersion"`
|
||||
Preferences
|
||||
}
|
||||
|
||||
type Preferences struct {
|
||||
Pinned bool `json:"pinned"`
|
||||
Muted bool `json:"muted"`
|
||||
}
|
||||
|
||||
type Member struct {
|
||||
@@ -53,12 +59,12 @@ func (s *Store) Create(ctx context.Context, owner, name string) (Group, error) {
|
||||
}
|
||||
|
||||
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
|
||||
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 g.created_at DESC, g.id LIMIT 200", user)
|
||||
rows, err := s.db.Query(ctx, groupRows+" ORDER BY m.pinned DESC, g.created_at DESC, g.id LIMIT 200", user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -66,7 +72,7 @@ func (s *Store) List(ctx context.Context, user string) ([]Group, error) {
|
||||
result := make([]Group, 0)
|
||||
for rows.Next() {
|
||||
var g Group
|
||||
if err := rows.Scan(&g.ID, &g.Name, &g.State, &g.Role, &g.Version); err != nil {
|
||||
if err := rows.Scan(&g.ID, &g.Name, &g.State, &g.Role, &g.Version, &g.Pinned, &g.Muted); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, g)
|
||||
@@ -82,7 +88,7 @@ func (s *Store) Detail(ctx context.Context, user, id string) (Detail, error) {
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
err = tx.QueryRow(ctx, groupRows+" AND g.id=$2", user, id).
|
||||
Scan(&result.ID, &result.Name, &result.State, &result.Role, &result.Version)
|
||||
Scan(&result.ID, &result.Name, &result.State, &result.Role, &result.Version, &result.Pinned, &result.Muted)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return result, ErrUnavailable
|
||||
}
|
||||
@@ -115,6 +121,26 @@ func (s *Store) Detail(ctx context.Context, user, id string) (Detail, error) {
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) SetPreferences(ctx context.Context, id, user, device string, pinned, muted *bool) (Preferences, error) {
|
||||
var result Preferences
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, _, err := lockTransport(ctx, tx, id, user, device); err != nil {
|
||||
return result, err
|
||||
}
|
||||
err = tx.QueryRow(ctx, `
|
||||
UPDATE group_members SET pinned=COALESCE($3,pinned), muted=COALESCE($4,muted)
|
||||
WHERE group_id=$1 AND user_id=$2 AND state='active' RETURNING pinned,muted
|
||||
`, id, user, pinned, muted).Scan(&result.Pinned, &result.Muted)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Change(ctx context.Context, user, id string, change Change) ([]string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
|
||||
@@ -150,7 +150,12 @@ func retryEpoch(ctx context.Context, tx pgx.Tx, group, sender string, input Epoc
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) ([]string, error) {
|
||||
type EpochDevice struct {
|
||||
ID string
|
||||
Muted bool
|
||||
}
|
||||
|
||||
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) ([]EpochDevice, error) {
|
||||
version, _, err := lockTransport(ctx, tx, group, user, device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -171,37 +176,38 @@ func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch
|
||||
// Match the device revocation transaction's row lock. Once a bound device is
|
||||
// revoked the generation cannot accept any further application ciphertext.
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT d.id,d.revoked_at IS NULL FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
SELECT d.id,d.revoked_at IS NULL,m.muted FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
JOIN group_members m ON m.group_id=e.group_id AND m.user_id=d.user_id AND m.state='active'
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 ORDER BY d.id FOR UPDATE OF d
|
||||
`, group, epoch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
devices := make([]string, 0, 32)
|
||||
devices := make([]EpochDevice, 0, 32)
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var member EpochDevice
|
||||
var active bool
|
||||
if err = rows.Scan(&id, &active); err != nil {
|
||||
if err = rows.Scan(&member.ID, &active, &member.Muted); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !active {
|
||||
return nil, ErrVersion
|
||||
}
|
||||
devices = append(devices, id)
|
||||
devices = append(devices, member)
|
||||
}
|
||||
return devices, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, []string, error) {
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, []EpochDevice, bool, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
devices, err := lockSendingEpoch(ctx, tx, group, user, device, message.Epoch)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
if message.AttachmentID != nil {
|
||||
var found string
|
||||
@@ -209,10 +215,10 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
|
||||
SELECT id FROM group_attachments WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND id=$4 FOR KEY SHARE
|
||||
`, group, message.Epoch, device, message.AttachmentID).Scan(&found)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil, ErrAttachment
|
||||
return "", nil, false, ErrAttachment
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +227,7 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
|
||||
INSERT INTO group_messages(group_id,epoch_id,sender_device,message_id,ciphertext,attachment_id)
|
||||
VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING sequence::text
|
||||
`, group, message.Epoch, device, message.ID, message.Wire, message.AttachmentID).Scan(&sequence)
|
||||
created := err == nil
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
var same bool
|
||||
err = tx.QueryRow(ctx, `
|
||||
@@ -228,13 +235,13 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
|
||||
WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND message_id=$4
|
||||
`, group, message.Epoch, device, message.ID, message.Wire, message.AttachmentID).Scan(&sequence, &same)
|
||||
if errors.Is(err, pgx.ErrNoRows) || err == nil && !same {
|
||||
return "", nil, ErrVersion
|
||||
return "", nil, false, ErrVersion
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return "", nil, false, err
|
||||
}
|
||||
return sequence, devices, tx.Commit(ctx)
|
||||
return sequence, devices, created, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Inbox(ctx context.Context, group, user, device string, after int64) ([]Message, error) {
|
||||
|
||||
Reference in new issue
Block a user