按成员版本与设备名单限制群组密文投递
This commit is contained in:
1 parent
43216c205c
commit
636419e948
12 files changed
+592
-7
No files matched your search
@@ -0,0 +1,38 @@
|
||||
ALTER TABLE group_members ADD COLUMN joined_version bigint;
|
||||
UPDATE group_members m SET joined_version=g.membership_version
|
||||
FROM chat_groups g WHERE g.id=m.group_id AND m.state='active';
|
||||
ALTER TABLE group_members ADD CHECK ((state='active') = (joined_version IS NOT NULL));
|
||||
|
||||
CREATE TABLE group_epochs (
|
||||
group_id uuid NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
|
||||
id uuid NOT NULL,
|
||||
membership_version bigint NOT NULL,
|
||||
creator_device uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
retired_at timestamptz,
|
||||
PRIMARY KEY (group_id,id)
|
||||
);
|
||||
CREATE UNIQUE INDEX group_epochs_current ON group_epochs(group_id,membership_version) WHERE retired_at IS NULL;
|
||||
|
||||
CREATE TABLE group_epoch_devices (
|
||||
group_id uuid NOT NULL,
|
||||
epoch_id uuid NOT NULL,
|
||||
device_id uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
welcome bytea CHECK (octet_length(welcome) BETWEEN 16 AND 65536),
|
||||
PRIMARY KEY (group_id,epoch_id,device_id),
|
||||
FOREIGN KEY (group_id,epoch_id) REFERENCES group_epochs(group_id,id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE group_messages (
|
||||
sequence bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
group_id uuid NOT NULL,
|
||||
epoch_id uuid NOT NULL,
|
||||
sender_device uuid NOT NULL,
|
||||
message_id uuid NOT NULL,
|
||||
ciphertext bytea NOT NULL CHECK (octet_length(ciphertext) BETWEEN 16 AND 65536),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
FOREIGN KEY (group_id,epoch_id,sender_device) REFERENCES group_epoch_devices(group_id,epoch_id,device_id) ON DELETE CASCADE,
|
||||
UNIQUE (group_id,epoch_id,sender_device,message_id)
|
||||
);
|
||||
CREATE INDEX group_messages_cursor ON group_messages(group_id,sequence);
|
||||
CREATE INDEX group_messages_expiry ON group_messages(created_at);
|
||||
@@ -28,6 +28,9 @@ var attachmentsMigration string
|
||||
//go:embed 007_groups.sql
|
||||
var groupsMigration string
|
||||
|
||||
//go:embed 008_group_transport.sql
|
||||
var groupTransportMigration string
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -40,7 +43,7 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
if _, err = tx.Exec(ctx, "CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY)"); err != nil {
|
||||
return err
|
||||
}
|
||||
for index, migration := range []string{identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration, attachmentsMigration, groupsMigration} {
|
||||
for index, migration := range []string{identityMigration, contactsMigration, deliveryMigration, keyPackagesMigration, mlsCredentialsMigration, attachmentsMigration, groupsMigration, groupTransportMigration} {
|
||||
version := index + 1
|
||||
var applied bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version=$1)", version).Scan(&applied); err != nil {
|
||||
|
||||
@@ -46,8 +46,8 @@ func (s *Store) Create(ctx context.Context, owner, name string) (Group, error) {
|
||||
result := Group{Name: name, State: "active", Role: "owner", Version: "1"}
|
||||
err := s.db.QueryRow(ctx, `
|
||||
WITH created AS (INSERT INTO chat_groups(name,owner_id) VALUES($2,$1) RETURNING id)
|
||||
INSERT INTO group_members(group_id,user_id,state)
|
||||
SELECT id,$1,'active' FROM created RETURNING group_id
|
||||
INSERT INTO group_members(group_id,user_id,state,joined_version)
|
||||
SELECT id,$1,'active',1 FROM created RETURNING group_id
|
||||
`, owner, name).Scan(&result.ID)
|
||||
return result, err
|
||||
}
|
||||
@@ -124,7 +124,8 @@ func (s *Store) Change(ctx context.Context, user, id string, change Change) erro
|
||||
|
||||
// Serialize membership and version changes on the group row.
|
||||
var owner string
|
||||
err = tx.QueryRow(ctx, "SELECT owner_id FROM chat_groups WHERE id=$1 FOR UPDATE", id).Scan(&owner)
|
||||
var version int64
|
||||
err = tx.QueryRow(ctx, "SELECT owner_id,membership_version FROM chat_groups WHERE id=$1 FOR UPDATE", id).Scan(&owner, &version)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrUnavailable
|
||||
}
|
||||
@@ -147,7 +148,7 @@ func (s *Store) Change(ctx context.Context, user, id string, change Change) erro
|
||||
if state == "invited" {
|
||||
switch change.Action {
|
||||
case "accept":
|
||||
_, err = tx.Exec(ctx, "UPDATE group_members SET state='active' WHERE group_id=$1 AND user_id=$2", id, user)
|
||||
_, err = tx.Exec(ctx, "UPDATE group_members SET state='active',joined_version=$3 WHERE group_id=$1 AND user_id=$2", id, user, version+1)
|
||||
activeChanged = true
|
||||
case "decline":
|
||||
_, err = tx.Exec(ctx, "DELETE FROM group_members WHERE group_id=$1 AND user_id=$2", id, user)
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var ErrVersion = errors.New("group membership or device set changed")
|
||||
|
||||
type DeviceWelcome struct {
|
||||
Device string `json:"device"`
|
||||
Wire []byte `json:"wire,omitempty"`
|
||||
}
|
||||
|
||||
type Epoch struct {
|
||||
ID string `json:"id"`
|
||||
Version string `json:"membershipVersion"`
|
||||
Devices []DeviceWelcome `json:"devices"`
|
||||
}
|
||||
|
||||
type AvailableEpoch struct {
|
||||
ID string `json:"id"`
|
||||
Version string `json:"membershipVersion"`
|
||||
Creator string `json:"creatorDevice"`
|
||||
Welcome []byte `json:"welcome,omitempty"`
|
||||
Current bool `json:"current"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Sequence string `json:"sequence"`
|
||||
Epoch string `json:"epoch"`
|
||||
ID string `json:"id"`
|
||||
Sender string `json:"senderDevice"`
|
||||
Wire []byte `json:"wire"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
func lockTransport(ctx context.Context, tx pgx.Tx, group, user, device string) (string, bool, error) {
|
||||
var version string
|
||||
var owner bool
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT g.membership_version::text,g.owner_id=$2
|
||||
FROM chat_groups g JOIN group_members m ON m.group_id=g.id
|
||||
JOIN devices d ON d.user_id=m.user_id
|
||||
WHERE g.id=$1 AND m.user_id=$2 AND m.state='active' AND d.id=$3 AND d.revoked_at IS NULL
|
||||
FOR UPDATE OF g
|
||||
`, group, user, device).Scan(&version, &owner)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
err = ErrUnavailable
|
||||
}
|
||||
return version, owner, err
|
||||
}
|
||||
|
||||
func (s *Store) PublishEpoch(ctx context.Context, group, user, sender string, epoch Epoch) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
version, owner, err := lockTransport(ctx, tx, group, user, sender)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !owner {
|
||||
return ErrAction
|
||||
}
|
||||
if version != epoch.Version {
|
||||
return ErrVersion
|
||||
}
|
||||
|
||||
// Lock the selected devices so a revocation cannot interleave with enrollment.
|
||||
devices := make([]string, 0, len(epoch.Devices))
|
||||
for _, welcome := range epoch.Devices {
|
||||
devices = append(devices, welcome.Device)
|
||||
}
|
||||
var size, people, active int
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT count(*), count(DISTINCT user_id),
|
||||
(SELECT count(*) FROM group_members WHERE group_id=$1 AND state='active')
|
||||
FROM (
|
||||
SELECT d.user_id FROM devices d JOIN group_members m ON m.user_id=d.user_id
|
||||
JOIN device_mls_credentials c ON c.device_id=d.id
|
||||
WHERE m.group_id=$1 AND m.state='active' AND d.revoked_at IS NULL AND d.id=ANY($2::uuid[])
|
||||
ORDER BY d.id FOR UPDATE OF d
|
||||
) AS locked
|
||||
`, group, devices).Scan(&size, &people, &active)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if size != len(devices) || people != active || people != size {
|
||||
return ErrVersion
|
||||
}
|
||||
|
||||
if _, err = tx.Exec(ctx, `
|
||||
UPDATE group_epochs g SET retired_at=now()
|
||||
WHERE g.group_id=$1 AND g.membership_version=$2::bigint AND g.retired_at IS NULL AND EXISTS (
|
||||
SELECT 1 FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
WHERE e.group_id=g.group_id AND e.epoch_id=g.id AND d.revoked_at IS NOT NULL
|
||||
)
|
||||
`, group, version); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.Exec(ctx, `
|
||||
INSERT INTO group_epochs(group_id,id,membership_version,creator_device) VALUES($1,$2,$3::bigint,$4)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, group, epoch.ID, version, sender)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.RowsAffected() == 0 {
|
||||
return retryEpoch(ctx, tx, group, sender, epoch)
|
||||
}
|
||||
for _, welcome := range epoch.Devices {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO group_epoch_devices VALUES($1,$2,$3,$4)", group, epoch.ID, welcome.Device, welcome.Wire); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func retryEpoch(ctx context.Context, tx pgx.Tx, group, sender string, input Epoch) error {
|
||||
var matches bool
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM group_epochs WHERE group_id=$1 AND id=$2 AND membership_version=$3::bigint AND creator_device=$4 AND retired_at IS NULL
|
||||
) AND (SELECT count(*) FROM group_epoch_devices WHERE group_id=$1 AND epoch_id=$2)=$5
|
||||
`, group, input.ID, input.Version, sender, len(input.Devices)).Scan(&matches)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !matches {
|
||||
return ErrVersion
|
||||
}
|
||||
for _, item := range input.Devices {
|
||||
var wire []byte
|
||||
err = tx.QueryRow(ctx, "SELECT welcome FROM group_epoch_devices WHERE group_id=$1 AND epoch_id=$2 AND device_id=$3", group, input.ID, item.Device).Scan(&wire)
|
||||
if errors.Is(err, pgx.ErrNoRows) || err == nil && !bytes.Equal(wire, item.Wire) {
|
||||
return ErrVersion
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
version, _, err := lockTransport(ctx, tx, group, user, device)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var enrolled bool
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM group_epoch_devices e JOIN group_epochs g
|
||||
ON g.group_id=e.group_id AND g.id=e.epoch_id
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 AND e.device_id=$3 AND g.membership_version=$4::bigint AND g.retired_at IS NULL)
|
||||
`, group, message.Epoch, device, version).Scan(&enrolled)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !enrolled {
|
||||
return "", ErrVersion
|
||||
}
|
||||
|
||||
// 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.revoked_at IS NULL FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
|
||||
WHERE e.group_id=$1 AND e.epoch_id=$2 ORDER BY d.id FOR UPDATE OF d
|
||||
`, group, message.Epoch)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for rows.Next() {
|
||||
var active bool
|
||||
if err = rows.Scan(&active); err != nil || !active {
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", ErrVersion
|
||||
}
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var sequence string
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO group_messages(group_id,epoch_id,sender_device,message_id,ciphertext)
|
||||
VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING RETURNING sequence::text
|
||||
`, group, message.Epoch, device, message.ID, message.Wire).Scan(&sequence)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
var previous []byte
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT sequence::text,ciphertext FROM group_messages
|
||||
WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND message_id=$4
|
||||
`, group, message.Epoch, device, message.ID).Scan(&sequence, &previous)
|
||||
if err == nil && !bytes.Equal(previous, message.Wire) {
|
||||
return "", ErrVersion
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sequence, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Inbox(ctx context.Context, group, user, device string, after int64) ([]Message, error) {
|
||||
tx, err := s.db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = readAccess(ctx, tx, group, user, device); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT x.sequence::text,x.epoch_id,x.message_id,x.sender_device,x.ciphertext,x.created_at
|
||||
FROM group_messages x JOIN group_epochs g ON g.group_id=x.group_id AND g.id=x.epoch_id
|
||||
JOIN group_epoch_devices e ON e.group_id=g.group_id AND e.epoch_id=g.id AND e.device_id=$3
|
||||
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=$3 AND d.user_id=m.user_id AND d.revoked_at IS NULL
|
||||
WHERE x.group_id=$1 AND x.sequence>$4 AND g.membership_version>=m.joined_version
|
||||
ORDER BY x.sequence LIMIT 64
|
||||
`, group, user, device, after)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]Message, 0)
|
||||
for rows.Next() {
|
||||
var m Message
|
||||
if err := rows.Scan(&m.Sequence, &m.Epoch, &m.ID, &m.Sender, &m.Wire, &m.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, m)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func readAccess(ctx context.Context, tx pgx.Tx, group, user, device string) error {
|
||||
var ok bool
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM group_members m JOIN devices d ON d.user_id=m.user_id
|
||||
WHERE m.group_id=$1 AND m.user_id=$2 AND m.state='active' AND d.id=$3 AND d.revoked_at IS NULL)
|
||||
`, group, user, device).Scan(&ok)
|
||||
if err == nil && !ok {
|
||||
return ErrUnavailable
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Epochs(ctx context.Context, group, user, device string) ([]AvailableEpoch, error) {
|
||||
tx, err := s.db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if err = readAccess(ctx, tx, group, user, device); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT e.id,e.membership_version::text,e.creator_device,d.welcome,
|
||||
e.membership_version=g.membership_version AND e.retired_at IS NULL AND NOT EXISTS (
|
||||
SELECT 1 FROM group_epoch_devices x JOIN devices bound ON bound.id=x.device_id
|
||||
WHERE x.group_id=e.group_id AND x.epoch_id=e.id AND bound.revoked_at IS NOT NULL
|
||||
)
|
||||
FROM group_epochs e JOIN chat_groups g ON g.id=e.group_id
|
||||
JOIN group_epoch_devices d ON d.group_id=e.group_id AND d.epoch_id=e.id AND d.device_id=$3
|
||||
JOIN group_members m ON m.group_id=e.group_id AND m.user_id=$2
|
||||
WHERE e.group_id=$1 AND e.membership_version>=m.joined_version
|
||||
ORDER BY e.created_at DESC,e.id LIMIT 200
|
||||
`, group, user, device)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]AvailableEpoch, 0)
|
||||
for rows.Next() {
|
||||
var e AvailableEpoch
|
||||
if err = rows.Scan(&e.ID, &e.Version, &e.Creator, &e.Welcome, &e.Current); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, e)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) Cleanup(ctx context.Context, retention time.Duration) error {
|
||||
cutoff := time.Now().Add(-retention)
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM group_messages WHERE created_at<$1", cutoff); err != nil {
|
||||
return err
|
||||
}
|
||||
// Keep the current epoch and any old epoch with retained application messages.
|
||||
_, err = tx.Exec(ctx, `
|
||||
DELETE FROM group_epochs e USING chat_groups g
|
||||
WHERE e.group_id=g.id AND (e.membership_version<g.membership_version OR e.retired_at IS NOT NULL) AND e.created_at<$1
|
||||
AND NOT EXISTS(SELECT 1 FROM group_messages m WHERE m.group_id=e.group_id AND m.epoch_id=e.id)
|
||||
`, cutoff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
@@ -64,6 +64,10 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/groups", a.authorized(a.createGroup))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}", a.authorized(a.groupDetail))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}", a.authorized(a.changeGroup))
|
||||
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))
|
||||
mux.HandleFunc("POST /api/v1/groups/{id}/messages", a.authorized(a.sendGroupMessage))
|
||||
return a.boundary(mux)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xhchat/server/internal/account"
|
||||
"xhchat/server/internal/groups"
|
||||
)
|
||||
|
||||
func (a *API) groupEpochs(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
epochs, err := a.groups.Epochs(r.Context(), id, actor.User.ID, actor.Device.ID)
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, epochs)
|
||||
}
|
||||
|
||||
func (a *API) publishGroupEpoch(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-epoch:"+actor.Device.ID, 12) {
|
||||
return
|
||||
}
|
||||
|
||||
var input groups.Epoch
|
||||
if !parseLimit(w, r, &input, 3<<20) {
|
||||
return
|
||||
}
|
||||
version, err := strconv.ParseInt(input.Version, 10, 64)
|
||||
if !uuidPattern.MatchString(input.ID) || err != nil || version < 1 || strconv.FormatInt(version, 10) != input.Version ||
|
||||
len(input.Devices) < 1 || len(input.Devices) > 32 {
|
||||
problem(w, 400, "invalid_group_epoch", "群加密世代信息无效")
|
||||
return
|
||||
}
|
||||
seen := make(map[string]bool, len(input.Devices))
|
||||
for index := range input.Devices {
|
||||
device := &input.Devices[index]
|
||||
device.Device = strings.ToLower(device.Device)
|
||||
if !uuidPattern.MatchString(device.Device) || seen[device.Device] ||
|
||||
(device.Device == actor.Device.ID && len(device.Wire) != 0) ||
|
||||
(device.Device != actor.Device.ID && (!mlsFrame(device.Wire) || device.Wire[3] != 3)) {
|
||||
problem(w, 400, "invalid_group_epoch", "群设备或 Welcome 无效")
|
||||
return
|
||||
}
|
||||
seen[device.Device] = true
|
||||
}
|
||||
if !seen[actor.Device.ID] {
|
||||
problem(w, 400, "invalid_group_epoch", "创建设备必须加入群加密世代")
|
||||
return
|
||||
}
|
||||
if err := a.groups.PublishEpoch(r.Context(), id, actor.User.ID, actor.Device.ID, input); err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
func (a *API) sendGroupMessage(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-send:"+actor.Device.ID, 180) {
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
ID string `json:"id"`
|
||||
Epoch string `json:"epoch"`
|
||||
Wire []byte `json:"wire"`
|
||||
}
|
||||
if !parseLimit(w, r, &input, 96<<10) {
|
||||
return
|
||||
}
|
||||
if !uuidPattern.MatchString(input.ID) || !uuidPattern.MatchString(input.Epoch) || !mlsFrame(input.Wire) || input.Wire[3] != 2 {
|
||||
problem(w, 400, "invalid_group_message", "群消息密文无效")
|
||||
return
|
||||
}
|
||||
sequence, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire})
|
||||
if err != nil {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]string{"sequence": sequence})
|
||||
}
|
||||
|
||||
func (a *API) groupInbox(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
groupError(w, groups.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 180) {
|
||||
return
|
||||
}
|
||||
after := int64(0)
|
||||
if raw := r.URL.Query().Get("after"); raw != "" {
|
||||
var err error
|
||||
after, err = strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || after < 0 {
|
||||
problem(w, 400, "invalid_cursor", "无效的投递游标")
|
||||
return
|
||||
}
|
||||
}
|
||||
items, err := a.groups.Inbox(r.Context(), id, actor.User.ID, actor.Device.ID, after)
|
||||
if errors.Is(err, groups.ErrUnavailable) {
|
||||
groupError(w, err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, items)
|
||||
}
|
||||
@@ -23,6 +23,8 @@ func groupError(w http.ResponseWriter, err error) {
|
||||
problem(w, 409, "group_action_unavailable", "当前无法执行此群操作")
|
||||
case errors.Is(err, groups.ErrLimit):
|
||||
problem(w, 409, "group_member_limit", "群成员与待处理邀请已达上限")
|
||||
case errors.Is(err, groups.ErrVersion):
|
||||
problem(w, 409, "group_epoch_changed", "群成员或设备版本已变更")
|
||||
default:
|
||||
unavailable(w, err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user