2020-05-26 15:42:42 +01:00
|
|
|
package tables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-08-01 14:11:00 +01:00
|
|
|
"errors"
|
2020-05-26 15:42:42 +01:00
|
|
|
|
2020-05-26 16:45:28 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-09-07 12:38:09 +01:00
|
|
|
"github.com/tidwall/gjson"
|
2022-08-05 10:12:41 +01:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-05-26 15:42:42 +01:00
|
|
|
)
|
|
|
|
|
2022-08-01 14:11:00 +01:00
|
|
|
var OptimisationNotSupportedError = errors.New("optimisation not supported")
|
|
|
|
|
2020-05-26 16:45:28 +01:00
|
|
|
type EventJSONPair struct {
|
2022-05-09 14:30:32 +01:00
|
|
|
EventNID types.EventNID
|
|
|
|
EventJSON []byte
|
2020-05-26 16:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type EventJSON interface {
|
2020-07-06 17:49:15 +01:00
|
|
|
// Insert the event JSON. On conflict, replace the event JSON with the new value (for redactions).
|
2020-05-26 16:45:28 +01:00
|
|
|
InsertEventJSON(ctx context.Context, tx *sql.Tx, eventNID types.EventNID, eventJSON []byte) error
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectEventJSON(ctx context.Context, tx *sql.Tx, eventNIDs []types.EventNID) ([]EventJSONPair, error)
|
2020-05-26 16:45:28 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:42:42 +01:00
|
|
|
type EventTypes interface {
|
|
|
|
InsertEventTypeNID(ctx context.Context, tx *sql.Tx, eventType string) (types.EventTypeNID, error)
|
|
|
|
SelectEventTypeNID(ctx context.Context, tx *sql.Tx, eventType string) (types.EventTypeNID, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectEventTypeNID(ctx context.Context, txn *sql.Tx, eventTypes []string) (map[string]types.EventTypeNID, error)
|
2020-05-26 15:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type EventStateKeys interface {
|
|
|
|
InsertEventStateKeyNID(ctx context.Context, txn *sql.Tx, eventStateKey string) (types.EventStateKeyNID, error)
|
|
|
|
SelectEventStateKeyNID(ctx context.Context, txn *sql.Tx, eventStateKey string) (types.EventStateKeyNID, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectEventStateKeyNID(ctx context.Context, txn *sql.Tx, eventStateKeys []string) (map[string]types.EventStateKeyNID, error)
|
|
|
|
BulkSelectEventStateKey(ctx context.Context, txn *sql.Tx, eventStateKeyNIDs []types.EventStateKeyNID) (map[types.EventStateKeyNID]string, error)
|
2020-05-26 15:42:42 +01:00
|
|
|
}
|
2020-05-26 16:45:28 +01:00
|
|
|
|
|
|
|
type Events interface {
|
2020-09-16 13:00:52 +01:00
|
|
|
InsertEvent(
|
2022-05-09 14:30:32 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, eventTypeNID types.EventTypeNID,
|
|
|
|
eventStateKeyNID types.EventStateKeyNID, eventID string,
|
2020-09-16 13:00:52 +01:00
|
|
|
referenceSHA256 []byte, authEventNIDs []types.EventNID, depth int64, isRejected bool,
|
|
|
|
) (types.EventNID, types.StateSnapshotNID, error)
|
2020-05-26 16:45:28 +01:00
|
|
|
SelectEvent(ctx context.Context, txn *sql.Tx, eventID string) (types.EventNID, types.StateSnapshotNID, error)
|
2022-11-01 15:07:17 +00:00
|
|
|
BulkSelectSnapshotsFromEventIDs(ctx context.Context, txn *sql.Tx, eventIDs []string) (map[types.StateSnapshotNID][]string, error)
|
2020-05-26 16:45:28 +01:00
|
|
|
// bulkSelectStateEventByID lookups a list of state events by event ID.
|
|
|
|
// If any of the requested events are missing from the database it returns a types.MissingEventError
|
2022-08-18 17:06:13 +01:00
|
|
|
BulkSelectStateEventByID(ctx context.Context, txn *sql.Tx, eventIDs []string, excludeRejected bool) ([]types.StateEntry, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectStateEventByNID(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID, stateKeyTuples []types.StateKeyTuple) ([]types.StateEntry, error)
|
2020-05-26 16:45:28 +01:00
|
|
|
// BulkSelectStateAtEventByID lookups the state at a list of events by event ID.
|
|
|
|
// If any of the requested events are missing from the database it returns a types.MissingEventError.
|
|
|
|
// If we do not have the state for any of the requested events it returns a types.MissingEventError.
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectStateAtEventByID(ctx context.Context, txn *sql.Tx, eventIDs []string) ([]types.StateAtEvent, error)
|
2020-08-20 16:24:33 +01:00
|
|
|
UpdateEventState(ctx context.Context, txn *sql.Tx, eventNID types.EventNID, stateNID types.StateSnapshotNID) error
|
2020-05-26 16:45:28 +01:00
|
|
|
SelectEventSentToOutput(ctx context.Context, txn *sql.Tx, eventNID types.EventNID) (sentToOutput bool, err error)
|
|
|
|
UpdateEventSentToOutput(ctx context.Context, txn *sql.Tx, eventNID types.EventNID) error
|
|
|
|
SelectEventID(ctx context.Context, txn *sql.Tx, eventNID types.EventNID) (eventID string, err error)
|
|
|
|
BulkSelectStateAtEventAndReference(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) ([]types.StateAtEventAndReference, error)
|
|
|
|
BulkSelectEventReference(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) ([]gomatrixserverlib.EventReference, error)
|
|
|
|
// BulkSelectEventID returns a map from numeric event ID to string event ID.
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectEventID(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (map[types.EventNID]string, error)
|
2020-05-26 16:45:28 +01:00
|
|
|
// BulkSelectEventNIDs returns a map from string event ID to numeric event ID.
|
|
|
|
// If an event ID is not in the database then it is omitted from the map.
|
2023-02-24 08:40:20 +00:00
|
|
|
BulkSelectEventNID(ctx context.Context, txn *sql.Tx, eventIDs []string) (map[string]types.EventMetadata, error)
|
|
|
|
BulkSelectUnsentEventNID(ctx context.Context, txn *sql.Tx, eventIDs []string) (map[string]types.EventMetadata, error)
|
2020-05-26 16:45:28 +01:00
|
|
|
SelectMaxEventDepth(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (int64, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectRoomNIDsForEventNIDs(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (roomNIDs map[types.EventNID]types.RoomNID, err error)
|
2022-08-18 10:37:47 +01:00
|
|
|
SelectEventRejected(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, eventID string) (rejected bool, err error)
|
2020-05-26 18:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Rooms interface {
|
|
|
|
InsertRoomNID(ctx context.Context, txn *sql.Tx, roomID string, roomVersion gomatrixserverlib.RoomVersion) (types.RoomNID, error)
|
|
|
|
SelectRoomNID(ctx context.Context, txn *sql.Tx, roomID string) (types.RoomNID, error)
|
2023-01-19 20:02:32 +00:00
|
|
|
SelectRoomNIDForUpdate(ctx context.Context, txn *sql.Tx, roomID string) (types.RoomNID, error)
|
2020-05-26 18:23:39 +01:00
|
|
|
SelectLatestEventNIDs(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID) ([]types.EventNID, types.StateSnapshotNID, error)
|
|
|
|
SelectLatestEventsNIDsForUpdate(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID) ([]types.EventNID, types.EventNID, types.StateSnapshotNID, error)
|
|
|
|
UpdateLatestEventNIDs(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, eventNIDs []types.EventNID, lastEventSentNID types.EventNID, stateSnapshotNID types.StateSnapshotNID) error
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectRoomVersionsForRoomNIDs(ctx context.Context, txn *sql.Tx, roomNID []types.RoomNID) (map[types.RoomNID]gomatrixserverlib.RoomVersion, error)
|
|
|
|
SelectRoomInfo(ctx context.Context, txn *sql.Tx, roomID string) (*types.RoomInfo, error)
|
2022-05-16 18:33:16 +01:00
|
|
|
SelectRoomIDsWithEvents(ctx context.Context, txn *sql.Tx) ([]string, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectRoomIDs(ctx context.Context, txn *sql.Tx, roomNIDs []types.RoomNID) ([]string, error)
|
|
|
|
BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, roomIDs []string) ([]types.RoomNID, error)
|
2020-05-26 18:23:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:36:09 +01:00
|
|
|
type StateSnapshot interface {
|
2021-04-26 13:25:57 +01:00
|
|
|
InsertState(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, stateBlockNIDs types.StateBlockNIDs) (stateNID types.StateSnapshotNID, err error)
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectStateBlockNIDs(ctx context.Context, txn *sql.Tx, stateNIDs []types.StateSnapshotNID) ([]types.StateBlockNIDList, error)
|
2022-08-01 14:11:00 +01:00
|
|
|
// BulkSelectStateForHistoryVisibility is a PostgreSQL-only optimisation for finding
|
|
|
|
// which users are in a room faster than having to load the entire room state. In the
|
|
|
|
// case of SQLite, this will return tables.OptimisationNotSupportedError.
|
|
|
|
BulkSelectStateForHistoryVisibility(ctx context.Context, txn *sql.Tx, stateSnapshotNID types.StateSnapshotNID, domain string) ([]types.EventNID, error)
|
2023-02-07 13:31:23 +00:00
|
|
|
|
|
|
|
BulkSelectMembershipForHistoryVisibility(
|
|
|
|
ctx context.Context, txn *sql.Tx, userNID types.EventStateKeyNID, roomInfo *types.RoomInfo, eventIDs ...string,
|
|
|
|
) (map[string]*gomatrixserverlib.HeaderedEvent, error)
|
2020-05-27 09:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type StateBlock interface {
|
2021-04-26 13:25:57 +01:00
|
|
|
BulkInsertStateData(ctx context.Context, txn *sql.Tx, entries types.StateEntries) (types.StateBlockNID, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
BulkSelectStateBlockEntries(ctx context.Context, txn *sql.Tx, stateBlockNIDs types.StateBlockNIDs) ([][]types.EventNID, error)
|
2021-04-26 13:25:57 +01:00
|
|
|
//BulkSelectFilteredStateBlockEntries(ctx context.Context, stateBlockNIDs []types.StateBlockNID, stateKeyTuples []types.StateKeyTuple) ([]types.StateEntryList, error)
|
2020-05-27 09:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type RoomAliases interface {
|
2020-08-20 16:24:33 +01:00
|
|
|
InsertRoomAlias(ctx context.Context, txn *sql.Tx, alias string, roomID string, creatorUserID string) (err error)
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectRoomIDFromAlias(ctx context.Context, txn *sql.Tx, alias string) (roomID string, err error)
|
|
|
|
SelectAliasesFromRoomID(ctx context.Context, txn *sql.Tx, roomID string) ([]string, error)
|
|
|
|
SelectCreatorIDFromAlias(ctx context.Context, txn *sql.Tx, alias string) (creatorID string, err error)
|
2020-08-20 16:24:33 +01:00
|
|
|
DeleteRoomAlias(ctx context.Context, txn *sql.Tx, alias string) (err error)
|
2020-05-27 09:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type PreviousEvents interface {
|
|
|
|
InsertPreviousEvent(ctx context.Context, txn *sql.Tx, previousEventID string, previousEventReferenceSHA256 []byte, eventNID types.EventNID) error
|
|
|
|
// Check if the event reference exists
|
|
|
|
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
|
|
|
SelectPreviousEventExists(ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte) error
|
|
|
|
}
|
2020-05-27 11:03:47 +01:00
|
|
|
|
|
|
|
type Invites interface {
|
|
|
|
InsertInviteEvent(ctx context.Context, txn *sql.Tx, inviteEventID string, roomNID types.RoomNID, targetUserNID, senderUserNID types.EventStateKeyNID, inviteEventJSON []byte) (bool, error)
|
|
|
|
UpdateInviteRetired(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID) ([]string, error)
|
2020-06-26 11:07:52 +01:00
|
|
|
// SelectInviteActiveForUserInRoom returns a list of sender state key NIDs and invite event IDs matching those nids.
|
2022-10-07 15:00:12 +01:00
|
|
|
SelectInviteActiveForUserInRoom(ctx context.Context, txn *sql.Tx, targetUserNID types.EventStateKeyNID, roomNID types.RoomNID) ([]types.EventStateKeyNID, []string, []byte, error)
|
2020-05-27 11:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type MembershipState int64
|
|
|
|
|
|
|
|
const (
|
|
|
|
MembershipStateLeaveOrBan MembershipState = 1
|
|
|
|
MembershipStateInvite MembershipState = 2
|
|
|
|
MembershipStateJoin MembershipState = 3
|
2021-07-22 12:26:58 +01:00
|
|
|
MembershipStateKnock MembershipState = 4
|
2020-05-27 11:03:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Membership interface {
|
|
|
|
InsertMembership(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID, localTarget bool) error
|
|
|
|
SelectMembershipForUpdate(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID) (MembershipState, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectMembershipFromRoomAndTarget(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID) (types.EventNID, MembershipState, bool, error)
|
|
|
|
SelectMembershipsFromRoom(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, localOnly bool) (eventNIDs []types.EventNID, err error)
|
|
|
|
SelectMembershipsFromRoomAndMembership(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, membership MembershipState, localOnly bool) (eventNIDs []types.EventNID, err error)
|
2022-03-17 17:05:21 +00:00
|
|
|
UpdateMembership(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID, senderUserNID types.EventStateKeyNID, membership MembershipState, eventNID types.EventNID, forgotten bool) (bool, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectRoomsWithMembership(ctx context.Context, txn *sql.Tx, userID types.EventStateKeyNID, membershipState MembershipState) ([]types.RoomNID, error)
|
2022-03-01 13:01:38 +00:00
|
|
|
// SelectJoinedUsersSetForRooms returns how many times each of the given users appears across the given rooms.
|
2022-10-05 12:47:53 +01:00
|
|
|
SelectJoinedUsersSetForRooms(ctx context.Context, txn *sql.Tx, roomNIDs []types.RoomNID, userNIDs []types.EventStateKeyNID, localOnly bool) (map[types.EventStateKeyNID]int, error)
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectKnownUsers(ctx context.Context, txn *sql.Tx, userID types.EventStateKeyNID, searchString string, limit int) ([]string, error)
|
2020-11-05 10:19:23 +00:00
|
|
|
UpdateForgetMembership(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID, forget bool) error
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectLocalServerInRoom(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID) (bool, error)
|
2023-04-19 15:50:33 +01:00
|
|
|
SelectServerInRoom(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, serverName spec.ServerName) (bool, error)
|
2022-07-22 14:44:04 +01:00
|
|
|
DeleteMembership(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID) error
|
2022-12-12 07:20:59 +00:00
|
|
|
SelectJoinedUsers(ctx context.Context, txn *sql.Tx, targetUserNIDs []types.EventStateKeyNID) ([]types.EventStateKeyNID, error)
|
2020-05-27 11:03:47 +01:00
|
|
|
}
|
2020-07-02 15:41:18 +01:00
|
|
|
|
|
|
|
type Published interface {
|
2022-10-27 13:40:35 +01:00
|
|
|
UpsertRoomPublished(ctx context.Context, txn *sql.Tx, roomID, appserviceID, networkID string, published bool) (err error)
|
2022-02-04 10:39:34 +00:00
|
|
|
SelectPublishedFromRoomID(ctx context.Context, txn *sql.Tx, roomID string) (published bool, err error)
|
2022-10-27 13:40:35 +01:00
|
|
|
SelectAllPublishedRooms(ctx context.Context, txn *sql.Tx, networkdID string, published, includeAllNetworks bool) ([]string, error)
|
2020-07-02 15:41:18 +01:00
|
|
|
}
|
2020-07-06 17:49:15 +01:00
|
|
|
|
|
|
|
type RedactionInfo struct {
|
|
|
|
// whether this redaction is validated (we have both events)
|
|
|
|
Validated bool
|
|
|
|
// the ID of the event being redacted
|
|
|
|
RedactsEventID string
|
|
|
|
// the ID of the redaction event
|
|
|
|
RedactionEventID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Redactions interface {
|
|
|
|
InsertRedaction(ctx context.Context, txn *sql.Tx, info RedactionInfo) error
|
2020-07-07 12:51:55 +01:00
|
|
|
// SelectRedactionInfoByRedactionEventID returns the redaction info for the given redaction event ID, or nil if there is no match.
|
|
|
|
SelectRedactionInfoByRedactionEventID(ctx context.Context, txn *sql.Tx, redactionEventID string) (*RedactionInfo, error)
|
|
|
|
// SelectRedactionInfoByEventBeingRedacted returns the redaction info for the given redacted event ID, or nil if there is no match.
|
|
|
|
SelectRedactionInfoByEventBeingRedacted(ctx context.Context, txn *sql.Tx, eventID string) (*RedactionInfo, error)
|
2020-07-06 17:49:15 +01:00
|
|
|
// Mark this redaction event as having been validated. This means we have both sides of the redaction and have
|
|
|
|
// successfully redacted the event JSON.
|
|
|
|
MarkRedactionValidated(ctx context.Context, txn *sql.Tx, redactionEventID string, validated bool) error
|
|
|
|
}
|
2020-09-07 12:38:09 +01:00
|
|
|
|
2023-01-19 20:02:32 +00:00
|
|
|
type Purge interface {
|
|
|
|
PurgeRoom(
|
|
|
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, roomID string,
|
|
|
|
) error
|
|
|
|
}
|
|
|
|
|
2020-09-07 12:38:09 +01:00
|
|
|
// StrippedEvent represents a stripped event for returning extracted content values.
|
|
|
|
type StrippedEvent struct {
|
|
|
|
RoomID string
|
|
|
|
EventType string
|
|
|
|
StateKey string
|
|
|
|
ContentValue string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractContentValue from the given state event. For example, given an m.room.name event with:
|
2022-08-05 10:12:41 +01:00
|
|
|
// content: { name: "Foo" }
|
2020-09-07 12:38:09 +01:00
|
|
|
// this returns "Foo".
|
|
|
|
func ExtractContentValue(ev *gomatrixserverlib.HeaderedEvent) string {
|
|
|
|
content := ev.Content()
|
|
|
|
key := ""
|
|
|
|
switch ev.Type() {
|
2023-04-19 15:50:33 +01:00
|
|
|
case spec.MRoomCreate:
|
2020-09-07 12:38:09 +01:00
|
|
|
key = "creator"
|
2023-04-19 15:50:33 +01:00
|
|
|
case spec.MRoomCanonicalAlias:
|
2020-09-07 12:38:09 +01:00
|
|
|
key = "alias"
|
2023-04-19 15:50:33 +01:00
|
|
|
case spec.MRoomHistoryVisibility:
|
2020-09-07 12:38:09 +01:00
|
|
|
key = "history_visibility"
|
2023-04-19 15:50:33 +01:00
|
|
|
case spec.MRoomJoinRules:
|
2020-09-07 12:38:09 +01:00
|
|
|
key = "join_rule"
|
2023-04-19 15:50:33 +01:00
|
|
|
case spec.MRoomMember:
|
2020-09-07 12:38:09 +01:00
|
|
|
key = "membership"
|
2023-04-19 15:50:33 +01:00
|
|
|
case spec.MRoomName:
|
2020-09-07 12:38:09 +01:00
|
|
|
key = "name"
|
|
|
|
case "m.room.avatar":
|
|
|
|
key = "url"
|
|
|
|
case "m.room.topic":
|
|
|
|
key = "topic"
|
|
|
|
case "m.room.guest_access":
|
|
|
|
key = "guest_access"
|
|
|
|
}
|
|
|
|
result := gjson.GetBytes(content, key)
|
|
|
|
if !result.Exists() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// this returns the empty string if this is not a string type
|
|
|
|
return result.Str
|
|
|
|
}
|