2020-05-26 16:42:42 +02:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-26 19:23:39 +02:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2020-07-21 16:48:21 +02:00
|
|
|
"fmt"
|
2020-05-26 16:42:42 +02:00
|
|
|
|
2020-08-25 13:32:29 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-26 19:23:39 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-05-26 16:42:42 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-05-26 19:23:39 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-07-06 18:49:15 +02:00
|
|
|
"github.com/tidwall/gjson"
|
2020-05-26 16:42:42 +02:00
|
|
|
)
|
|
|
|
|
2020-07-06 18:49:15 +02:00
|
|
|
// Ideally, when we have both events we should redact the event JSON and forget about the redaction, but we currently
|
|
|
|
// don't because the redaction code is brand new. When we are more certain that redactions don't misbehave or are
|
|
|
|
// vulnerable to attacks from remote servers (e.g a server bypassing event auth rules shouldn't redact our data)
|
|
|
|
// then we should flip this to true. This will mean redactions /actually delete information irretrievably/ which
|
|
|
|
// will be necessary for compliance with the law. Note that downstream components (syncapi) WILL delete information
|
|
|
|
// in their database on receipt of a redaction. Also note that we still modify the event JSON to set the field
|
|
|
|
// unsigned.redacted_because - we just don't clear out the content fields yet.
|
|
|
|
//
|
|
|
|
// If this hasn't been done by 09/2020 this should be flipped to true.
|
|
|
|
const redactionsArePermanent = false
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
type Database struct {
|
2020-05-26 19:23:39 +02:00
|
|
|
DB *sql.DB
|
2020-08-25 13:32:29 +02:00
|
|
|
Cache caching.RoomServerCaches
|
2020-08-21 11:42:08 +02:00
|
|
|
Writer sqlutil.Writer
|
2020-05-26 17:45:28 +02:00
|
|
|
EventsTable tables.Events
|
|
|
|
EventJSONTable tables.EventJSON
|
2020-05-26 16:42:42 +02:00
|
|
|
EventTypesTable tables.EventTypes
|
|
|
|
EventStateKeysTable tables.EventStateKeys
|
2020-05-26 19:23:39 +02:00
|
|
|
RoomsTable tables.Rooms
|
|
|
|
TransactionsTable tables.Transactions
|
2020-05-27 10:36:09 +02:00
|
|
|
StateSnapshotTable tables.StateSnapshot
|
|
|
|
StateBlockTable tables.StateBlock
|
|
|
|
RoomAliasesTable tables.RoomAliases
|
|
|
|
PrevEventsTable tables.PreviousEvents
|
2020-05-27 12:03:47 +02:00
|
|
|
InvitesTable tables.Invites
|
|
|
|
MembershipTable tables.Membership
|
2020-07-02 16:41:18 +02:00
|
|
|
PublishedTable tables.Published
|
2020-07-06 18:49:15 +02:00
|
|
|
RedactionsTable tables.Redactions
|
2020-05-26 16:42:42 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 17:24:33 +02:00
|
|
|
func (d *Database) SupportsConcurrentRoomInputs() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
func (d *Database) EventTypeNIDs(
|
|
|
|
ctx context.Context, eventTypes []string,
|
|
|
|
) (map[string]types.EventTypeNID, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
result := make(map[string]types.EventTypeNID)
|
|
|
|
remaining := []string{}
|
|
|
|
for _, eventType := range eventTypes {
|
|
|
|
if nid, ok := d.Cache.GetRoomServerEventTypeNID(eventType); ok {
|
|
|
|
result[eventType] = nid
|
|
|
|
} else {
|
|
|
|
remaining = append(remaining, eventType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(remaining) > 0 {
|
|
|
|
nids, err := d.EventTypesTable.BulkSelectEventTypeNID(ctx, remaining)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for eventType, nid := range nids {
|
|
|
|
result[eventType] = nid
|
|
|
|
d.Cache.StoreRoomServerEventTypeNID(eventType, nid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-26 16:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) EventStateKeys(
|
|
|
|
ctx context.Context, eventStateKeyNIDs []types.EventStateKeyNID,
|
|
|
|
) (map[types.EventStateKeyNID]string, error) {
|
|
|
|
return d.EventStateKeysTable.BulkSelectEventStateKey(ctx, eventStateKeyNIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) EventStateKeyNIDs(
|
|
|
|
ctx context.Context, eventStateKeys []string,
|
|
|
|
) (map[string]types.EventStateKeyNID, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
result := make(map[string]types.EventStateKeyNID)
|
|
|
|
remaining := []string{}
|
|
|
|
for _, eventStateKey := range eventStateKeys {
|
|
|
|
if nid, ok := d.Cache.GetRoomServerStateKeyNID(eventStateKey); ok {
|
|
|
|
result[eventStateKey] = nid
|
|
|
|
} else {
|
|
|
|
remaining = append(remaining, eventStateKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(remaining) > 0 {
|
|
|
|
nids, err := d.EventStateKeysTable.BulkSelectEventStateKeyNID(ctx, remaining)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for eventStateKey, nid := range nids {
|
|
|
|
result[eventStateKey] = nid
|
|
|
|
d.Cache.StoreRoomServerStateKeyNID(eventStateKey, nid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-26 16:42:42 +02:00
|
|
|
}
|
2020-05-26 17:45:28 +02:00
|
|
|
|
|
|
|
func (d *Database) StateEntriesForEventIDs(
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
) ([]types.StateEntry, error) {
|
|
|
|
return d.EventsTable.BulkSelectStateEventByID(ctx, eventIDs)
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (d *Database) StateEntriesForTuples(
|
|
|
|
ctx context.Context,
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
stateKeyTuples []types.StateKeyTuple,
|
|
|
|
) ([]types.StateEntryList, error) {
|
|
|
|
return d.StateBlockTable.BulkSelectFilteredStateBlockEntries(
|
|
|
|
ctx, stateBlockNIDs, stateKeyTuples,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) AddState(
|
|
|
|
ctx context.Context,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
state []types.StateEntry,
|
|
|
|
) (stateNID types.StateSnapshotNID, err error) {
|
2020-08-19 16:38:27 +02:00
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2020-05-27 10:36:09 +02:00
|
|
|
if len(state) > 0 {
|
|
|
|
var stateBlockNID types.StateBlockNID
|
|
|
|
stateBlockNID, err = d.StateBlockTable.BulkInsertStateData(ctx, txn, state)
|
|
|
|
if err != nil {
|
2020-08-19 16:38:27 +02:00
|
|
|
return fmt.Errorf("d.StateBlockTable.BulkInsertStateData: %w", err)
|
2020-05-27 10:36:09 +02:00
|
|
|
}
|
|
|
|
stateBlockNIDs = append(stateBlockNIDs[:len(stateBlockNIDs):len(stateBlockNIDs)], stateBlockNID)
|
|
|
|
}
|
|
|
|
stateNID, err = d.StateSnapshotTable.InsertState(ctx, txn, roomNID, stateBlockNIDs)
|
2020-08-19 16:38:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("d.StateSnapshotTable.InsertState: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-27 10:36:09 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-08-19 16:38:27 +02:00
|
|
|
return 0, fmt.Errorf("d.Writer.Do: %w", err)
|
2020-05-27 10:36:09 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:45:28 +02:00
|
|
|
func (d *Database) EventNIDs(
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
) (map[string]types.EventNID, error) {
|
|
|
|
return d.EventsTable.BulkSelectEventNID(ctx, eventIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) SetState(
|
|
|
|
ctx context.Context, eventNID types.EventNID, stateNID types.StateSnapshotNID,
|
|
|
|
) error {
|
2020-08-20 17:24:33 +02:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.EventsTable.UpdateEventState(ctx, txn, eventNID, stateNID)
|
2020-08-19 16:38:27 +02:00
|
|
|
})
|
2020-05-26 17:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) StateAtEventIDs(
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
) ([]types.StateAtEvent, error) {
|
|
|
|
return d.EventsTable.BulkSelectStateAtEventByID(ctx, eventIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) SnapshotNIDFromEventID(
|
|
|
|
ctx context.Context, eventID string,
|
|
|
|
) (types.StateSnapshotNID, error) {
|
|
|
|
_, stateNID, err := d.EventsTable.SelectEvent(ctx, nil, eventID)
|
|
|
|
return stateNID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) EventIDs(
|
|
|
|
ctx context.Context, eventNIDs []types.EventNID,
|
|
|
|
) (map[types.EventNID]string, error) {
|
|
|
|
return d.EventsTable.BulkSelectEventID(ctx, eventNIDs)
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) RoomNID(ctx context.Context, roomID string) (types.RoomNID, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
if nid, ok := d.Cache.GetRoomServerRoomNID(roomID); ok {
|
|
|
|
return nid, nil
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, nil, roomID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2020-08-25 13:32:29 +02:00
|
|
|
d.Cache.StoreRoomServerRoomNID(roomID, roomNID)
|
2020-05-26 19:23:39 +02:00
|
|
|
return roomNID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) RoomNIDExcludingStubs(ctx context.Context, roomID string) (roomNID types.RoomNID, err error) {
|
|
|
|
roomNID, err = d.RoomNID(ctx, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
latestEvents, _, err := d.RoomsTable.SelectLatestEventNIDs(ctx, nil, roomNID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(latestEvents) == 0 {
|
|
|
|
roomNID = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) LatestEventIDs(
|
|
|
|
ctx context.Context, roomNID types.RoomNID,
|
|
|
|
) (references []gomatrixserverlib.EventReference, currentStateSnapshotNID types.StateSnapshotNID, depth int64, err error) {
|
2020-08-20 10:24:52 +02:00
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
eventNIDs, currentStateSnapshotNID, err = d.RoomsTable.SelectLatestEventNIDs(ctx, nil, roomNID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
references, err = d.EventsTable.BulkSelectEventReference(ctx, nil, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
depth, err = d.EventsTable.SelectMaxEventDepth(ctx, nil, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (d *Database) StateBlockNIDs(
|
|
|
|
ctx context.Context, stateNIDs []types.StateSnapshotNID,
|
|
|
|
) ([]types.StateBlockNIDList, error) {
|
|
|
|
return d.StateSnapshotTable.BulkSelectStateBlockNIDs(ctx, stateNIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) StateEntries(
|
|
|
|
ctx context.Context, stateBlockNIDs []types.StateBlockNID,
|
|
|
|
) ([]types.StateEntryList, error) {
|
|
|
|
return d.StateBlockTable.BulkSelectStateBlockEntries(ctx, stateBlockNIDs)
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:23:39 +02:00
|
|
|
func (d *Database) GetRoomVersionForRoom(
|
|
|
|
ctx context.Context, roomID string,
|
|
|
|
) (gomatrixserverlib.RoomVersion, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
if roomVersion, ok := d.Cache.GetRoomVersion(roomID); ok {
|
|
|
|
return roomVersion, nil
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
return d.RoomsTable.SelectRoomVersionForRoomID(
|
|
|
|
ctx, nil, roomID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetRoomVersionForRoomNID(
|
|
|
|
ctx context.Context, roomNID types.RoomNID,
|
|
|
|
) (gomatrixserverlib.RoomVersion, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
if roomID, ok := d.Cache.GetRoomServerRoomID(roomNID); ok {
|
|
|
|
if roomVersion, ok := d.Cache.GetRoomVersion(roomID); ok {
|
|
|
|
return roomVersion, nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
return d.RoomsTable.SelectRoomVersionForRoomNID(
|
|
|
|
ctx, roomNID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (d *Database) SetRoomAlias(ctx context.Context, alias string, roomID string, creatorUserID string) error {
|
2020-08-20 17:24:33 +02:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.RoomAliasesTable.InsertRoomAlias(ctx, txn, alias, roomID, creatorUserID)
|
2020-08-19 16:38:27 +02:00
|
|
|
})
|
2020-05-27 10:36:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetRoomIDForAlias(ctx context.Context, alias string) (string, error) {
|
|
|
|
return d.RoomAliasesTable.SelectRoomIDFromAlias(ctx, alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error) {
|
|
|
|
return d.RoomAliasesTable.SelectAliasesFromRoomID(ctx, roomID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetCreatorIDForAlias(
|
|
|
|
ctx context.Context, alias string,
|
|
|
|
) (string, error) {
|
|
|
|
return d.RoomAliasesTable.SelectCreatorIDFromAlias(ctx, alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) RemoveRoomAlias(ctx context.Context, alias string) error {
|
2020-08-20 17:24:33 +02:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.RoomAliasesTable.DeleteRoomAlias(ctx, txn, alias)
|
2020-08-19 16:38:27 +02:00
|
|
|
})
|
2020-05-27 10:36:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 12:03:47 +02:00
|
|
|
func (d *Database) GetMembership(
|
|
|
|
ctx context.Context, roomNID types.RoomNID, requestSenderUserID string,
|
|
|
|
) (membershipEventNID types.EventNID, stillInRoom bool, err error) {
|
2020-08-19 16:38:27 +02:00
|
|
|
var requestSenderUserNID types.EventStateKeyNID
|
2020-08-20 17:24:33 +02:00
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
requestSenderUserNID, err = d.assignStateKeyNID(ctx, txn, requestSenderUserID)
|
2020-08-19 16:38:27 +02:00
|
|
|
return err
|
|
|
|
})
|
2020-05-27 12:03:47 +02:00
|
|
|
if err != nil {
|
2020-08-19 16:38:27 +02:00
|
|
|
return 0, false, fmt.Errorf("d.assignStateKeyNID: %w", err)
|
2020-05-27 12:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
senderMembershipEventNID, senderMembership, err :=
|
|
|
|
d.MembershipTable.SelectMembershipFromRoomAndTarget(
|
|
|
|
ctx, roomNID, requestSenderUserNID,
|
|
|
|
)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// The user has never been a member of that room
|
|
|
|
return 0, false, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return senderMembershipEventNID, senderMembership == tables.MembershipStateJoin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetMembershipEventNIDsForRoom(
|
|
|
|
ctx context.Context, roomNID types.RoomNID, joinOnly bool, localOnly bool,
|
|
|
|
) ([]types.EventNID, error) {
|
|
|
|
if joinOnly {
|
|
|
|
return d.MembershipTable.SelectMembershipsFromRoomAndMembership(
|
|
|
|
ctx, roomNID, tables.MembershipStateJoin, localOnly,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.MembershipTable.SelectMembershipsFromRoom(ctx, roomNID, localOnly)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetInvitesForUser(
|
|
|
|
ctx context.Context,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
2020-06-26 12:07:52 +02:00
|
|
|
) (senderUserIDs []types.EventStateKeyNID, eventIDs []string, err error) {
|
2020-05-27 12:03:47 +02:00
|
|
|
return d.InvitesTable.SelectInviteActiveForUserInRoom(ctx, targetUserNID, roomNID)
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:23:39 +02:00
|
|
|
func (d *Database) Events(
|
|
|
|
ctx context.Context, eventNIDs []types.EventNID,
|
|
|
|
) ([]types.Event, error) {
|
|
|
|
eventJSONs, err := d.EventJSONTable.BulkSelectEventJSON(ctx, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results := make([]types.Event, len(eventJSONs))
|
|
|
|
for i, eventJSON := range eventJSONs {
|
|
|
|
var roomNID types.RoomNID
|
|
|
|
var roomVersion gomatrixserverlib.RoomVersion
|
|
|
|
result := &results[i]
|
|
|
|
result.EventNID = eventJSON.EventNID
|
|
|
|
roomNID, err = d.EventsTable.SelectRoomNIDForEventNID(ctx, eventJSON.EventNID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
roomVersion, err = d.RoomsTable.SelectRoomVersionForRoomNID(ctx, roomNID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.Event, err = gomatrixserverlib.NewEventFromTrustedJSON(
|
|
|
|
eventJSON.EventJSON, false, roomVersion,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 18:49:15 +02:00
|
|
|
if !redactionsArePermanent {
|
|
|
|
d.applyRedactions(results)
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetTransactionEventID(
|
|
|
|
ctx context.Context, transactionID string,
|
|
|
|
sessionID int64, userID string,
|
|
|
|
) (string, error) {
|
|
|
|
eventID, err := d.TransactionsTable.SelectTransactionEventID(ctx, transactionID, sessionID, userID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return eventID, err
|
|
|
|
}
|
|
|
|
|
2020-05-28 12:15:21 +02:00
|
|
|
func (d *Database) MembershipUpdater(
|
|
|
|
ctx context.Context, roomID, targetUserID string,
|
|
|
|
targetLocal bool, roomVersion gomatrixserverlib.RoomVersion,
|
2020-08-19 14:24:54 +02:00
|
|
|
) (*MembershipUpdater, error) {
|
|
|
|
txn, err := d.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-20 10:24:52 +02:00
|
|
|
var updater *MembershipUpdater
|
|
|
|
_ = d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
|
|
|
updater, err = NewMembershipUpdater(ctx, d, txn, roomID, targetUserID, targetLocal, roomVersion)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return updater, err
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetLatestEventsForUpdate(
|
|
|
|
ctx context.Context, roomNID types.RoomNID,
|
2020-08-19 14:24:54 +02:00
|
|
|
) (*LatestEventsUpdater, error) {
|
|
|
|
txn, err := d.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-20 10:24:52 +02:00
|
|
|
var updater *LatestEventsUpdater
|
|
|
|
_ = d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
|
|
|
updater, err = NewLatestEventsUpdater(ctx, d, txn, roomNID)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return updater, err
|
2020-05-28 12:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 16:38:27 +02:00
|
|
|
// nolint:gocyclo
|
2020-05-26 19:23:39 +02:00
|
|
|
func (d *Database) StoreEvent(
|
|
|
|
ctx context.Context, event gomatrixserverlib.Event,
|
|
|
|
txnAndSessionID *api.TransactionID, authEventNIDs []types.EventNID,
|
2020-07-07 13:51:55 +02:00
|
|
|
) (types.RoomNID, types.StateAtEvent, *gomatrixserverlib.Event, string, error) {
|
2020-05-26 19:23:39 +02:00
|
|
|
var (
|
|
|
|
roomNID types.RoomNID
|
|
|
|
eventTypeNID types.EventTypeNID
|
|
|
|
eventStateKeyNID types.EventStateKeyNID
|
|
|
|
eventNID types.EventNID
|
|
|
|
stateNID types.StateSnapshotNID
|
2020-07-07 13:51:55 +02:00
|
|
|
redactionEvent *gomatrixserverlib.Event
|
|
|
|
redactedEventID string
|
2020-05-26 19:23:39 +02:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-08-19 16:38:27 +02:00
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2020-05-26 19:23:39 +02:00
|
|
|
if txnAndSessionID != nil {
|
|
|
|
if err = d.TransactionsTable.InsertTransaction(
|
2020-08-20 10:24:52 +02:00
|
|
|
ctx, txn, txnAndSessionID.TransactionID,
|
2020-05-26 19:23:39 +02:00
|
|
|
txnAndSessionID.SessionID, event.Sender(), event.EventID(),
|
|
|
|
); err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return fmt.Errorf("d.TransactionsTable.InsertTransaction: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Here we should aim to have two different code paths for new rooms
|
|
|
|
// vs existing ones.
|
|
|
|
|
|
|
|
// Get the default room version. If the client doesn't supply a room_version
|
|
|
|
// then we will use our configured default to create the room.
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-createroom
|
|
|
|
// Note that the below logic depends on the m.room.create event being the
|
|
|
|
// first event that is persisted to the database when creating or joining a
|
|
|
|
// room.
|
|
|
|
var roomVersion gomatrixserverlib.RoomVersion
|
|
|
|
if roomVersion, err = extractRoomVersionFromCreateEvent(event); err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return fmt.Errorf("extractRoomVersionFromCreateEvent: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if roomNID, err = d.assignRoomNID(ctx, txn, event.RoomID(), roomVersion); err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return fmt.Errorf("d.assignRoomNID: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if eventTypeNID, err = d.assignEventTypeNID(ctx, txn, event.Type()); err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return fmt.Errorf("d.assignEventTypeNID: %w", err)
|
2020-05-26 19:23:39 +02: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 {
|
|
|
|
if eventStateKeyNID, err = d.assignStateKeyNID(ctx, txn, *eventStateKey); err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return fmt.Errorf("d.assignStateKeyNID: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if eventNID, stateNID, err = d.EventsTable.InsertEvent(
|
|
|
|
ctx,
|
|
|
|
txn,
|
|
|
|
roomNID,
|
|
|
|
eventTypeNID,
|
|
|
|
eventStateKeyNID,
|
|
|
|
event.EventID(),
|
|
|
|
event.EventReference().EventSHA256,
|
|
|
|
authEventNIDs,
|
|
|
|
event.Depth(),
|
|
|
|
); err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We've already inserted the event so select the numeric event ID
|
|
|
|
eventNID, stateNID, err = d.EventsTable.SelectEvent(ctx, txn, event.EventID())
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-07-21 18:21:48 +02:00
|
|
|
return err
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.EventJSONTable.InsertEventJSON(ctx, txn, eventNID, event.JSON()); err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return fmt.Errorf("d.EventJSONTable.InsertEventJSON: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
2020-07-07 13:51:55 +02:00
|
|
|
redactionEvent, redactedEventID, err = d.handleRedactions(ctx, txn, eventNID, event)
|
2020-07-21 16:48:21 +02:00
|
|
|
return nil
|
2020-05-26 19:23:39 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-08-19 16:38:27 +02:00
|
|
|
return 0, types.StateAtEvent{}, nil, "", fmt.Errorf("d.Writer.Do: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return roomNID, types.StateAtEvent{
|
|
|
|
BeforeStateSnapshotNID: stateNID,
|
|
|
|
StateEntry: types.StateEntry{
|
|
|
|
StateKeyTuple: types.StateKeyTuple{
|
|
|
|
EventTypeNID: eventTypeNID,
|
|
|
|
EventStateKeyNID: eventStateKeyNID,
|
|
|
|
},
|
|
|
|
EventNID: eventNID,
|
|
|
|
},
|
2020-07-07 13:51:55 +02:00
|
|
|
}, redactionEvent, redactedEventID, nil
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:41:18 +02:00
|
|
|
func (d *Database) PublishRoom(ctx context.Context, roomID string, publish bool) error {
|
2020-08-20 17:24:33 +02:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.PublishedTable.UpsertRoomPublished(ctx, txn, roomID, publish)
|
2020-08-19 16:38:27 +02:00
|
|
|
})
|
2020-07-02 16:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetPublishedRooms(ctx context.Context) ([]string, error) {
|
|
|
|
return d.PublishedTable.SelectAllPublishedRooms(ctx, true)
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:23:39 +02:00
|
|
|
func (d *Database) assignRoomNID(
|
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
|
|
|
) (types.RoomNID, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
if roomNID, ok := d.Cache.GetRoomServerRoomNID(roomID); ok {
|
|
|
|
return roomNID, nil
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
// Check if we already have a numeric ID in the database.
|
|
|
|
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
|
|
|
roomNID, err = d.RoomsTable.InsertRoomNID(ctx, txn, roomID, roomVersion)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We raced with another insert so run the select again.
|
|
|
|
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 13:32:29 +02:00
|
|
|
if err == nil {
|
|
|
|
d.Cache.StoreRoomServerRoomNID(roomID, roomNID)
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
return roomNID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) assignEventTypeNID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventType string,
|
2020-08-25 13:32:29 +02:00
|
|
|
) (types.EventTypeNID, error) {
|
|
|
|
if eventTypeNID, ok := d.Cache.GetRoomServerEventTypeNID(eventType); ok {
|
|
|
|
return eventTypeNID, nil
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
// Check if we already have a numeric ID in the database.
|
2020-08-25 13:32:29 +02:00
|
|
|
eventTypeNID, err := d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
2020-05-26 19:23:39 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
|
|
|
eventTypeNID, err = d.EventTypesTable.InsertEventTypeNID(ctx, txn, eventType)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We raced with another insert so run the select again.
|
|
|
|
eventTypeNID, err = d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 13:32:29 +02:00
|
|
|
if err == nil {
|
|
|
|
d.Cache.StoreRoomServerEventTypeNID(eventType, eventTypeNID)
|
|
|
|
}
|
|
|
|
return eventTypeNID, err
|
2020-05-26 19:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) assignStateKeyNID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
|
|
|
) (types.EventStateKeyNID, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
if eventStateKeyNID, ok := d.Cache.GetRoomServerStateKeyNID(eventStateKey); ok {
|
|
|
|
return eventStateKeyNID, nil
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
// Check if we already have a numeric ID in the database.
|
|
|
|
eventStateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We don't have a numeric ID so insert one into the database.
|
|
|
|
eventStateKeyNID, err = d.EventStateKeysTable.InsertEventStateKeyNID(ctx, txn, eventStateKey)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// We raced with another insert so run the select again.
|
|
|
|
eventStateKeyNID, err = d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 13:32:29 +02:00
|
|
|
if err == nil {
|
|
|
|
d.Cache.StoreRoomServerStateKeyNID(eventStateKey, eventStateKeyNID)
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
return eventStateKeyNID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractRoomVersionFromCreateEvent(event gomatrixserverlib.Event) (
|
|
|
|
gomatrixserverlib.RoomVersion, error,
|
|
|
|
) {
|
|
|
|
var err error
|
|
|
|
var roomVersion gomatrixserverlib.RoomVersion
|
|
|
|
// Look for m.room.create events.
|
|
|
|
if event.Type() != gomatrixserverlib.MRoomCreate {
|
|
|
|
return gomatrixserverlib.RoomVersion(""), nil
|
|
|
|
}
|
|
|
|
roomVersion = gomatrixserverlib.RoomVersionV1
|
|
|
|
var createContent gomatrixserverlib.CreateContent
|
|
|
|
// The m.room.create event contains an optional "room_version" key in
|
|
|
|
// the event content, so we need to unmarshal that first.
|
|
|
|
if err = json.Unmarshal(event.Content(), &createContent); err != nil {
|
|
|
|
return gomatrixserverlib.RoomVersion(""), err
|
|
|
|
}
|
|
|
|
// A room version was specified in the event content?
|
|
|
|
if createContent.RoomVersion != nil {
|
|
|
|
roomVersion = gomatrixserverlib.RoomVersion(*createContent.RoomVersion)
|
|
|
|
}
|
|
|
|
return roomVersion, err
|
|
|
|
}
|
2020-07-06 18:49:15 +02:00
|
|
|
|
|
|
|
// handleRedactions manages the redacted status of events. There's two cases to consider in order to comply with the spec:
|
|
|
|
// "servers should not apply or send redactions to clients until both the redaction event and original event have been seen, and are valid."
|
|
|
|
// https://matrix.org/docs/spec/rooms/v3#authorization-rules-for-events
|
|
|
|
// These cases are:
|
|
|
|
// - This is a redaction event, redact the event it references if we know about it.
|
|
|
|
// - This is a normal event which may have been previously redacted.
|
|
|
|
// In the first case, check if we have the referenced event then apply the redaction, else store it
|
|
|
|
// in the redactions table with validated=FALSE. In the second case, check if there is a redaction for it:
|
|
|
|
// if there is then apply the redactions and set validated=TRUE.
|
|
|
|
//
|
|
|
|
// When an event is redacted, the redacted event JSON is modified to add an `unsigned.redacted_because` field. We use this field
|
|
|
|
// when loading events to determine whether to apply redactions. This keeps the hot-path of reading events quick as we don't need
|
|
|
|
// to cross-reference with other tables when loading.
|
2020-07-07 13:51:55 +02:00
|
|
|
//
|
|
|
|
// Returns the redaction event and the event ID of the redacted event if this call resulted in a redaction.
|
|
|
|
func (d *Database) handleRedactions(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID, event gomatrixserverlib.Event,
|
|
|
|
) (*gomatrixserverlib.Event, string, error) {
|
|
|
|
var err error
|
|
|
|
isRedactionEvent := event.Type() == gomatrixserverlib.MRoomRedaction && event.StateKey() == nil
|
|
|
|
if isRedactionEvent {
|
|
|
|
// an event which redacts itself should be ignored
|
|
|
|
if event.EventID() == event.Redacts() {
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.RedactionsTable.InsertRedaction(ctx, txn, tables.RedactionInfo{
|
|
|
|
Validated: false,
|
|
|
|
RedactionEventID: event.EventID(),
|
|
|
|
RedactsEventID: event.Redacts(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 18:49:15 +02:00
|
|
|
redactionEvent, redactedEvent, validated, err := d.loadRedactionPair(ctx, txn, eventNID, event)
|
|
|
|
if err != nil {
|
2020-07-07 13:51:55 +02:00
|
|
|
return nil, "", err
|
2020-07-06 18:49:15 +02:00
|
|
|
}
|
|
|
|
if validated || redactedEvent == nil || redactionEvent == nil {
|
|
|
|
// we've seen this redaction before or there is nothing to redact
|
2020-07-07 13:51:55 +02:00
|
|
|
return nil, "", nil
|
2020-07-06 18:49:15 +02:00
|
|
|
}
|
2020-07-08 18:45:39 +02:00
|
|
|
if redactedEvent.RoomID() != redactionEvent.RoomID() {
|
|
|
|
// redactions across rooms aren't allowed
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
2020-07-06 18:49:15 +02:00
|
|
|
|
|
|
|
// mark the event as redacted
|
|
|
|
err = redactedEvent.SetUnsignedField("redacted_because", redactionEvent)
|
|
|
|
if err != nil {
|
2020-07-07 13:51:55 +02:00
|
|
|
return nil, "", err
|
2020-07-06 18:49:15 +02:00
|
|
|
}
|
|
|
|
if redactionsArePermanent {
|
|
|
|
redactedEvent.Event = redactedEvent.Redact()
|
|
|
|
}
|
|
|
|
// overwrite the eventJSON table
|
|
|
|
err = d.EventJSONTable.InsertEventJSON(ctx, txn, redactedEvent.EventNID, redactedEvent.JSON())
|
|
|
|
if err != nil {
|
2020-07-07 13:51:55 +02:00
|
|
|
return nil, "", err
|
2020-07-06 18:49:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 13:51:55 +02:00
|
|
|
return &redactionEvent.Event, redactedEvent.EventID(), d.RedactionsTable.MarkRedactionValidated(ctx, txn, redactionEvent.EventID(), true)
|
2020-07-06 18:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadRedactionPair returns both the redaction event and the redacted event, else nil.
|
|
|
|
func (d *Database) loadRedactionPair(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID, event gomatrixserverlib.Event,
|
|
|
|
) (*types.Event, *types.Event, bool, error) {
|
|
|
|
var redactionEvent, redactedEvent *types.Event
|
|
|
|
var info *tables.RedactionInfo
|
|
|
|
var err error
|
|
|
|
isRedactionEvent := event.Type() == gomatrixserverlib.MRoomRedaction && event.StateKey() == nil
|
2020-07-07 13:51:55 +02:00
|
|
|
|
|
|
|
var eventBeingRedacted string
|
2020-07-06 18:49:15 +02:00
|
|
|
if isRedactionEvent {
|
2020-07-07 13:51:55 +02:00
|
|
|
eventBeingRedacted = event.Redacts()
|
2020-07-06 18:49:15 +02:00
|
|
|
redactionEvent = &types.Event{
|
|
|
|
EventNID: eventNID,
|
|
|
|
Event: event,
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-07 13:51:55 +02:00
|
|
|
eventBeingRedacted = event.EventID() // maybe, we'll see if we have info
|
2020-07-06 18:49:15 +02:00
|
|
|
redactedEvent = &types.Event{
|
|
|
|
EventNID: eventNID,
|
|
|
|
Event: event,
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 13:51:55 +02:00
|
|
|
|
|
|
|
info, err = d.RedactionsTable.SelectRedactionInfoByEventBeingRedacted(ctx, txn, eventBeingRedacted)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, false, err
|
|
|
|
}
|
|
|
|
if info == nil {
|
|
|
|
// this event hasn't been redacted or we don't have the redaction for it yet
|
|
|
|
return nil, nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if isRedactionEvent {
|
|
|
|
redactedEvent = d.loadEvent(ctx, info.RedactsEventID)
|
|
|
|
} else {
|
|
|
|
redactionEvent = d.loadEvent(ctx, info.RedactionEventID)
|
|
|
|
}
|
|
|
|
|
2020-07-06 18:49:15 +02:00
|
|
|
return redactionEvent, redactedEvent, info.Validated, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyRedactions will redact events that have an `unsigned.redacted_because` field.
|
|
|
|
func (d *Database) applyRedactions(events []types.Event) {
|
|
|
|
for i := range events {
|
|
|
|
if result := gjson.GetBytes(events[i].Unsigned(), "redacted_because"); result.Exists() {
|
|
|
|
events[i].Event = events[i].Redact()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 13:51:55 +02:00
|
|
|
|
|
|
|
// loadEvent loads a single event or returns nil on any problems/missing event
|
|
|
|
func (d *Database) loadEvent(ctx context.Context, eventID string) *types.Event {
|
|
|
|
nids, err := d.EventNIDs(ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(nids) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
evs, err := d.Events(ctx, []types.EventNID{nids[eventID]})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(evs) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &evs[0]
|
|
|
|
}
|