增加一次性群邀请链接和二维码
This commit is contained in:
1 parent
dfeb5bfefa
commit
5ec9c81f90
18 files changed
+727
-14
No files matched your search
@@ -0,0 +1,97 @@
|
||||
package groups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *Store) SetInviteLink(ctx context.Context, group, user, device string, digest []byte) (time.Time, error) {
|
||||
var expiry time.Time
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return expiry, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
_, owner, err := lockTransport(ctx, tx, group, user, device)
|
||||
if err != nil {
|
||||
return expiry, err
|
||||
}
|
||||
if !owner {
|
||||
return expiry, ErrAction
|
||||
}
|
||||
if digest == nil {
|
||||
_, err = tx.Exec(ctx, "DELETE FROM group_invite_links WHERE group_id=$1", group)
|
||||
} else {
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO group_invite_links(group_id,token_hash,expires_at) VALUES($1,$2,now()+interval '24 hours')
|
||||
ON CONFLICT(group_id) DO UPDATE SET token_hash=EXCLUDED.token_hash, expires_at=EXCLUDED.expires_at
|
||||
RETURNING expires_at
|
||||
`, group, digest).Scan(&expiry)
|
||||
}
|
||||
if err != nil {
|
||||
return expiry, err
|
||||
}
|
||||
return expiry, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) RedeemInviteLink(ctx context.Context, user, device string, digest []byte) (string, []string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var group, owner string
|
||||
// All membership changes lock the group row before touching its invitation.
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT g.id,g.owner_id FROM chat_groups g JOIN group_invite_links l ON l.group_id=g.id
|
||||
WHERE l.token_hash=$1 AND l.expires_at>now() FOR UPDATE OF g
|
||||
`, digest).Scan(&group, &owner)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil, ErrUnavailable
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
// Recheck the token after acquiring the group lock so rotation and redemption serialize.
|
||||
var valid bool
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM group_invite_links WHERE group_id=$1 AND token_hash=$2 AND expires_at>clock_timestamp())
|
||||
`, group, digest).Scan(&valid)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if !valid {
|
||||
return "", nil, ErrUnavailable
|
||||
}
|
||||
var currentDevice string
|
||||
err = tx.QueryRow(ctx, "SELECT id FROM devices WHERE id=$1 AND user_id=$2 AND revoked_at IS NULL FOR UPDATE", device, user).Scan(¤tDevice)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil, ErrUnavailable
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if err = checkMemberLimit(ctx, tx, group); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
INSERT INTO group_members(group_id,user_id,state) VALUES($1,$2,'invited') ON CONFLICT DO NOTHING
|
||||
`, group, user)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return "", nil, ErrAction
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM group_invite_links WHERE group_id=$1", group); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
users, err := groupChangeAudience(ctx, tx, group, owner, "managers")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return group, append(users, user), tx.Commit(ctx)
|
||||
}
|
||||
@@ -289,6 +289,10 @@ func changeActive(ctx context.Context, tx pgx.Tx, user, id, owner, role string,
|
||||
return false, ErrAction
|
||||
}
|
||||
_, err = tx.Exec(ctx, "UPDATE group_members SET role='member' WHERE group_id=$1 AND user_id IN ($2,$3)", id, owner, change.Target)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
_, err = tx.Exec(ctx, "DELETE FROM group_invite_links WHERE group_id=$1", id)
|
||||
// The new owner establishes a new authenticated MLS session for the same roster.
|
||||
return true, err
|
||||
case "remove", "promote", "demote":
|
||||
@@ -302,13 +306,9 @@ func changeActive(ctx context.Context, tx pgx.Tx, user, id, owner, role string,
|
||||
}
|
||||
|
||||
func invite(ctx context.Context, tx pgx.Tx, user, id, target string) error {
|
||||
var count int
|
||||
if err := tx.QueryRow(ctx, "SELECT count(*) FROM group_members WHERE group_id=$1", id).Scan(&count); err != nil {
|
||||
if err := checkMemberLimit(ctx, tx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if count >= 32 {
|
||||
return ErrLimit
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
INSERT INTO group_members(group_id,user_id,state)
|
||||
SELECT $1,$3,'invited' FROM contact_relationships
|
||||
@@ -322,6 +322,17 @@ func invite(ctx context.Context, tx pgx.Tx, user, id, target string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func checkMemberLimit(ctx context.Context, tx pgx.Tx, id string) error {
|
||||
var count int
|
||||
if err := tx.QueryRow(ctx, "SELECT count(*) FROM group_members WHERE group_id=$1", id).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count >= 32 {
|
||||
return ErrLimit
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func changeMember(ctx context.Context, tx pgx.Tx, id, role string, change Change) (bool, error) {
|
||||
if change.Action == "remove" {
|
||||
var state string
|
||||
|
||||
Reference in new issue
Block a user