2020-02-13 17:27:33 +00:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
|
2020-04-16 09:06:55 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-03-17 15:12:01 +00:00
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-05-26 14:42:42 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/shared"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2020-02-13 17:27:33 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Database is used to store room events and stream offsets.
|
|
|
|
type Database struct {
|
2020-05-26 14:42:42 +00:00
|
|
|
shared.Database
|
2020-05-26 15:45:28 +00:00
|
|
|
events tables.Events
|
|
|
|
eventJSON tables.EventJSON
|
2020-05-26 14:42:42 +00:00
|
|
|
eventTypes tables.EventTypes
|
|
|
|
eventStateKeys tables.EventStateKeys
|
2020-05-26 17:23:39 +00:00
|
|
|
rooms tables.Rooms
|
|
|
|
transactions tables.Transactions
|
2020-05-27 08:36:09 +00:00
|
|
|
prevEvents tables.PreviousEvents
|
2020-05-27 10:03:47 +00:00
|
|
|
invites tables.Invites
|
|
|
|
membership tables.Membership
|
2020-05-26 14:42:42 +00:00
|
|
|
db *sql.DB
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:53 +00:00
|
|
|
// Open a sqlite database.
|
2020-05-27 08:36:09 +00:00
|
|
|
// nolint: gocyclo
|
2020-02-13 17:27:33 +00:00
|
|
|
func Open(dataSourceName string) (*Database, error) {
|
|
|
|
var d Database
|
|
|
|
uri, err := url.Parse(dataSourceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var cs string
|
|
|
|
if uri.Opaque != "" { // file:filename.db
|
|
|
|
cs = uri.Opaque
|
|
|
|
} else if uri.Path != "" { // file:///path/to/filename.db
|
|
|
|
cs = uri.Path
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("no filename or path in connect string")
|
|
|
|
}
|
2020-05-21 13:40:13 +00:00
|
|
|
if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//d.db.Exec("PRAGMA journal_mode=WAL;")
|
|
|
|
//d.db.Exec("PRAGMA read_uncommitted = true;")
|
2020-02-20 09:28:03 +00:00
|
|
|
|
|
|
|
// FIXME: We are leaking connections somewhere. Setting this to 2 will eventually
|
|
|
|
// cause the roomserver to be unresponsive to new events because something will
|
|
|
|
// acquire the global mutex and never unlock it because it is waiting for a connection
|
|
|
|
// which it will never obtain.
|
|
|
|
d.db.SetMaxOpenConns(20)
|
2020-05-27 10:03:47 +00:00
|
|
|
|
2020-05-26 14:42:42 +00:00
|
|
|
d.eventStateKeys, err = NewSqliteEventStateKeysTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.eventTypes, err = NewSqliteEventTypesTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 15:45:28 +00:00
|
|
|
d.eventJSON, err = NewSqliteEventJSONTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.events, err = NewSqliteEventsTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 17:23:39 +00:00
|
|
|
d.rooms, err = NewSqliteRoomsTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.transactions, err = NewSqliteTransactionsTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-27 08:36:09 +00:00
|
|
|
stateBlock, err := NewSqliteStateBlockTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stateSnapshot, err := NewSqliteStateSnapshotTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.prevEvents, err = NewSqlitePrevEventsTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
roomAliases, err := NewSqliteRoomAliasesTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-27 10:03:47 +00:00
|
|
|
d.invites, err = NewSqliteInvitesTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.membership, err = NewSqliteMembershipTable(d.db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 14:42:42 +00:00
|
|
|
d.Database = shared.Database{
|
2020-05-26 17:23:39 +00:00
|
|
|
DB: d.db,
|
2020-05-26 15:45:28 +00:00
|
|
|
EventsTable: d.events,
|
2020-05-26 14:42:42 +00:00
|
|
|
EventTypesTable: d.eventTypes,
|
|
|
|
EventStateKeysTable: d.eventStateKeys,
|
2020-05-26 15:45:28 +00:00
|
|
|
EventJSONTable: d.eventJSON,
|
2020-05-26 17:23:39 +00:00
|
|
|
RoomsTable: d.rooms,
|
|
|
|
TransactionsTable: d.transactions,
|
2020-05-27 08:36:09 +00:00
|
|
|
StateBlockTable: stateBlock,
|
|
|
|
StateSnapshotTable: stateSnapshot,
|
|
|
|
PrevEventsTable: d.prevEvents,
|
|
|
|
RoomAliasesTable: roomAliases,
|
2020-05-27 10:03:47 +00:00
|
|
|
InvitesTable: d.invites,
|
|
|
|
MembershipTable: d.membership,
|
2020-05-26 14:42:42 +00:00
|
|
|
}
|
2020-02-13 17:27:33 +00:00
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) assignRoomNID(
|
2020-03-16 16:05:29 +00:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
2020-02-13 17:27:33 +00:00
|
|
|
) (roomNID types.RoomNID, err error) {
|
|
|
|
// Check if we already have a numeric ID in the database.
|
2020-05-26 17:23:39 +00:00
|
|
|
roomNID, err = d.rooms.SelectRoomNID(ctx, txn, roomID)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
2020-05-26 17:23:39 +00:00
|
|
|
roomNID, err = d.rooms.InsertRoomNID(ctx, txn, roomID, roomVersion)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err == nil {
|
|
|
|
// Now get the numeric ID back out of the database
|
2020-05-26 17:23:39 +00:00
|
|
|
roomNID, err = d.rooms.SelectRoomNID(ctx, txn, roomID)
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) assignStateKeyNID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
|
|
|
) (eventStateKeyNID types.EventStateKeyNID, err error) {
|
|
|
|
// Check if we already have a numeric ID in the database.
|
2020-05-26 14:42:42 +00:00
|
|
|
eventStateKeyNID, err = d.eventStateKeys.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
2020-05-26 14:42:42 +00:00
|
|
|
eventStateKeyNID, err = d.eventStateKeys.InsertEventStateKeyNID(ctx, txn, eventStateKey)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We raced with another insert so run the select again.
|
2020-05-26 14:42:42 +00:00
|
|
|
eventStateKeyNID, err = d.eventStateKeys.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLatestEventsForUpdate implements input.EventDatabase
|
|
|
|
func (d *Database) GetLatestEventsForUpdate(
|
|
|
|
ctx context.Context, roomNID types.RoomNID,
|
|
|
|
) (types.RoomRecentEventsUpdater, error) {
|
|
|
|
txn, err := d.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eventNIDs, lastEventNIDSent, currentStateSnapshotNID, err :=
|
2020-05-26 17:23:39 +00:00
|
|
|
d.rooms.SelectLatestEventsNIDsForUpdate(ctx, txn, roomNID)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
txn.Rollback() // nolint: errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 15:45:28 +00:00
|
|
|
stateAndRefs, err := d.events.BulkSelectStateAtEventAndReference(ctx, txn, eventNIDs)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
txn.Rollback() // nolint: errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var lastEventIDSent string
|
|
|
|
if lastEventNIDSent != 0 {
|
2020-05-26 15:45:28 +00:00
|
|
|
lastEventIDSent, err = d.events.SelectEventID(ctx, txn, lastEventNIDSent)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
txn.Rollback() // nolint: errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: we probably want to support long-lived txns in sqlite somehow, but we don't because we get
|
|
|
|
// 'database is locked' errors caused by multiple write txns (one being the long-lived txn created here)
|
|
|
|
// so for now let's not use a long-lived txn at all, and just commit it here and set the txn to nil so
|
|
|
|
// we fail fast if someone tries to use the underlying txn object.
|
|
|
|
err = txn.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &roomRecentEventsUpdater{
|
|
|
|
transaction{ctx, nil}, d, roomNID, stateAndRefs, lastEventIDSent, currentStateSnapshotNID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type roomRecentEventsUpdater struct {
|
|
|
|
transaction
|
|
|
|
d *Database
|
|
|
|
roomNID types.RoomNID
|
|
|
|
latestEvents []types.StateAtEventAndReference
|
|
|
|
lastEventIDSent string
|
|
|
|
currentStateSnapshotNID types.StateSnapshotNID
|
|
|
|
}
|
|
|
|
|
2020-04-03 13:29:06 +00:00
|
|
|
// RoomVersion implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) RoomVersion() (version gomatrixserverlib.RoomVersion) {
|
|
|
|
version, _ = u.d.GetRoomVersionForRoomNID(u.ctx, u.roomNID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:27:33 +00:00
|
|
|
// LatestEvents implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) LatestEvents() []types.StateAtEventAndReference {
|
|
|
|
return u.latestEvents
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastEventIDSent implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) LastEventIDSent() string {
|
|
|
|
return u.lastEventIDSent
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentStateSnapshotNID implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) CurrentStateSnapshotNID() types.StateSnapshotNID {
|
|
|
|
return u.currentStateSnapshotNID
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorePreviousEvents implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) StorePreviousEvents(eventNID types.EventNID, previousEventReferences []gomatrixserverlib.EventReference) error {
|
2020-05-21 13:40:13 +00:00
|
|
|
err := internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-02-13 17:27:33 +00:00
|
|
|
for _, ref := range previousEventReferences {
|
2020-05-27 08:36:09 +00:00
|
|
|
if err := u.d.prevEvents.InsertPreviousEvent(u.ctx, txn, ref.EventID, ref.EventSHA256, eventNID); err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsReferenced implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) IsReferenced(eventReference gomatrixserverlib.EventReference) (res bool, err error) {
|
2020-05-21 13:40:13 +00:00
|
|
|
err = internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-05-27 08:36:09 +00:00
|
|
|
err := u.d.prevEvents.SelectPreviousEventExists(u.ctx, txn, eventReference.EventID, eventReference.EventSHA256)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err == nil {
|
|
|
|
res = true
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
res = false
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLatestEvents implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) SetLatestEvents(
|
|
|
|
roomNID types.RoomNID, latest []types.StateAtEventAndReference, lastEventNIDSent types.EventNID,
|
|
|
|
currentStateSnapshotNID types.StateSnapshotNID,
|
|
|
|
) error {
|
2020-05-21 13:40:13 +00:00
|
|
|
err := internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-02-13 17:27:33 +00:00
|
|
|
eventNIDs := make([]types.EventNID, len(latest))
|
|
|
|
for i := range latest {
|
|
|
|
eventNIDs[i] = latest[i].EventNID
|
|
|
|
}
|
2020-05-26 17:23:39 +00:00
|
|
|
return u.d.rooms.UpdateLatestEventNIDs(u.ctx, txn, roomNID, eventNIDs, lastEventNIDSent, currentStateSnapshotNID)
|
2020-02-13 17:27:33 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasEventBeenSent implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) HasEventBeenSent(eventNID types.EventNID) (res bool, err error) {
|
2020-05-21 13:40:13 +00:00
|
|
|
err = internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-05-26 15:45:28 +00:00
|
|
|
res, err = u.d.events.SelectEventSentToOutput(u.ctx, txn, eventNID)
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkEventAsSent implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) MarkEventAsSent(eventNID types.EventNID) error {
|
2020-05-21 13:40:13 +00:00
|
|
|
err := internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-05-26 15:45:28 +00:00
|
|
|
return u.d.events.UpdateEventSentToOutput(u.ctx, txn, eventNID)
|
2020-02-13 17:27:33 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:03:06 +00:00
|
|
|
func (u *roomRecentEventsUpdater) MembershipUpdater(targetUserNID types.EventStateKeyNID, targetLocal bool) (mu types.MembershipUpdater, err error) {
|
2020-05-21 13:40:13 +00:00
|
|
|
err = internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-05-20 17:03:06 +00:00
|
|
|
mu, err = u.d.membershipUpdaterTxn(u.ctx, txn, u.roomNID, targetUserNID, targetLocal)
|
2020-02-13 17:27:33 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MembershipUpdater implements input.RoomEventDatabase
|
|
|
|
func (d *Database) MembershipUpdater(
|
|
|
|
ctx context.Context, roomID, targetUserID string,
|
2020-05-20 17:03:06 +00:00
|
|
|
targetLocal bool, roomVersion gomatrixserverlib.RoomVersion,
|
2020-02-20 09:28:03 +00:00
|
|
|
) (updater types.MembershipUpdater, err error) {
|
|
|
|
var txn *sql.Tx
|
|
|
|
txn, err = d.db.Begin()
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
succeeded := false
|
|
|
|
defer func() {
|
|
|
|
if !succeeded {
|
|
|
|
txn.Rollback() // nolint: errcheck
|
2020-02-20 09:28:03 +00:00
|
|
|
} else {
|
|
|
|
// TODO: We should be holding open this transaction but we cannot have
|
|
|
|
// multiple write transactions on sqlite. The code will perform additional
|
|
|
|
// write transactions independent of this one which will consistently cause
|
|
|
|
// 'database is locked' errors. For now, we'll break up the transaction and
|
|
|
|
// hope we don't race too catastrophically. Long term, we should be able to
|
|
|
|
// thread in txn objects where appropriate (either at the interface level or
|
|
|
|
// bring matrix business logic into the storage layer).
|
|
|
|
txerr := txn.Commit()
|
|
|
|
if err == nil && txerr != nil {
|
|
|
|
err = txerr
|
|
|
|
}
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-04-03 13:29:06 +00:00
|
|
|
roomNID, err := d.assignRoomNID(ctx, txn, roomID, roomVersion)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetUserNID, err := d.assignStateKeyNID(ctx, txn, targetUserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:03:06 +00:00
|
|
|
updater, err = d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID, targetLocal)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
succeeded = true
|
|
|
|
return updater, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type membershipUpdater struct {
|
|
|
|
transaction
|
|
|
|
d *Database
|
|
|
|
roomNID types.RoomNID
|
|
|
|
targetUserNID types.EventStateKeyNID
|
2020-05-27 10:03:47 +00:00
|
|
|
membership tables.MembershipState
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) membershipUpdaterTxn(
|
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
2020-05-20 17:03:06 +00:00
|
|
|
targetLocal bool,
|
2020-02-13 17:27:33 +00:00
|
|
|
) (types.MembershipUpdater, error) {
|
|
|
|
|
2020-05-27 10:03:47 +00:00
|
|
|
if err := d.membership.InsertMembership(ctx, txn, roomNID, targetUserNID, targetLocal); err != nil {
|
2020-02-13 17:27:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:03:47 +00:00
|
|
|
membership, err := d.membership.SelectMembershipForUpdate(ctx, txn, roomNID, targetUserNID)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &membershipUpdater{
|
2020-02-20 09:28:03 +00:00
|
|
|
// purposefully set the txn to nil so if we try to use it we panic and fail fast
|
|
|
|
transaction{ctx, nil}, d, roomNID, targetUserNID, membership,
|
2020-02-13 17:27:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvite implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) IsInvite() bool {
|
2020-05-27 10:03:47 +00:00
|
|
|
return u.membership == tables.MembershipStateInvite
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsJoin implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) IsJoin() bool {
|
2020-05-27 10:03:47 +00:00
|
|
|
return u.membership == tables.MembershipStateJoin
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsLeave implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) IsLeave() bool {
|
2020-05-27 10:03:47 +00:00
|
|
|
return u.membership == tables.MembershipStateLeaveOrBan
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetToInvite implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) SetToInvite(event gomatrixserverlib.Event) (inserted bool, err error) {
|
2020-05-21 13:40:13 +00:00
|
|
|
err = internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-02-13 17:27:33 +00:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, txn, event.Sender())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-27 10:03:47 +00:00
|
|
|
inserted, err = u.d.invites.InsertInviteEvent(
|
2020-02-13 17:27:33 +00:00
|
|
|
u.ctx, txn, event.EventID(), u.roomNID, u.targetUserNID, senderUserNID, event.JSON(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-27 10:03:47 +00:00
|
|
|
if u.membership != tables.MembershipStateInvite {
|
|
|
|
if err = u.d.membership.UpdateMembership(
|
|
|
|
u.ctx, txn, u.roomNID, u.targetUserNID, senderUserNID, tables.MembershipStateInvite, 0,
|
2020-02-13 17:27:33 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetToJoin implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) SetToJoin(senderUserID string, eventID string, isUpdate bool) (inviteEventIDs []string, err error) {
|
2020-05-21 13:40:13 +00:00
|
|
|
err = internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-02-13 17:27:33 +00:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, txn, senderUserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a join event update, there is no invite to update
|
|
|
|
if !isUpdate {
|
2020-05-27 10:03:47 +00:00
|
|
|
inviteEventIDs, err = u.d.invites.UpdateInviteRetired(
|
2020-02-13 17:27:33 +00:00
|
|
|
u.ctx, txn, u.roomNID, u.targetUserNID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the NID of the new join event
|
|
|
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:03:47 +00:00
|
|
|
if u.membership != tables.MembershipStateJoin || isUpdate {
|
|
|
|
if err = u.d.membership.UpdateMembership(
|
2020-02-13 17:27:33 +00:00
|
|
|
u.ctx, txn, u.roomNID, u.targetUserNID, senderUserNID,
|
2020-05-27 10:03:47 +00:00
|
|
|
tables.MembershipStateJoin, nIDs[eventID],
|
2020-02-13 17:27:33 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetToLeave implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) SetToLeave(senderUserID string, eventID string) (inviteEventIDs []string, err error) {
|
2020-05-21 13:40:13 +00:00
|
|
|
err = internal.WithTransaction(u.d.db, func(txn *sql.Tx) error {
|
2020-02-13 17:27:33 +00:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, txn, senderUserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-27 10:03:47 +00:00
|
|
|
inviteEventIDs, err = u.d.invites.UpdateInviteRetired(
|
2020-02-13 17:27:33 +00:00
|
|
|
u.ctx, txn, u.roomNID, u.targetUserNID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the NID of the new leave event
|
|
|
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:03:47 +00:00
|
|
|
if u.membership != tables.MembershipStateLeaveOrBan {
|
|
|
|
if err = u.d.membership.UpdateMembership(
|
2020-02-13 17:27:33 +00:00
|
|
|
u.ctx, txn, u.roomNID, u.targetUserNID, senderUserNID,
|
2020-05-27 10:03:47 +00:00
|
|
|
tables.MembershipStateLeaveOrBan, nIDs[eventID],
|
2020-02-13 17:27:33 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type transaction struct {
|
|
|
|
ctx context.Context
|
|
|
|
txn *sql.Tx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit implements types.Transaction
|
|
|
|
func (t *transaction) Commit() error {
|
|
|
|
if t.txn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.txn.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback implements types.Transaction
|
|
|
|
func (t *transaction) Rollback() error {
|
|
|
|
if t.txn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.txn.Rollback()
|
|
|
|
}
|