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 api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-13 07:37:50 -05:00
|
|
|
"context"
|
2017-03-06 08:29:39 -06:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-07-12 08:13:10 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-03-06 08:29:39 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// QueryLatestEventsAndStateRequest is a request to QueryLatestEventsAndState
|
|
|
|
type QueryLatestEventsAndStateRequest struct {
|
2017-05-30 11:44:31 -05:00
|
|
|
// The room ID to query the latest events for.
|
2017-07-12 08:13:10 -05:00
|
|
|
RoomID string `json:"room_id"`
|
2017-03-06 08:29:39 -06:00
|
|
|
// The state key tuples to fetch from the room current state.
|
|
|
|
// If this list is empty or nil then no state events are returned.
|
2017-07-12 08:13:10 -05:00
|
|
|
StateToFetch []gomatrixserverlib.StateKeyTuple `json:"state_to_fetch"`
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryLatestEventsAndStateResponse is a response to QueryLatestEventsAndState
|
2017-06-27 09:28:44 -05:00
|
|
|
// This is used when sending events to set the prev_events, auth_events and depth.
|
|
|
|
// It is also used to tell whether the event is allowed by the event auth rules.
|
2017-03-06 08:29:39 -06:00
|
|
|
type QueryLatestEventsAndStateResponse struct {
|
|
|
|
// Copy of the request for debugging.
|
|
|
|
QueryLatestEventsAndStateRequest
|
|
|
|
// Does the room exist?
|
|
|
|
// If the room doesn't exist this will be false and LatestEvents will be empty.
|
2017-07-12 08:13:10 -05:00
|
|
|
RoomExists bool `json:"room_exists"`
|
2017-03-06 08:29:39 -06:00
|
|
|
// The latest events in the room.
|
2017-06-27 09:28:44 -05:00
|
|
|
// These are used to set the prev_events when sending an event.
|
2017-07-12 08:13:10 -05:00
|
|
|
LatestEvents []gomatrixserverlib.EventReference `json:"latest_events"`
|
2017-03-06 08:29:39 -06:00
|
|
|
// The state events requested.
|
2017-06-02 08:32:36 -05:00
|
|
|
// This list will be in an arbitrary order.
|
2017-06-27 09:28:44 -05:00
|
|
|
// These are used to set the auth_events when sending an event.
|
|
|
|
// These are used to check whether the event is allowed.
|
2017-07-12 08:13:10 -05:00
|
|
|
StateEvents []gomatrixserverlib.Event `json:"state_events"`
|
2017-06-27 09:28:44 -05:00
|
|
|
// The depth of the latest events.
|
|
|
|
// This is one greater than the maximum depth of the latest events.
|
|
|
|
// This is used to set the depth when sending an event.
|
2017-07-12 08:13:10 -05:00
|
|
|
Depth int64 `json:"depth"`
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:44:31 -05:00
|
|
|
// QueryStateAfterEventsRequest is a request to QueryStateAfterEvents
|
|
|
|
type QueryStateAfterEventsRequest struct {
|
|
|
|
// The room ID to query the state in.
|
2017-07-12 08:13:10 -05:00
|
|
|
RoomID string `json:"room_id"`
|
2017-05-30 11:44:31 -05:00
|
|
|
// The list of previous events to return the events after.
|
2017-07-12 08:13:10 -05:00
|
|
|
PrevEventIDs []string `json:"prev_event_ids"`
|
2017-05-30 11:44:31 -05:00
|
|
|
// The state key tuples to fetch from the state
|
2017-07-12 08:13:10 -05:00
|
|
|
StateToFetch []gomatrixserverlib.StateKeyTuple `json:"state_to_fetch"`
|
2017-05-30 11:44:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStateAfterEventsResponse is a response to QueryStateAfterEvents
|
|
|
|
type QueryStateAfterEventsResponse struct {
|
|
|
|
// Copy of the request for debugging.
|
|
|
|
QueryStateAfterEventsRequest
|
|
|
|
// Does the room exist on this roomserver?
|
|
|
|
// If the room doesn't exist this will be false and StateEvents will be empty.
|
2017-07-12 08:13:10 -05:00
|
|
|
RoomExists bool `json:"room_exists"`
|
2017-05-30 11:44:31 -05:00
|
|
|
// Do all the previous events exist on this roomserver?
|
|
|
|
// If some of previous events do not exist this will be false and StateEvents will be empty.
|
2017-07-12 08:13:10 -05:00
|
|
|
PrevEventsExist bool `json:"prev_events_exist"`
|
2017-05-30 11:44:31 -05:00
|
|
|
// The state events requested.
|
2017-06-02 08:32:36 -05:00
|
|
|
// This list will be in an arbitrary order.
|
2017-07-12 08:13:10 -05:00
|
|
|
StateEvents []gomatrixserverlib.Event `json:"state_events"`
|
2017-05-30 11:44:31 -05:00
|
|
|
}
|
|
|
|
|
2017-06-02 08:32:36 -05:00
|
|
|
// QueryEventsByIDRequest is a request to QueryEventsByID
|
|
|
|
type QueryEventsByIDRequest struct {
|
|
|
|
// The event IDs to look up.
|
2017-07-12 08:13:10 -05:00
|
|
|
EventIDs []string `json:"event_ids"`
|
2017-06-02 08:32:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryEventsByIDResponse is a response to QueryEventsByID
|
|
|
|
type QueryEventsByIDResponse struct {
|
|
|
|
// Copy of the request for debugging.
|
|
|
|
QueryEventsByIDRequest
|
|
|
|
// A list of events with the requested IDs.
|
|
|
|
// If the roomserver does not have a copy of a requested event
|
|
|
|
// then it will omit that event from the list.
|
|
|
|
// If the roomserver thinks it has a copy of the event, but
|
|
|
|
// fails to read it from the database then it will fail
|
|
|
|
// the entire request.
|
|
|
|
// This list will be in an arbitrary order.
|
2017-07-12 08:13:10 -05:00
|
|
|
Events []gomatrixserverlib.Event `json:"events"`
|
2017-06-02 08:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-08-21 10:34:26 -05:00
|
|
|
// QueryMembershipsForRoomRequest is a request to QueryMembershipsForRoom
|
|
|
|
type QueryMembershipsForRoomRequest struct {
|
2017-08-24 10:00:14 -05:00
|
|
|
// If true, only returns the membership events of "join" membership
|
|
|
|
JoinedOnly bool `json:"joined_only"`
|
2017-08-21 10:34:26 -05:00
|
|
|
// ID of the room to fetch memberships from
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
// ID of the user sending the request
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryMembershipsForRoomResponse is a response to QueryMembershipsForRoom
|
|
|
|
type QueryMembershipsForRoomResponse struct {
|
|
|
|
// The "m.room.member" events (of "join" membership) in the client format
|
|
|
|
JoinEvents []gomatrixserverlib.ClientEvent `json:"join_events"`
|
|
|
|
// True if the user has been in room before and has either stayed in it or
|
|
|
|
// left it.
|
|
|
|
HasBeenInRoom bool `json:"has_been_in_room"`
|
|
|
|
}
|
|
|
|
|
2017-08-23 09:08:48 -05:00
|
|
|
// QueryInvitesForUserRequest is a request to QueryInvitesForUser
|
|
|
|
type QueryInvitesForUserRequest struct {
|
|
|
|
// The room ID to look up invites in.
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
// The User ID to look up invites for.
|
|
|
|
TargetUserID string `json:"target_user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryInvitesForUserResponse is a response to QueryInvitesForUser
|
|
|
|
// This is used when accepting an invite or rejecting a invite to tell which
|
|
|
|
// remote matrix servers to contact.
|
|
|
|
type QueryInvitesForUserResponse struct {
|
|
|
|
// A list of matrix user IDs for each sender of an active invite targeting
|
|
|
|
// the requested user ID.
|
|
|
|
InviteSenderUserIDs []string `json:"invite_sender_user_ids"`
|
|
|
|
}
|
|
|
|
|
2017-09-06 06:38:22 -05:00
|
|
|
// QueryServerAllowedToSeeEventRequest is a request to QueryServerAllowedToSeeEvent
|
|
|
|
type QueryServerAllowedToSeeEventRequest struct {
|
|
|
|
// The event ID to look up invites in.
|
|
|
|
EventID string `json:"event_id"`
|
|
|
|
// The server interested in the event
|
|
|
|
ServerName gomatrixserverlib.ServerName `json:"server_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryServerAllowedToSeeEventResponse is a response to QueryServerAllowedToSeeEvent
|
|
|
|
type QueryServerAllowedToSeeEventResponse struct {
|
|
|
|
// Wether the server in question is allowed to see the event
|
|
|
|
AllowedToSeeEvent bool `json:"can_see_event"`
|
|
|
|
}
|
|
|
|
|
2017-03-06 08:29:39 -06:00
|
|
|
// RoomserverQueryAPI is used to query information from the room server.
|
|
|
|
type RoomserverQueryAPI interface {
|
|
|
|
// Query the latest events and state for a room from the room server.
|
|
|
|
QueryLatestEventsAndState(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-03-06 08:29:39 -06:00
|
|
|
request *QueryLatestEventsAndStateRequest,
|
|
|
|
response *QueryLatestEventsAndStateResponse,
|
|
|
|
) error
|
2017-05-30 11:44:31 -05:00
|
|
|
|
|
|
|
// Query the state after a list of events in a room from the room server.
|
|
|
|
QueryStateAfterEvents(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-05-30 11:44:31 -05:00
|
|
|
request *QueryStateAfterEventsRequest,
|
|
|
|
response *QueryStateAfterEventsResponse,
|
|
|
|
) error
|
2017-06-02 08:32:36 -05:00
|
|
|
|
|
|
|
// Query a list of events by event ID.
|
|
|
|
QueryEventsByID(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-06-02 08:32:36 -05:00
|
|
|
request *QueryEventsByIDRequest,
|
|
|
|
response *QueryEventsByIDResponse,
|
|
|
|
) error
|
2017-08-21 10:34:26 -05:00
|
|
|
|
|
|
|
// Query a list of membership events for a room
|
|
|
|
QueryMembershipsForRoom(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-08-21 10:34:26 -05:00
|
|
|
request *QueryMembershipsForRoomRequest,
|
|
|
|
response *QueryMembershipsForRoomResponse,
|
|
|
|
) error
|
2017-08-23 09:08:48 -05:00
|
|
|
|
|
|
|
// Query a list of invite event senders for a user in a room.
|
|
|
|
QueryInvitesForUser(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-08-23 09:08:48 -05:00
|
|
|
request *QueryInvitesForUserRequest,
|
|
|
|
response *QueryInvitesForUserResponse,
|
|
|
|
) error
|
2017-09-06 06:38:22 -05:00
|
|
|
|
|
|
|
// Query whether a server is allowed to see an event
|
|
|
|
QueryServerAllowedToSeeEvent(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-09-06 06:38:22 -05:00
|
|
|
request *QueryServerAllowedToSeeEventRequest,
|
|
|
|
response *QueryServerAllowedToSeeEventResponse,
|
|
|
|
) error
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API.
|
2017-07-12 08:13:10 -05:00
|
|
|
const RoomserverQueryLatestEventsAndStatePath = "/api/roomserver/queryLatestEventsAndState"
|
2017-03-06 08:29:39 -06:00
|
|
|
|
2017-05-30 11:44:31 -05:00
|
|
|
// RoomserverQueryStateAfterEventsPath is the HTTP path for the QueryStateAfterEvents API.
|
2017-07-12 08:13:10 -05:00
|
|
|
const RoomserverQueryStateAfterEventsPath = "/api/roomserver/queryStateAfterEvents"
|
2017-05-30 11:44:31 -05:00
|
|
|
|
2017-06-02 08:32:36 -05:00
|
|
|
// RoomserverQueryEventsByIDPath is the HTTP path for the QueryEventsByID API.
|
2017-07-12 08:13:10 -05:00
|
|
|
const RoomserverQueryEventsByIDPath = "/api/roomserver/queryEventsByID"
|
2017-06-02 08:32:36 -05:00
|
|
|
|
2017-08-21 10:34:26 -05:00
|
|
|
// RoomserverQueryMembershipsForRoomPath is the HTTP path for the QueryMembershipsForRoom API
|
|
|
|
const RoomserverQueryMembershipsForRoomPath = "/api/roomserver/queryMembershipsForRoom"
|
|
|
|
|
2017-08-23 09:08:48 -05:00
|
|
|
// RoomserverQueryInvitesForUserPath is the HTTP path for the QueryInvitesForUser API
|
|
|
|
const RoomserverQueryInvitesForUserPath = "/api/roomserver/queryInvitesForUser"
|
|
|
|
|
2017-09-06 06:38:22 -05:00
|
|
|
// RoomserverQueryServerAllowedToSeeEventPath is the HTTP path for the QueryServerAllowedToSeeEvent API
|
|
|
|
const RoomserverQueryServerAllowedToSeeEventPath = "/api/roomserver/queryServerAllowedToSeeEvent"
|
|
|
|
|
2017-03-06 08:29:39 -06:00
|
|
|
// NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API.
|
|
|
|
// If httpClient is nil then it uses the http.DefaultClient
|
|
|
|
func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI {
|
|
|
|
if httpClient == nil {
|
|
|
|
httpClient = http.DefaultClient
|
|
|
|
}
|
2017-07-13 05:41:30 -05:00
|
|
|
return &httpRoomserverQueryAPI{roomserverURL, httpClient}
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpRoomserverQueryAPI struct {
|
|
|
|
roomserverURL string
|
2017-07-13 05:41:30 -05:00
|
|
|
httpClient *http.Client
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryLatestEventsAndState implements RoomserverQueryAPI
|
|
|
|
func (h *httpRoomserverQueryAPI) QueryLatestEventsAndState(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-03-06 08:29:39 -06:00
|
|
|
request *QueryLatestEventsAndStateRequest,
|
|
|
|
response *QueryLatestEventsAndStateResponse,
|
|
|
|
) error {
|
|
|
|
apiURL := h.roomserverURL + RoomserverQueryLatestEventsAndStatePath
|
2017-09-13 07:37:50 -05:00
|
|
|
return postJSON(ctx, h.httpClient, apiURL, request, response)
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:44:31 -05:00
|
|
|
// QueryStateAfterEvents implements RoomserverQueryAPI
|
|
|
|
func (h *httpRoomserverQueryAPI) QueryStateAfterEvents(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-05-30 11:44:31 -05:00
|
|
|
request *QueryStateAfterEventsRequest,
|
|
|
|
response *QueryStateAfterEventsResponse,
|
|
|
|
) error {
|
|
|
|
apiURL := h.roomserverURL + RoomserverQueryStateAfterEventsPath
|
2017-09-13 07:37:50 -05:00
|
|
|
return postJSON(ctx, h.httpClient, apiURL, request, response)
|
2017-05-30 11:44:31 -05:00
|
|
|
}
|
|
|
|
|
2017-06-02 08:32:36 -05:00
|
|
|
// QueryEventsByID implements RoomserverQueryAPI
|
|
|
|
func (h *httpRoomserverQueryAPI) QueryEventsByID(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-06-02 08:32:36 -05:00
|
|
|
request *QueryEventsByIDRequest,
|
|
|
|
response *QueryEventsByIDResponse,
|
|
|
|
) error {
|
|
|
|
apiURL := h.roomserverURL + RoomserverQueryEventsByIDPath
|
2017-09-13 07:37:50 -05:00
|
|
|
return postJSON(ctx, h.httpClient, apiURL, request, response)
|
2017-06-02 08:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-08-21 10:34:26 -05:00
|
|
|
// QueryMembershipsForRoom implements RoomserverQueryAPI
|
|
|
|
func (h *httpRoomserverQueryAPI) QueryMembershipsForRoom(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-08-21 10:34:26 -05:00
|
|
|
request *QueryMembershipsForRoomRequest,
|
|
|
|
response *QueryMembershipsForRoomResponse,
|
|
|
|
) error {
|
|
|
|
apiURL := h.roomserverURL + RoomserverQueryMembershipsForRoomPath
|
2017-09-13 07:37:50 -05:00
|
|
|
return postJSON(ctx, h.httpClient, apiURL, request, response)
|
2017-08-21 10:34:26 -05:00
|
|
|
}
|
|
|
|
|
2017-08-23 09:08:48 -05:00
|
|
|
// QueryInvitesForUser implements RoomserverQueryAPI
|
|
|
|
func (h *httpRoomserverQueryAPI) QueryInvitesForUser(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-08-23 09:08:48 -05:00
|
|
|
request *QueryInvitesForUserRequest,
|
|
|
|
response *QueryInvitesForUserResponse,
|
|
|
|
) error {
|
|
|
|
apiURL := h.roomserverURL + RoomserverQueryInvitesForUserPath
|
2017-09-13 07:37:50 -05:00
|
|
|
return postJSON(ctx, h.httpClient, apiURL, request, response)
|
2017-08-23 09:08:48 -05:00
|
|
|
}
|
|
|
|
|
2017-09-06 06:38:22 -05:00
|
|
|
// QueryServerAllowedToSeeEvent implements RoomserverQueryAPI
|
|
|
|
func (h *httpRoomserverQueryAPI) QueryServerAllowedToSeeEvent(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-09-06 06:38:22 -05:00
|
|
|
request *QueryServerAllowedToSeeEventRequest,
|
|
|
|
response *QueryServerAllowedToSeeEventResponse,
|
2017-09-20 04:59:19 -05:00
|
|
|
) (err error) {
|
2017-09-06 06:38:22 -05:00
|
|
|
apiURL := h.roomserverURL + RoomserverQueryServerAllowedToSeeEventPath
|
2017-09-13 07:37:50 -05:00
|
|
|
return postJSON(ctx, h.httpClient, apiURL, request, response)
|
2017-09-06 06:38:22 -05:00
|
|
|
}
|
|
|
|
|
2017-09-13 07:37:50 -05:00
|
|
|
func postJSON(
|
|
|
|
ctx context.Context, httpClient *http.Client,
|
|
|
|
apiURL string, request, response interface{},
|
|
|
|
) error {
|
2017-03-06 08:29:39 -06:00
|
|
|
jsonBytes, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-13 07:37:50 -05:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", apiURL, bytes.NewReader(jsonBytes))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
res, err := httpClient.Do(req.WithContext(ctx))
|
2017-03-06 08:29:39 -06:00
|
|
|
if res != nil {
|
2017-09-20 04:59:19 -05:00
|
|
|
defer (func() { err = res.Body.Close() })()
|
2017-03-06 08:29:39 -06:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
var errorBody struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
if err = json.NewDecoder(res.Body).Decode(&errorBody); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("api: %d: %s", res.StatusCode, errorBody.Message)
|
|
|
|
}
|
|
|
|
return json.NewDecoder(res.Body).Decode(response)
|
|
|
|
}
|