2020-05-28 10:15:21 +00:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-08-19 14:38:27 +00:00
|
|
|
"fmt"
|
2020-05-28 10:15:21 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
2020-08-19 12:24:54 +00:00
|
|
|
type MembershipUpdater struct {
|
2020-05-28 10:15:21 +00:00
|
|
|
transaction
|
|
|
|
d *Database
|
|
|
|
roomNID types.RoomNID
|
|
|
|
targetUserNID types.EventStateKeyNID
|
|
|
|
membership tables.MembershipState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipUpdater(
|
2020-08-19 12:24:54 +00:00
|
|
|
ctx context.Context, d *Database, txn *sql.Tx, roomID, targetUserID string,
|
2020-05-28 10:15:21 +00:00
|
|
|
targetLocal bool, roomVersion gomatrixserverlib.RoomVersion,
|
2020-08-19 12:24:54 +00:00
|
|
|
) (*MembershipUpdater, error) {
|
2020-10-13 09:20:27 +00:00
|
|
|
var roomNID types.RoomNID
|
|
|
|
var targetUserNID types.EventStateKeyNID
|
|
|
|
var err error
|
|
|
|
err = d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
|
|
|
roomNID, err = d.assignRoomNID(ctx, txn, roomID, roomVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-28 10:15:21 +00:00
|
|
|
|
2020-10-13 09:20:27 +00:00
|
|
|
targetUserNID, err = d.assignStateKeyNID(ctx, txn, targetUserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-05-28 10:15:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 12:24:54 +00:00
|
|
|
return d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID, targetLocal)
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) membershipUpdaterTxn(
|
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
|
|
|
targetLocal bool,
|
2020-08-19 12:24:54 +00:00
|
|
|
) (*MembershipUpdater, error) {
|
2020-08-19 14:38:27 +00: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 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
membership, err := d.MembershipTable.SelectMembershipForUpdate(ctx, txn, roomNID, targetUserNID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 12:24:54 +00:00
|
|
|
return &MembershipUpdater{
|
2020-05-28 10:15:21 +00:00
|
|
|
transaction{ctx, txn}, d, roomNID, targetUserNID, membership,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvite implements types.MembershipUpdater
|
2020-08-19 12:24:54 +00:00
|
|
|
func (u *MembershipUpdater) IsInvite() bool {
|
2020-05-28 10:15:21 +00:00
|
|
|
return u.membership == tables.MembershipStateInvite
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsJoin implements types.MembershipUpdater
|
2020-08-19 12:24:54 +00:00
|
|
|
func (u *MembershipUpdater) IsJoin() bool {
|
2020-05-28 10:15:21 +00:00
|
|
|
return u.membership == tables.MembershipStateJoin
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLeave implements types.MembershipUpdater
|
2020-08-19 12:24:54 +00:00
|
|
|
func (u *MembershipUpdater) IsLeave() bool {
|
2020-05-28 10:15:21 +00:00
|
|
|
return u.membership == tables.MembershipStateLeaveOrBan
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:26:58 +00:00
|
|
|
// IsKnock implements types.MembershipUpdater
|
|
|
|
func (u *MembershipUpdater) IsKnock() bool {
|
|
|
|
return u.membership == tables.MembershipStateKnock
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:15:21 +00:00
|
|
|
// SetToInvite implements types.MembershipUpdater
|
2020-08-19 12:24:54 +00:00
|
|
|
func (u *MembershipUpdater) SetToInvite(event gomatrixserverlib.Event) (bool, error) {
|
2020-09-08 16:48:07 +00:00
|
|
|
var inserted bool
|
|
|
|
err := u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, event.Sender())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.AssignStateKeyNID: %w", err)
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
2020-09-08 16:48:07 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
if u.membership != tables.MembershipStateInvite {
|
2020-11-05 10:19:23 +00:00
|
|
|
if err = u.d.MembershipTable.UpdateMembership(u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, tables.MembershipStateInvite, 0, false); err != nil {
|
2020-09-08 16:48:07 +00:00
|
|
|
return fmt.Errorf("u.d.MembershipTable.UpdateMembership: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return inserted, err
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetToJoin implements types.MembershipUpdater
|
2020-08-19 12:24:54 +00:00
|
|
|
func (u *MembershipUpdater) SetToJoin(senderUserID string, eventID string, isUpdate bool) ([]string, error) {
|
2020-05-28 10:15:21 +00:00
|
|
|
var inviteEventIDs []string
|
|
|
|
|
2020-09-08 16:48:07 +00:00
|
|
|
err := u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, senderUserID)
|
2020-05-28 10:15:21 +00:00
|
|
|
if err != nil {
|
2020-09-08 16:48:07 +00:00
|
|
|
return fmt.Errorf("u.d.AssignStateKeyNID: %w", err)
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:48:07 +00:00
|
|
|
// If this is a join event update, there is no invite to update
|
|
|
|
if !isUpdate {
|
|
|
|
inviteEventIDs, err = u.d.InvitesTable.UpdateInviteRetired(
|
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.InvitesTables.UpdateInviteRetired: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:15:21 +00:00
|
|
|
|
2020-09-08 16:48:07 +00:00
|
|
|
// Look up the NID of the new join event
|
|
|
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.EventNIDs: %w", err)
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:48:07 +00:00
|
|
|
if u.membership != tables.MembershipStateJoin || isUpdate {
|
2020-11-05 10:19:23 +00:00
|
|
|
if err = u.d.MembershipTable.UpdateMembership(u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, tables.MembershipStateJoin, nIDs[eventID], false); err != nil {
|
2020-09-08 16:48:07 +00:00
|
|
|
return fmt.Errorf("u.d.MembershipTable.UpdateMembership: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return inviteEventIDs, err
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetToLeave implements types.MembershipUpdater
|
2020-08-19 12:24:54 +00:00
|
|
|
func (u *MembershipUpdater) SetToLeave(senderUserID string, eventID string) ([]string, error) {
|
2020-09-08 16:48:07 +00:00
|
|
|
var inviteEventIDs []string
|
2020-05-28 10:15:21 +00:00
|
|
|
|
2020-09-08 16:48:07 +00:00
|
|
|
err := u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, senderUserID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.AssignStateKeyNID: %w", err)
|
|
|
|
}
|
|
|
|
inviteEventIDs, err = u.d.InvitesTable.UpdateInviteRetired(
|
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.InvitesTable.updateInviteRetired: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the NID of the new leave event
|
|
|
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.EventNIDs: %w", err)
|
|
|
|
}
|
2020-05-28 10:15:21 +00:00
|
|
|
|
2020-09-08 16:48:07 +00:00
|
|
|
if u.membership != tables.MembershipStateLeaveOrBan {
|
2020-11-05 10:19:23 +00:00
|
|
|
if err = u.d.MembershipTable.UpdateMembership(u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, tables.MembershipStateLeaveOrBan, nIDs[eventID], false); err != nil {
|
2020-09-08 16:48:07 +00:00
|
|
|
return fmt.Errorf("u.d.MembershipTable.UpdateMembership: %w", err)
|
|
|
|
}
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
2020-09-08 16:48:07 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return inviteEventIDs, err
|
2020-05-28 10:15:21 +00:00
|
|
|
}
|
2021-07-22 11:26:58 +00:00
|
|
|
|
|
|
|
// SetToKnock implements types.MembershipUpdater
|
|
|
|
func (u *MembershipUpdater) SetToKnock(event *gomatrixserverlib.Event) (bool, error) {
|
|
|
|
var inserted bool
|
|
|
|
err := u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, event.Sender())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.AssignStateKeyNID: %w", err)
|
|
|
|
}
|
|
|
|
if u.membership != tables.MembershipStateKnock {
|
|
|
|
// Look up the NID of the new knock event
|
|
|
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{event.EventID()})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("u.d.EventNIDs: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = u.d.MembershipTable.UpdateMembership(u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, tables.MembershipStateKnock, nIDs[event.EventID()], false); err != nil {
|
|
|
|
return fmt.Errorf("u.d.MembershipTable.UpdateMembership: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return inserted, err
|
|
|
|
}
|