2020-05-28 11:15:21 +01:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-08-19 15:38:27 +01:00
|
|
|
"fmt"
|
2020-05-28 11:15:21 +01:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
2020-08-19 13:24:54 +01:00
|
|
|
type MembershipUpdater struct {
|
2020-05-28 11:15:21 +01:00
|
|
|
transaction
|
|
|
|
d *Database
|
|
|
|
roomNID types.RoomNID
|
|
|
|
targetUserNID types.EventStateKeyNID
|
2022-07-22 14:44:04 +01:00
|
|
|
oldMembership tables.MembershipState
|
2020-05-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipUpdater(
|
2020-08-19 13:24:54 +01:00
|
|
|
ctx context.Context, d *Database, txn *sql.Tx, roomID, targetUserID string,
|
2020-05-28 11:15:21 +01:00
|
|
|
targetLocal bool, roomVersion gomatrixserverlib.RoomVersion,
|
2020-08-19 13:24:54 +01:00
|
|
|
) (*MembershipUpdater, error) {
|
2020-10-13 10:20:27 +01:00
|
|
|
var roomNID types.RoomNID
|
|
|
|
var targetUserNID types.EventStateKeyNID
|
|
|
|
var err error
|
|
|
|
err = d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
2022-09-09 13:13:04 +01:00
|
|
|
roomNID, err = d.assignRoomNID(ctx, txn, roomID, roomVersion)
|
2020-10-13 10:20:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-09 13:13:04 +01:00
|
|
|
targetUserNID, err = d.assignStateKeyNID(ctx, txn, targetUserID)
|
2020-10-13 10:20:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-05-28 11:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:24:54 +01:00
|
|
|
return d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID, targetLocal)
|
2020-05-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) membershipUpdaterTxn(
|
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
|
|
|
targetLocal bool,
|
2020-08-19 13:24:54 +01:00
|
|
|
) (*MembershipUpdater, error) {
|
2020-08-19 15:38:27 +01:00
|
|
|
err := d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
|
|
|
if err := d.MembershipTable.InsertMembership(ctx, txn, roomNID, targetUserNID, targetLocal); err != nil {
|
|
|
|
return fmt.Errorf("d.MembershipTable.InsertMembership: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("u.d.Writer.Do: %w", err)
|
2020-05-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
membership, err := d.MembershipTable.SelectMembershipForUpdate(ctx, txn, roomNID, targetUserNID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:24:54 +01:00
|
|
|
return &MembershipUpdater{
|
2020-05-28 11:15:21 +01:00
|
|
|
transaction{ctx, txn}, d, roomNID, targetUserNID, membership,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvite implements types.MembershipUpdater
|
2020-08-19 13:24:54 +01:00
|
|
|
func (u *MembershipUpdater) IsInvite() bool {
|
2022-07-22 14:44:04 +01:00
|
|
|
return u.oldMembership == tables.MembershipStateInvite
|
2020-05-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsJoin implements types.MembershipUpdater
|
2020-08-19 13:24:54 +01:00
|
|
|
func (u *MembershipUpdater) IsJoin() bool {
|
2022-07-22 14:44:04 +01:00
|
|
|
return u.oldMembership == tables.MembershipStateJoin
|
2020-05-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsLeave implements types.MembershipUpdater
|
2020-08-19 13:24:54 +01:00
|
|
|
func (u *MembershipUpdater) IsLeave() bool {
|
2022-07-22 14:44:04 +01:00
|
|
|
return u.oldMembership == tables.MembershipStateLeaveOrBan
|
2020-05-28 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:26:58 +01:00
|
|
|
// IsKnock implements types.MembershipUpdater
|
|
|
|
func (u *MembershipUpdater) IsKnock() bool {
|
2022-07-22 14:44:04 +01:00
|
|
|
return u.oldMembership == tables.MembershipStateKnock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MembershipUpdater) Delete() error {
|
|
|
|
if _, err := u.d.InvitesTable.UpdateInviteRetired(u.ctx, u.txn, u.roomNID, u.targetUserNID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.d.MembershipTable.DeleteMembership(u.ctx, u.txn, u.roomNID, u.targetUserNID)
|
2021-07-22 12:26:58 +01:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:44:04 +01:00
|
|
|
func (u *MembershipUpdater) Update(newMembership tables.MembershipState, event *types.Event) (bool, []string, error) {
|
|
|
|
var inserted bool // Did the query result in a membership change?
|
|
|
|
var retired []string // Did we retire any updates in the process?
|
|
|
|
return inserted, retired, u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
2023-06-07 18:14:35 +01:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, string(event.SenderID()))
|
2020-09-08 17:48:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.AssignStateKeyNID: %w", err)
|
2020-05-28 11:15:21 +01:00
|
|
|
}
|
2022-07-22 14:44:04 +01:00
|
|
|
inserted, err = u.d.MembershipTable.UpdateMembership(u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, newMembership, event.EventNID, false)
|
2020-09-08 17:48:07 +01:00
|
|
|
if err != nil {
|
2022-07-22 14:44:04 +01:00
|
|
|
return fmt.Errorf("u.d.MembershipTable.UpdateMembership: %w", err)
|
2020-09-08 17:48:07 +01:00
|
|
|
}
|
2022-07-22 14:44:04 +01:00
|
|
|
if !inserted {
|
|
|
|
return nil
|
2022-07-20 11:39:06 +01:00
|
|
|
}
|
2022-07-22 14:44:04 +01:00
|
|
|
switch {
|
|
|
|
case u.oldMembership != tables.MembershipStateInvite && newMembership == tables.MembershipStateInvite:
|
|
|
|
inserted, err = u.d.InvitesTable.InsertInviteEvent(
|
|
|
|
u.ctx, u.txn, event.EventID(), u.roomNID, u.targetUserNID, senderUserNID, event.JSON(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.InvitesTable.InsertInviteEvent: %w", err)
|
2020-09-08 17:48:07 +01:00
|
|
|
}
|
2022-07-22 14:44:04 +01:00
|
|
|
case u.oldMembership == tables.MembershipStateInvite && newMembership != tables.MembershipStateInvite:
|
|
|
|
retired, err = u.d.InvitesTable.UpdateInviteRetired(
|
2020-09-08 17:48:07 +01:00
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.InvitesTables.UpdateInviteRetired: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 12:26:58 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|