2017-04-20 17:40:52 -05: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.
|
|
|
|
|
2017-03-06 08:29:39 -06:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-07-13 05:41:30 -05:00
|
|
|
"net/http"
|
|
|
|
|
2017-05-30 11:44:31 -05:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-03-06 08:29:39 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-03-09 09:07:18 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2017-03-06 08:29:39 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RoomserverQueryAPIDatabase has the storage APIs needed to implement the query API.
|
|
|
|
type RoomserverQueryAPIDatabase interface {
|
2017-03-09 09:07:18 -06:00
|
|
|
state.RoomStateDatabase
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the numeric ID for the room.
|
2017-03-06 08:29:39 -06:00
|
|
|
// Returns 0 if the room doesn't exists.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
|
|
|
RoomNID(roomID string) (types.RoomNID, error)
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up event references for the latest events in the room and the current state snapshot.
|
2017-06-27 09:28:44 -05:00
|
|
|
// Returns the latest events, the current state and the maximum depth of the latest events plus 1.
|
2017-03-06 08:29:39 -06:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-06-27 09:28:44 -05:00
|
|
|
LatestEventIDs(roomNID types.RoomNID) ([]gomatrixserverlib.EventReference, types.StateSnapshotNID, int64, error)
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the numeric IDs for a list of events.
|
2017-06-02 08:32:36 -05:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
|
|
|
EventNIDs(eventIDs []string) (map[string]types.EventNID, error)
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the join events for all members in a room as requested by a given
|
2017-08-21 10:34:26 -05:00
|
|
|
// user. If the user is currently in the room, returns the room's current
|
|
|
|
// members, if not returns an empty array (TODO: Fix it)
|
|
|
|
// If the user requesting the list of members has never been in the room,
|
|
|
|
// returns nil.
|
|
|
|
// If there was an issue retrieving the events, returns an error.
|
|
|
|
GetMembershipEvents(roomNID types.RoomNID, requestSenderUserID string) (events []types.Event, err error)
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the active invites targeting a user in a room and return the
|
|
|
|
// numeric state key IDs for the user IDs who sent them.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
|
|
|
GetInvitesForUser(roomNID types.RoomNID, targetUserNID types.EventStateKeyNID) (senderUserNIDs []types.EventStateKeyNID, err error)
|
|
|
|
// Look up the string event state keys for a list of numeric event state keys
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
|
|
|
EventStateKeys([]types.EventStateKeyNID) (map[types.EventStateKeyNID]string, error)
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
2017-07-28 05:31:43 -05:00
|
|
|
// RoomserverQueryAPI is an implementation of api.RoomserverQueryAPI
|
2017-03-06 08:29:39 -06:00
|
|
|
type RoomserverQueryAPI struct {
|
|
|
|
DB RoomserverQueryAPIDatabase
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryLatestEventsAndState implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryLatestEventsAndState(
|
|
|
|
request *api.QueryLatestEventsAndStateRequest,
|
|
|
|
response *api.QueryLatestEventsAndStateResponse,
|
2017-06-02 08:32:36 -05:00
|
|
|
) error {
|
2017-03-06 08:29:39 -06:00
|
|
|
response.QueryLatestEventsAndStateRequest = *request
|
|
|
|
roomNID, err := r.DB.RoomNID(request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
2017-03-09 09:07:18 -06:00
|
|
|
var currentStateSnapshotNID types.StateSnapshotNID
|
2017-06-27 09:28:44 -05:00
|
|
|
response.LatestEvents, currentStateSnapshotNID, response.Depth, err = r.DB.LatestEventIDs(roomNID)
|
2017-03-09 09:07:18 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the currrent state for the requested tuples.
|
2017-03-09 09:07:18 -06:00
|
|
|
stateEntries, err := state.LoadStateAtSnapshotForStringTuples(r.DB, currentStateSnapshotNID, request.StateToFetch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-30 11:44:31 -05:00
|
|
|
stateEvents, err := r.loadStateEvents(stateEntries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.StateEvents = stateEvents
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStateAfterEvents implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryStateAfterEvents(
|
|
|
|
request *api.QueryStateAfterEventsRequest,
|
|
|
|
response *api.QueryStateAfterEventsResponse,
|
2017-06-02 08:32:36 -05:00
|
|
|
) error {
|
2017-05-30 11:44:31 -05:00
|
|
|
response.QueryStateAfterEventsRequest = *request
|
|
|
|
roomNID, err := r.DB.RoomNID(request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
|
|
|
|
|
|
|
prevStates, err := r.DB.StateAtEventIDs(request.PrevEventIDs)
|
|
|
|
if err != nil {
|
2017-06-07 08:32:53 -05:00
|
|
|
switch err.(type) {
|
|
|
|
case types.MissingEventError:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 11:44:31 -05:00
|
|
|
}
|
|
|
|
response.PrevEventsExist = true
|
|
|
|
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the currrent state for the requested tuples.
|
2017-05-30 11:44:31 -05:00
|
|
|
stateEntries, err := state.LoadStateAfterEventsForStringTuples(r.DB, prevStates, request.StateToFetch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stateEvents, err := r.loadStateEvents(stateEntries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.StateEvents = stateEvents
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 08:32:36 -05:00
|
|
|
// QueryEventsByID implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryEventsByID(
|
|
|
|
request *api.QueryEventsByIDRequest,
|
|
|
|
response *api.QueryEventsByIDResponse,
|
|
|
|
) error {
|
|
|
|
response.QueryEventsByIDRequest = *request
|
|
|
|
|
|
|
|
eventNIDMap, err := r.DB.EventNIDs(request.EventIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
for _, nid := range eventNIDMap {
|
|
|
|
eventNIDs = append(eventNIDs, nid)
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := r.loadEvents(eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Events = events
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-30 11:44:31 -05:00
|
|
|
func (r *RoomserverQueryAPI) loadStateEvents(stateEntries []types.StateEntry) ([]gomatrixserverlib.Event, error) {
|
2017-03-09 09:07:18 -06:00
|
|
|
eventNIDs := make([]types.EventNID, len(stateEntries))
|
|
|
|
for i := range stateEntries {
|
|
|
|
eventNIDs[i] = stateEntries[i].EventNID
|
|
|
|
}
|
2017-06-02 08:32:36 -05:00
|
|
|
return r.loadEvents(eventNIDs)
|
|
|
|
}
|
2017-03-09 09:07:18 -06:00
|
|
|
|
2017-06-02 08:32:36 -05:00
|
|
|
func (r *RoomserverQueryAPI) loadEvents(eventNIDs []types.EventNID) ([]gomatrixserverlib.Event, error) {
|
2017-03-09 09:07:18 -06:00
|
|
|
stateEvents, err := r.DB.Events(eventNIDs)
|
|
|
|
if err != nil {
|
2017-05-30 11:44:31 -05:00
|
|
|
return nil, err
|
2017-03-09 09:07:18 -06:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:44:31 -05:00
|
|
|
result := make([]gomatrixserverlib.Event, len(stateEvents))
|
2017-03-09 09:07:18 -06:00
|
|
|
for i := range stateEvents {
|
2017-05-30 11:44:31 -05:00
|
|
|
result[i] = stateEvents[i].Event
|
2017-03-09 09:07:18 -06:00
|
|
|
}
|
2017-05-30 11:44:31 -05:00
|
|
|
return result, nil
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
2017-08-21 10:34:26 -05:00
|
|
|
// QueryMembershipsForRoom implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryMembershipsForRoom(
|
|
|
|
request *api.QueryMembershipsForRoomRequest,
|
|
|
|
response *api.QueryMembershipsForRoomResponse,
|
|
|
|
) error {
|
|
|
|
roomNID, err := r.DB.RoomNID(request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := r.DB.GetMembershipEvents(roomNID, request.Sender)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if events == nil {
|
|
|
|
response.HasBeenInRoom = false
|
|
|
|
response.JoinEvents = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response.HasBeenInRoom = true
|
|
|
|
response.JoinEvents = []gomatrixserverlib.ClientEvent{}
|
|
|
|
for _, event := range events {
|
|
|
|
clientEvent := gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll)
|
|
|
|
response.JoinEvents = append(response.JoinEvents, clientEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-23 09:08:48 -05:00
|
|
|
// QueryInvitesForUser implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryInvitesForUser(
|
|
|
|
request *api.QueryInvitesForUserRequest,
|
|
|
|
response *api.QueryInvitesForUserResponse,
|
|
|
|
) error {
|
|
|
|
roomNID, err := r.DB.RoomNID(request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetUserNIDs, err := r.DB.EventStateKeyNIDs([]string{request.TargetUserID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
targetUserNID := targetUserNIDs[request.TargetUserID]
|
|
|
|
|
|
|
|
senderUserNIDs, err := r.DB.GetInvitesForUser(roomNID, targetUserNID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
senderUserIDs, err := r.DB.EventStateKeys(senderUserNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, senderUserID := range senderUserIDs {
|
|
|
|
response.InviteSenderUserIDs = append(response.InviteSenderUserIDs, senderUserID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 08:29:39 -06:00
|
|
|
// SetupHTTP adds the RoomserverQueryAPI handlers to the http.ServeMux.
|
|
|
|
func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryLatestEventsAndStatePath,
|
2017-07-13 05:41:30 -05:00
|
|
|
common.MakeAPI("queryLatestEventsAndState", func(req *http.Request) util.JSONResponse {
|
2017-03-06 08:29:39 -06:00
|
|
|
var request api.QueryLatestEventsAndStateRequest
|
|
|
|
var response api.QueryLatestEventsAndStateResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryLatestEventsAndState(&request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: 200, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-05-30 11:44:31 -05:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryStateAfterEventsPath,
|
2017-07-13 05:41:30 -05:00
|
|
|
common.MakeAPI("queryStateAfterEvents", func(req *http.Request) util.JSONResponse {
|
2017-05-30 11:44:31 -05:00
|
|
|
var request api.QueryStateAfterEventsRequest
|
|
|
|
var response api.QueryStateAfterEventsResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryStateAfterEvents(&request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: 200, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-06-02 08:32:36 -05:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryEventsByIDPath,
|
2017-07-13 05:41:30 -05:00
|
|
|
common.MakeAPI("queryEventsByID", func(req *http.Request) util.JSONResponse {
|
2017-06-02 08:32:36 -05:00
|
|
|
var request api.QueryEventsByIDRequest
|
|
|
|
var response api.QueryEventsByIDResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryEventsByID(&request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: 200, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-08-21 10:34:26 -05:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryMembershipsForRoomPath,
|
|
|
|
common.MakeAPI("queryMembershipsForRoom", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryMembershipsForRoomRequest
|
|
|
|
var response api.QueryMembershipsForRoomResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryMembershipsForRoom(&request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: 200, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-08-23 09:08:48 -05:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryInvitesForUserPath,
|
|
|
|
common.MakeAPI("queryInvitesForUser", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryInvitesForUserRequest
|
|
|
|
var response api.QueryInvitesForUserResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryInvitesForUser(&request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: 200, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|