2017-11-15 15:42:39 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2020-04-14 17:36:08 +00:00
|
|
|
"context"
|
2017-11-15 15:42:39 +00:00
|
|
|
"encoding/json"
|
2020-07-28 09:09:10 +00:00
|
|
|
"fmt"
|
2017-11-15 15:42:39 +00:00
|
|
|
"net/http"
|
|
|
|
|
2020-04-14 17:36:08 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 11:54:20 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2023-04-04 17:16:53 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/synctypes"
|
2020-07-28 09:09:10 +00:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-11-15 15:42:39 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-11-15 15:42:39 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stateEventInStateResp struct {
|
2023-04-04 17:16:53 +00:00
|
|
|
synctypes.ClientEvent
|
2017-11-15 15:42:39 +00:00
|
|
|
PrevContent json.RawMessage `json:"prev_content,omitempty"`
|
|
|
|
ReplacesState string `json:"replaces_state,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnIncomingStateRequest is called when a client makes a /rooms/{roomID}/state
|
|
|
|
// request. It will fetch all the state events from the specified room and will
|
|
|
|
// append the necessary keys to them if applicable before returning them.
|
|
|
|
// Returns an error if something went wrong in the process.
|
|
|
|
// TODO: Check if the user is in the room. If not, check if the room's history
|
|
|
|
// is publicly visible. Current behaviour is returning an empty array if the
|
|
|
|
// user cannot see the room's history.
|
2022-05-05 12:17:38 +00:00
|
|
|
func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI api.ClientRoomserverAPI, roomID string) util.JSONResponse {
|
2020-08-25 17:43:56 +00:00
|
|
|
var worldReadable bool
|
|
|
|
var wantLatestState bool
|
2017-11-15 15:42:39 +00:00
|
|
|
|
2020-08-25 17:43:56 +00:00
|
|
|
// First of all, get the latest state of the room. We need to do this
|
|
|
|
// so that we can look at the history visibility of the room. If the
|
|
|
|
// room is world-readable then we will always return the latest state.
|
|
|
|
stateRes := api.QueryLatestEventsAndStateResponse{}
|
|
|
|
if err := rsAPI.QueryLatestEventsAndState(ctx, &api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateToFetch: []gomatrixserverlib.StateKeyTuple{},
|
|
|
|
}, &stateRes); err != nil {
|
2020-04-14 17:36:08 +00:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
2022-05-11 10:29:23 +00:00
|
|
|
if !stateRes.RoomExists {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-09 22:46:49 +00:00
|
|
|
JSON: spec.Forbidden("room does not exist"),
|
2022-05-11 10:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 15:42:39 +00:00
|
|
|
|
2020-08-25 17:43:56 +00:00
|
|
|
// Look at the room state and see if we have a history visibility event
|
|
|
|
// that marks the room as world-readable. If we don't then we assume that
|
|
|
|
// the room is not world-readable.
|
|
|
|
for _, ev := range stateRes.StateEvents {
|
2023-04-19 14:50:33 +00:00
|
|
|
if ev.Type() == spec.MRoomHistoryVisibility {
|
2020-08-25 17:43:56 +00:00
|
|
|
content := map[string]string{}
|
|
|
|
if err := json.Unmarshal(ev.Content(), &content); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("json.Unmarshal for history visibility failed")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-08-25 17:43:56 +00:00
|
|
|
}
|
|
|
|
if visibility, ok := content["history_visibility"]; ok {
|
|
|
|
worldReadable = visibility == "world_readable"
|
|
|
|
break
|
|
|
|
}
|
2020-04-14 17:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:43:56 +00:00
|
|
|
// If the room isn't world-readable then we will instead try to find out
|
|
|
|
// the state of the room based on the user's membership. If the user is
|
|
|
|
// in the room then we'll want the latest state. If the user has never
|
|
|
|
// been in the room and the room isn't world-readable, then we won't
|
|
|
|
// return any state. If the user was in the room previously but is no
|
|
|
|
// longer then we will return the state at the time that the user left.
|
|
|
|
// membershipRes will only be populated if the room is not world-readable.
|
|
|
|
var membershipRes api.QueryMembershipForUserResponse
|
|
|
|
if !worldReadable {
|
|
|
|
// The room isn't world-readable so try to work out based on the
|
|
|
|
// user's membership if we want the latest state or not.
|
2023-06-12 11:19:25 +00:00
|
|
|
userID, err := spec.NewUserID(device.UserID, true)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("UserID is invalid")
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.Unknown("Device UserID is invalid"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = rsAPI.QueryMembershipForUser(ctx, &api.QueryMembershipForUserRequest{
|
2020-08-25 17:43:56 +00:00
|
|
|
RoomID: roomID,
|
2023-06-12 11:19:25 +00:00
|
|
|
UserID: *userID,
|
2020-08-25 17:43:56 +00:00
|
|
|
}, &membershipRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
2020-08-25 17:43:56 +00:00
|
|
|
// If the user has never been in the room then stop at this point.
|
|
|
|
// We won't tell the user about a room they have never joined.
|
|
|
|
if !membershipRes.HasBeenInRoom {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-09 22:46:49 +00:00
|
|
|
JSON: spec.Forbidden(fmt.Sprintf("Unknown room %q or user %q has never joined this room", roomID, device.UserID)),
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 17:43:56 +00:00
|
|
|
// Otherwise, if the user has been in the room, whether or not we
|
|
|
|
// give them the latest state will depend on if they are *still* in
|
|
|
|
// the room.
|
|
|
|
wantLatestState = membershipRes.IsInRoom
|
|
|
|
} else {
|
|
|
|
// The room is world-readable so the user join state is irrelevant,
|
|
|
|
// just get the latest room state instead.
|
|
|
|
wantLatestState = true
|
|
|
|
}
|
2017-11-15 15:42:39 +00:00
|
|
|
|
2020-08-25 17:43:56 +00:00
|
|
|
util.GetLogger(ctx).WithFields(log.Fields{
|
|
|
|
"roomID": roomID,
|
|
|
|
"state_at_event": !wantLatestState,
|
|
|
|
}).Info("Fetching all state")
|
|
|
|
|
2023-04-04 17:16:53 +00:00
|
|
|
stateEvents := []synctypes.ClientEvent{}
|
2020-08-25 17:43:56 +00:00
|
|
|
if wantLatestState {
|
|
|
|
// If we are happy to use the latest state, either because the user is
|
|
|
|
// still in the room, or because the room is world-readable, then just
|
|
|
|
// use the result of the previous QueryLatestEventsAndState response
|
|
|
|
// to find the state event, if provided.
|
|
|
|
for _, ev := range stateRes.StateEvents {
|
|
|
|
stateEvents = append(
|
|
|
|
stateEvents,
|
2023-06-14 14:23:46 +00:00
|
|
|
synctypes.ToClientEventDefault(func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-12 11:19:25 +00:00
|
|
|
return rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
}, ev),
|
2020-08-25 17:43:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, take the event ID of their leave event and work out what
|
|
|
|
// the state of the room was before that event.
|
|
|
|
var stateAfterRes api.QueryStateAfterEventsResponse
|
|
|
|
err := rsAPI.QueryStateAfterEvents(ctx, &api.QueryStateAfterEventsRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
PrevEventIDs: []string{membershipRes.EventID},
|
|
|
|
StateToFetch: []gomatrixserverlib.StateKeyTuple{},
|
|
|
|
}, &stateAfterRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-08-25 17:43:56 +00:00
|
|
|
}
|
2020-10-18 04:07:34 +00:00
|
|
|
for _, ev := range stateAfterRes.StateEvents {
|
2023-09-15 15:25:09 +00:00
|
|
|
clientEvent, err := synctypes.ToClientEvent(ev, synctypes.FormatAll, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
return rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed converting to ClientEvent")
|
|
|
|
continue
|
2023-06-12 11:19:25 +00:00
|
|
|
}
|
2020-08-25 17:43:56 +00:00
|
|
|
stateEvents = append(
|
|
|
|
stateEvents,
|
2023-09-15 15:25:09 +00:00
|
|
|
*clientEvent,
|
2020-08-25 17:43:56 +00:00
|
|
|
)
|
|
|
|
}
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 17:43:56 +00:00
|
|
|
// Return the results to the requestor.
|
2017-11-15 15:42:39 +00:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2020-08-25 17:43:56 +00:00
|
|
|
JSON: stateEvents,
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnIncomingStateTypeRequest is called when a client makes a
|
|
|
|
// /rooms/{roomID}/state/{type}/{statekey} request. It will look in current
|
|
|
|
// state to see if there is an event with that type and state key, if there
|
|
|
|
// is then (by default) we return the content, otherwise a 404.
|
2020-06-17 15:21:42 +00:00
|
|
|
// If eventFormat=true, sends the whole event else just the content.
|
2020-07-28 09:09:10 +00:00
|
|
|
func OnIncomingStateTypeRequest(
|
2022-05-05 12:17:38 +00:00
|
|
|
ctx context.Context, device *userapi.Device, rsAPI api.ClientRoomserverAPI,
|
2020-07-28 09:09:10 +00:00
|
|
|
roomID, evType, stateKey string, eventFormat bool,
|
|
|
|
) util.JSONResponse {
|
|
|
|
var worldReadable bool
|
|
|
|
var wantLatestState bool
|
2017-11-15 15:42:39 +00:00
|
|
|
|
2023-08-24 15:43:51 +00:00
|
|
|
roomVer, err := rsAPI.QueryRoomVersionForRoom(ctx, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: spec.Forbidden(fmt.Sprintf("Unknown room %q or user %q has never joined this room", roomID, device.UserID)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate user ID state keys to room keys in pseudo ID rooms
|
|
|
|
if roomVer == gomatrixserverlib.RoomVersionPseudoIDs {
|
|
|
|
parsedRoomID, err := spec.NewRoomID(roomID)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: spec.InvalidParam("invalid room ID"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newStateKey, err := synctypes.FromClientStateKey(*parsedRoomID, stateKey, func(roomID spec.RoomID, userID spec.UserID) (*spec.SenderID, error) {
|
|
|
|
return rsAPI.QuerySenderIDForUser(ctx, roomID, userID)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// TODO: work out better logic for failure cases (e.g. sender ID not found)
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("synctypes.FromClientStateKey failed")
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.Unknown("internal server error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stateKey = *newStateKey
|
|
|
|
}
|
|
|
|
|
2020-07-28 09:09:10 +00:00
|
|
|
// Always fetch visibility so that we can work out whether to show
|
|
|
|
// the latest events or the last event from when the user was joined.
|
|
|
|
// Then include the requested event type and state key, assuming it
|
|
|
|
// isn't for the same.
|
|
|
|
stateToFetch := []gomatrixserverlib.StateKeyTuple{
|
|
|
|
{
|
|
|
|
EventType: evType,
|
|
|
|
StateKey: stateKey,
|
2020-04-14 17:36:08 +00:00
|
|
|
},
|
|
|
|
}
|
2023-04-19 14:50:33 +00:00
|
|
|
if evType != spec.MRoomHistoryVisibility && stateKey != "" {
|
2020-07-28 09:09:10 +00:00
|
|
|
stateToFetch = append(stateToFetch, gomatrixserverlib.StateKeyTuple{
|
2023-04-19 14:50:33 +00:00
|
|
|
EventType: spec.MRoomHistoryVisibility,
|
2020-07-28 09:09:10 +00:00
|
|
|
StateKey: "",
|
|
|
|
})
|
|
|
|
}
|
2020-04-14 17:36:08 +00:00
|
|
|
|
2020-07-28 09:09:10 +00:00
|
|
|
// First of all, get the latest state of the room. We need to do this
|
|
|
|
// so that we can look at the history visibility of the room. If the
|
|
|
|
// room is world-readable then we will always return the latest state.
|
|
|
|
stateRes := api.QueryLatestEventsAndStateResponse{}
|
|
|
|
if err := rsAPI.QueryLatestEventsAndState(ctx, &api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateToFetch: stateToFetch,
|
|
|
|
}, &stateRes); err != nil {
|
2020-04-14 17:36:08 +00:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 09:09:10 +00:00
|
|
|
// Look at the room state and see if we have a history visibility event
|
|
|
|
// that marks the room as world-readable. If we don't then we assume that
|
|
|
|
// the room is not world-readable.
|
|
|
|
for _, ev := range stateRes.StateEvents {
|
2023-04-19 14:50:33 +00:00
|
|
|
if ev.Type() == spec.MRoomHistoryVisibility {
|
2020-07-28 09:09:10 +00:00
|
|
|
content := map[string]string{}
|
|
|
|
if err := json.Unmarshal(ev.Content(), &content); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("json.Unmarshal for history visibility failed")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-07-28 09:09:10 +00:00
|
|
|
}
|
|
|
|
if visibility, ok := content["history_visibility"]; ok {
|
|
|
|
worldReadable = visibility == "world_readable"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the room isn't world-readable then we will instead try to find out
|
|
|
|
// the state of the room based on the user's membership. If the user is
|
|
|
|
// in the room then we'll want the latest state. If the user has never
|
|
|
|
// been in the room and the room isn't world-readable, then we won't
|
|
|
|
// return any state. If the user was in the room previously but is no
|
|
|
|
// longer then we will return the state at the time that the user left.
|
|
|
|
// membershipRes will only be populated if the room is not world-readable.
|
|
|
|
var membershipRes api.QueryMembershipForUserResponse
|
|
|
|
if !worldReadable {
|
2023-06-12 11:19:25 +00:00
|
|
|
userID, err := spec.NewUserID(device.UserID, true)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("UserID is invalid")
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.Unknown("Device UserID is invalid"),
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 09:09:10 +00:00
|
|
|
// The room isn't world-readable so try to work out based on the
|
|
|
|
// user's membership if we want the latest state or not.
|
2023-06-12 11:19:25 +00:00
|
|
|
err = rsAPI.QueryMembershipForUser(ctx, &api.QueryMembershipForUserRequest{
|
2020-07-28 09:09:10 +00:00
|
|
|
RoomID: roomID,
|
2023-06-12 11:19:25 +00:00
|
|
|
UserID: *userID,
|
2020-07-28 09:09:10 +00:00
|
|
|
}, &membershipRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-07-28 09:09:10 +00:00
|
|
|
}
|
|
|
|
// If the user has never been in the room then stop at this point.
|
|
|
|
// We won't tell the user about a room they have never joined.
|
2023-04-19 14:50:33 +00:00
|
|
|
if !membershipRes.HasBeenInRoom || membershipRes.Membership == spec.Ban {
|
2020-07-28 09:09:10 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
2023-05-09 22:46:49 +00:00
|
|
|
JSON: spec.Forbidden(fmt.Sprintf("Unknown room %q or user %q has never joined this room", roomID, device.UserID)),
|
2020-07-28 09:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise, if the user has been in the room, whether or not we
|
|
|
|
// give them the latest state will depend on if they are *still* in
|
|
|
|
// the room.
|
|
|
|
wantLatestState = membershipRes.IsInRoom
|
|
|
|
} else {
|
|
|
|
// The room is world-readable so the user join state is irrelevant,
|
|
|
|
// just get the latest room state instead.
|
|
|
|
wantLatestState = true
|
|
|
|
}
|
|
|
|
|
|
|
|
util.GetLogger(ctx).WithFields(log.Fields{
|
|
|
|
"roomID": roomID,
|
|
|
|
"evType": evType,
|
|
|
|
"stateKey": stateKey,
|
|
|
|
"state_at_event": !wantLatestState,
|
|
|
|
}).Info("Fetching state")
|
|
|
|
|
2023-04-27 11:54:20 +00:00
|
|
|
var event *types.HeaderedEvent
|
2020-07-28 09:09:10 +00:00
|
|
|
if wantLatestState {
|
|
|
|
// If we are happy to use the latest state, either because the user is
|
|
|
|
// still in the room, or because the room is world-readable, then just
|
|
|
|
// use the result of the previous QueryLatestEventsAndState response
|
|
|
|
// to find the state event, if provided.
|
|
|
|
for _, ev := range stateRes.StateEvents {
|
|
|
|
if ev.Type() == evType && ev.StateKeyEquals(stateKey) {
|
2020-11-16 15:44:53 +00:00
|
|
|
event = ev
|
2020-07-28 09:09:10 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, take the event ID of their leave event and work out what
|
|
|
|
// the state of the room was before that event.
|
|
|
|
var stateAfterRes api.QueryStateAfterEventsResponse
|
|
|
|
err := rsAPI.QueryStateAfterEvents(ctx, &api.QueryStateAfterEventsRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
PrevEventIDs: []string{membershipRes.EventID},
|
|
|
|
StateToFetch: []gomatrixserverlib.StateKeyTuple{
|
|
|
|
{
|
|
|
|
EventType: evType,
|
|
|
|
StateKey: stateKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &stateAfterRes)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to QueryMembershipForUser")
|
2023-05-17 00:33:27 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2020-07-28 09:09:10 +00:00
|
|
|
}
|
|
|
|
if len(stateAfterRes.StateEvents) > 0 {
|
2020-11-16 15:44:53 +00:00
|
|
|
event = stateAfterRes.StateEvents[0]
|
2020-07-28 09:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was no event found that matches all of the above criteria then
|
|
|
|
// return an error.
|
|
|
|
if event == nil {
|
2017-11-15 15:42:39 +00:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusNotFound,
|
2023-05-09 22:46:49 +00:00
|
|
|
JSON: spec.NotFound(fmt.Sprintf("Cannot find state event for %q", evType)),
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stateEvent := stateEventInStateResp{
|
2023-06-14 14:23:46 +00:00
|
|
|
ClientEvent: synctypes.ToClientEventDefault(func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-12 11:19:25 +00:00
|
|
|
return rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
}, event),
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 15:21:42 +00:00
|
|
|
var res interface{}
|
|
|
|
if eventFormat {
|
|
|
|
res = stateEvent
|
|
|
|
} else {
|
|
|
|
res = stateEvent.Content
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:42:39 +00:00
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusOK,
|
2020-06-17 15:21:42 +00:00
|
|
|
JSON: res,
|
2017-11-15 15:42:39 +00:00
|
|
|
}
|
|
|
|
}
|