2020-05-28 12:15:21 +02:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-08-19 16:38:27 +02:00
|
|
|
"fmt"
|
2020-05-28 12:15:21 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:24:54 +02:00
|
|
|
type MembershipUpdater struct {
|
2020-05-28 12:15:21 +02:00
|
|
|
transaction
|
|
|
|
d *Database
|
|
|
|
roomNID types.RoomNID
|
|
|
|
targetUserNID types.EventStateKeyNID
|
2022-07-22 15:44:04 +02:00
|
|
|
oldMembership tables.MembershipState
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipUpdater(
|
2020-08-19 14:24:54 +02:00
|
|
|
ctx context.Context, d *Database, txn *sql.Tx, roomID, targetUserID string,
|
2020-05-28 12:15:21 +02:00
|
|
|
targetLocal bool, roomVersion gomatrixserverlib.RoomVersion,
|
2020-08-19 14:24:54 +02:00
|
|
|
) (*MembershipUpdater, error) {
|
2020-10-13 11:20:27 +02: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-03-17 19:24:27 +01:00
|
|
|
roomNID, err = d.assignRoomNID(ctx, roomID, roomVersion)
|
2020-10-13 11:20:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-17 19:24:27 +01:00
|
|
|
targetUserNID, err = d.assignStateKeyNID(ctx, targetUserID)
|
2020-10-13 11:20:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-05-28 12:15:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:24:54 +02:00
|
|
|
return d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID, targetLocal)
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) membershipUpdaterTxn(
|
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
|
|
|
targetLocal bool,
|
2020-08-19 14:24:54 +02:00
|
|
|
) (*MembershipUpdater, error) {
|
2020-08-19 16:38:27 +02: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 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
membership, err := d.MembershipTable.SelectMembershipForUpdate(ctx, txn, roomNID, targetUserNID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:24:54 +02:00
|
|
|
return &MembershipUpdater{
|
2020-05-28 12:15:21 +02:00
|
|
|
transaction{ctx, txn}, d, roomNID, targetUserNID, membership,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvite implements types.MembershipUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *MembershipUpdater) IsInvite() bool {
|
2022-07-22 15:44:04 +02:00
|
|
|
return u.oldMembership == tables.MembershipStateInvite
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsJoin implements types.MembershipUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *MembershipUpdater) IsJoin() bool {
|
2022-07-22 15:44:04 +02:00
|
|
|
return u.oldMembership == tables.MembershipStateJoin
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsLeave implements types.MembershipUpdater
|
2020-08-19 14:24:54 +02:00
|
|
|
func (u *MembershipUpdater) IsLeave() bool {
|
2022-07-22 15:44:04 +02:00
|
|
|
return u.oldMembership == tables.MembershipStateLeaveOrBan
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 13:26:58 +02:00
|
|
|
// IsKnock implements types.MembershipUpdater
|
|
|
|
func (u *MembershipUpdater) IsKnock() bool {
|
2022-07-22 15:44:04 +02: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 13:26:58 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 15:44:04 +02: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 {
|
2022-03-17 19:24:27 +01:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, event.Sender())
|
2020-09-08 18:48:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.AssignStateKeyNID: %w", err)
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
2022-07-22 15:44:04 +02:00
|
|
|
inserted, err = u.d.MembershipTable.UpdateMembership(u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, newMembership, event.EventNID, false)
|
2020-09-08 18:48:07 +02:00
|
|
|
if err != nil {
|
2022-07-22 15:44:04 +02:00
|
|
|
return fmt.Errorf("u.d.MembershipTable.UpdateMembership: %w", err)
|
2020-09-08 18:48:07 +02:00
|
|
|
}
|
2022-07-22 15:44:04 +02:00
|
|
|
if !inserted {
|
|
|
|
return nil
|
2022-07-20 12:39:06 +02:00
|
|
|
}
|
2022-07-22 15:44:04 +02: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 18:48:07 +02:00
|
|
|
}
|
2022-07-22 15:44:04 +02:00
|
|
|
case u.oldMembership == tables.MembershipStateInvite && newMembership != tables.MembershipStateInvite:
|
|
|
|
retired, err = u.d.InvitesTable.UpdateInviteRetired(
|
2020-09-08 18:48:07 +02:00
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.InvitesTables.UpdateInviteRetired: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 13:26:58 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|