2017-04-20 22:40:52 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-02-03 13:52:32 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-09-13 15:30:19 +00:00
|
|
|
"context"
|
2017-02-03 13:52:32 +00:00
|
|
|
"database/sql"
|
2017-08-08 15:38:03 +00:00
|
|
|
|
2017-02-03 13:52:32 +00:00
|
|
|
// Import the postgres database driver.
|
|
|
|
_ "github.com/lib/pq"
|
2018-05-26 11:03:35 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-02-03 13:52:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2017-02-07 17:20:05 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-02-03 13:52:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Database is used to store room events and stream offsets.
|
|
|
|
type Database struct {
|
|
|
|
statements statements
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a postgres database.
|
|
|
|
func Open(dataSourceName string) (*Database, error) {
|
|
|
|
var d Database
|
|
|
|
var err error
|
|
|
|
if d.db, err = sql.Open("postgres", dataSourceName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = d.statements.prepare(d.db); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:20:05 +00:00
|
|
|
// StoreEvent implements input.EventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) StoreEvent(
|
2018-05-26 11:03:35 +00:00
|
|
|
ctx context.Context, event gomatrixserverlib.Event,
|
|
|
|
txnAndDeviceID *api.TransactionID, authEventNIDs []types.EventNID,
|
2017-09-13 15:30:19 +00:00
|
|
|
) (types.RoomNID, types.StateAtEvent, error) {
|
2017-02-07 17:20:05 +00:00
|
|
|
var (
|
2017-02-15 11:05:45 +00:00
|
|
|
roomNID types.RoomNID
|
|
|
|
eventTypeNID types.EventTypeNID
|
|
|
|
eventStateKeyNID types.EventStateKeyNID
|
|
|
|
eventNID types.EventNID
|
|
|
|
stateNID types.StateSnapshotNID
|
2017-02-07 17:20:05 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2018-05-26 11:03:35 +00:00
|
|
|
if txnAndDeviceID != nil {
|
|
|
|
if err = d.statements.insertTransaction(
|
|
|
|
ctx, txnAndDeviceID.TransactionID,
|
|
|
|
txnAndDeviceID.DeviceID, event.Sender(), event.EventID(),
|
|
|
|
); err != nil {
|
|
|
|
return 0, types.StateAtEvent{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
if roomNID, err = d.assignRoomNID(ctx, nil, event.RoomID()); err != nil {
|
2017-02-15 11:05:45 +00:00
|
|
|
return 0, types.StateAtEvent{}, err
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
if eventTypeNID, err = d.assignEventTypeNID(ctx, event.Type()); err != nil {
|
2017-02-15 11:05:45 +00:00
|
|
|
return 0, types.StateAtEvent{}, err
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eventStateKey := event.StateKey()
|
|
|
|
// Assigned a numeric ID for the state_key if there is one present.
|
|
|
|
// Otherwise set the numeric ID for the state_key to 0.
|
|
|
|
if eventStateKey != nil {
|
2017-09-13 15:30:19 +00:00
|
|
|
if eventStateKeyNID, err = d.assignStateKeyNID(ctx, nil, *eventStateKey); err != nil {
|
2017-02-15 11:05:45 +00:00
|
|
|
return 0, types.StateAtEvent{}, err
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:05:45 +00:00
|
|
|
if eventNID, stateNID, err = d.statements.insertEvent(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx,
|
2017-02-07 17:20:05 +00:00
|
|
|
roomNID,
|
|
|
|
eventTypeNID,
|
|
|
|
eventStateKeyNID,
|
|
|
|
event.EventID(),
|
|
|
|
event.EventReference().EventSHA256,
|
2017-02-09 16:48:14 +00:00
|
|
|
authEventNIDs,
|
2017-06-27 14:28:44 +00:00
|
|
|
event.Depth(),
|
2017-02-07 17:20:05 +00:00
|
|
|
); err != nil {
|
2017-02-22 16:51:10 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We've already inserted the event so select the numeric event ID
|
2017-09-13 15:30:19 +00:00
|
|
|
eventNID, stateNID, err = d.statements.selectEvent(ctx, event.EventID())
|
2017-02-22 16:51:10 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, types.StateAtEvent{}, err
|
|
|
|
}
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
if err = d.statements.insertEventJSON(ctx, eventNID, event.JSON()); err != nil {
|
2017-02-15 11:05:45 +00:00
|
|
|
return 0, types.StateAtEvent{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return roomNID, types.StateAtEvent{
|
|
|
|
BeforeStateSnapshotNID: stateNID,
|
|
|
|
StateEntry: types.StateEntry{
|
|
|
|
StateKeyTuple: types.StateKeyTuple{
|
|
|
|
EventTypeNID: eventTypeNID,
|
|
|
|
EventStateKeyNID: eventStateKeyNID,
|
|
|
|
},
|
|
|
|
EventNID: eventNID,
|
|
|
|
},
|
|
|
|
}, nil
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) assignRoomNID(
|
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
|
|
) (types.RoomNID, error) {
|
2017-02-07 17:20:05 +00:00
|
|
|
// Check if we already have a numeric ID in the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
roomNID, err := d.statements.selectRoomNID(ctx, txn, roomID)
|
2017-02-07 17:20:05 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID)
|
2017-02-22 16:51:10 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We raced with another insert so run the select again.
|
2017-09-13 15:30:19 +00:00
|
|
|
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
2017-02-22 16:51:10 +00:00
|
|
|
}
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
2017-02-22 16:51:10 +00:00
|
|
|
return roomNID, err
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) assignEventTypeNID(
|
|
|
|
ctx context.Context, eventType string,
|
|
|
|
) (types.EventTypeNID, error) {
|
2017-02-07 17:20:05 +00:00
|
|
|
// Check if we already have a numeric ID in the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
eventTypeNID, err := d.statements.selectEventTypeNID(ctx, eventType)
|
2017-02-07 17:20:05 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
eventTypeNID, err = d.statements.insertEventTypeNID(ctx, eventType)
|
2017-02-22 16:51:10 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We raced with another insert so run the select again.
|
2017-09-13 15:30:19 +00:00
|
|
|
eventTypeNID, err = d.statements.selectEventTypeNID(ctx, eventType)
|
2017-02-22 16:51:10 +00:00
|
|
|
}
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
2017-02-22 16:51:10 +00:00
|
|
|
return eventTypeNID, err
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) assignStateKeyNID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
|
|
|
) (types.EventStateKeyNID, error) {
|
2017-02-07 17:20:05 +00:00
|
|
|
// Check if we already have a numeric ID in the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
eventStateKeyNID, err := d.statements.selectEventStateKeyNID(ctx, txn, eventStateKey)
|
2017-02-07 17:20:05 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
eventStateKeyNID, err = d.statements.insertEventStateKeyNID(ctx, txn, eventStateKey)
|
2017-02-22 16:51:10 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We raced with another insert so run the select again.
|
2017-09-13 15:30:19 +00:00
|
|
|
eventStateKeyNID, err = d.statements.selectEventStateKeyNID(ctx, txn, eventStateKey)
|
2017-02-22 16:51:10 +00:00
|
|
|
}
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
2017-02-22 16:51:10 +00:00
|
|
|
return eventStateKeyNID, err
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
2017-02-09 16:48:14 +00:00
|
|
|
|
|
|
|
// StateEntriesForEventIDs implements input.EventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) StateEntriesForEventIDs(
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
) ([]types.StateEntry, error) {
|
|
|
|
return d.statements.bulkSelectStateEventByID(ctx, eventIDs)
|
2017-02-09 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 15:07:18 +00:00
|
|
|
// EventTypeNIDs implements state.RoomStateDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) EventTypeNIDs(
|
|
|
|
ctx context.Context, eventTypes []string,
|
|
|
|
) (map[string]types.EventTypeNID, error) {
|
|
|
|
return d.statements.bulkSelectEventTypeNID(ctx, eventTypes)
|
2017-03-09 15:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EventStateKeyNIDs implements state.RoomStateDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) EventStateKeyNIDs(
|
|
|
|
ctx context.Context, eventStateKeys []string,
|
|
|
|
) (map[string]types.EventStateKeyNID, error) {
|
|
|
|
return d.statements.bulkSelectEventStateKeyNID(ctx, eventStateKeys)
|
2017-02-09 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 14:08:48 +00:00
|
|
|
// EventStateKeys implements query.RoomserverQueryAPIDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) EventStateKeys(
|
|
|
|
ctx context.Context, eventStateKeyNIDs []types.EventStateKeyNID,
|
|
|
|
) (map[types.EventStateKeyNID]string, error) {
|
|
|
|
return d.statements.bulkSelectEventStateKey(ctx, eventStateKeyNIDs)
|
2017-08-23 14:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EventNIDs implements query.RoomserverQueryAPIDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) EventNIDs(
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
) (map[string]types.EventNID, error) {
|
|
|
|
return d.statements.bulkSelectEventNID(ctx, eventIDs)
|
2017-06-02 13:32:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:48:14 +00:00
|
|
|
// Events implements input.EventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) Events(
|
|
|
|
ctx context.Context, eventNIDs []types.EventNID,
|
|
|
|
) ([]types.Event, error) {
|
|
|
|
eventJSONs, err := d.statements.bulkSelectEventJSON(ctx, eventNIDs)
|
2017-02-09 16:48:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results := make([]types.Event, len(eventJSONs))
|
|
|
|
for i, eventJSON := range eventJSONs {
|
|
|
|
result := &results[i]
|
|
|
|
result.EventNID = eventJSON.EventNID
|
|
|
|
// TODO: Use NewEventFromTrustedJSON for efficiency
|
|
|
|
result.Event, err = gomatrixserverlib.NewEventFromUntrustedJSON(eventJSON.EventJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
2017-02-15 11:05:45 +00:00
|
|
|
|
|
|
|
// AddState implements input.EventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) AddState(
|
|
|
|
ctx context.Context,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
state []types.StateEntry,
|
|
|
|
) (types.StateSnapshotNID, error) {
|
2017-02-15 11:05:45 +00:00
|
|
|
if len(state) > 0 {
|
2017-09-13 15:30:19 +00:00
|
|
|
stateBlockNID, err := d.statements.selectNextStateBlockNID(ctx)
|
2017-02-15 11:05:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
if err = d.statements.bulkInsertStateData(ctx, stateBlockNID, state); err != nil {
|
2017-02-15 11:05:45 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
stateBlockNIDs = append(stateBlockNIDs[:len(stateBlockNIDs):len(stateBlockNIDs)], stateBlockNID)
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
return d.statements.insertState(ctx, roomNID, stateBlockNIDs)
|
2017-02-15 11:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetState implements input.EventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) SetState(
|
|
|
|
ctx context.Context, eventNID types.EventNID, stateNID types.StateSnapshotNID,
|
|
|
|
) error {
|
|
|
|
return d.statements.updateEventState(ctx, eventNID, stateNID)
|
2017-02-15 11:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StateAtEventIDs implements input.EventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) StateAtEventIDs(
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
) ([]types.StateAtEvent, error) {
|
|
|
|
return d.statements.bulkSelectStateAtEventByID(ctx, eventIDs)
|
2017-02-15 11:05:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 15:07:18 +00:00
|
|
|
// StateBlockNIDs implements state.RoomStateDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) StateBlockNIDs(
|
|
|
|
ctx context.Context, stateNIDs []types.StateSnapshotNID,
|
|
|
|
) ([]types.StateBlockNIDList, error) {
|
|
|
|
return d.statements.bulkSelectStateBlockNIDs(ctx, stateNIDs)
|
2017-02-15 11:05:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 15:07:18 +00:00
|
|
|
// StateEntries implements state.RoomStateDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) StateEntries(
|
|
|
|
ctx context.Context, stateBlockNIDs []types.StateBlockNID,
|
|
|
|
) ([]types.StateEntryList, error) {
|
|
|
|
return d.statements.bulkSelectStateBlockEntries(ctx, stateBlockNIDs)
|
2017-02-15 11:05:45 +00:00
|
|
|
}
|
2017-02-21 14:50:30 +00:00
|
|
|
|
2017-09-06 11:38:22 +00:00
|
|
|
// SnapshotNIDFromEventID implements state.RoomStateDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) SnapshotNIDFromEventID(
|
|
|
|
ctx context.Context, eventID string,
|
|
|
|
) (types.StateSnapshotNID, error) {
|
|
|
|
_, stateNID, err := d.statements.selectEvent(ctx, eventID)
|
2017-09-06 11:38:22 +00:00
|
|
|
return stateNID, err
|
|
|
|
}
|
|
|
|
|
2017-03-07 10:25:01 +00:00
|
|
|
// EventIDs implements input.RoomEventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) EventIDs(
|
|
|
|
ctx context.Context, eventNIDs []types.EventNID,
|
|
|
|
) (map[types.EventNID]string, error) {
|
|
|
|
return d.statements.bulkSelectEventID(ctx, eventNIDs)
|
2017-03-07 10:25:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 14:50:30 +00:00
|
|
|
// GetLatestEventsForUpdate implements input.EventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) GetLatestEventsForUpdate(
|
|
|
|
ctx context.Context, roomNID types.RoomNID,
|
|
|
|
) (types.RoomRecentEventsUpdater, error) {
|
2017-02-21 14:50:30 +00:00
|
|
|
txn, err := d.db.Begin()
|
|
|
|
if err != nil {
|
2017-03-07 10:25:01 +00:00
|
|
|
return nil, err
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
eventNIDs, lastEventNIDSent, currentStateSnapshotNID, err :=
|
|
|
|
d.statements.selectLatestEventsNIDsForUpdate(ctx, txn, roomNID)
|
2017-02-21 14:50:30 +00:00
|
|
|
if err != nil {
|
2017-09-20 09:59:19 +00:00
|
|
|
txn.Rollback() // nolint: errcheck
|
2017-03-07 10:25:01 +00:00
|
|
|
return nil, err
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
stateAndRefs, err := d.statements.bulkSelectStateAtEventAndReference(ctx, txn, eventNIDs)
|
2017-02-21 14:50:30 +00:00
|
|
|
if err != nil {
|
2017-09-20 09:59:19 +00:00
|
|
|
txn.Rollback() // nolint: errcheck
|
2017-03-07 10:25:01 +00:00
|
|
|
return nil, err
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
2017-02-27 11:25:35 +00:00
|
|
|
var lastEventIDSent string
|
|
|
|
if lastEventNIDSent != 0 {
|
2017-09-13 15:30:19 +00:00
|
|
|
lastEventIDSent, err = d.statements.selectEventID(ctx, txn, lastEventNIDSent)
|
2017-02-27 11:25:35 +00:00
|
|
|
if err != nil {
|
2017-09-20 09:59:19 +00:00
|
|
|
txn.Rollback() // nolint: errcheck
|
2017-03-07 10:25:01 +00:00
|
|
|
return nil, err
|
2017-02-27 11:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 15:38:03 +00:00
|
|
|
return &roomRecentEventsUpdater{
|
2017-09-13 15:30:19 +00:00
|
|
|
transaction{ctx, txn}, d, roomNID, stateAndRefs, lastEventIDSent, currentStateSnapshotNID,
|
2017-08-08 15:38:03 +00:00
|
|
|
}, nil
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 11:03:35 +00:00
|
|
|
// GetTransactionEventID implements input.EventDatabase
|
|
|
|
func (d *Database) GetTransactionEventID(
|
|
|
|
ctx context.Context, transactionID string,
|
|
|
|
deviceID string, userID string,
|
|
|
|
) (string, error) {
|
|
|
|
eventID, err := d.statements.selectTransactionEventID(ctx, transactionID, deviceID, userID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return eventID, err
|
|
|
|
}
|
|
|
|
|
2017-02-21 14:50:30 +00:00
|
|
|
type roomRecentEventsUpdater struct {
|
2017-08-08 15:38:03 +00:00
|
|
|
transaction
|
2017-03-07 10:25:01 +00:00
|
|
|
d *Database
|
2017-08-08 15:38:03 +00:00
|
|
|
roomNID types.RoomNID
|
2017-03-07 10:25:01 +00:00
|
|
|
latestEvents []types.StateAtEventAndReference
|
|
|
|
lastEventIDSent string
|
|
|
|
currentStateSnapshotNID types.StateSnapshotNID
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 10:25:01 +00:00
|
|
|
// StorePreviousEvents implements types.RoomRecentEventsUpdater
|
2017-02-21 14:50:30 +00:00
|
|
|
func (u *roomRecentEventsUpdater) StorePreviousEvents(eventNID types.EventNID, previousEventReferences []gomatrixserverlib.EventReference) error {
|
|
|
|
for _, ref := range previousEventReferences {
|
2017-09-13 15:30:19 +00:00
|
|
|
if err := u.d.statements.insertPreviousEvent(u.ctx, u.txn, ref.EventID, ref.EventSHA256, eventNID); err != nil {
|
2017-02-21 14:50:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-07 10:25:01 +00:00
|
|
|
// IsReferenced implements types.RoomRecentEventsUpdater
|
2017-02-21 14:50:30 +00:00
|
|
|
func (u *roomRecentEventsUpdater) IsReferenced(eventReference gomatrixserverlib.EventReference) (bool, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
err := u.d.statements.selectPreviousEventExists(u.ctx, u.txn, eventReference.EventID, eventReference.EventSHA256)
|
2017-02-21 14:50:30 +00:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-03-07 10:25:01 +00:00
|
|
|
// SetLatestEvents implements types.RoomRecentEventsUpdater
|
|
|
|
func (u *roomRecentEventsUpdater) SetLatestEvents(
|
|
|
|
roomNID types.RoomNID, latest []types.StateAtEventAndReference, lastEventNIDSent types.EventNID,
|
|
|
|
currentStateSnapshotNID types.StateSnapshotNID,
|
|
|
|
) error {
|
2017-02-21 14:50:30 +00:00
|
|
|
eventNIDs := make([]types.EventNID, len(latest))
|
|
|
|
for i := range latest {
|
|
|
|
eventNIDs[i] = latest[i].EventNID
|
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
return u.d.statements.updateLatestEventNIDs(u.ctx, u.txn, roomNID, eventNIDs, lastEventNIDSent, currentStateSnapshotNID)
|
2017-02-27 11:25:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 10:25:01 +00:00
|
|
|
// HasEventBeenSent implements types.RoomRecentEventsUpdater
|
2017-02-27 11:25:35 +00:00
|
|
|
func (u *roomRecentEventsUpdater) HasEventBeenSent(eventNID types.EventNID) (bool, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
return u.d.statements.selectEventSentToOutput(u.ctx, u.txn, eventNID)
|
2017-02-27 11:25:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 10:25:01 +00:00
|
|
|
// MarkEventAsSent implements types.RoomRecentEventsUpdater
|
2017-02-27 11:25:35 +00:00
|
|
|
func (u *roomRecentEventsUpdater) MarkEventAsSent(eventNID types.EventNID) error {
|
2017-09-13 15:30:19 +00:00
|
|
|
return u.d.statements.updateEventSentToOutput(u.ctx, u.txn, eventNID)
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 15:38:03 +00:00
|
|
|
func (u *roomRecentEventsUpdater) MembershipUpdater(targetUserNID types.EventStateKeyNID) (types.MembershipUpdater, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
return u.d.membershipUpdaterTxn(u.ctx, u.txn, u.roomNID, targetUserNID)
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
2017-03-06 14:29:39 +00:00
|
|
|
|
|
|
|
// RoomNID implements query.RoomserverQueryAPIDB
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) RoomNID(ctx context.Context, roomID string) (types.RoomNID, error) {
|
|
|
|
roomNID, err := d.statements.selectRoomNID(ctx, nil, roomID)
|
2017-03-06 14:29:39 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return roomNID, err
|
|
|
|
}
|
|
|
|
|
2017-08-23 14:08:48 +00:00
|
|
|
// LatestEventIDs implements query.RoomserverQueryAPIDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) LatestEventIDs(
|
|
|
|
ctx context.Context, roomNID types.RoomNID,
|
|
|
|
) ([]gomatrixserverlib.EventReference, types.StateSnapshotNID, int64, error) {
|
|
|
|
eventNIDs, currentStateSnapshotNID, err := d.statements.selectLatestEventNIDs(ctx, roomNID)
|
2017-03-06 14:29:39 +00:00
|
|
|
if err != nil {
|
2017-06-27 14:28:44 +00:00
|
|
|
return nil, 0, 0, err
|
2017-03-06 14:29:39 +00:00
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
references, err := d.statements.bulkSelectEventReference(ctx, eventNIDs)
|
2017-03-09 15:07:18 +00:00
|
|
|
if err != nil {
|
2017-06-27 14:28:44 +00:00
|
|
|
return nil, 0, 0, err
|
2017-03-09 15:07:18 +00:00
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
depth, err := d.statements.selectMaxEventDepth(ctx, eventNIDs)
|
2017-06-27 14:28:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
return references, currentStateSnapshotNID, depth, nil
|
2017-03-09 15:07:18 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 14:08:48 +00:00
|
|
|
// GetInvitesForUser implements query.RoomserverQueryAPIDatabase
|
|
|
|
func (d *Database) GetInvitesForUser(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
2017-08-23 14:08:48 +00:00
|
|
|
) (senderUserIDs []types.EventStateKeyNID, err error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
return d.statements.selectInviteActiveForUserInRoom(ctx, targetUserNID, roomNID)
|
2017-08-23 14:08:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 10:31:43 +00:00
|
|
|
// SetRoomAlias implements alias.RoomserverAliasAPIDB
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) SetRoomAlias(ctx context.Context, alias string, roomID string) error {
|
|
|
|
return d.statements.insertRoomAlias(ctx, alias, roomID)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
// GetRoomIDForAlias implements alias.RoomserverAliasAPIDB
|
|
|
|
func (d *Database) GetRoomIDForAlias(ctx context.Context, alias string) (string, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
return d.statements.selectRoomIDFromAlias(ctx, alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
// GetAliasesForRoomID implements alias.RoomserverAliasAPIDB
|
|
|
|
func (d *Database) GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
return d.statements.selectAliasesFromRoomID(ctx, roomID)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRoomAlias implements alias.RoomserverAliasAPIDB
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) RemoveRoomAlias(ctx context.Context, alias string) error {
|
|
|
|
return d.statements.deleteRoomAlias(ctx, alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 15:07:18 +00:00
|
|
|
// StateEntriesForTuples implements state.RoomStateDatabase
|
|
|
|
func (d *Database) StateEntriesForTuples(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context,
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
stateKeyTuples []types.StateKeyTuple,
|
2017-03-09 15:07:18 +00:00
|
|
|
) ([]types.StateEntryList, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
return d.statements.bulkSelectFilteredStateBlockEntries(
|
|
|
|
ctx, stateBlockNIDs, stateKeyTuples,
|
|
|
|
)
|
2017-03-06 14:29:39 +00:00
|
|
|
}
|
2017-08-08 15:38:03 +00:00
|
|
|
|
2017-08-21 15:37:11 +00:00
|
|
|
// MembershipUpdater implements input.RoomEventDatabase
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) MembershipUpdater(
|
|
|
|
ctx context.Context, roomID, targetUserID string,
|
|
|
|
) (types.MembershipUpdater, error) {
|
2017-08-21 15:37:11 +00:00
|
|
|
txn, err := d.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
succeeded := false
|
|
|
|
defer func() {
|
|
|
|
if !succeeded {
|
2017-09-20 09:59:19 +00:00
|
|
|
txn.Rollback() // nolint: errcheck
|
2017-08-21 15:37:11 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
roomNID, err := d.assignRoomNID(ctx, txn, roomID)
|
2017-08-21 15:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
targetUserNID, err := d.assignStateKeyNID(ctx, txn, targetUserID)
|
2017-08-21 15:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
updater, err := d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID)
|
2017-08-21 15:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
succeeded = true
|
|
|
|
return updater, nil
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:38:03 +00:00
|
|
|
type membershipUpdater struct {
|
|
|
|
transaction
|
|
|
|
d *Database
|
|
|
|
roomNID types.RoomNID
|
|
|
|
targetUserNID types.EventStateKeyNID
|
|
|
|
membership membershipState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) membershipUpdaterTxn(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
2017-08-08 15:38:03 +00:00
|
|
|
) (types.MembershipUpdater, error) {
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
if err := d.statements.insertMembership(ctx, txn, roomNID, targetUserNID); err != nil {
|
2017-08-08 15:38:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
membership, err := d.statements.selectMembershipForUpdate(ctx, txn, roomNID, targetUserNID)
|
2017-08-08 15:38:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &membershipUpdater{
|
2017-09-13 15:30:19 +00:00
|
|
|
transaction{ctx, txn}, d, roomNID, targetUserNID, membership,
|
2017-08-08 15:38:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvite implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) IsInvite() bool {
|
|
|
|
return u.membership == membershipStateInvite
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsJoin implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) IsJoin() bool {
|
|
|
|
return u.membership == membershipStateJoin
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLeave implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) IsLeave() bool {
|
|
|
|
return u.membership == membershipStateLeaveOrBan
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetToInvite implements types.MembershipUpdater
|
|
|
|
func (u *membershipUpdater) SetToInvite(event gomatrixserverlib.Event) (bool, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, event.Sender())
|
2017-08-08 15:38:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
inserted, err := u.d.statements.insertInviteEvent(
|
2017-09-13 15:30:19 +00:00
|
|
|
u.ctx, u.txn, event.EventID(), u.roomNID, u.targetUserNID, senderUserNID, event.JSON(),
|
2017-08-08 15:38:03 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if u.membership != membershipStateInvite {
|
|
|
|
if err = u.d.statements.updateMembership(
|
2017-09-13 15:30:19 +00:00
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID, membershipStateInvite, 0,
|
2017-08-08 15:38:03 +00:00
|
|
|
); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inserted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetToJoin implements types.MembershipUpdater
|
2017-08-21 15:34:26 +00:00
|
|
|
func (u *membershipUpdater) SetToJoin(senderUserID string, eventID string, isUpdate bool) ([]string, error) {
|
|
|
|
var inviteEventIDs []string
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, senderUserID)
|
2017-08-08 15:38:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-21 15:34:26 +00:00
|
|
|
|
|
|
|
// If this is a join event update, there is no invite to update
|
|
|
|
if !isUpdate {
|
|
|
|
inviteEventIDs, err = u.d.statements.updateInviteRetired(
|
2017-09-13 15:30:19 +00:00
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
2017-08-21 15:34:26 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 14:08:48 +00:00
|
|
|
// Look up the NID of the new join event
|
2017-09-13 15:30:19 +00:00
|
|
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
2017-08-08 15:38:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-21 15:34:26 +00:00
|
|
|
|
|
|
|
if u.membership != membershipStateJoin || isUpdate {
|
2017-08-08 15:38:03 +00:00
|
|
|
if err = u.d.statements.updateMembership(
|
2017-09-13 15:30:19 +00:00
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID,
|
|
|
|
membershipStateJoin, nIDs[eventID],
|
2017-08-08 15:38:03 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-08-21 15:34:26 +00:00
|
|
|
|
2017-08-08 15:38:03 +00:00
|
|
|
return inviteEventIDs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetToLeave implements types.MembershipUpdater
|
2017-08-21 15:34:26 +00:00
|
|
|
func (u *membershipUpdater) SetToLeave(senderUserID string, eventID string) ([]string, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
senderUserNID, err := u.d.assignStateKeyNID(u.ctx, u.txn, senderUserID)
|
2017-08-08 15:38:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inviteEventIDs, err := u.d.statements.updateInviteRetired(
|
2017-09-13 15:30:19 +00:00
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID,
|
2017-08-08 15:38:03 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-21 15:34:26 +00:00
|
|
|
|
2017-08-23 14:08:48 +00:00
|
|
|
// Look up the NID of the new leave event
|
2017-09-13 15:30:19 +00:00
|
|
|
nIDs, err := u.d.EventNIDs(u.ctx, []string{eventID})
|
2017-08-21 15:34:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:38:03 +00:00
|
|
|
if u.membership != membershipStateLeaveOrBan {
|
|
|
|
if err = u.d.statements.updateMembership(
|
2017-09-13 15:30:19 +00:00
|
|
|
u.ctx, u.txn, u.roomNID, u.targetUserNID, senderUserNID,
|
|
|
|
membershipStateLeaveOrBan, nIDs[eventID],
|
2017-08-08 15:38:03 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inviteEventIDs, nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 15:00:14 +00:00
|
|
|
// GetMembership implements query.RoomserverQueryAPIDB
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) GetMembership(
|
|
|
|
ctx context.Context, roomNID types.RoomNID, requestSenderUserID string,
|
|
|
|
) (membershipEventNID types.EventNID, stillInRoom bool, err error) {
|
|
|
|
requestSenderUserNID, err := d.assignStateKeyNID(ctx, nil, requestSenderUserID)
|
2017-08-21 15:34:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
senderMembershipEventNID, senderMembership, err :=
|
|
|
|
d.statements.selectMembershipFromRoomAndTarget(
|
|
|
|
ctx, roomNID, requestSenderUserNID,
|
|
|
|
)
|
2017-08-21 15:34:26 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// The user has never been a member of that room
|
2017-08-24 15:00:14 +00:00
|
|
|
return 0, false, nil
|
2017-08-21 15:34:26 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-24 15:00:14 +00:00
|
|
|
return senderMembershipEventNID, senderMembership == membershipStateJoin, nil
|
|
|
|
}
|
2017-08-21 15:34:26 +00:00
|
|
|
|
2017-08-24 15:00:14 +00:00
|
|
|
// GetMembershipEventNIDsForRoom implements query.RoomserverQueryAPIDB
|
2017-09-13 15:30:19 +00:00
|
|
|
func (d *Database) GetMembershipEventNIDsForRoom(
|
|
|
|
ctx context.Context, roomNID types.RoomNID, joinOnly bool,
|
|
|
|
) ([]types.EventNID, error) {
|
2017-08-24 15:00:14 +00:00
|
|
|
if joinOnly {
|
2017-09-13 15:30:19 +00:00
|
|
|
return d.statements.selectMembershipsFromRoomAndMembership(
|
|
|
|
ctx, roomNID, membershipStateJoin,
|
|
|
|
)
|
2017-08-21 15:34:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:30:19 +00:00
|
|
|
return d.statements.selectMembershipsFromRoom(ctx, roomNID)
|
2017-08-21 15:34:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:20:00 +00:00
|
|
|
// EventsFromIDs implements query.RoomserverQueryAPIEventDB
|
|
|
|
func (d *Database) EventsFromIDs(ctx context.Context, eventIDs []string) ([]types.Event, error) {
|
|
|
|
nidMap, err := d.EventNIDs(ctx, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var nids []types.EventNID
|
|
|
|
for _, nid := range nidMap {
|
|
|
|
nids = append(nids, nid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Events(ctx, nids)
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:38:03 +00:00
|
|
|
type transaction struct {
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context
|
2017-08-08 15:38:03 +00:00
|
|
|
txn *sql.Tx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit implements types.Transaction
|
|
|
|
func (t *transaction) Commit() error {
|
|
|
|
return t.txn.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback implements types.Transaction
|
|
|
|
func (t *transaction) Rollback() error {
|
|
|
|
return t.txn.Rollback()
|
|
|
|
}
|