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-09-03 19:27:02 +02:00
|
|
|
|
"sort"
|
2020-05-26 16:42:42 +02:00
|
|
|
|
|
2022-10-07 16:00:12 +02:00
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-10-07 16:00:12 +02:00
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
|
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"
|
2023-03-03 14:03:17 +01:00
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2020-05-26 16:42:42 +02:00
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
|
)
|
|
|
|
|
|
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.
|
2020-08-25 16:44:19 +02:00
|
|
|
|
const redactionsArePermanent = true
|
2020-07-06 18:49:15 +02:00
|
|
|
|
|
2020-05-26 16:42:42 +02:00
|
|
|
|
type Database struct {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
DB *sql.DB
|
|
|
|
|
EventDatabase
|
|
|
|
|
Cache caching.RoomServerCaches
|
|
|
|
|
Writer sqlutil.Writer
|
|
|
|
|
RoomsTable tables.Rooms
|
|
|
|
|
StateSnapshotTable tables.StateSnapshot
|
|
|
|
|
StateBlockTable tables.StateBlock
|
|
|
|
|
RoomAliasesTable tables.RoomAliases
|
|
|
|
|
InvitesTable tables.Invites
|
|
|
|
|
MembershipTable tables.Membership
|
|
|
|
|
PublishedTable tables.Published
|
|
|
|
|
Purge tables.Purge
|
|
|
|
|
GetRoomUpdaterFn func(ctx context.Context, roomInfo *types.RoomInfo) (*RoomUpdater, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EventDatabase contains all tables needed to work with events
|
|
|
|
|
type EventDatabase struct {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
DB *sql.DB
|
|
|
|
|
Cache caching.RoomServerCaches
|
|
|
|
|
Writer sqlutil.Writer
|
|
|
|
|
EventsTable tables.Events
|
|
|
|
|
EventJSONTable tables.EventJSON
|
|
|
|
|
EventTypesTable tables.EventTypes
|
|
|
|
|
EventStateKeysTable tables.EventStateKeys
|
|
|
|
|
PrevEventsTable tables.PreviousEvents
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 14:31:23 +01:00
|
|
|
|
func (d *Database) GetMembershipForHistoryVisibility(
|
|
|
|
|
ctx context.Context, userNID types.EventStateKeyNID, roomInfo *types.RoomInfo, eventIDs ...string,
|
|
|
|
|
) (map[string]*gomatrixserverlib.HeaderedEvent, error) {
|
|
|
|
|
return d.StateSnapshotTable.BulkSelectMembershipForHistoryVisibility(ctx, nil, userNID, roomInfo, eventIDs...)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) EventTypeNIDs(
|
2020-05-26 16:42:42 +02:00
|
|
|
|
ctx context.Context, eventTypes []string,
|
2022-02-04 11:39:34 +01:00
|
|
|
|
) (map[string]types.EventTypeNID, error) {
|
|
|
|
|
return d.eventTypeNIDs(ctx, nil, eventTypes)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) eventTypeNIDs(
|
2022-02-04 11:39:34 +01:00
|
|
|
|
ctx context.Context, txn *sql.Tx, eventTypes []string,
|
2020-05-26 16:42:42 +02:00
|
|
|
|
) (map[string]types.EventTypeNID, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
|
result := make(map[string]types.EventTypeNID)
|
2023-02-24 11:45:01 +01:00
|
|
|
|
// first try the cache
|
|
|
|
|
fetchEventTypes := make([]string, 0, len(eventTypes))
|
|
|
|
|
for _, eventType := range eventTypes {
|
|
|
|
|
eventTypeNID, ok := d.Cache.GetEventTypeKey(eventType)
|
|
|
|
|
if ok {
|
|
|
|
|
result[eventType] = eventTypeNID
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
fetchEventTypes = append(fetchEventTypes, eventType)
|
2020-08-25 13:32:29 +02:00
|
|
|
|
}
|
2023-02-24 11:45:01 +01:00
|
|
|
|
if len(fetchEventTypes) > 0 {
|
|
|
|
|
nids, err := d.EventTypesTable.BulkSelectEventTypeNID(ctx, txn, fetchEventTypes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for eventType, nid := range nids {
|
|
|
|
|
result[eventType] = nid
|
|
|
|
|
d.Cache.StoreEventTypeKey(nid, eventType)
|
|
|
|
|
}
|
2020-08-25 13:32:29 +02:00
|
|
|
|
}
|
|
|
|
|
return result, nil
|
2020-05-26 16:42:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) EventStateKeys(
|
2020-05-26 16:42:42 +02:00
|
|
|
|
ctx context.Context, eventStateKeyNIDs []types.EventStateKeyNID,
|
|
|
|
|
) (map[types.EventStateKeyNID]string, error) {
|
2022-07-19 13:15:48 +02:00
|
|
|
|
result := make(map[types.EventStateKeyNID]string, len(eventStateKeyNIDs))
|
|
|
|
|
fetch := make([]types.EventStateKeyNID, 0, len(eventStateKeyNIDs))
|
|
|
|
|
for _, nid := range eventStateKeyNIDs {
|
|
|
|
|
if key, ok := d.Cache.GetEventStateKey(nid); ok {
|
|
|
|
|
result[nid] = key
|
|
|
|
|
} else {
|
|
|
|
|
fetch = append(fetch, nid)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-24 11:45:01 +01:00
|
|
|
|
if len(fetch) > 0 {
|
|
|
|
|
fromDB, err := d.EventStateKeysTable.BulkSelectEventStateKey(ctx, nil, fetch)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for nid, key := range fromDB {
|
|
|
|
|
result[nid] = key
|
|
|
|
|
d.Cache.StoreEventStateKey(nid, key)
|
|
|
|
|
}
|
2022-07-19 13:15:48 +02:00
|
|
|
|
}
|
|
|
|
|
return result, nil
|
2020-05-26 16:42:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) EventStateKeyNIDs(
|
2020-05-26 16:42:42 +02:00
|
|
|
|
ctx context.Context, eventStateKeys []string,
|
2022-02-04 11:39:34 +01:00
|
|
|
|
) (map[string]types.EventStateKeyNID, error) {
|
|
|
|
|
return d.eventStateKeyNIDs(ctx, nil, eventStateKeys)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) eventStateKeyNIDs(
|
2022-02-04 11:39:34 +01:00
|
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKeys []string,
|
2020-05-26 16:42:42 +02:00
|
|
|
|
) (map[string]types.EventStateKeyNID, error) {
|
2020-08-25 13:32:29 +02:00
|
|
|
|
result := make(map[string]types.EventStateKeyNID)
|
2022-11-14 13:06:27 +01:00
|
|
|
|
eventStateKeys = util.UniqueStrings(eventStateKeys)
|
2023-02-24 09:40:20 +01:00
|
|
|
|
// first ask the cache about these keys
|
|
|
|
|
fetchEventStateKeys := make([]string, 0, len(eventStateKeys))
|
|
|
|
|
for _, eventStateKey := range eventStateKeys {
|
|
|
|
|
eventStateKeyNID, ok := d.Cache.GetEventStateKeyNID(eventStateKey)
|
|
|
|
|
if ok {
|
|
|
|
|
result[eventStateKey] = eventStateKeyNID
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
fetchEventStateKeys = append(fetchEventStateKeys, eventStateKey)
|
2020-08-25 13:32:29 +02:00
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
|
|
|
|
|
if len(fetchEventStateKeys) > 0 {
|
|
|
|
|
nids, err := d.EventStateKeysTable.BulkSelectEventStateKeyNID(ctx, txn, fetchEventStateKeys)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for eventStateKey, nid := range nids {
|
|
|
|
|
result[eventStateKey] = nid
|
2023-02-24 11:45:01 +01:00
|
|
|
|
d.Cache.StoreEventStateKey(nid, eventStateKey)
|
2023-02-24 09:40:20 +01:00
|
|
|
|
}
|
2020-08-25 13:32:29 +02:00
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
|
2022-11-11 10:52:43 +01:00
|
|
|
|
// We received some nids, but are still missing some, work out which and create them
|
2022-11-11 16:44:59 +01:00
|
|
|
|
if len(eventStateKeys) > len(result) {
|
2022-11-14 13:06:27 +01:00
|
|
|
|
var nid types.EventStateKeyNID
|
2023-02-24 09:40:20 +01:00
|
|
|
|
var err error
|
2022-11-14 13:06:27 +01:00
|
|
|
|
err = d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
|
|
|
|
for _, eventStateKey := range eventStateKeys {
|
|
|
|
|
if _, ok := result[eventStateKey]; ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nid, err = d.assignStateKeyNID(ctx, txn, eventStateKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
result[eventStateKey] = nid
|
2022-11-11 10:52:43 +01:00
|
|
|
|
}
|
2022-11-14 13:06:27 +01:00
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2022-11-11 10:52:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-25 13:32:29 +02:00
|
|
|
|
return result, nil
|
2020-05-26 16:42:42 +02:00
|
|
|
|
}
|
2020-05-26 17:45:28 +02:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) StateEntriesForEventIDs(
|
2022-08-18 18:06:13 +02:00
|
|
|
|
ctx context.Context, eventIDs []string, excludeRejected bool,
|
2020-05-26 17:45:28 +02:00
|
|
|
|
) ([]types.StateEntry, error) {
|
2022-08-18 18:06:13 +02:00
|
|
|
|
return d.EventsTable.BulkSelectStateEventByID(ctx, nil, eventIDs, excludeRejected)
|
2020-05-26 17:45:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
|
func (d *Database) StateEntriesForTuples(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
|
stateKeyTuples []types.StateKeyTuple,
|
2022-02-04 11:39:34 +01:00
|
|
|
|
) ([]types.StateEntryList, error) {
|
|
|
|
|
return d.stateEntriesForTuples(ctx, nil, stateBlockNIDs, stateKeyTuples)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) stateEntriesForTuples(
|
|
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
|
stateKeyTuples []types.StateKeyTuple,
|
2020-05-27 10:36:09 +02:00
|
|
|
|
) ([]types.StateEntryList, error) {
|
2021-04-26 14:25:57 +02:00
|
|
|
|
entries, err := d.StateBlockTable.BulkSelectStateBlockEntries(
|
2022-02-04 11:39:34 +01:00
|
|
|
|
ctx, txn, stateBlockNIDs,
|
2020-05-27 10:36:09 +02:00
|
|
|
|
)
|
2021-04-26 14:25:57 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("d.StateBlockTable.BulkSelectStateBlockEntries: %w", err)
|
|
|
|
|
}
|
|
|
|
|
lists := []types.StateEntryList{}
|
|
|
|
|
for i, entry := range entries {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
entries, err := d.EventsTable.BulkSelectStateEventByNID(ctx, txn, entry, stateKeyTuples)
|
2021-04-26 14:25:57 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("d.EventsTable.BulkSelectStateEventByNID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
lists = append(lists, types.StateEntryList{
|
|
|
|
|
StateBlockNID: stateBlockNIDs[i],
|
|
|
|
|
StateEntries: entries,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return lists, nil
|
2020-05-27 10:36:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *Database) RoomInfoByNID(ctx context.Context, roomNID types.RoomNID) (*types.RoomInfo, error) {
|
|
|
|
|
roomIDs, err := d.RoomsTable.BulkSelectRoomIDs(ctx, nil, []types.RoomNID{roomNID})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(roomIDs) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("room does not exist")
|
|
|
|
|
}
|
|
|
|
|
return d.roomInfo(ctx, nil, roomIDs[0])
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 13:40:49 +02:00
|
|
|
|
func (d *Database) RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.roomInfo(ctx, nil, roomID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) roomInfo(ctx context.Context, txn *sql.Tx, roomID string) (*types.RoomInfo, error) {
|
2022-08-03 18:14:21 +02:00
|
|
|
|
roomInfo, err := d.RoomsTable.SelectRoomInfo(ctx, txn, roomID)
|
2022-08-02 13:27:15 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if roomInfo != nil {
|
2022-08-03 18:14:21 +02:00
|
|
|
|
d.Cache.StoreRoomServerRoomID(roomInfo.RoomNID, roomID)
|
|
|
|
|
d.Cache.StoreRoomVersion(roomID, roomInfo.RoomVersion)
|
2022-08-02 13:27:15 +02:00
|
|
|
|
}
|
2022-08-03 18:14:21 +02:00
|
|
|
|
return roomInfo, err
|
2020-09-01 13:40:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
|
func (d *Database) AddState(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
|
state []types.StateEntry,
|
2022-02-04 11:39:34 +01:00
|
|
|
|
) (stateNID types.StateSnapshotNID, err error) {
|
|
|
|
|
return d.addState(ctx, nil, roomNID, stateBlockNIDs, state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) addState(
|
|
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
|
state []types.StateEntry,
|
2020-05-27 10:36:09 +02:00
|
|
|
|
) (stateNID types.StateSnapshotNID, err error) {
|
2021-06-21 10:41:12 +02:00
|
|
|
|
if len(stateBlockNIDs) > 0 && len(state) > 0 {
|
2021-04-26 14:25:57 +02:00
|
|
|
|
// Check to see if the event already appears in any of the existing state
|
|
|
|
|
// blocks. If it does then we should not add it again, as this will just
|
|
|
|
|
// result in excess state blocks and snapshots.
|
|
|
|
|
// TODO: Investigate why this is happening - probably input_events.go!
|
2022-02-04 11:39:34 +01:00
|
|
|
|
blocks, berr := d.StateBlockTable.BulkSelectStateBlockEntries(ctx, txn, stateBlockNIDs)
|
2021-04-26 14:25:57 +02:00
|
|
|
|
if berr != nil {
|
|
|
|
|
return 0, fmt.Errorf("d.StateBlockTable.BulkSelectStateBlockEntries: %w", berr)
|
|
|
|
|
}
|
2021-11-02 17:47:39 +01:00
|
|
|
|
var found bool
|
2021-04-26 14:25:57 +02:00
|
|
|
|
for i := len(state) - 1; i >= 0; i-- {
|
2021-11-02 17:47:39 +01:00
|
|
|
|
found = false
|
2021-04-26 14:25:57 +02:00
|
|
|
|
for _, events := range blocks {
|
|
|
|
|
for _, event := range events {
|
|
|
|
|
if state[i].EventNID == event {
|
2021-11-02 17:47:39 +01:00
|
|
|
|
found = true
|
|
|
|
|
break
|
2021-04-26 14:25:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-02 17:47:39 +01:00
|
|
|
|
if found {
|
|
|
|
|
state = append(state[:i], state[i+1:]...)
|
|
|
|
|
i--
|
|
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
err = d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
2020-05-27 10:36:09 +02:00
|
|
|
|
if len(state) > 0 {
|
2021-04-26 14:25:57 +02:00
|
|
|
|
// If there's any state left to add then let's add new blocks.
|
2020-05-27 10:36:09 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) EventNIDs(
|
2020-05-26 17:45:28 +02:00
|
|
|
|
ctx context.Context, eventIDs []string,
|
2023-02-24 09:40:20 +01:00
|
|
|
|
) (map[string]types.EventMetadata, error) {
|
2022-02-17 14:53:48 +01:00
|
|
|
|
return d.eventNIDs(ctx, nil, eventIDs, NoFilter)
|
2022-02-04 11:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 14:53:48 +01:00
|
|
|
|
type UnsentFilter bool
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
NoFilter UnsentFilter = false
|
|
|
|
|
FilterUnsentOnly UnsentFilter = true
|
|
|
|
|
)
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) eventNIDs(
|
2022-02-17 14:53:48 +01:00
|
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string, filter UnsentFilter,
|
2023-02-24 09:40:20 +01:00
|
|
|
|
) (map[string]types.EventMetadata, error) {
|
2022-02-17 14:53:48 +01:00
|
|
|
|
switch filter {
|
|
|
|
|
case FilterUnsentOnly:
|
|
|
|
|
return d.EventsTable.BulkSelectUnsentEventNID(ctx, txn, eventIDs)
|
|
|
|
|
case NoFilter:
|
|
|
|
|
return d.EventsTable.BulkSelectEventNID(ctx, txn, eventIDs)
|
|
|
|
|
default:
|
|
|
|
|
panic("impossible case")
|
|
|
|
|
}
|
2020-05-26 17:45:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) SetState(
|
2020-05-26 17:45:28 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) StateAtEventIDs(
|
2020-05-26 17:45:28 +02:00
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
|
) ([]types.StateAtEvent, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.EventsTable.BulkSelectStateAtEventByID(ctx, nil, eventIDs)
|
2020-05-26 17:45:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) SnapshotNIDFromEventID(
|
2020-05-26 17:45:28 +02:00
|
|
|
|
ctx context.Context, eventID string,
|
|
|
|
|
) (types.StateSnapshotNID, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.snapshotNIDFromEventID(ctx, nil, eventID)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) snapshotNIDFromEventID(
|
2022-02-04 11:39:34 +01:00
|
|
|
|
ctx context.Context, txn *sql.Tx, eventID string,
|
|
|
|
|
) (types.StateSnapshotNID, error) {
|
|
|
|
|
_, stateNID, err := d.EventsTable.SelectEvent(ctx, txn, eventID)
|
2022-07-05 16:01:34 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
if stateNID == 0 {
|
|
|
|
|
return 0, sql.ErrNoRows // effectively there's no state entry
|
|
|
|
|
}
|
2020-05-26 17:45:28 +02:00
|
|
|
|
return stateNID, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) EventIDs(
|
2020-05-26 17:45:28 +02:00
|
|
|
|
ctx context.Context, eventNIDs []types.EventNID,
|
|
|
|
|
) (map[types.EventNID]string, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.EventsTable.BulkSelectEventID(ctx, nil, eventNIDs)
|
2020-05-26 17:45:28 +02:00
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) EventsFromIDs(ctx context.Context, roomInfo *types.RoomInfo, eventIDs []string) ([]types.Event, error) {
|
|
|
|
|
return d.eventsFromIDs(ctx, nil, roomInfo, eventIDs, NoFilter)
|
2022-02-04 11:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) eventsFromIDs(ctx context.Context, txn *sql.Tx, roomInfo *types.RoomInfo, eventIDs []string, filter UnsentFilter) ([]types.Event, error) {
|
2022-02-17 14:53:48 +01:00
|
|
|
|
nidMap, err := d.eventNIDs(ctx, txn, eventIDs, filter)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var nids []types.EventNID
|
|
|
|
|
for _, nid := range nidMap {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
nids = append(nids, nid.EventNID)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
return d.events(ctx, txn, roomInfo, nids)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.stateBlockNIDs(ctx, nil, stateNIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) stateBlockNIDs(
|
|
|
|
|
ctx context.Context, txn *sql.Tx, stateNIDs []types.StateSnapshotNID,
|
|
|
|
|
) ([]types.StateBlockNIDList, error) {
|
|
|
|
|
return d.StateSnapshotTable.BulkSelectStateBlockNIDs(ctx, txn, stateNIDs)
|
2020-05-27 10:36:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) StateEntries(
|
|
|
|
|
ctx context.Context, stateBlockNIDs []types.StateBlockNID,
|
2022-02-04 11:39:34 +01:00
|
|
|
|
) ([]types.StateEntryList, error) {
|
|
|
|
|
return d.stateEntries(ctx, nil, stateBlockNIDs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) stateEntries(
|
|
|
|
|
ctx context.Context, txn *sql.Tx, stateBlockNIDs []types.StateBlockNID,
|
2020-05-27 10:36:09 +02:00
|
|
|
|
) ([]types.StateEntryList, error) {
|
2021-04-26 14:25:57 +02:00
|
|
|
|
entries, err := d.StateBlockTable.BulkSelectStateBlockEntries(
|
2022-02-04 11:39:34 +01:00
|
|
|
|
ctx, txn, stateBlockNIDs,
|
2021-04-26 14:25:57 +02:00
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("d.StateBlockTable.BulkSelectStateBlockEntries: %w", err)
|
|
|
|
|
}
|
|
|
|
|
lists := make([]types.StateEntryList, 0, len(entries))
|
|
|
|
|
for i, entry := range entries {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
eventNIDs, err := d.EventsTable.BulkSelectStateEventByNID(ctx, txn, entry, nil)
|
2021-04-26 14:25:57 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("d.EventsTable.BulkSelectStateEventByNID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
lists = append(lists, types.StateEntryList{
|
|
|
|
|
StateBlockNID: stateBlockNIDs[i],
|
|
|
|
|
StateEntries: eventNIDs,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return lists, nil
|
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) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.RoomAliasesTable.SelectRoomIDFromAlias(ctx, nil, alias)
|
2020-05-27 10:36:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.RoomAliasesTable.SelectAliasesFromRoomID(ctx, nil, roomID)
|
2020-05-27 10:36:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) GetCreatorIDForAlias(
|
|
|
|
|
ctx context.Context, alias string,
|
|
|
|
|
) (string, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.RoomAliasesTable.SelectCreatorIDFromAlias(ctx, nil, alias)
|
2020-05-27 10:36:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-11-05 11:19:23 +01:00
|
|
|
|
func (d *Database) GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderUserID string) (membershipEventNID types.EventNID, stillInRoom, isRoomforgotten 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 {
|
2022-09-09 14:13:04 +02:00
|
|
|
|
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-11-05 11:19:23 +01:00
|
|
|
|
return 0, false, false, fmt.Errorf("d.assignStateKeyNID: %w", err)
|
2020-05-27 12:03:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 11:19:23 +01:00
|
|
|
|
senderMembershipEventNID, senderMembership, isRoomforgotten, err :=
|
2020-05-27 12:03:47 +02:00
|
|
|
|
d.MembershipTable.SelectMembershipFromRoomAndTarget(
|
2022-02-04 11:39:34 +01:00
|
|
|
|
ctx, nil, roomNID, requestSenderUserNID,
|
2020-05-27 12:03:47 +02:00
|
|
|
|
)
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
// The user has never been a member of that room
|
2020-11-05 11:19:23 +01:00
|
|
|
|
return 0, false, false, nil
|
2020-05-27 12:03:47 +02:00
|
|
|
|
} else if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 11:19:23 +01:00
|
|
|
|
return senderMembershipEventNID, senderMembership == tables.MembershipStateJoin, isRoomforgotten, nil
|
2020-05-27 12:03:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) GetMembershipEventNIDsForRoom(
|
|
|
|
|
ctx context.Context, roomNID types.RoomNID, joinOnly bool, localOnly bool,
|
2022-02-04 11:39:34 +01:00
|
|
|
|
) ([]types.EventNID, error) {
|
|
|
|
|
return d.getMembershipEventNIDsForRoom(ctx, nil, roomNID, joinOnly, localOnly)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) getMembershipEventNIDsForRoom(
|
|
|
|
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, joinOnly bool, localOnly bool,
|
2020-05-27 12:03:47 +02:00
|
|
|
|
) ([]types.EventNID, error) {
|
|
|
|
|
if joinOnly {
|
|
|
|
|
return d.MembershipTable.SelectMembershipsFromRoomAndMembership(
|
2022-02-04 11:39:34 +01:00
|
|
|
|
ctx, txn, roomNID, tables.MembershipStateJoin, localOnly,
|
2020-05-27 12:03:47 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.MembershipTable.SelectMembershipsFromRoom(ctx, txn, roomNID, localOnly)
|
2020-05-27 12:03:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) GetInvitesForUser(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
2022-10-07 16:00:12 +02:00
|
|
|
|
) (senderUserIDs []types.EventStateKeyNID, eventIDs []string, inviteEventJSON []byte, err error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.InvitesTable.SelectInviteActiveForUserInRoom(ctx, nil, targetUserNID, roomNID)
|
2020-05-27 12:03:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) Events(ctx context.Context, roomInfo *types.RoomInfo, eventNIDs []types.EventNID) ([]types.Event, error) {
|
|
|
|
|
return d.events(ctx, nil, roomInfo, eventNIDs)
|
2022-02-04 11:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) events(
|
|
|
|
|
ctx context.Context, txn *sql.Tx, roomInfo *types.RoomInfo, inputEventNIDs types.EventNIDs,
|
2022-02-04 11:39:34 +01:00
|
|
|
|
) ([]types.Event, error) {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
if roomInfo == nil { // this should never happen
|
|
|
|
|
return nil, fmt.Errorf("unable to parse events without roomInfo")
|
2023-02-24 09:40:20 +01:00
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
|
2022-07-11 15:31:31 +02:00
|
|
|
|
sort.Sort(inputEventNIDs)
|
|
|
|
|
events := make(map[types.EventNID]*gomatrixserverlib.Event, len(inputEventNIDs))
|
|
|
|
|
eventNIDs := make([]types.EventNID, 0, len(inputEventNIDs))
|
|
|
|
|
for _, nid := range inputEventNIDs {
|
|
|
|
|
if event, ok := d.Cache.GetRoomServerEvent(nid); ok && event != nil {
|
|
|
|
|
events[nid] = event
|
|
|
|
|
} else {
|
|
|
|
|
eventNIDs = append(eventNIDs, nid)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-01 16:07:17 +01:00
|
|
|
|
// If we don't need to get any events from the database, short circuit now
|
|
|
|
|
if len(eventNIDs) == 0 {
|
|
|
|
|
results := make([]types.Event, 0, len(inputEventNIDs))
|
|
|
|
|
for _, nid := range inputEventNIDs {
|
|
|
|
|
event, ok := events[nid]
|
|
|
|
|
if !ok || event == nil {
|
|
|
|
|
return nil, fmt.Errorf("event %d missing", nid)
|
|
|
|
|
}
|
|
|
|
|
results = append(results, types.Event{
|
|
|
|
|
EventNID: nid,
|
|
|
|
|
Event: event,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if !redactionsArePermanent {
|
|
|
|
|
d.applyRedactions(results)
|
|
|
|
|
}
|
2023-03-09 09:52:13 +01:00
|
|
|
|
return results, nil
|
2022-11-01 16:07:17 +01:00
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
eventJSONs, err := d.EventJSONTable.BulkSelectEventJSON(ctx, txn, eventNIDs)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
eventIDs, err := d.EventsTable.BulkSelectEventID(ctx, txn, eventNIDs)
|
2020-12-04 11:41:07 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
eventIDs = map[types.EventNID]string{}
|
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
|
2023-04-21 18:06:29 +02:00
|
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomInfo.RoomVersion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 15:31:31 +02:00
|
|
|
|
for _, eventJSON := range eventJSONs {
|
2023-03-09 09:52:13 +01:00
|
|
|
|
redacted := gjson.GetBytes(eventJSON.EventJSON, "unsigned.redacted_because").Exists()
|
2023-04-21 18:06:29 +02:00
|
|
|
|
events[eventJSON.EventNID], err = verImpl.NewEventFromTrustedJSONWithEventID(
|
2023-04-20 20:07:31 +02:00
|
|
|
|
eventIDs[eventJSON.EventNID], eventJSON.EventJSON, redacted,
|
2020-05-26 19:23:39 +02:00
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-07-11 15:31:31 +02:00
|
|
|
|
if event := events[eventJSON.EventNID]; event != nil {
|
|
|
|
|
d.Cache.StoreRoomServerEvent(eventJSON.EventNID, event)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
results := make([]types.Event, 0, len(inputEventNIDs))
|
|
|
|
|
for _, nid := range inputEventNIDs {
|
|
|
|
|
event, ok := events[nid]
|
|
|
|
|
if !ok || event == nil {
|
|
|
|
|
return nil, fmt.Errorf("event %d missing", nid)
|
|
|
|
|
}
|
|
|
|
|
results = append(results, types.Event{
|
|
|
|
|
EventNID: nid,
|
|
|
|
|
Event: event,
|
|
|
|
|
})
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
2020-07-06 18:49:15 +02:00
|
|
|
|
if !redactionsArePermanent {
|
|
|
|
|
d.applyRedactions(results)
|
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
return results, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 16:07:17 +01:00
|
|
|
|
func (d *Database) BulkSelectSnapshotsFromEventIDs(
|
|
|
|
|
ctx context.Context, eventIDs []string,
|
|
|
|
|
) (map[types.StateSnapshotNID][]string, error) {
|
|
|
|
|
return d.EventsTable.BulkSelectSnapshotsFromEventIDs(ctx, nil, eventIDs)
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2020-09-10 15:39:18 +02:00
|
|
|
|
return err
|
2020-08-20 10:24:52 +02:00
|
|
|
|
})
|
|
|
|
|
return updater, err
|
2020-05-28 12:15:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 11:39:34 +01:00
|
|
|
|
func (d *Database) GetRoomUpdater(
|
|
|
|
|
ctx context.Context, roomInfo *types.RoomInfo,
|
|
|
|
|
) (*RoomUpdater, error) {
|
|
|
|
|
if d.GetRoomUpdaterFn != nil {
|
|
|
|
|
return d.GetRoomUpdaterFn(ctx, roomInfo)
|
2020-10-20 20:32:33 +02:00
|
|
|
|
}
|
2020-08-19 14:24:54 +02:00
|
|
|
|
txn, err := d.DB.Begin()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
var updater *RoomUpdater
|
2020-08-20 10:24:52 +02:00
|
|
|
|
_ = d.Writer.Do(d.DB, txn, func(txn *sql.Tx) error {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
updater, err = NewRoomUpdater(ctx, d, txn, roomInfo)
|
2020-09-10 15:39:18 +02:00
|
|
|
|
return err
|
2020-08-20 10:24:52 +02:00
|
|
|
|
})
|
|
|
|
|
return updater, err
|
2020-05-28 12:15:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 11:37:47 +02:00
|
|
|
|
func (d *Database) IsEventRejected(ctx context.Context, roomNID types.RoomNID, eventID string) (bool, error) {
|
|
|
|
|
return d.EventsTable.SelectEventRejected(ctx, nil, roomNID, eventID)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
// GetOrCreateRoomInfo gets or creates a new RoomInfo, which is only safe to use with functions only needing a roomVersion or roomNID.
|
|
|
|
|
func (d *Database) GetOrCreateRoomInfo(ctx context.Context, event *gomatrixserverlib.Event) (roomInfo *types.RoomInfo, err error) {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
// 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 {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
return nil, fmt.Errorf("extractRoomVersionFromCreateEvent: %w", err)
|
2022-02-04 11:39:34 +01:00
|
|
|
|
}
|
2023-03-03 14:03:17 +01:00
|
|
|
|
if roomVersion == "" {
|
|
|
|
|
rv, ok := d.Cache.GetRoomVersion(event.RoomID())
|
|
|
|
|
if ok {
|
|
|
|
|
roomVersion = rv
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
var roomNID types.RoomNID
|
2022-10-07 15:05:06 +02:00
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
roomNID, err = d.assignRoomNID(ctx, txn, event.RoomID(), roomVersion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
return nil
|
|
|
|
|
})
|
2023-03-03 14:03:17 +01:00
|
|
|
|
if roomVersion != "" {
|
|
|
|
|
d.Cache.StoreRoomVersion(event.RoomID(), roomVersion)
|
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
return &types.RoomInfo{
|
|
|
|
|
RoomVersion: roomVersion,
|
|
|
|
|
RoomNID: roomNID,
|
|
|
|
|
}, err
|
2023-02-24 09:40:20 +01:00
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
|
2023-02-24 09:40:20 +01:00
|
|
|
|
func (d *Database) GetOrCreateEventTypeNID(ctx context.Context, eventType string) (eventTypeNID types.EventTypeNID, err error) {
|
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
|
if eventTypeNID, err = d.assignEventTypeNID(ctx, txn, eventType); 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
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
return eventTypeNID, err
|
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
|
2023-02-24 09:40:20 +01:00
|
|
|
|
func (d *Database) GetOrCreateEventStateKeyNID(ctx context.Context, eventStateKey *string) (eventStateKeyNID types.EventStateKeyNID, err error) {
|
|
|
|
|
if eventStateKey == nil {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
|
2023-02-24 09:40:20 +01:00
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
|
if eventStateKeyNID, err = d.assignStateKeyNID(ctx, txn, *eventStateKey); err != nil {
|
|
|
|
|
return fmt.Errorf("d.assignStateKeyNID: %w", err)
|
|
|
|
|
}
|
2022-09-09 14:13:04 +02:00
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
return 0, err
|
2022-09-09 14:13:04 +02:00
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
|
|
|
|
|
return eventStateKeyNID, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) StoreEvent(
|
2023-02-24 09:40:20 +01:00
|
|
|
|
ctx context.Context, event *gomatrixserverlib.Event,
|
2023-03-01 17:06:47 +01:00
|
|
|
|
roomInfo *types.RoomInfo, eventTypeNID types.EventTypeNID, eventStateKeyNID types.EventStateKeyNID,
|
2023-02-24 09:40:20 +01:00
|
|
|
|
authEventNIDs []types.EventNID, isRejected bool,
|
2023-03-01 17:06:47 +01:00
|
|
|
|
) (types.EventNID, types.StateAtEvent, error) {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
var (
|
2023-03-01 17:06:47 +01:00
|
|
|
|
eventNID types.EventNID
|
|
|
|
|
stateNID types.StateSnapshotNID
|
|
|
|
|
err error
|
2023-02-24 09:40:20 +01:00
|
|
|
|
)
|
2023-03-01 17:06:47 +01:00
|
|
|
|
|
2023-02-24 09:40:20 +01:00
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2020-05-26 19:23:39 +02:00
|
|
|
|
if eventNID, stateNID, err = d.EventsTable.InsertEvent(
|
|
|
|
|
ctx,
|
|
|
|
|
txn,
|
2023-03-01 17:06:47 +01:00
|
|
|
|
roomInfo.RoomNID,
|
2020-05-26 19:23:39 +02:00
|
|
|
|
eventTypeNID,
|
|
|
|
|
eventStateKeyNID,
|
|
|
|
|
event.EventID(),
|
|
|
|
|
event.EventReference().EventSHA256,
|
|
|
|
|
authEventNIDs,
|
|
|
|
|
event.Depth(),
|
2020-09-16 14:00:52 +02:00
|
|
|
|
isRejected,
|
2020-05-26 19:23:39 +02:00
|
|
|
|
); 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())
|
2022-02-10 11:05:14 +01:00
|
|
|
|
} else if err != nil {
|
|
|
|
|
return fmt.Errorf("d.EventsTable.InsertEvent: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2020-10-15 15:14:17 +02:00
|
|
|
|
return fmt.Errorf("d.EventsTable.SelectEvent: %w", 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
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
|
|
|
|
|
if prevEvents := event.PrevEvents(); len(prevEvents) > 0 {
|
|
|
|
|
// Create an updater - NB: on sqlite this WILL create a txn as we are directly calling the shared DB form of
|
|
|
|
|
// GetLatestEventsForUpdate - not via the SQLiteDatabase form which has `nil` txns. This
|
|
|
|
|
// function only does SELECTs though so the created txn (at this point) is just a read txn like
|
|
|
|
|
// any other so this is fine. If we ever update GetLatestEventsForUpdate or NewLatestEventsUpdater
|
|
|
|
|
// to do writes however then this will need to go inside `Writer.Do`.
|
|
|
|
|
|
|
|
|
|
// The following is a copy of RoomUpdater.StorePreviousEvents
|
|
|
|
|
for _, ref := range prevEvents {
|
|
|
|
|
if err = d.PrevEventsTable.InsertPreviousEvent(ctx, txn, ref.EventID, ref.EventSHA256, eventNID); err != nil {
|
|
|
|
|
return fmt.Errorf("u.d.PrevEventsTable.InsertPreviousEvent: %w", err)
|
|
|
|
|
}
|
2020-10-15 15:14:17 +02:00
|
|
|
|
}
|
2020-09-16 14:00:52 +02:00
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
|
2020-07-21 16:48:21 +02:00
|
|
|
|
return nil
|
2020-05-26 19:23:39 +02:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
return 0, types.StateAtEvent{}, fmt.Errorf("d.Writer.Do: %w", err)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 15:05:33 +02:00
|
|
|
|
// We should attempt to update the previous events table with any
|
|
|
|
|
// references that this new event makes. We do this using a latest
|
|
|
|
|
// events updater because it somewhat works as a mutex, ensuring
|
|
|
|
|
// that there's a row-level lock on the latest room events (well,
|
|
|
|
|
// on Postgres at least).
|
|
|
|
|
|
2023-02-24 09:40:20 +01:00
|
|
|
|
return eventNID, types.StateAtEvent{
|
2020-05-26 19:23:39 +02:00
|
|
|
|
BeforeStateSnapshotNID: stateNID,
|
|
|
|
|
StateEntry: types.StateEntry{
|
|
|
|
|
StateKeyTuple: types.StateKeyTuple{
|
|
|
|
|
EventTypeNID: eventTypeNID,
|
|
|
|
|
EventStateKeyNID: eventStateKeyNID,
|
|
|
|
|
},
|
|
|
|
|
EventNID: eventNID,
|
|
|
|
|
},
|
2023-03-01 17:06:47 +01:00
|
|
|
|
}, err
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 14:40:35 +02:00
|
|
|
|
func (d *Database) PublishRoom(ctx context.Context, roomID, appserviceID, networkID string, publish bool) error {
|
2020-08-20 17:24:33 +02:00
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-10-27 14:40:35 +02:00
|
|
|
|
return d.PublishedTable.UpsertRoomPublished(ctx, txn, roomID, appserviceID, networkID, publish)
|
2020-08-19 16:38:27 +02:00
|
|
|
|
})
|
2020-07-02 16:41:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 11:42:24 +01:00
|
|
|
|
func (d *Database) GetPublishedRoom(ctx context.Context, roomID string) (bool, error) {
|
|
|
|
|
return d.PublishedTable.SelectPublishedFromRoomID(ctx, nil, roomID)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 14:40:35 +02:00
|
|
|
|
func (d *Database) GetPublishedRooms(ctx context.Context, networkID string, includeAllNetworks bool) ([]string, error) {
|
|
|
|
|
return d.PublishedTable.SelectAllPublishedRooms(ctx, nil, networkID, true, includeAllNetworks)
|
2020-07-02 16:41:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 16:41:32 +01:00
|
|
|
|
func (d *Database) MissingAuthPrevEvents(
|
|
|
|
|
ctx context.Context, e *gomatrixserverlib.Event,
|
|
|
|
|
) (missingAuth, missingPrev []string, err error) {
|
|
|
|
|
authEventNIDs, err := d.EventNIDs(ctx, e.AuthEventIDs())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, fmt.Errorf("d.EventNIDs: %w", err)
|
|
|
|
|
}
|
|
|
|
|
for _, authEventID := range e.AuthEventIDs() {
|
|
|
|
|
if _, ok := authEventNIDs[authEventID]; !ok {
|
|
|
|
|
missingAuth = append(missingAuth, authEventID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, prevEventID := range e.PrevEventIDs() {
|
|
|
|
|
state, err := d.StateAtEventIDs(ctx, []string{prevEventID})
|
|
|
|
|
if err != nil || len(state) == 0 || (!state[0].IsCreate() && state[0].BeforeStateSnapshotNID == 0) {
|
|
|
|
|
missingPrev = append(missingPrev, prevEventID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 19:23:39 +02:00
|
|
|
|
func (d *Database) assignRoomNID(
|
2022-09-09 14:13:04 +02:00
|
|
|
|
ctx context.Context, txn *sql.Tx, roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
2020-05-26 19:23:39 +02:00
|
|
|
|
) (types.RoomNID, error) {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
roomNID, ok := d.Cache.GetRoomServerRoomNID(roomID)
|
|
|
|
|
if ok {
|
|
|
|
|
return roomNID, nil
|
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
// Check if we already have a numeric ID in the database.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
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.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
roomNID, err = d.RoomsTable.InsertRoomNID(ctx, txn, roomID, roomVersion)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
// We raced with another insert so run the select again.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
d.Cache.StoreRoomServerRoomID(roomNID, roomID)
|
2023-03-03 14:03:17 +01:00
|
|
|
|
d.Cache.StoreRoomVersion(roomID, roomVersion)
|
2023-02-24 09:40:20 +01:00
|
|
|
|
return roomNID, nil
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Database) assignEventTypeNID(
|
2022-09-09 14:13:04 +02:00
|
|
|
|
ctx context.Context, txn *sql.Tx, eventType string,
|
2020-08-25 13:32:29 +02:00
|
|
|
|
) (types.EventTypeNID, error) {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
eventTypeNID, ok := d.Cache.GetEventTypeKey(eventType)
|
|
|
|
|
if ok {
|
|
|
|
|
return eventTypeNID, nil
|
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
// Check if we already have a numeric ID in the database.
|
2022-09-09 14:13:04 +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.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
eventTypeNID, err = d.EventTypesTable.InsertEventTypeNID(ctx, txn, eventType)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
// We raced with another insert so run the select again.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
eventTypeNID, err = d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
d.Cache.StoreEventTypeKey(eventTypeNID, eventType)
|
|
|
|
|
return eventTypeNID, nil
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) assignStateKeyNID(
|
2022-09-09 14:13:04 +02:00
|
|
|
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
2020-05-26 19:23:39 +02:00
|
|
|
|
) (types.EventStateKeyNID, error) {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
eventStateKeyNID, ok := d.Cache.GetEventStateKeyNID(eventStateKey)
|
|
|
|
|
if ok {
|
|
|
|
|
return eventStateKeyNID, nil
|
|
|
|
|
}
|
2020-05-26 19:23:39 +02:00
|
|
|
|
// Check if we already have a numeric ID in the database.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
eventStateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
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.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
eventStateKeyNID, err = d.EventStateKeysTable.InsertEventStateKeyNID(ctx, txn, eventStateKey)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
// We raced with another insert so run the select again.
|
2022-09-09 14:13:04 +02:00
|
|
|
|
eventStateKeyNID, err = d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
d.Cache.StoreEventStateKey(eventStateKeyNID, eventStateKey)
|
2020-05-26 19:23:39 +02:00
|
|
|
|
return eventStateKeyNID, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 16:44:53 +01:00
|
|
|
|
func extractRoomVersionFromCreateEvent(event *gomatrixserverlib.Event) (
|
2020-05-26 19:23:39 +02:00
|
|
|
|
gomatrixserverlib.RoomVersion, error,
|
|
|
|
|
) {
|
|
|
|
|
var err error
|
|
|
|
|
var roomVersion gomatrixserverlib.RoomVersion
|
|
|
|
|
// Look for m.room.create events.
|
2023-04-19 16:50:33 +02:00
|
|
|
|
if event.Type() != spec.MRoomCreate {
|
2020-05-26 19:23:39 +02:00
|
|
|
|
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
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
// MaybeRedactEvent manages the redacted status of events. There's two cases to consider in order to comply with the spec:
|
2020-07-06 18:49:15 +02:00
|
|
|
|
// "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:
|
2022-08-03 18:14:21 +02:00
|
|
|
|
// - 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.
|
|
|
|
|
//
|
2020-07-06 18:49:15 +02:00
|
|
|
|
// 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
|
|
|
|
//
|
2023-03-01 17:06:47 +01:00
|
|
|
|
// Returns the redaction event and the redacted event if this call resulted in a redaction.
|
|
|
|
|
func (d *EventDatabase) MaybeRedactEvent(
|
2023-03-03 14:03:17 +01:00
|
|
|
|
ctx context.Context, roomInfo *types.RoomInfo, eventNID types.EventNID, event *gomatrixserverlib.Event, plResolver state.PowerLevelResolver,
|
2023-03-01 17:06:47 +01:00
|
|
|
|
) (*gomatrixserverlib.Event, *gomatrixserverlib.Event, error) {
|
|
|
|
|
var (
|
|
|
|
|
redactionEvent, redactedEvent *types.Event
|
|
|
|
|
err error
|
|
|
|
|
validated bool
|
|
|
|
|
ignoreRedaction bool
|
|
|
|
|
)
|
2020-07-07 13:51:55 +02:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
wErr := d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2023-04-19 16:50:33 +02:00
|
|
|
|
isRedactionEvent := event.Type() == spec.MRoomRedaction && event.StateKey() == nil
|
2023-03-01 17:06:47 +01:00
|
|
|
|
if isRedactionEvent {
|
|
|
|
|
// an event which redacts itself should be ignored
|
|
|
|
|
if event.EventID() == event.Redacts() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = d.RedactionsTable.InsertRedaction(ctx, txn, tables.RedactionInfo{
|
|
|
|
|
Validated: false,
|
|
|
|
|
RedactionEventID: event.EventID(),
|
|
|
|
|
RedactsEventID: event.Redacts(),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("d.RedactionsTable.InsertRedaction: %w", err)
|
|
|
|
|
}
|
2020-07-07 13:51:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
redactionEvent, redactedEvent, validated, err = d.loadRedactionPair(ctx, txn, roomInfo, eventNID, event)
|
|
|
|
|
switch {
|
|
|
|
|
case err != nil:
|
|
|
|
|
return fmt.Errorf("d.loadRedactionPair: %w", err)
|
|
|
|
|
case validated || redactedEvent == nil || redactionEvent == nil:
|
|
|
|
|
// we've seen this redaction before or there is nothing to redact
|
|
|
|
|
return nil
|
|
|
|
|
case redactedEvent.RoomID() != redactionEvent.RoomID():
|
|
|
|
|
// redactions across rooms aren't allowed
|
|
|
|
|
ignoreRedaction = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-07-06 18:49:15 +02:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
_, sender1, _ := gomatrixserverlib.SplitID('@', redactedEvent.Sender())
|
|
|
|
|
_, sender2, _ := gomatrixserverlib.SplitID('@', redactionEvent.Sender())
|
2023-03-03 14:03:17 +01:00
|
|
|
|
var powerlevels *gomatrixserverlib.PowerLevelContent
|
|
|
|
|
powerlevels, err = plResolver.Resolve(ctx, redactionEvent.EventID())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
case powerlevels.UserLevel(redactionEvent.Sender()) >= powerlevels.Redact:
|
|
|
|
|
// 1. The power level of the redaction event’s sender is greater than or equal to the redact level.
|
|
|
|
|
case sender1 == sender2:
|
|
|
|
|
// 2. The domain of the redaction event’s sender matches that of the original event’s sender.
|
|
|
|
|
default:
|
2023-03-01 17:06:47 +01:00
|
|
|
|
ignoreRedaction = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-06-09 18:38:07 +02:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
// mark the event as redacted
|
|
|
|
|
if redactionsArePermanent {
|
|
|
|
|
redactedEvent.Redact()
|
|
|
|
|
}
|
2022-06-09 18:38:07 +02:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
err = redactedEvent.SetUnsignedField("redacted_because", redactionEvent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("redactedEvent.SetUnsignedField: %w", err)
|
|
|
|
|
}
|
|
|
|
|
// NOTSPEC: sytest relies on this unspecced field existing :(
|
|
|
|
|
err = redactedEvent.SetUnsignedField("redacted_by", redactionEvent.EventID())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("redactedEvent.SetUnsignedField: %w", err)
|
|
|
|
|
}
|
|
|
|
|
// overwrite the eventJSON table
|
|
|
|
|
err = d.EventJSONTable.InsertEventJSON(ctx, txn, redactedEvent.EventNID, redactedEvent.JSON())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("d.EventJSONTable.InsertEventJSON: %w", err)
|
|
|
|
|
}
|
2022-06-09 18:38:07 +02:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
err = d.RedactionsTable.MarkRedactionValidated(ctx, txn, redactionEvent.EventID(), true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("d.RedactionsTable.MarkRedactionValidated: %w", err)
|
|
|
|
|
}
|
2023-03-03 14:49:41 +01:00
|
|
|
|
|
2023-03-09 09:52:13 +01:00
|
|
|
|
// We remove the entry from the cache, as if we just "StoreRoomServerEvent", we can't be
|
|
|
|
|
// certain that the cached entry actually is updated, since ristretto is eventual-persistent.
|
|
|
|
|
d.Cache.InvalidateRoomServerEvent(redactedEvent.EventNID)
|
2023-03-03 14:49:41 +01:00
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if wErr != nil {
|
|
|
|
|
return nil, nil, err
|
2020-10-15 15:14:17 +02:00
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
if ignoreRedaction || redactionEvent == nil || redactedEvent == nil {
|
|
|
|
|
return nil, nil, nil
|
2020-07-06 18:49:15 +02:00
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
return redactionEvent.Event, redactedEvent.Event, nil
|
2020-07-06 18:49:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// loadRedactionPair returns both the redaction event and the redacted event, else nil.
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) loadRedactionPair(
|
|
|
|
|
ctx context.Context, txn *sql.Tx, roomInfo *types.RoomInfo, eventNID types.EventNID, event *gomatrixserverlib.Event,
|
2020-07-06 18:49:15 +02:00
|
|
|
|
) (*types.Event, *types.Event, bool, error) {
|
|
|
|
|
var redactionEvent, redactedEvent *types.Event
|
|
|
|
|
var info *tables.RedactionInfo
|
|
|
|
|
var err error
|
2023-04-19 16:50:33 +02:00
|
|
|
|
isRedactionEvent := event.Type() == spec.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 {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
redactedEvent = d.loadEvent(ctx, roomInfo, info.RedactsEventID)
|
2020-07-07 13:51:55 +02:00
|
|
|
|
} else {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
redactionEvent = d.loadEvent(ctx, roomInfo, info.RedactionEventID)
|
2020-07-07 13:51:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
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.
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) applyRedactions(events []types.Event) {
|
2020-07-06 18:49:15 +02:00
|
|
|
|
for i := range events {
|
|
|
|
|
if result := gjson.GetBytes(events[i].Unsigned(), "redacted_because"); result.Exists() {
|
2022-07-11 15:31:31 +02:00
|
|
|
|
events[i].Redact()
|
2020-07-06 18:49:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-07 13:51:55 +02:00
|
|
|
|
|
|
|
|
|
// loadEvent loads a single event or returns nil on any problems/missing event
|
2023-03-01 17:06:47 +01:00
|
|
|
|
func (d *EventDatabase) loadEvent(ctx context.Context, roomInfo *types.RoomInfo, eventID string) *types.Event {
|
2022-10-07 15:05:06 +02:00
|
|
|
|
nids, err := d.EventNIDs(ctx, []string{eventID})
|
2020-07-07 13:51:55 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if len(nids) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
evs, err := d.Events(ctx, roomInfo, []types.EventNID{nids[eventID].EventNID})
|
2020-07-07 13:51:55 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if len(evs) != 1 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &evs[0]
|
|
|
|
|
}
|
2020-09-03 18:20:54 +02:00
|
|
|
|
|
2022-08-01 15:11:00 +02:00
|
|
|
|
func (d *Database) GetHistoryVisibilityState(ctx context.Context, roomInfo *types.RoomInfo, eventID string, domain string) ([]*gomatrixserverlib.Event, error) {
|
|
|
|
|
eventStates, err := d.EventsTable.BulkSelectStateAtEventByID(ctx, nil, []string{eventID})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stateSnapshotNID := eventStates[0].BeforeStateSnapshotNID
|
|
|
|
|
if stateSnapshotNID == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
eventNIDs, err := d.StateSnapshotTable.BulkSelectStateForHistoryVisibility(ctx, nil, stateSnapshotNID, domain)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
eventIDs, _ := d.EventsTable.BulkSelectEventID(ctx, nil, eventNIDs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
eventIDs = map[types.EventNID]string{}
|
|
|
|
|
}
|
2023-04-21 18:06:29 +02:00
|
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomInfo.RoomVersion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-08-01 15:11:00 +02:00
|
|
|
|
events := make([]*gomatrixserverlib.Event, 0, len(eventNIDs))
|
|
|
|
|
for _, eventNID := range eventNIDs {
|
|
|
|
|
data, err := d.EventJSONTable.BulkSelectEventJSON(ctx, nil, []types.EventNID{eventNID})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-04-21 18:06:29 +02:00
|
|
|
|
ev, err := verImpl.NewEventFromTrustedJSONWithEventID(eventIDs[eventNID], data[0].EventJSON, false)
|
2022-08-01 15:11:00 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
events = append(events, ev)
|
|
|
|
|
}
|
|
|
|
|
return events, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 18:20:54 +02:00
|
|
|
|
// GetStateEvent returns the current state event of a given type for a given room with a given state key
|
|
|
|
|
// If no event could be found, returns nil
|
|
|
|
|
// If there was an issue during the retrieval, returns an error
|
|
|
|
|
func (d *Database) GetStateEvent(ctx context.Context, roomID, evType, stateKey string) (*gomatrixserverlib.HeaderedEvent, error) {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
roomInfo, err := d.roomInfo(ctx, nil, roomID)
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-01-21 15:23:37 +01:00
|
|
|
|
if roomInfo == nil {
|
2021-07-21 17:53:50 +02:00
|
|
|
|
return nil, fmt.Errorf("room %s doesn't exist", roomID)
|
|
|
|
|
}
|
2022-01-21 15:23:37 +01:00
|
|
|
|
// e.g invited rooms
|
2022-08-01 16:29:19 +02:00
|
|
|
|
if roomInfo.IsStub() {
|
2022-01-21 15:23:37 +01:00
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-09-03 19:27:02 +02:00
|
|
|
|
eventTypeNID, err := d.EventTypesTable.SelectEventTypeNID(ctx, nil, evType)
|
2020-09-04 20:40:21 +02:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
// No rooms have an event of this type, otherwise we'd have an event type NID
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, nil, stateKey)
|
2021-07-19 19:33:05 +02:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
// No rooms have a state event with this state key, otherwise we'd have an state key NID
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
|
entries, err := d.loadStateAtSnapshot(ctx, roomInfo.StateSnapshotNID())
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-04 11:41:07 +01:00
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
|
for _, e := range entries {
|
|
|
|
|
if e.EventTypeNID == eventTypeNID && e.EventStateKeyNID == stateKeyNID {
|
|
|
|
|
eventNIDs = append(eventNIDs, e.EventNID)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-21 18:06:29 +02:00
|
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomInfo.RoomVersion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
eventIDs, _ := d.EventsTable.BulkSelectEventID(ctx, nil, eventNIDs)
|
2020-12-04 11:41:07 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
eventIDs = map[types.EventNID]string{}
|
|
|
|
|
}
|
2020-09-03 19:27:02 +02:00
|
|
|
|
// return the event requested
|
|
|
|
|
for _, e := range entries {
|
|
|
|
|
if e.EventTypeNID == eventTypeNID && e.EventStateKeyNID == stateKeyNID {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
data, err := d.EventJSONTable.BulkSelectEventJSON(ctx, nil, []types.EventNID{e.EventNID})
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("GetStateEvent: no json for event nid %d", e.EventNID)
|
|
|
|
|
}
|
2023-04-21 18:06:29 +02:00
|
|
|
|
ev, err := verImpl.NewEventFromTrustedJSONWithEventID(eventIDs[e.EventNID], data[0].EventJSON, false)
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-16 16:44:53 +01:00
|
|
|
|
return ev.Headered(roomInfo.RoomVersion), nil
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
2020-09-03 19:27:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 20:40:21 +02:00
|
|
|
|
return nil, nil
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 14:40:07 +01:00
|
|
|
|
// Same as GetStateEvent but returns all matching state events with this event type. Returns no error
|
|
|
|
|
// if there are no events with this event type.
|
|
|
|
|
func (d *Database) GetStateEventsWithEventType(ctx context.Context, roomID, evType string) ([]*gomatrixserverlib.HeaderedEvent, error) {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
roomInfo, err := d.roomInfo(ctx, nil, roomID)
|
2022-03-01 14:40:07 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if roomInfo == nil {
|
|
|
|
|
return nil, fmt.Errorf("room %s doesn't exist", roomID)
|
|
|
|
|
}
|
|
|
|
|
// e.g invited rooms
|
2022-08-01 16:29:19 +02:00
|
|
|
|
if roomInfo.IsStub() {
|
2022-03-01 14:40:07 +01:00
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
eventTypeNID, err := d.EventTypesTable.SelectEventTypeNID(ctx, nil, evType)
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
// No rooms have an event of this type, otherwise we'd have an event type NID
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
|
entries, err := d.loadStateAtSnapshot(ctx, roomInfo.StateSnapshotNID())
|
2022-03-01 14:40:07 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
|
for _, e := range entries {
|
|
|
|
|
if e.EventTypeNID == eventTypeNID {
|
|
|
|
|
eventNIDs = append(eventNIDs, e.EventNID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
eventIDs, _ := d.EventsTable.BulkSelectEventID(ctx, nil, eventNIDs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
eventIDs = map[types.EventNID]string{}
|
|
|
|
|
}
|
|
|
|
|
// return the events requested
|
|
|
|
|
eventPairs, err := d.EventJSONTable.BulkSelectEventJSON(ctx, nil, eventNIDs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(eventPairs) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2023-04-21 18:06:29 +02:00
|
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomInfo.RoomVersion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-03-01 14:40:07 +01:00
|
|
|
|
var result []*gomatrixserverlib.HeaderedEvent
|
|
|
|
|
for _, pair := range eventPairs {
|
2023-04-21 18:06:29 +02:00
|
|
|
|
ev, err := verImpl.NewEventFromTrustedJSONWithEventID(eventIDs[pair.EventNID], pair.EventJSON, false)
|
2022-03-01 14:40:07 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
result = append(result, ev.Headered(roomInfo.RoomVersion))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 18:20:54 +02:00
|
|
|
|
// GetRoomsByMembership returns a list of room IDs matching the provided membership and user ID (as state_key).
|
|
|
|
|
func (d *Database) GetRoomsByMembership(ctx context.Context, userID, membership string) ([]string, error) {
|
|
|
|
|
var membershipState tables.MembershipState
|
|
|
|
|
switch membership {
|
|
|
|
|
case "join":
|
|
|
|
|
membershipState = tables.MembershipStateJoin
|
|
|
|
|
case "invite":
|
|
|
|
|
membershipState = tables.MembershipStateInvite
|
|
|
|
|
case "leave":
|
|
|
|
|
membershipState = tables.MembershipStateLeaveOrBan
|
|
|
|
|
case "ban":
|
|
|
|
|
membershipState = tables.MembershipStateLeaveOrBan
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("GetRoomsByMembership: invalid membership %s", membership)
|
|
|
|
|
}
|
|
|
|
|
stateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, nil, userID)
|
|
|
|
|
if err != nil {
|
2020-09-04 15:25:01 +02:00
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-09-03 18:20:54 +02:00
|
|
|
|
return nil, fmt.Errorf("GetRoomsByMembership: cannot map user ID to state key NID: %w", err)
|
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
roomNIDs, err := d.MembershipTable.SelectRoomsWithMembership(ctx, nil, stateKeyNID, membershipState)
|
2020-09-03 18:20:54 +02:00
|
|
|
|
if err != nil {
|
2020-09-04 15:25:01 +02:00
|
|
|
|
return nil, fmt.Errorf("GetRoomsByMembership: failed to SelectRoomsWithMembership: %w", err)
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
roomIDs, err := d.RoomsTable.BulkSelectRoomIDs(ctx, nil, roomNIDs)
|
2020-09-03 18:20:54 +02:00
|
|
|
|
if err != nil {
|
2020-09-04 15:25:01 +02:00
|
|
|
|
return nil, fmt.Errorf("GetRoomsByMembership: failed to lookup room nids: %w", err)
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
|
|
|
|
if len(roomIDs) != len(roomNIDs) {
|
|
|
|
|
return nil, fmt.Errorf("GetRoomsByMembership: missing room IDs, got %d want %d", len(roomIDs), len(roomNIDs))
|
|
|
|
|
}
|
|
|
|
|
return roomIDs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetBulkStateContent returns all state events which match a given room ID and a given state key tuple. Both must be satisfied for a match.
|
|
|
|
|
// If a tuple has the StateKey of '*' and allowWildcards=true then all state events with the EventType should be returned.
|
2020-09-07 13:38:09 +02:00
|
|
|
|
func (d *Database) GetBulkStateContent(ctx context.Context, roomIDs []string, tuples []gomatrixserverlib.StateKeyTuple, allowWildcards bool) ([]tables.StrippedEvent, error) {
|
|
|
|
|
eventTypes := make([]string, 0, len(tuples))
|
|
|
|
|
for _, tuple := range tuples {
|
|
|
|
|
eventTypes = append(eventTypes, tuple.EventType)
|
|
|
|
|
}
|
|
|
|
|
// we don't bother failing the request if we get asked for event types we don't know about, as all that would result in is no matches which
|
|
|
|
|
// isn't a failure.
|
2022-02-04 11:39:34 +01:00
|
|
|
|
eventTypeNIDMap, err := d.EventTypesTable.BulkSelectEventTypeNID(ctx, nil, eventTypes)
|
2020-09-07 13:38:09 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetBulkStateContent: failed to map event type nids: %w", err)
|
|
|
|
|
}
|
|
|
|
|
typeNIDSet := make(map[types.EventTypeNID]bool)
|
|
|
|
|
for _, nid := range eventTypeNIDMap {
|
|
|
|
|
typeNIDSet[nid] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
allowWildcard := make(map[types.EventTypeNID]bool)
|
|
|
|
|
eventStateKeys := make([]string, 0, len(tuples))
|
|
|
|
|
for _, tuple := range tuples {
|
|
|
|
|
if allowWildcards && tuple.StateKey == "*" {
|
|
|
|
|
allowWildcard[eventTypeNIDMap[tuple.EventType]] = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
eventStateKeys = append(eventStateKeys, tuple.StateKey)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 10:52:43 +01:00
|
|
|
|
eventStateKeyNIDMap, err := d.eventStateKeyNIDs(ctx, nil, eventStateKeys)
|
2020-09-07 13:38:09 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetBulkStateContent: failed to map state key nids: %w", err)
|
|
|
|
|
}
|
|
|
|
|
stateKeyNIDSet := make(map[types.EventStateKeyNID]bool)
|
|
|
|
|
for _, nid := range eventStateKeyNIDMap {
|
|
|
|
|
stateKeyNIDSet[nid] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
|
eventNIDToVer := make(map[types.EventNID]gomatrixserverlib.RoomVersion)
|
|
|
|
|
// TODO: This feels like this is going to be really slow...
|
|
|
|
|
for _, roomID := range roomIDs {
|
2023-03-01 17:06:47 +01:00
|
|
|
|
roomInfo, err2 := d.roomInfo(ctx, nil, roomID)
|
2020-09-07 13:38:09 +02:00
|
|
|
|
if err2 != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetBulkStateContent: failed to load room info for room %s : %w", roomID, err2)
|
|
|
|
|
}
|
|
|
|
|
// for unknown rooms or rooms which we don't have the current state, skip them.
|
2022-08-01 16:29:19 +02:00
|
|
|
|
if roomInfo == nil || roomInfo.IsStub() {
|
2020-09-07 13:38:09 +02:00
|
|
|
|
continue
|
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
|
entries, err2 := d.loadStateAtSnapshot(ctx, roomInfo.StateSnapshotNID())
|
2020-09-07 13:38:09 +02:00
|
|
|
|
if err2 != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetBulkStateContent: failed to load state for room %s : %w", roomID, err2)
|
|
|
|
|
}
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
if typeNIDSet[entry.EventTypeNID] {
|
|
|
|
|
if allowWildcard[entry.EventTypeNID] || stateKeyNIDSet[entry.EventStateKeyNID] {
|
|
|
|
|
eventNIDs = append(eventNIDs, entry.EventNID)
|
|
|
|
|
eventNIDToVer[entry.EventNID] = roomInfo.RoomVersion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
eventIDs, _ := d.EventsTable.BulkSelectEventID(ctx, nil, eventNIDs)
|
2020-12-04 11:41:07 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
eventIDs = map[types.EventNID]string{}
|
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
events, err := d.EventJSONTable.BulkSelectEventJSON(ctx, nil, eventNIDs)
|
2020-09-07 13:38:09 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetBulkStateContent: failed to load event JSON for event nids: %w", err)
|
|
|
|
|
}
|
|
|
|
|
result := make([]tables.StrippedEvent, len(events))
|
|
|
|
|
for i := range events {
|
|
|
|
|
roomVer := eventNIDToVer[events[i].EventNID]
|
2023-04-21 18:06:29 +02:00
|
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomVer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ev, err := verImpl.NewEventFromTrustedJSONWithEventID(eventIDs[events[i].EventNID], events[i].EventJSON, false)
|
2020-09-07 13:38:09 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetBulkStateContent: failed to load event JSON for event NID %v : %w", events[i].EventNID, err)
|
|
|
|
|
}
|
|
|
|
|
result[i] = tables.StrippedEvent{
|
|
|
|
|
EventType: ev.Type(),
|
|
|
|
|
RoomID: ev.RoomID(),
|
|
|
|
|
StateKey: *ev.StateKey(),
|
2020-11-16 16:44:53 +01:00
|
|
|
|
ContentValue: tables.ExtractContentValue(ev.Headered(roomVer)),
|
2020-09-07 13:38:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 14:01:38 +01:00
|
|
|
|
// JoinedUsersSetInRooms returns a map of how many times the given users appear in the specified rooms.
|
2022-10-05 13:47:53 +02:00
|
|
|
|
func (d *Database) JoinedUsersSetInRooms(ctx context.Context, roomIDs, userIDs []string, localOnly bool) (map[string]int, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
roomNIDs, err := d.RoomsTable.BulkSelectRoomNIDs(ctx, nil, roomIDs)
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-11-11 10:52:43 +01:00
|
|
|
|
userNIDsMap, err := d.eventStateKeyNIDs(ctx, nil, userIDs)
|
2022-03-01 14:01:38 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
userNIDs := make([]types.EventStateKeyNID, 0, len(userNIDsMap))
|
|
|
|
|
nidToUserID := make(map[types.EventStateKeyNID]string, len(userNIDsMap))
|
|
|
|
|
for id, nid := range userNIDsMap {
|
|
|
|
|
userNIDs = append(userNIDs, nid)
|
|
|
|
|
nidToUserID[nid] = id
|
|
|
|
|
}
|
2022-10-05 13:47:53 +02:00
|
|
|
|
userNIDToCount, err := d.MembershipTable.SelectJoinedUsersSetForRooms(ctx, nil, roomNIDs, userNIDs, localOnly)
|
2020-09-03 19:27:02 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stateKeyNIDs := make([]types.EventStateKeyNID, len(userNIDToCount))
|
|
|
|
|
i := 0
|
|
|
|
|
for nid := range userNIDToCount {
|
|
|
|
|
stateKeyNIDs[i] = nid
|
|
|
|
|
i++
|
|
|
|
|
}
|
2022-07-05 14:50:56 +02:00
|
|
|
|
// If we didn't have any userIDs to look up, get the UserIDs for the returned userNIDToCount now
|
|
|
|
|
if len(userIDs) == 0 {
|
|
|
|
|
nidToUserID, err = d.EventStateKeys(ctx, stateKeyNIDs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-03 19:27:02 +02:00
|
|
|
|
result := make(map[string]int, len(userNIDToCount))
|
|
|
|
|
for nid, count := range userNIDToCount {
|
|
|
|
|
result[nidToUserID[nid]] = count
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 08:20:59 +01:00
|
|
|
|
// GetLeftUsers calculates users we (the server) don't share a room with anymore.
|
|
|
|
|
func (d *Database) GetLeftUsers(ctx context.Context, userIDs []string) ([]string, error) {
|
|
|
|
|
// Get the userNID for all users with a stale device list
|
|
|
|
|
stateKeyNIDMap, err := d.EventStateKeyNIDs(ctx, userIDs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userNIDs := make([]types.EventStateKeyNID, 0, len(stateKeyNIDMap))
|
|
|
|
|
userNIDtoUserID := make(map[types.EventStateKeyNID]string, len(stateKeyNIDMap))
|
|
|
|
|
// Create a map from userNID -> userID
|
|
|
|
|
for userID, nid := range stateKeyNIDMap {
|
|
|
|
|
userNIDs = append(userNIDs, nid)
|
|
|
|
|
userNIDtoUserID[nid] = userID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get all users whose membership is still join, knock or invite.
|
|
|
|
|
stillJoinedUsersNIDs, err := d.MembershipTable.SelectJoinedUsers(ctx, nil, userNIDs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove joined users from the "user with stale devices" list, which contains left AND joined users
|
|
|
|
|
for _, joinedUser := range stillJoinedUsersNIDs {
|
|
|
|
|
delete(userNIDtoUserID, joinedUser)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The users still in our userNIDtoUserID map are the users we don't share a room with anymore,
|
|
|
|
|
// and the return value we are looking for.
|
|
|
|
|
leftUsers := make([]string, 0, len(userNIDtoUserID))
|
|
|
|
|
for _, userID := range userNIDtoUserID {
|
|
|
|
|
leftUsers = append(leftUsers, userID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return leftUsers, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 17:36:45 +02:00
|
|
|
|
// GetLocalServerInRoom returns true if we think we're in a given room or false otherwise.
|
|
|
|
|
func (d *Database) GetLocalServerInRoom(ctx context.Context, roomNID types.RoomNID) (bool, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.MembershipTable.SelectLocalServerInRoom(ctx, nil, roomNID)
|
2021-07-09 17:36:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 14:06:32 +02:00
|
|
|
|
// GetServerInRoom returns true if we think a server is in a given room or false otherwise.
|
2023-04-19 16:50:33 +02:00
|
|
|
|
func (d *Database) GetServerInRoom(ctx context.Context, roomNID types.RoomNID, serverName spec.ServerName) (bool, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.MembershipTable.SelectServerInRoom(ctx, nil, roomNID, serverName)
|
2021-07-21 14:06:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 18:20:54 +02:00
|
|
|
|
// GetKnownUsers searches all users that userID knows about.
|
|
|
|
|
func (d *Database) GetKnownUsers(ctx context.Context, userID, searchString string, limit int) ([]string, error) {
|
2020-09-03 19:27:02 +02:00
|
|
|
|
stateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, nil, userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
|
return d.MembershipTable.SelectKnownUsers(ctx, nil, stateKeyNID, searchString, limit)
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetKnownRooms returns a list of all rooms we know about.
|
|
|
|
|
func (d *Database) GetKnownRooms(ctx context.Context) ([]string, error) {
|
2022-05-16 19:33:16 +02:00
|
|
|
|
return d.RoomsTable.SelectRoomIDsWithEvents(ctx, nil)
|
2020-09-03 18:20:54 +02:00
|
|
|
|
}
|
2020-09-03 19:27:02 +02:00
|
|
|
|
|
2020-11-05 11:19:23 +01:00
|
|
|
|
// ForgetRoom sets a users room to forgotten
|
|
|
|
|
func (d *Database) ForgetRoom(ctx context.Context, userID, roomID string, forget bool) error {
|
2022-02-04 11:39:34 +01:00
|
|
|
|
roomNIDs, err := d.RoomsTable.BulkSelectRoomNIDs(ctx, nil, []string{roomID})
|
2020-11-05 11:19:23 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if len(roomNIDs) > 1 {
|
|
|
|
|
return fmt.Errorf("expected one room, got %d", len(roomNIDs))
|
|
|
|
|
}
|
|
|
|
|
stateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, nil, userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-10-07 15:05:06 +02:00
|
|
|
|
return d.MembershipTable.UpdateForgetMembership(ctx, nil, roomNIDs[0], stateKeyNID, forget)
|
2020-11-05 11:19:23 +01:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 21:02:32 +01:00
|
|
|
|
// PurgeRoom removes all information about a given room from the roomserver.
|
|
|
|
|
// For large rooms this operation may take a considerable amount of time.
|
|
|
|
|
func (d *Database) PurgeRoom(ctx context.Context, roomID string) error {
|
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
|
roomNID, err := d.RoomsTable.SelectRoomNIDForUpdate(ctx, txn, roomID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return fmt.Errorf("room %s does not exist", roomID)
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("failed to lock the room: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return d.Purge.PurgeRoom(ctx, txn, roomNID, roomID)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 13:07:13 +01:00
|
|
|
|
func (d *Database) UpgradeRoom(ctx context.Context, oldRoomID, newRoomID, eventSender string) error {
|
|
|
|
|
|
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2023-02-24 09:40:20 +01:00
|
|
|
|
published, err := d.PublishedTable.SelectPublishedFromRoomID(ctx, txn, oldRoomID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to get published room: %w", err)
|
2022-11-14 13:07:13 +01:00
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
if published {
|
|
|
|
|
// un-publish old room
|
|
|
|
|
if err = d.PublishedTable.UpsertRoomPublished(ctx, txn, oldRoomID, "", "", false); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to unpublish room: %w", err)
|
|
|
|
|
}
|
|
|
|
|
// publish new room
|
|
|
|
|
if err = d.PublishedTable.UpsertRoomPublished(ctx, txn, newRoomID, "", "", true); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to publish room: %w", err)
|
|
|
|
|
}
|
2022-11-14 13:07:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Migrate any existing room aliases
|
|
|
|
|
aliases, err := d.RoomAliasesTable.SelectAliasesFromRoomID(ctx, txn, oldRoomID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to get room aliases: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, alias := range aliases {
|
|
|
|
|
if err = d.RoomAliasesTable.DeleteRoomAlias(ctx, txn, alias); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to remove room alias: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err = d.RoomAliasesTable.InsertRoomAlias(ctx, txn, alias, newRoomID, eventSender); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to set room alias: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 19:27:02 +02:00
|
|
|
|
// FIXME TODO: Remove all this - horrible dupe with roomserver/state. Can't use the original impl because of circular loops
|
|
|
|
|
// it should live in this package!
|
|
|
|
|
|
|
|
|
|
func (d *Database) loadStateAtSnapshot(
|
|
|
|
|
ctx context.Context, stateNID types.StateSnapshotNID,
|
|
|
|
|
) ([]types.StateEntry, error) {
|
|
|
|
|
stateBlockNIDLists, err := d.StateBlockNIDs(ctx, []types.StateSnapshotNID{stateNID})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
// We've asked for exactly one snapshot from the db so we should have exactly one entry in the result.
|
|
|
|
|
stateBlockNIDList := stateBlockNIDLists[0]
|
|
|
|
|
|
|
|
|
|
stateEntryLists, err := d.StateEntries(ctx, stateBlockNIDList.StateBlockNIDs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stateEntriesMap := stateEntryListMap(stateEntryLists)
|
|
|
|
|
|
|
|
|
|
// Combine all the state entries for this snapshot.
|
|
|
|
|
// The order of state block NIDs in the list tells us the order to combine them in.
|
|
|
|
|
var fullState []types.StateEntry
|
|
|
|
|
for _, stateBlockNID := range stateBlockNIDList.StateBlockNIDs {
|
|
|
|
|
entries, ok := stateEntriesMap.lookup(stateBlockNID)
|
|
|
|
|
if !ok {
|
|
|
|
|
// This should only get hit if the database is corrupt.
|
|
|
|
|
// It should be impossible for an event to reference a NID that doesn't exist
|
2021-09-08 18:31:03 +02:00
|
|
|
|
panic(fmt.Errorf("corrupt DB: Missing state block numeric ID %d", stateBlockNID))
|
2020-09-03 19:27:02 +02:00
|
|
|
|
}
|
|
|
|
|
fullState = append(fullState, entries...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stable sort so that the most recent entry for each state key stays
|
|
|
|
|
// remains later in the list than the older entries for the same state key.
|
|
|
|
|
sort.Stable(stateEntryByStateKeySorter(fullState))
|
|
|
|
|
// Unique returns the last entry and hence the most recent entry for each state key.
|
|
|
|
|
fullState = fullState[:util.Unique(stateEntryByStateKeySorter(fullState))]
|
|
|
|
|
return fullState, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type stateEntryListMap []types.StateEntryList
|
|
|
|
|
|
|
|
|
|
func (m stateEntryListMap) lookup(stateBlockNID types.StateBlockNID) (stateEntries []types.StateEntry, ok bool) {
|
|
|
|
|
list := []types.StateEntryList(m)
|
|
|
|
|
i := sort.Search(len(list), func(i int) bool {
|
|
|
|
|
return list[i].StateBlockNID >= stateBlockNID
|
|
|
|
|
})
|
|
|
|
|
if i < len(list) && list[i].StateBlockNID == stateBlockNID {
|
|
|
|
|
ok = true
|
|
|
|
|
stateEntries = list[i].StateEntries
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type stateEntryByStateKeySorter []types.StateEntry
|
|
|
|
|
|
|
|
|
|
func (s stateEntryByStateKeySorter) Len() int { return len(s) }
|
|
|
|
|
func (s stateEntryByStateKeySorter) Less(i, j int) bool {
|
|
|
|
|
return s[i].StateKeyTuple.LessThan(s[j].StateKeyTuple)
|
|
|
|
|
}
|
|
|
|
|
func (s stateEntryByStateKeySorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|