2017-11-15 16:42:39 +01: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 19:36:08 +02:00
|
|
|
"context"
|
2017-11-15 16:42:39 +01:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-04-14 19:36:08 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-11-15 16:42:39 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stateEventInStateResp struct {
|
|
|
|
gomatrixserverlib.ClientEvent
|
|
|
|
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.
|
2020-05-01 11:48:17 +02:00
|
|
|
func OnIncomingStateRequest(ctx context.Context, rsAPI api.RoomserverInternalAPI, roomID string) util.JSONResponse {
|
2017-11-15 16:42:39 +01:00
|
|
|
// TODO(#287): Auth request and handle the case where the user has left (where
|
|
|
|
// we should return the state at the poin they left)
|
2020-04-14 19:36:08 +02:00
|
|
|
stateReq := api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
}
|
|
|
|
stateRes := api.QueryLatestEventsAndStateResponse{}
|
2017-11-15 16:42:39 +01:00
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
if err := rsAPI.QueryLatestEventsAndState(ctx, &stateReq, &stateRes); err != nil {
|
2020-04-14 19:36:08 +02:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-11-15 16:42:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 19:36:08 +02:00
|
|
|
if len(stateRes.StateEvents) == 0 {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.NotFound("cannot find state"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:42:39 +01:00
|
|
|
resp := []stateEventInStateResp{}
|
|
|
|
// Fill the prev_content and replaces_state keys if necessary
|
2020-04-14 19:36:08 +02:00
|
|
|
for _, event := range stateRes.StateEvents {
|
2017-11-15 16:42:39 +01:00
|
|
|
stateEvent := stateEventInStateResp{
|
2020-03-19 13:07:01 +01:00
|
|
|
ClientEvent: gomatrixserverlib.HeaderedToClientEvents(
|
|
|
|
[]gomatrixserverlib.HeaderedEvent{event}, gomatrixserverlib.FormatAll,
|
|
|
|
)[0],
|
2017-11-15 16:42:39 +01:00
|
|
|
}
|
|
|
|
var prevEventRef types.PrevEventRef
|
|
|
|
if len(event.Unsigned()) > 0 {
|
|
|
|
if err := json.Unmarshal(event.Unsigned(), &prevEventRef); err != nil {
|
2020-04-14 19:36:08 +02:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("json.Unmarshal failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-11-15 16:42:39 +01:00
|
|
|
}
|
|
|
|
// Fills the previous state event ID if the state event replaces another
|
|
|
|
// state event
|
|
|
|
if len(prevEventRef.ReplacesState) > 0 {
|
|
|
|
stateEvent.ReplacesState = prevEventRef.ReplacesState
|
|
|
|
}
|
|
|
|
// Fill the previous event if the state event references a previous event
|
|
|
|
if prevEventRef.PrevContent != nil {
|
|
|
|
stateEvent.PrevContent = prevEventRef.PrevContent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = append(resp, stateEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-11-15 16:42:39 +01:00
|
|
|
JSON: resp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 17:21:42 +02:00
|
|
|
// If eventFormat=true, sends the whole event else just the content.
|
|
|
|
func OnIncomingStateTypeRequest(ctx context.Context, rsAPI api.RoomserverInternalAPI, roomID, evType, stateKey string, eventFormat bool) util.JSONResponse {
|
2017-11-15 16:42:39 +01:00
|
|
|
// TODO(#287): Auth request and handle the case where the user has left (where
|
|
|
|
// we should return the state at the poin they left)
|
2020-04-14 19:36:08 +02:00
|
|
|
util.GetLogger(ctx).WithFields(log.Fields{
|
2017-11-15 16:42:39 +01:00
|
|
|
"roomID": roomID,
|
|
|
|
"evType": evType,
|
|
|
|
"stateKey": stateKey,
|
|
|
|
}).Info("Fetching state")
|
|
|
|
|
2020-04-14 19:36:08 +02:00
|
|
|
stateReq := api.QueryLatestEventsAndStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateToFetch: []gomatrixserverlib.StateKeyTuple{
|
|
|
|
gomatrixserverlib.StateKeyTuple{
|
|
|
|
EventType: evType,
|
|
|
|
StateKey: stateKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
stateRes := api.QueryLatestEventsAndStateResponse{}
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
if err := rsAPI.QueryLatestEventsAndState(ctx, &stateReq, &stateRes); err != nil {
|
2020-04-14 19:36:08 +02:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("queryAPI.QueryLatestEventsAndState failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-11-15 16:42:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 19:36:08 +02:00
|
|
|
if len(stateRes.StateEvents) == 0 {
|
2017-11-15 16:42:39 +01:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2017-11-15 16:42:39 +01:00
|
|
|
JSON: jsonerror.NotFound("cannot find state"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stateEvent := stateEventInStateResp{
|
2020-04-14 19:36:08 +02:00
|
|
|
ClientEvent: gomatrixserverlib.HeaderedToClientEvent(stateRes.StateEvents[0], gomatrixserverlib.FormatAll),
|
2017-11-15 16:42:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 17:21:42 +02:00
|
|
|
var res interface{}
|
|
|
|
if eventFormat {
|
|
|
|
res = stateEvent
|
|
|
|
} else {
|
|
|
|
res = stateEvent.Content
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:42:39 +01:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2020-06-17 17:21:42 +02:00
|
|
|
JSON: res,
|
2017-11-15 16:42:39 +01:00
|
|
|
}
|
|
|
|
}
|