2020-09-02 18:13:15 +02:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2017-04-21 00:40:52 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-09-02 18:13:15 +02:00
|
|
|
package query
|
2017-03-06 15:29:39 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-13 14:37:50 +02:00
|
|
|
"context"
|
2023-06-14 16:23:46 +02:00
|
|
|
"crypto/ed25519"
|
2022-07-22 20:43:48 +02:00
|
|
|
"database/sql"
|
2020-09-03 18:20:54 +02:00
|
|
|
"errors"
|
2020-04-28 12:46:47 +02:00
|
|
|
"fmt"
|
2017-07-13 12:41:30 +02:00
|
|
|
|
2023-06-06 15:16:55 +02:00
|
|
|
//"github.com/matrix-org/dendrite/roomserver/internal"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-08-12 13:33:31 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-08-12 13:33:31 +02:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2023-04-04 19:16:53 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/synctypes"
|
2023-03-01 17:06:47 +01:00
|
|
|
|
2020-09-03 18:20:54 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2020-09-02 18:13:15 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-09-03 18:20:54 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/acls"
|
2017-03-06 15:29:39 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 14:47:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2017-03-09 16:07:18 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2020-09-02 18:13:15 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2017-03-06 15:29:39 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
2020-09-02 18:13:15 +02:00
|
|
|
type Queryer struct {
|
2022-11-15 16:05:23 +01:00
|
|
|
DB storage.Database
|
|
|
|
Cache caching.RoomServerCaches
|
2023-04-19 16:50:33 +02:00
|
|
|
IsLocalServerName func(spec.ServerName) bool
|
2022-11-15 16:05:23 +01:00
|
|
|
ServerACLs *acls.ServerACLs
|
2023-06-06 15:16:55 +02:00
|
|
|
Cfg *config.Dendrite
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
func (r *Queryer) RestrictedRoomJoinInfo(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID, localServerName spec.ServerName) (*gomatrixserverlib.RestrictedRoomJoinInfo, error) {
|
2023-06-06 15:16:55 +02:00
|
|
|
roomInfo, err := r.QueryRoomInfo(ctx, roomID)
|
|
|
|
if err != nil || roomInfo == nil || roomInfo.IsStub() {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := api.QueryServerJoinedToRoomRequest{
|
|
|
|
ServerName: localServerName,
|
|
|
|
RoomID: roomID.String(),
|
|
|
|
}
|
|
|
|
res := api.QueryServerJoinedToRoomResponse{}
|
|
|
|
if err = r.QueryServerJoinedToRoom(ctx, &req, &res); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("rsAPI.QueryServerJoinedToRoom failed")
|
|
|
|
return nil, fmt.Errorf("InternalServerError: Failed to query room: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
userJoinedToRoom, err := r.UserJoinedToRoom(ctx, types.RoomNID(roomInfo.RoomNID), senderID)
|
2023-06-06 15:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("rsAPI.UserJoinedToRoom failed")
|
|
|
|
return nil, fmt.Errorf("InternalServerError: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
locallyJoinedUsers, err := r.LocallyJoinedUsers(ctx, roomInfo.RoomVersion, types.RoomNID(roomInfo.RoomNID))
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("rsAPI.GetLocallyJoinedUsers failed")
|
|
|
|
return nil, fmt.Errorf("InternalServerError: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gomatrixserverlib.RestrictedRoomJoinInfo{
|
|
|
|
LocalServerInRoom: res.RoomExists && res.IsInRoom,
|
|
|
|
UserJoinedToRoom: userJoinedToRoom,
|
|
|
|
JoinedUsers: locallyJoinedUsers,
|
|
|
|
}, nil
|
2020-09-02 18:13:15 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
// QueryLatestEventsAndState implements api.RoomserverInternalAPI
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryLatestEventsAndState(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-03-06 15:29:39 +01:00
|
|
|
request *api.QueryLatestEventsAndStateRequest,
|
|
|
|
response *api.QueryLatestEventsAndStateResponse,
|
2017-06-02 15:32:36 +02:00
|
|
|
) error {
|
2023-06-14 16:23:46 +02:00
|
|
|
return helpers.QueryLatestEventsAndState(ctx, r.DB, r, request, response)
|
2017-05-30 18:44:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
// QueryStateAfterEvents implements api.RoomserverInternalAPI
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryStateAfterEvents(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-05-30 18:44:31 +02:00
|
|
|
request *api.QueryStateAfterEventsRequest,
|
|
|
|
response *api.QueryStateAfterEventsResponse,
|
2017-06-02 15:32:36 +02:00
|
|
|
) error {
|
2020-09-01 13:40:49 +02:00
|
|
|
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2017-05-30 18:44:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-02 11:02:48 +02:00
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
roomState := state.NewStateResolution(r.DB, info, r)
|
2017-05-30 18:44:31 +02:00
|
|
|
response.RoomExists = true
|
2020-09-02 11:02:48 +02:00
|
|
|
response.RoomVersion = info.RoomVersion
|
2017-05-30 18:44:31 +02:00
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
prevStates, err := r.DB.StateAtEventIDs(ctx, request.PrevEventIDs)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
2022-08-18 11:37:47 +02:00
|
|
|
if _, ok := err.(types.MissingEventError); ok {
|
2017-06-07 15:32:53 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-08-18 11:37:47 +02:00
|
|
|
return err
|
2017-05-30 18:44:31 +02:00
|
|
|
}
|
|
|
|
response.PrevEventsExist = true
|
|
|
|
|
2020-10-14 13:39:37 +02:00
|
|
|
var stateEntries []types.StateEntry
|
|
|
|
if len(request.StateToFetch) == 0 {
|
|
|
|
// Look up all of the current room state.
|
|
|
|
stateEntries, err = roomState.LoadCombinedStateAfterEvents(
|
|
|
|
ctx, prevStates,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Look up the current state for the requested tuples.
|
|
|
|
stateEntries, err = roomState.LoadStateAfterEventsForStringTuples(
|
|
|
|
ctx, prevStates, request.StateToFetch,
|
|
|
|
)
|
|
|
|
}
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
2022-08-18 11:37:47 +02:00
|
|
|
if _, ok := err.(types.MissingEventError); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.MissingStateError); ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-30 18:44:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
stateEvents, err := helpers.LoadStateEvents(ctx, r.DB, info, stateEntries)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-13 10:37:38 +02:00
|
|
|
if len(request.PrevEventIDs) > 1 {
|
2020-10-14 13:39:37 +02:00
|
|
|
var authEventIDs []string
|
|
|
|
for _, e := range stateEvents {
|
|
|
|
authEventIDs = append(authEventIDs, e.AuthEventIDs()...)
|
|
|
|
}
|
|
|
|
authEventIDs = util.UniqueStrings(authEventIDs)
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
authEvents, err := GetAuthChain(ctx, r.DB.EventsFromIDs, info, authEventIDs)
|
2020-10-14 13:39:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getAuthChain: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:03:16 +02:00
|
|
|
stateEvents, err = gomatrixserverlib.ResolveConflicts(
|
2023-06-14 16:23:46 +02:00
|
|
|
info.RoomVersion, gomatrixserverlib.ToPDUs(stateEvents), gomatrixserverlib.ToPDUs(authEvents), func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
return r.QueryUserIDForSender(ctx, roomID, senderID)
|
2023-06-06 22:55:18 +02:00
|
|
|
},
|
2023-04-27 17:35:19 +02:00
|
|
|
)
|
2020-10-14 13:39:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("state.ResolveConflictsAdhoc: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
for _, event := range stateEvents {
|
2023-05-02 16:03:16 +02:00
|
|
|
response.StateEvents = append(response.StateEvents, &types.HeaderedEvent{PDU: event})
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 18:44:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
// QueryEventsByID queries a list of events by event ID for one room. If no room is specified, it will try to determine
|
|
|
|
// which room to use by querying the first events roomID.
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryEventsByID(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-06-02 15:32:36 +02:00
|
|
|
request *api.QueryEventsByIDRequest,
|
|
|
|
response *api.QueryEventsByIDResponse,
|
|
|
|
) error {
|
2023-03-01 17:06:47 +01:00
|
|
|
if len(request.EventIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
// We didn't receive a room ID, we need to fetch it first before we can continue.
|
|
|
|
// This happens for e.g. ` /_matrix/federation/v1/event/{eventId}`
|
|
|
|
var roomInfo *types.RoomInfo
|
|
|
|
if request.RoomID == "" {
|
|
|
|
var eventNIDs map[string]types.EventMetadata
|
|
|
|
eventNIDs, err = r.DB.EventNIDs(ctx, []string{request.EventIDs[0]})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(eventNIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
roomInfo, err = r.DB.RoomInfoByNID(ctx, eventNIDs[request.EventIDs[0]].RoomNID)
|
|
|
|
} else {
|
|
|
|
roomInfo, err = r.DB.RoomInfo(ctx, request.RoomID)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomInfo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
events, err := r.DB.EventsFromIDs(ctx, roomInfo, request.EventIDs)
|
2017-06-02 15:32:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
for _, event := range events {
|
2023-05-02 16:03:16 +02:00
|
|
|
response.Events = append(response.Events, &types.HeaderedEvent{PDU: event.PDU})
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-06-02 15:32:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
// QueryMembershipForSenderID implements api.RoomserverInternalAPI
|
|
|
|
func (r *Queryer) QueryMembershipForSenderID(
|
2018-07-11 12:10:37 +02:00
|
|
|
ctx context.Context,
|
2023-06-12 13:19:25 +02:00
|
|
|
roomID spec.RoomID,
|
|
|
|
senderID spec.SenderID,
|
2018-07-11 12:10:37 +02:00
|
|
|
response *api.QueryMembershipForUserResponse,
|
|
|
|
) error {
|
2023-06-12 13:19:25 +02:00
|
|
|
info, err := r.DB.RoomInfo(ctx, roomID.String())
|
2018-07-11 12:10:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-07 13:38:09 +02:00
|
|
|
if info == nil {
|
2022-05-11 12:29:23 +02:00
|
|
|
response.RoomExists = false
|
|
|
|
return nil
|
2020-09-07 13:38:09 +02:00
|
|
|
}
|
2022-05-11 12:29:23 +02:00
|
|
|
response.RoomExists = true
|
2018-07-11 12:10:37 +02:00
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
membershipEventNID, stillInRoom, isRoomforgotten, err := r.DB.GetMembership(ctx, info.RoomNID, senderID)
|
2018-07-11 12:10:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:19:23 +01:00
|
|
|
response.IsRoomForgotten = isRoomforgotten
|
|
|
|
|
2018-07-11 12:10:37 +02:00
|
|
|
if membershipEventNID == 0 {
|
|
|
|
response.HasBeenInRoom = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response.IsInRoom = stillInRoom
|
2020-07-28 11:09:10 +02:00
|
|
|
response.HasBeenInRoom = true
|
2020-06-24 19:19:54 +02:00
|
|
|
|
2023-05-08 21:25:44 +02:00
|
|
|
evs, err := r.DB.Events(ctx, info.RoomVersion, []types.EventNID{membershipEventNID})
|
2018-07-11 12:10:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-24 19:19:54 +02:00
|
|
|
if len(evs) != 1 {
|
|
|
|
return fmt.Errorf("failed to load membership event for event NID %d", membershipEventNID)
|
|
|
|
}
|
2018-07-11 12:10:37 +02:00
|
|
|
|
2020-06-24 19:19:54 +02:00
|
|
|
response.EventID = evs[0].EventID()
|
|
|
|
response.Membership, err = evs[0].Membership()
|
|
|
|
return err
|
2018-07-11 12:10:37 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
// QueryMembershipForUser implements api.RoomserverInternalAPI
|
|
|
|
func (r *Queryer) QueryMembershipForUser(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryMembershipForUserRequest,
|
|
|
|
response *api.QueryMembershipForUserResponse,
|
|
|
|
) error {
|
2023-06-14 16:23:46 +02:00
|
|
|
roomID, err := spec.NewRoomID(request.RoomID)
|
2023-06-12 13:19:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-14 16:23:46 +02:00
|
|
|
senderID, err := r.QuerySenderIDForUser(ctx, *roomID, request.UserID)
|
2023-06-12 13:19:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-14 16:23:46 +02:00
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
return r.QueryMembershipForSenderID(ctx, *roomID, senderID, response)
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:52:09 +02:00
|
|
|
// QueryMembershipAtEvent returns the known memberships at a given event.
|
|
|
|
// If the state before an event is not known, an empty list will be returned
|
|
|
|
// for that event instead.
|
2022-08-11 18:23:35 +02:00
|
|
|
func (r *Queryer) QueryMembershipAtEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryMembershipAtEventRequest,
|
|
|
|
response *api.QueryMembershipAtEventResponse,
|
|
|
|
) error {
|
2023-04-27 13:54:20 +02:00
|
|
|
response.Membership = make(map[string]*types.HeaderedEvent)
|
2023-02-07 14:31:23 +01:00
|
|
|
|
2022-08-11 18:23:35 +02:00
|
|
|
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get roomInfo: %w", err)
|
|
|
|
}
|
|
|
|
if info == nil {
|
|
|
|
return fmt.Errorf("no roomInfo found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the users stateKeyNID
|
|
|
|
stateKeyNIDs, err := r.DB.EventStateKeyNIDs(ctx, []string{request.UserID})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get stateKeyNIDs for %s: %w", request.UserID, err)
|
|
|
|
}
|
|
|
|
if _, ok := stateKeyNIDs[request.UserID]; !ok {
|
|
|
|
return fmt.Errorf("requested stateKeyNID for %s was not found", request.UserID)
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:31:23 +01:00
|
|
|
response.Membership, err = r.DB.GetMembershipForHistoryVisibility(ctx, stateKeyNIDs[request.UserID], info, request.EventIDs...)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case tables.OptimisationNotSupportedError: // fallthrough, slow way of getting the membership events for each event
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
response.Membership = make(map[string]*types.HeaderedEvent)
|
2023-06-14 16:23:46 +02:00
|
|
|
stateEntries, err := helpers.MembershipAtEvent(ctx, r.DB, nil, request.EventIDs, stateKeyNIDs[request.UserID], r)
|
2022-08-11 18:23:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get state before event: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-01 16:07:17 +01:00
|
|
|
// If we only have one or less state entries, we can short circuit the below
|
|
|
|
// loop and avoid hitting the database
|
|
|
|
allStateEventNIDs := make(map[types.EventNID]types.StateEntry)
|
|
|
|
for _, eventID := range request.EventIDs {
|
|
|
|
stateEntry := stateEntries[eventID]
|
|
|
|
for _, s := range stateEntry {
|
|
|
|
allStateEventNIDs[s.EventNID] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var canShortCircuit bool
|
|
|
|
if len(allStateEventNIDs) <= 1 {
|
|
|
|
canShortCircuit = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var memberships []types.Event
|
2022-08-11 18:23:35 +02:00
|
|
|
for _, eventID := range request.EventIDs {
|
2022-09-13 13:52:09 +02:00
|
|
|
stateEntry, ok := stateEntries[eventID]
|
2022-11-01 16:07:17 +01:00
|
|
|
if !ok || len(stateEntry) == 0 {
|
2023-02-07 14:31:23 +01:00
|
|
|
response.Membership[eventID] = nil
|
2022-09-13 13:52:09 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-11-01 16:07:17 +01:00
|
|
|
|
|
|
|
// If we can short circuit, e.g. we only have 0 or 1 membership events, we only get the memberships
|
|
|
|
// once. If we have more than one membership event, we need to get the state for each state entry.
|
|
|
|
if canShortCircuit {
|
|
|
|
if len(memberships) == 0 {
|
2023-03-01 17:06:47 +01:00
|
|
|
memberships, err = helpers.GetMembershipsAtState(ctx, r.DB, info, stateEntry, false)
|
2022-11-01 16:07:17 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-01 17:06:47 +01:00
|
|
|
memberships, err = helpers.GetMembershipsAtState(ctx, r.DB, info, stateEntry, false)
|
2022-11-01 16:07:17 +01:00
|
|
|
}
|
2022-08-11 18:23:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get memberships at state: %w", err)
|
|
|
|
}
|
2022-11-01 16:07:17 +01:00
|
|
|
|
2023-02-07 14:31:23 +01:00
|
|
|
// Iterate over all membership events we got. Given we only query the membership for
|
|
|
|
// one user and assuming this user only ever has one membership event associated to
|
|
|
|
// a given event, overwrite any other existing membership events.
|
2022-08-11 18:23:35 +02:00
|
|
|
for i := range memberships {
|
|
|
|
ev := memberships[i]
|
2023-04-19 16:50:33 +02:00
|
|
|
if ev.Type() == spec.MRoomMember && ev.StateKeyEquals(request.UserID) {
|
2023-05-02 16:03:16 +02:00
|
|
|
response.Membership[eventID] = &types.HeaderedEvent{PDU: ev.PDU}
|
2022-08-11 18:23:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
// QueryMembershipsForRoom implements api.RoomserverInternalAPI
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryMembershipsForRoom(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-08-21 17:34:26 +02:00
|
|
|
request *api.QueryMembershipsForRoomRequest,
|
|
|
|
response *api.QueryMembershipsForRoomResponse,
|
|
|
|
) error {
|
2020-09-01 13:40:49 +02:00
|
|
|
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
2017-08-21 17:34:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-17 14:58:04 +01:00
|
|
|
if info == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-21 17:34:26 +02:00
|
|
|
|
2021-03-03 17:27:44 +01:00
|
|
|
// If no sender is specified then we will just return the entire
|
|
|
|
// set of memberships for the room, regardless of whether a specific
|
|
|
|
// user is allowed to see them or not.
|
2023-06-12 13:19:25 +02:00
|
|
|
if request.SenderID == "" {
|
2021-03-03 17:27:44 +01:00
|
|
|
var events []types.Event
|
|
|
|
var eventNIDs []types.EventNID
|
2022-04-06 11:43:54 +02:00
|
|
|
eventNIDs, err = r.DB.GetMembershipEventNIDsForRoom(ctx, info.RoomNID, request.JoinedOnly, request.LocalOnly)
|
2021-03-03 17:27:44 +01:00
|
|
|
if err != nil {
|
2022-07-22 20:43:48 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-03 17:27:44 +01:00
|
|
|
return fmt.Errorf("r.DB.GetMembershipEventNIDsForRoom: %w", err)
|
|
|
|
}
|
2023-05-08 21:25:44 +02:00
|
|
|
events, err = r.DB.Events(ctx, info.RoomVersion, eventNIDs)
|
2021-03-03 17:27:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.Events: %w", err)
|
|
|
|
}
|
|
|
|
for _, event := range events {
|
2023-06-14 16:23:46 +02:00
|
|
|
clientEvent := synctypes.ToClientEventDefault(func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-12 13:19:25 +02:00
|
|
|
return r.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
}, event)
|
2021-03-03 17:27:44 +01:00
|
|
|
response.JoinEvents = append(response.JoinEvents, clientEvent)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
membershipEventNID, stillInRoom, isRoomforgotten, err := r.DB.GetMembership(ctx, info.RoomNID, request.SenderID)
|
2017-08-21 17:34:26 +02:00
|
|
|
if err != nil {
|
2018-07-11 14:16:38 +02:00
|
|
|
return err
|
2017-08-21 17:34:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 11:19:23 +01:00
|
|
|
response.IsRoomForgotten = isRoomforgotten
|
|
|
|
|
2017-08-24 17:00:14 +02:00
|
|
|
if membershipEventNID == 0 {
|
2017-08-21 17:34:26 +02:00
|
|
|
response.HasBeenInRoom = false
|
|
|
|
response.JoinEvents = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response.HasBeenInRoom = true
|
2023-04-04 19:16:53 +02:00
|
|
|
response.JoinEvents = []synctypes.ClientEvent{}
|
2017-08-24 17:00:14 +02:00
|
|
|
|
|
|
|
var events []types.Event
|
2020-04-29 19:41:45 +02:00
|
|
|
var stateEntries []types.StateEntry
|
2017-08-24 17:00:14 +02:00
|
|
|
if stillInRoom {
|
|
|
|
var eventNIDs []types.EventNID
|
2020-09-01 13:40:49 +02:00
|
|
|
eventNIDs, err = r.DB.GetMembershipEventNIDsForRoom(ctx, info.RoomNID, request.JoinedOnly, false)
|
2017-08-24 17:00:14 +02:00
|
|
|
if err != nil {
|
2022-07-22 20:43:48 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-24 17:00:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:25:44 +02:00
|
|
|
events, err = r.DB.Events(ctx, info.RoomVersion, eventNIDs)
|
2017-08-24 17:00:14 +02:00
|
|
|
} else {
|
2023-06-14 16:23:46 +02:00
|
|
|
stateEntries, err = helpers.StateBeforeEvent(ctx, r.DB, info, membershipEventNID, r)
|
2020-04-29 19:41:45 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("membership_event_nid", membershipEventNID).WithError(err).Error("failed to load state before event")
|
|
|
|
return err
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
events, err = helpers.GetMembershipsAtState(ctx, r.DB, info, stateEntries, request.JoinedOnly)
|
2017-08-24 17:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:34:26 +02:00
|
|
|
for _, event := range events {
|
2023-06-14 16:23:46 +02:00
|
|
|
clientEvent := synctypes.ToClientEventDefault(func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-12 13:19:25 +02:00
|
|
|
return r.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
}, event)
|
2017-08-21 17:34:26 +02:00
|
|
|
response.JoinEvents = append(response.JoinEvents, clientEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:18:13 +02:00
|
|
|
// QueryServerJoinedToRoom implements api.RoomserverInternalAPI
|
|
|
|
func (r *Queryer) QueryServerJoinedToRoom(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryServerJoinedToRoomRequest,
|
|
|
|
response *api.QueryServerJoinedToRoomResponse,
|
|
|
|
) error {
|
|
|
|
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.RoomInfo: %w", err)
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2020-09-24 17:18:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
|
|
|
|
2022-11-15 16:05:23 +01:00
|
|
|
if r.IsLocalServerName(request.ServerName) || request.ServerName == "" {
|
2021-07-21 14:06:32 +02:00
|
|
|
response.IsInRoom, err = r.DB.GetLocalServerInRoom(ctx, info.RoomNID)
|
2021-07-09 17:36:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.GetLocalServerInRoom: %w", err)
|
|
|
|
}
|
2021-07-21 14:06:32 +02:00
|
|
|
} else {
|
|
|
|
response.IsInRoom, err = r.DB.GetServerInRoom(ctx, info.RoomNID, request.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.GetServerInRoom: %w", err)
|
2020-09-24 17:18:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
// QueryServerAllowedToSeeEvent implements api.RoomserverInternalAPI
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryServerAllowedToSeeEvent(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName spec.ServerName,
|
2023-03-01 17:06:47 +01:00
|
|
|
eventID string,
|
2023-06-12 13:19:25 +02:00
|
|
|
roomID string,
|
2023-03-01 17:06:47 +01:00
|
|
|
) (allowed bool, err error) {
|
|
|
|
events, err := r.DB.EventNIDs(ctx, []string{eventID})
|
2020-03-24 13:20:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
2023-03-01 17:06:47 +01:00
|
|
|
return allowed, nil
|
2020-03-24 13:20:10 +01:00
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
info, err := r.DB.RoomInfoByNID(ctx, events[eventID].RoomNID)
|
2020-09-02 11:02:48 +02:00
|
|
|
if err != nil {
|
2023-03-01 17:06:47 +01:00
|
|
|
return allowed, err
|
2020-09-02 11:02:48 +02:00
|
|
|
}
|
2022-08-02 13:27:15 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2023-03-01 17:06:47 +01:00
|
|
|
return allowed, nil
|
2020-09-02 11:02:48 +02:00
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
var isInRoom bool
|
|
|
|
if r.IsLocalServerName(serverName) || serverName == "" {
|
|
|
|
isInRoom, err = r.DB.GetLocalServerInRoom(ctx, info.RoomNID)
|
|
|
|
if err != nil {
|
|
|
|
return allowed, fmt.Errorf("r.DB.GetLocalServerInRoom: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isInRoom, err = r.DB.GetServerInRoom(ctx, info.RoomNID, serverName)
|
|
|
|
if err != nil {
|
|
|
|
return allowed, fmt.Errorf("r.DB.GetServerInRoom: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return helpers.CheckServerAllowedToSeeEvent(
|
2023-06-14 16:23:46 +02:00
|
|
|
ctx, r.DB, info, roomID, eventID, serverName, isInRoom, r,
|
2018-06-26 12:25:49 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
// QueryMissingEvents implements api.RoomserverInternalAPI
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryMissingEvents(
|
2018-06-26 12:25:49 +02:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryMissingEventsRequest,
|
|
|
|
response *api.QueryMissingEventsResponse,
|
|
|
|
) error {
|
|
|
|
var front []string
|
2019-01-14 11:20:19 +01:00
|
|
|
eventsToFilter := make(map[string]bool, len(request.LatestEvents))
|
2018-06-26 12:25:49 +02:00
|
|
|
visited := make(map[string]bool, request.Limit) // request.Limit acts as a hint to size.
|
|
|
|
for _, id := range request.EarliestEvents {
|
|
|
|
visited[id] = true
|
|
|
|
}
|
2017-09-06 13:38:22 +02:00
|
|
|
|
2018-06-26 12:25:49 +02:00
|
|
|
for _, id := range request.LatestEvents {
|
|
|
|
if !visited[id] {
|
|
|
|
front = append(front, id)
|
2019-01-14 11:20:19 +01:00
|
|
|
eventsToFilter[id] = true
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
2018-06-26 12:25:49 +02:00
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
if len(front) == 0 {
|
|
|
|
return nil // no events to query, give up.
|
|
|
|
}
|
|
|
|
events, err := r.DB.EventNIDs(ctx, []string{front[0]})
|
2020-09-02 11:02:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return nil // we are missing the events being asked to search from, give up.
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
info, err := r.DB.RoomInfoByNID(ctx, events[front[0]].RoomNID)
|
2020-09-02 11:02:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2023-03-01 17:06:47 +01:00
|
|
|
return fmt.Errorf("missing RoomInfo for room %d", events[front[0]].RoomNID)
|
2020-09-02 11:02:48 +02:00
|
|
|
}
|
2017-09-06 13:38:22 +02:00
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
resultNIDs, redactEventIDs, err := helpers.ScanEventTree(ctx, r.DB, info, front, visited, request.Limit, request.ServerName, r)
|
2018-11-07 12:38:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
loadedEvents, err := helpers.LoadEvents(ctx, r.DB, info, resultNIDs)
|
2019-01-14 11:20:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
response.Events = make([]*types.HeaderedEvent, 0, len(loadedEvents)-len(eventsToFilter))
|
2019-01-14 11:20:19 +01:00
|
|
|
for _, event := range loadedEvents {
|
|
|
|
if !eventsToFilter[event.EventID()] {
|
2022-10-11 16:04:02 +02:00
|
|
|
if _, ok := redactEventIDs[event.EventID()]; ok {
|
|
|
|
event.Redact()
|
|
|
|
}
|
2023-05-02 16:03:16 +02:00
|
|
|
response.Events = append(response.Events, &types.HeaderedEvent{PDU: event})
|
2019-01-14 11:20:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:38:01 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
// QueryStateAndAuthChain implements api.RoomserverInternalAPI
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryStateAndAuthChain(
|
2017-11-27 11:20:00 +01:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryStateAndAuthChainRequest,
|
|
|
|
response *api.QueryStateAndAuthChainResponse,
|
|
|
|
) error {
|
2020-09-01 13:40:49 +02:00
|
|
|
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
2017-11-27 11:20:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2017-11-27 11:20:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
2020-09-01 13:40:49 +02:00
|
|
|
response.RoomVersion = info.RoomVersion
|
2020-03-17 19:00:10 +01:00
|
|
|
|
2022-01-06 18:13:34 +01:00
|
|
|
// handle this entirely separately to the other case so we don't have to pull out
|
|
|
|
// the entire current state of the room
|
|
|
|
// TODO: this probably means it should be a different query operation...
|
|
|
|
if request.OnlyFetchAuthChain {
|
2023-05-02 16:03:16 +02:00
|
|
|
var authEvents []gomatrixserverlib.PDU
|
2023-03-01 17:06:47 +01:00
|
|
|
authEvents, err = GetAuthChain(ctx, r.DB.EventsFromIDs, info, request.AuthEventIDs)
|
2022-01-06 18:13:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, event := range authEvents {
|
2023-05-02 16:03:16 +02:00
|
|
|
response.AuthChainEvents = append(response.AuthChainEvents, &types.HeaderedEvent{PDU: event})
|
2022-01-06 18:13:34 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:03:16 +02:00
|
|
|
var stateEvents []gomatrixserverlib.PDU
|
2022-08-25 10:51:36 +02:00
|
|
|
stateEvents, rejected, stateMissing, err := r.loadStateAtEventIDs(ctx, info, request.PrevEventIDs)
|
2017-11-27 11:20:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-25 10:51:36 +02:00
|
|
|
response.StateKnown = !stateMissing
|
2022-04-25 19:05:01 +02:00
|
|
|
response.IsRejected = rejected
|
2020-03-17 18:18:48 +01:00
|
|
|
response.PrevEventsExist = true
|
2017-11-27 11:20:00 +01:00
|
|
|
|
2020-03-17 18:18:48 +01:00
|
|
|
// add the auth event IDs for the current state events too
|
|
|
|
var authEventIDs []string
|
|
|
|
authEventIDs = append(authEventIDs, request.AuthEventIDs...)
|
|
|
|
for _, se := range stateEvents {
|
|
|
|
authEventIDs = append(authEventIDs, se.AuthEventIDs()...)
|
2017-11-27 11:20:00 +01:00
|
|
|
}
|
2020-03-17 18:18:48 +01:00
|
|
|
authEventIDs = util.UniqueStrings(authEventIDs) // de-dupe
|
2017-11-27 11:20:00 +01:00
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
authEvents, err := GetAuthChain(ctx, r.DB.EventsFromIDs, info, authEventIDs)
|
2020-03-16 18:29:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Federation for v3/v4 rooms (#954)
* Update gomatrixserverlib
* Default to room version 4
* Update gomatrixserverlib
* Limit prev_events and auth_events
* Fix auth_events, prev_events
* Fix linter issues
* Update gomatrixserverlib
* Fix getState
* Update sytest-whitelist
* Squashed commit of the following:
commit 067b87506357c996fd6ddb11271db9469ad4ce80
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Fri Apr 3 14:29:06 2020 +0100
Invites v2 endpoint (#952)
* Start converting v1 invite endpoint to v2
* Update gomatrixserverlib
* Early federationsender code for sending invites
* Sending invites sorta happens now
* Populate invite request with stripped state
* Remodel a bit, don't reflect received invites
* Handle invite_room_state
* Handle room versions a bit better
* Update gomatrixserverlib
* Tweak order in destinationQueue.next
* Revert check in processMessage
* Tweak federation sender destination queue code a bit
* Add comments
commit 955244c09298d0e6c870377dad3af2ffa1f5e578
Author: Ben B <benne@klimlive.de>
Date: Fri Apr 3 12:40:50 2020 +0200
use custom http client instead of the http DefaultClient (#823)
This commit replaces the default client from the http lib with a custom one.
The previously used default client doesn't come with a timeout. This could cause
unwanted locks.
That solution chosen here creates a http client in the base component dendrite
with a constant timeout of 30 seconds. If it should be necessary to overwrite
this, we could include the timeout in the dendrite configuration.
Here it would be a good idea to extend the type "Address" by a timeout and
create an http client for each service.
Closes #820
Signed-off-by: Benedikt Bongartz <benne@klimlive.de>
Co-authored-by: Kegsay <kegan@matrix.org>
* Update sytest-whitelist, sytest-blacklist
* Update go.mod/go.sum
* Add some error wrapping for debug
* Add a NOTSPEC to common/events.go
* Perform state resolution at send_join
* Set default room version to v2 again
* Tweak GetCapabilities
* Add comments to ResolveConflictsAdhoc
* Update sytest-blacklist
* go mod tidy
* Update sytest-whitelist, sytest-blacklist
* Update versions
* Updates from review comments
* Update sytest-blacklist, sytest-whitelist
* Check room versions compatible at make_join, add some comments, update gomatrixserverlib, other tweaks
* Set default room version back to v2
* Update gomatrixserverlib, sytest-whitelist
2020-04-09 16:46:06 +02:00
|
|
|
if request.ResolveState {
|
2023-05-02 16:03:16 +02:00
|
|
|
stateEvents, err = gomatrixserverlib.ResolveConflicts(
|
2023-06-14 16:23:46 +02:00
|
|
|
info.RoomVersion, gomatrixserverlib.ToPDUs(stateEvents), gomatrixserverlib.ToPDUs(authEvents), func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
return r.QueryUserIDForSender(ctx, roomID, senderID)
|
2023-06-06 22:55:18 +02:00
|
|
|
},
|
2023-04-27 17:35:19 +02:00
|
|
|
)
|
2023-05-02 16:03:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
Federation for v3/v4 rooms (#954)
* Update gomatrixserverlib
* Default to room version 4
* Update gomatrixserverlib
* Limit prev_events and auth_events
* Fix auth_events, prev_events
* Fix linter issues
* Update gomatrixserverlib
* Fix getState
* Update sytest-whitelist
* Squashed commit of the following:
commit 067b87506357c996fd6ddb11271db9469ad4ce80
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Fri Apr 3 14:29:06 2020 +0100
Invites v2 endpoint (#952)
* Start converting v1 invite endpoint to v2
* Update gomatrixserverlib
* Early federationsender code for sending invites
* Sending invites sorta happens now
* Populate invite request with stripped state
* Remodel a bit, don't reflect received invites
* Handle invite_room_state
* Handle room versions a bit better
* Update gomatrixserverlib
* Tweak order in destinationQueue.next
* Revert check in processMessage
* Tweak federation sender destination queue code a bit
* Add comments
commit 955244c09298d0e6c870377dad3af2ffa1f5e578
Author: Ben B <benne@klimlive.de>
Date: Fri Apr 3 12:40:50 2020 +0200
use custom http client instead of the http DefaultClient (#823)
This commit replaces the default client from the http lib with a custom one.
The previously used default client doesn't come with a timeout. This could cause
unwanted locks.
That solution chosen here creates a http client in the base component dendrite
with a constant timeout of 30 seconds. If it should be necessary to overwrite
this, we could include the timeout in the dendrite configuration.
Here it would be a good idea to extend the type "Address" by a timeout and
create an http client for each service.
Closes #820
Signed-off-by: Benedikt Bongartz <benne@klimlive.de>
Co-authored-by: Kegsay <kegan@matrix.org>
* Update sytest-whitelist, sytest-blacklist
* Update go.mod/go.sum
* Add some error wrapping for debug
* Add a NOTSPEC to common/events.go
* Perform state resolution at send_join
* Set default room version to v2 again
* Tweak GetCapabilities
* Add comments to ResolveConflictsAdhoc
* Update sytest-blacklist
* go mod tidy
* Update sytest-whitelist, sytest-blacklist
* Update versions
* Updates from review comments
* Update sytest-blacklist, sytest-whitelist
* Check room versions compatible at make_join, add some comments, update gomatrixserverlib, other tweaks
* Set default room version back to v2
* Update gomatrixserverlib, sytest-whitelist
2020-04-09 16:46:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
for _, event := range stateEvents {
|
2023-05-02 16:03:16 +02:00
|
|
|
response.StateEvents = append(response.StateEvents, &types.HeaderedEvent{PDU: event})
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range authEvents {
|
2023-05-02 16:03:16 +02:00
|
|
|
response.AuthChainEvents = append(response.AuthChainEvents, &types.HeaderedEvent{PDU: event})
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:51:36 +02:00
|
|
|
// first bool: is rejected, second bool: state missing
|
2023-05-02 16:03:16 +02:00
|
|
|
func (r *Queryer) loadStateAtEventIDs(ctx context.Context, roomInfo *types.RoomInfo, eventIDs []string) ([]gomatrixserverlib.PDU, bool, bool, error) {
|
2023-06-14 16:23:46 +02:00
|
|
|
roomState := state.NewStateResolution(r.DB, roomInfo, r)
|
2020-03-17 18:18:48 +01:00
|
|
|
prevStates, err := r.DB.StateAtEventIDs(ctx, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case types.MissingEventError:
|
2022-08-25 10:51:36 +02:00
|
|
|
return nil, false, true, nil
|
|
|
|
case types.MissingStateError:
|
|
|
|
return nil, false, true, nil
|
2020-03-17 18:18:48 +01:00
|
|
|
default:
|
2022-08-25 10:51:36 +02:00
|
|
|
return nil, false, false, err
|
2022-04-25 19:05:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Currently only used on /state and /state_ids
|
|
|
|
rejected := false
|
|
|
|
for i := range prevStates {
|
|
|
|
if prevStates[i].IsRejected {
|
|
|
|
rejected = true
|
|
|
|
break
|
2020-03-17 18:18:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the currrent state for the requested tuples.
|
|
|
|
stateEntries, err := roomState.LoadCombinedStateAfterEvents(
|
|
|
|
ctx, prevStates,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-08-25 10:51:36 +02:00
|
|
|
return nil, rejected, false, err
|
2020-03-17 18:18:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
events, err := helpers.LoadStateEvents(ctx, r.DB, roomInfo, stateEntries)
|
2022-08-25 10:51:36 +02:00
|
|
|
return events, rejected, false, err
|
2020-03-17 18:18:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:06:47 +01:00
|
|
|
type eventsFromIDs func(context.Context, *types.RoomInfo, []string) ([]types.Event, error)
|
2020-04-24 11:38:58 +02:00
|
|
|
|
2021-01-22 15:55:08 +01:00
|
|
|
// GetAuthChain fetches the auth chain for the given auth events. An auth chain
|
2020-01-10 13:11:44 +01:00
|
|
|
// is the list of all events that are referenced in the auth_events section, and
|
|
|
|
// all their auth_events, recursively. The returned set of events contain the
|
|
|
|
// given events. Will *not* error if we don't have all auth events.
|
2021-01-22 15:55:08 +01:00
|
|
|
func GetAuthChain(
|
2023-03-01 17:06:47 +01:00
|
|
|
ctx context.Context, fn eventsFromIDs, roomInfo *types.RoomInfo, authEventIDs []string,
|
2023-05-02 16:03:16 +02:00
|
|
|
) ([]gomatrixserverlib.PDU, error) {
|
2020-01-10 13:11:44 +01:00
|
|
|
// List of event IDs to fetch. On each pass, these events will be requested
|
|
|
|
// from the database and the `eventsToFetch` will be updated with any new
|
|
|
|
// events that we have learned about and need to find. When `eventsToFetch`
|
|
|
|
// is eventually empty, we should have reached the end of the chain.
|
2017-11-27 11:20:00 +01:00
|
|
|
eventsToFetch := authEventIDs
|
2023-05-02 16:03:16 +02:00
|
|
|
authEventsMap := make(map[string]gomatrixserverlib.PDU)
|
2017-11-27 11:20:00 +01:00
|
|
|
|
|
|
|
for len(eventsToFetch) > 0 {
|
2020-01-10 13:11:44 +01:00
|
|
|
// Try to retrieve the events from the database.
|
2023-03-01 17:06:47 +01:00
|
|
|
events, err := fn(ctx, roomInfo, eventsToFetch)
|
2017-11-27 11:20:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:11:44 +01:00
|
|
|
// We've now fetched these events so clear out `eventsToFetch`. Soon we may
|
|
|
|
// add newly discovered events to this for the next pass.
|
2017-11-27 11:20:00 +01:00
|
|
|
eventsToFetch = eventsToFetch[:0]
|
2020-01-10 13:11:44 +01:00
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
for _, event := range events {
|
2020-01-10 13:11:44 +01:00
|
|
|
// Store the event in the event map - this prevents us from requesting it
|
|
|
|
// from the database again.
|
2023-05-02 16:03:16 +02:00
|
|
|
authEventsMap[event.EventID()] = event.PDU
|
2020-01-10 13:11:44 +01:00
|
|
|
|
|
|
|
// Extract all of the auth events from the newly obtained event. If we
|
|
|
|
// don't already have a record of the event, record it in the list of
|
|
|
|
// events we want to request for the next pass.
|
2023-05-02 16:03:16 +02:00
|
|
|
for _, authEventID := range event.AuthEventIDs() {
|
|
|
|
if _, ok := authEventsMap[authEventID]; !ok {
|
|
|
|
eventsToFetch = append(eventsToFetch, authEventID)
|
2017-11-27 11:20:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:11:44 +01:00
|
|
|
// We've now retrieved all of the events we can. Flatten them down into an
|
|
|
|
// array and return them.
|
2023-05-02 16:03:16 +02:00
|
|
|
var authEvents []gomatrixserverlib.PDU
|
2020-01-10 13:11:44 +01:00
|
|
|
for _, event := range authEventsMap {
|
|
|
|
authEvents = append(authEvents, event)
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
return authEvents, nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 21:42:46 +02:00
|
|
|
// QueryRoomVersionForRoom implements api.RoomserverInternalAPI
|
2023-04-27 08:07:13 +02:00
|
|
|
func (r *Queryer) QueryRoomVersionForRoom(ctx context.Context, roomID string) (gomatrixserverlib.RoomVersion, error) {
|
|
|
|
if roomVersion, ok := r.Cache.GetRoomVersion(roomID); ok {
|
|
|
|
return roomVersion, nil
|
2020-04-22 14:00:05 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 08:07:13 +02:00
|
|
|
info, err := r.DB.RoomInfo(ctx, roomID)
|
2020-03-27 17:28:22 +01:00
|
|
|
if err != nil {
|
2023-04-27 08:07:13 +02:00
|
|
|
return "", err
|
2020-03-27 17:28:22 +01:00
|
|
|
}
|
2020-09-02 11:02:48 +02:00
|
|
|
if info == nil {
|
2023-04-27 08:07:13 +02:00
|
|
|
return "", fmt.Errorf("QueryRoomVersionForRoom: missing room info for room %s", roomID)
|
2020-09-02 11:02:48 +02:00
|
|
|
}
|
2023-04-27 08:07:13 +02:00
|
|
|
r.Cache.StoreRoomVersion(roomID, info.RoomVersion)
|
|
|
|
return info.RoomVersion, nil
|
2020-09-02 11:02:48 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 18:13:15 +02:00
|
|
|
func (r *Queryer) QueryPublishedRooms(
|
2020-07-02 16:41:18 +02:00
|
|
|
ctx context.Context,
|
|
|
|
req *api.QueryPublishedRoomsRequest,
|
|
|
|
res *api.QueryPublishedRoomsResponse,
|
|
|
|
) error {
|
2022-03-09 11:42:24 +01:00
|
|
|
if req.RoomID != "" {
|
|
|
|
visible, err := r.DB.GetPublishedRoom(ctx, req.RoomID)
|
|
|
|
if err == nil && visible {
|
|
|
|
res.RoomIDs = []string{req.RoomID}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-10-27 14:40:35 +02:00
|
|
|
rooms, err := r.DB.GetPublishedRooms(ctx, req.NetworkID, req.IncludeAllNetworks)
|
2020-07-02 16:41:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.RoomIDs = rooms
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-03 18:20:54 +02:00
|
|
|
|
|
|
|
func (r *Queryer) QueryCurrentState(ctx context.Context, req *api.QueryCurrentStateRequest, res *api.QueryCurrentStateResponse) error {
|
2023-04-27 13:54:20 +02:00
|
|
|
res.StateEvents = make(map[gomatrixserverlib.StateKeyTuple]*types.HeaderedEvent)
|
2020-09-03 18:20:54 +02:00
|
|
|
for _, tuple := range req.StateTuples {
|
2022-03-01 14:40:07 +01:00
|
|
|
if tuple.StateKey == "*" && req.AllowWildcards {
|
|
|
|
events, err := r.DB.GetStateEventsWithEventType(ctx, req.RoomID, tuple.EventType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, e := range events {
|
|
|
|
res.StateEvents[gomatrixserverlib.StateKeyTuple{
|
|
|
|
EventType: e.Type(),
|
|
|
|
StateKey: *e.StateKey(),
|
|
|
|
}] = e
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ev, err := r.DB.GetStateEvent(ctx, req.RoomID, tuple.EventType, tuple.StateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ev != nil {
|
|
|
|
res.StateEvents[tuple] = ev
|
|
|
|
}
|
2020-09-03 18:20:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Queryer) QueryRoomsForUser(ctx context.Context, req *api.QueryRoomsForUserRequest, res *api.QueryRoomsForUserResponse) error {
|
|
|
|
roomIDs, err := r.DB.GetRoomsByMembership(ctx, req.UserID, req.WantMembership)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.RoomIDs = roomIDs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Queryer) QueryKnownUsers(ctx context.Context, req *api.QueryKnownUsersRequest, res *api.QueryKnownUsersResponse) error {
|
|
|
|
users, err := r.DB.GetKnownUsers(ctx, req.UserID, req.SearchString, req.Limit)
|
2022-08-12 13:33:31 +02:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2020-09-03 18:20:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
res.Users = append(res.Users, authtypes.FullyQualifiedProfile{
|
|
|
|
UserID: user,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Queryer) QueryBulkStateContent(ctx context.Context, req *api.QueryBulkStateContentRequest, res *api.QueryBulkStateContentResponse) error {
|
|
|
|
events, err := r.DB.GetBulkStateContent(ctx, req.RoomIDs, req.StateTuples, req.AllowWildcards)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.Rooms = make(map[string]map[gomatrixserverlib.StateKeyTuple]string)
|
|
|
|
for _, ev := range events {
|
|
|
|
if res.Rooms[ev.RoomID] == nil {
|
|
|
|
res.Rooms[ev.RoomID] = make(map[gomatrixserverlib.StateKeyTuple]string)
|
|
|
|
}
|
|
|
|
room := res.Rooms[ev.RoomID]
|
|
|
|
room[gomatrixserverlib.StateKeyTuple{
|
|
|
|
EventType: ev.EventType,
|
|
|
|
StateKey: ev.StateKey,
|
|
|
|
}] = ev.ContentValue
|
|
|
|
res.Rooms[ev.RoomID] = room
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-12 08:20:59 +01:00
|
|
|
func (r *Queryer) QueryLeftUsers(ctx context.Context, req *api.QueryLeftUsersRequest, res *api.QueryLeftUsersResponse) error {
|
|
|
|
var err error
|
|
|
|
res.LeftUsers, err = r.DB.GetLeftUsers(ctx, req.StaleDeviceListUsers)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-03 18:20:54 +02:00
|
|
|
func (r *Queryer) QuerySharedUsers(ctx context.Context, req *api.QuerySharedUsersRequest, res *api.QuerySharedUsersResponse) error {
|
|
|
|
roomIDs, err := r.DB.GetRoomsByMembership(ctx, req.UserID, "join")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
roomIDs = append(roomIDs, req.IncludeRoomIDs...)
|
|
|
|
excludeMap := make(map[string]bool)
|
|
|
|
for _, roomID := range req.ExcludeRoomIDs {
|
|
|
|
excludeMap[roomID] = true
|
|
|
|
}
|
|
|
|
// filter out excluded rooms
|
|
|
|
j := 0
|
|
|
|
for i := range roomIDs {
|
|
|
|
// move elements to include to the beginning of the slice
|
|
|
|
// then trim elements on the right
|
|
|
|
if !excludeMap[roomIDs[i]] {
|
|
|
|
roomIDs[j] = roomIDs[i]
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomIDs = roomIDs[:j]
|
|
|
|
|
2022-10-05 13:47:53 +02:00
|
|
|
users, err := r.DB.JoinedUsersSetInRooms(ctx, roomIDs, req.OtherUserIDs, req.LocalOnly)
|
2020-09-03 18:20:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.UserIDsToCount = users
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Queryer) QueryServerBannedFromRoom(ctx context.Context, req *api.QueryServerBannedFromRoomRequest, res *api.QueryServerBannedFromRoomResponse) error {
|
|
|
|
if r.ServerACLs == nil {
|
|
|
|
return errors.New("no server ACL tracking")
|
|
|
|
}
|
|
|
|
res.Banned = r.ServerACLs.IsServerBannedFromRoom(req.ServerName, req.RoomID)
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-04 15:11:01 +01:00
|
|
|
|
|
|
|
func (r *Queryer) QueryAuthChain(ctx context.Context, req *api.QueryAuthChainRequest, res *api.QueryAuthChainResponse) error {
|
2023-03-01 17:06:47 +01:00
|
|
|
chain, err := GetAuthChain(ctx, r.DB.EventsFromIDs, nil, req.EventIDs)
|
2020-12-04 15:11:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-27 13:54:20 +02:00
|
|
|
hchain := make([]*types.HeaderedEvent, len(chain))
|
2020-12-04 15:11:01 +01:00
|
|
|
for i := range chain {
|
2023-05-02 16:03:16 +02:00
|
|
|
hchain[i] = &types.HeaderedEvent{PDU: chain[i]}
|
2020-12-04 15:11:01 +01:00
|
|
|
}
|
|
|
|
res.AuthChain = hchain
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-25 11:05:30 +02:00
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
func (r *Queryer) InvitePending(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID) (bool, error) {
|
|
|
|
pending, _, _, _, err := helpers.IsInvitePending(ctx, r.DB, roomID.String(), senderID)
|
2023-05-17 02:33:27 +02:00
|
|
|
return pending, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Queryer) QueryRoomInfo(ctx context.Context, roomID spec.RoomID) (*types.RoomInfo, error) {
|
|
|
|
return r.DB.RoomInfo(ctx, roomID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Queryer) CurrentStateEvent(ctx context.Context, roomID spec.RoomID, eventType string, stateKey string) (gomatrixserverlib.PDU, error) {
|
2023-05-19 18:27:01 +02:00
|
|
|
res, err := r.DB.GetStateEvent(ctx, roomID.String(), eventType, "")
|
2023-05-18 21:41:47 +02:00
|
|
|
if res == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, err
|
2023-05-17 02:33:27 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
func (r *Queryer) UserJoinedToRoom(ctx context.Context, roomNID types.RoomNID, senderID spec.SenderID) (bool, error) {
|
|
|
|
_, isIn, _, err := r.DB.GetMembership(ctx, roomNID, senderID)
|
2023-05-17 02:33:27 +02:00
|
|
|
return isIn, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Queryer) LocallyJoinedUsers(ctx context.Context, roomVersion gomatrixserverlib.RoomVersion, roomNID types.RoomNID) ([]gomatrixserverlib.PDU, error) {
|
|
|
|
joinNIDs, err := r.DB.GetMembershipEventNIDsForRoom(ctx, roomNID, true, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := r.DB.Events(ctx, roomVersion, joinNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each of the joined users, let's see if we can get a valid
|
|
|
|
// membership event.
|
|
|
|
joinedUsers := []gomatrixserverlib.PDU{}
|
|
|
|
for _, event := range events {
|
|
|
|
if event.Type() != spec.MRoomMember || event.StateKey() == nil {
|
|
|
|
continue // shouldn't happen
|
|
|
|
}
|
|
|
|
|
|
|
|
joinedUsers = append(joinedUsers, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return joinedUsers, nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 11:05:30 +02:00
|
|
|
// nolint:gocyclo
|
2023-06-12 13:19:25 +02:00
|
|
|
func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID) (string, error) {
|
2022-05-25 11:05:30 +02:00
|
|
|
// Look up if we know anything about the room. If it doesn't exist
|
|
|
|
// or is a stub entry then we can't do anything.
|
2023-06-06 15:16:55 +02:00
|
|
|
roomInfo, err := r.DB.RoomInfo(ctx, roomID.String())
|
2022-05-25 11:05:30 +02:00
|
|
|
if err != nil {
|
2023-06-06 15:16:55 +02:00
|
|
|
return "", fmt.Errorf("r.DB.RoomInfo: %w", err)
|
2022-05-25 11:05:30 +02:00
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if roomInfo == nil || roomInfo.IsStub() {
|
2023-06-06 15:16:55 +02:00
|
|
|
return "", nil // fmt.Errorf("room %q doesn't exist or is stub room", req.RoomID)
|
2022-05-25 11:05:30 +02:00
|
|
|
}
|
2023-04-21 18:06:29 +02:00
|
|
|
verImpl, err := gomatrixserverlib.GetRoomVersion(roomInfo.RoomVersion)
|
|
|
|
if err != nil {
|
2023-06-06 15:16:55 +02:00
|
|
|
return "", err
|
2022-05-25 11:05:30 +02:00
|
|
|
}
|
2023-06-06 15:16:55 +02:00
|
|
|
|
2023-06-12 13:19:25 +02:00
|
|
|
return verImpl.CheckRestrictedJoin(ctx, r.Cfg.Global.ServerName, &api.JoinRoomQuerier{Roomserver: r}, roomID, senderID)
|
2022-05-25 11:05:30 +02:00
|
|
|
}
|
2023-06-06 22:55:18 +02:00
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
func (r *Queryer) QuerySenderIDForUser(ctx context.Context, roomID spec.RoomID, userID spec.UserID) (spec.SenderID, error) {
|
|
|
|
version, err := r.DB.GetRoomVersion(ctx, roomID.String())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch version {
|
|
|
|
case gomatrixserverlib.RoomVersionPseudoIDs:
|
|
|
|
key, err := r.DB.SelectUserRoomPublicKey(ctx, userID, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return spec.SenderID(spec.Base64Bytes(key).Encode()), nil
|
|
|
|
default:
|
|
|
|
return spec.SenderID(userID.String()), nil
|
|
|
|
}
|
2023-06-06 22:55:18 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
func (r *Queryer) QueryUserIDForSender(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
userID, err := spec.NewUserID(string(senderID), true)
|
|
|
|
if err == nil {
|
|
|
|
return userID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes := spec.Base64Bytes{}
|
|
|
|
err = bytes.Decode(string(senderID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queryMap := map[spec.RoomID][]ed25519.PublicKey{roomID: {ed25519.PublicKey(bytes)}}
|
|
|
|
result, err := r.DB.SelectUserIDsForPublicKeys(ctx, queryMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if userKeys, ok := result[roomID]; ok {
|
|
|
|
if userID, ok := userKeys[string(senderID)]; ok {
|
|
|
|
return spec.NewUserID(userID, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2023-06-06 22:55:18 +02:00
|
|
|
}
|