2020-05-04 14:53:47 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-06-25 16:04:48 +02:00
|
|
|
"encoding/json"
|
2020-06-24 16:06:14 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2020-05-04 14:53:47 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-06-11 20:50:40 +02:00
|
|
|
"github.com/matrix-org/util"
|
2022-08-19 11:04:26 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-05-04 14:53:47 +02:00
|
|
|
)
|
|
|
|
|
2020-06-24 16:06:14 +02:00
|
|
|
type PerformErrorCode int
|
|
|
|
|
|
|
|
type PerformError struct {
|
2020-06-25 16:04:48 +02:00
|
|
|
Msg string
|
|
|
|
RemoteCode int // remote HTTP status code, for PerformErrRemote
|
|
|
|
Code PerformErrorCode
|
2020-06-24 16:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PerformError) Error() string {
|
|
|
|
return fmt.Sprintf("%d : %s", p.Code, p.Msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONResponse maps error codes to suitable HTTP error codes, defaulting to 500.
|
|
|
|
func (p *PerformError) JSONResponse() util.JSONResponse {
|
|
|
|
switch p.Code {
|
|
|
|
case PerformErrorBadRequest:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.Unknown(p.Msg),
|
|
|
|
}
|
|
|
|
case PerformErrorNoRoom:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.NotFound(p.Msg),
|
|
|
|
}
|
|
|
|
case PerformErrorNotAllowed:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: jsonerror.Forbidden(p.Msg),
|
|
|
|
}
|
2020-06-24 19:19:14 +02:00
|
|
|
case PerformErrorNoOperation:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: jsonerror.Forbidden(p.Msg),
|
|
|
|
}
|
2020-06-25 16:04:48 +02:00
|
|
|
case PerformErrRemote:
|
|
|
|
// if the code is 0 then something bad happened and it isn't
|
|
|
|
// a remote HTTP error being encapsulated, e.g network error to remote.
|
|
|
|
if p.RemoteCode == 0 {
|
|
|
|
return util.ErrorResponse(fmt.Errorf("%s", p.Msg))
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: p.RemoteCode,
|
|
|
|
// TODO: Should we assert this is in fact JSON? E.g gjson parse?
|
|
|
|
JSON: json.RawMessage(p.Msg),
|
|
|
|
}
|
2020-06-24 16:06:14 +02:00
|
|
|
default:
|
|
|
|
return util.ErrorResponse(p)
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 10:59:59 +02:00
|
|
|
|
|
|
|
const (
|
2020-06-24 16:06:14 +02:00
|
|
|
// PerformErrorNotAllowed means the user is not allowed to invite/join/etc this room (e.g join_rule:invite or banned)
|
|
|
|
PerformErrorNotAllowed PerformErrorCode = 1
|
|
|
|
// PerformErrorBadRequest means the request was wrong in some way (invalid user ID, wrong server, etc)
|
|
|
|
PerformErrorBadRequest PerformErrorCode = 2
|
|
|
|
// PerformErrorNoRoom means that the room being joined doesn't exist.
|
|
|
|
PerformErrorNoRoom PerformErrorCode = 3
|
|
|
|
// PerformErrorNoOperation means that the request resulted in nothing happening e.g invite->invite or leave->leave.
|
|
|
|
PerformErrorNoOperation PerformErrorCode = 4
|
2020-06-25 16:04:48 +02:00
|
|
|
// PerformErrRemote means that the request failed and the PerformError.Msg is the raw remote JSON error response
|
|
|
|
PerformErrRemote PerformErrorCode = 5
|
2020-06-24 10:59:59 +02:00
|
|
|
)
|
|
|
|
|
2020-05-04 14:53:47 +02:00
|
|
|
type PerformJoinRequest struct {
|
|
|
|
RoomIDOrAlias string `json:"room_id_or_alias"`
|
|
|
|
UserID string `json:"user_id"`
|
2022-12-22 13:05:59 +01:00
|
|
|
IsGuest bool `json:"is_guest"`
|
2020-05-04 14:53:47 +02:00
|
|
|
Content map[string]interface{} `json:"content"`
|
|
|
|
ServerNames []gomatrixserverlib.ServerName `json:"server_names"`
|
2022-10-07 16:00:12 +02:00
|
|
|
Unsigned map[string]interface{} `json:"unsigned"`
|
2020-05-04 14:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformJoinResponse struct {
|
2020-06-24 10:59:59 +02:00
|
|
|
// The room ID, populated on success.
|
2020-11-19 12:34:59 +01:00
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
JoinedVia gomatrixserverlib.ServerName
|
2020-06-24 16:06:14 +02:00
|
|
|
// If non-nil, the join request failed. Contains more information why it failed.
|
|
|
|
Error *PerformError
|
2020-05-04 14:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformLeaveRequest struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformLeaveResponse struct {
|
2022-02-18 16:05:03 +01:00
|
|
|
Code int `json:"code,omitempty"`
|
|
|
|
Message interface{} `json:"message,omitempty"`
|
2020-05-04 14:53:47 +02:00
|
|
|
}
|
2020-06-11 20:50:40 +02:00
|
|
|
|
2020-06-24 16:06:14 +02:00
|
|
|
type PerformInviteRequest struct {
|
|
|
|
RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"`
|
2020-11-16 16:44:53 +01:00
|
|
|
Event *gomatrixserverlib.HeaderedEvent `json:"event"`
|
2020-06-24 16:06:14 +02:00
|
|
|
InviteRoomState []gomatrixserverlib.InviteV2StrippedState `json:"invite_room_state"`
|
|
|
|
SendAsServer string `json:"send_as_server"`
|
|
|
|
TransactionID *TransactionID `json:"transaction_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformInviteResponse struct {
|
|
|
|
Error *PerformError
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:39:18 +02:00
|
|
|
type PerformPeekRequest struct {
|
|
|
|
RoomIDOrAlias string `json:"room_id_or_alias"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DeviceID string `json:"device_id"`
|
|
|
|
ServerNames []gomatrixserverlib.ServerName `json:"server_names"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformPeekResponse struct {
|
|
|
|
// The room ID, populated on success.
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
// If non-nil, the join request failed. Contains more information why it failed.
|
|
|
|
Error *PerformError
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:11:46 +01:00
|
|
|
type PerformUnpeekRequest struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DeviceID string `json:"device_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformUnpeekResponse struct {
|
|
|
|
// If non-nil, the join request failed. Contains more information why it failed.
|
|
|
|
Error *PerformError
|
|
|
|
}
|
|
|
|
|
2020-06-11 20:50:40 +02:00
|
|
|
// PerformBackfillRequest is a request to PerformBackfill.
|
|
|
|
type PerformBackfillRequest struct {
|
|
|
|
// The room to backfill
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
// A map of backwards extremity event ID to a list of its prev_event IDs.
|
|
|
|
BackwardsExtremities map[string][]string `json:"backwards_extremities"`
|
|
|
|
// The maximum number of events to retrieve.
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
// The server interested in the events.
|
|
|
|
ServerName gomatrixserverlib.ServerName `json:"server_name"`
|
2022-11-15 16:05:23 +01:00
|
|
|
// Which virtual host are we doing this for?
|
|
|
|
VirtualHost gomatrixserverlib.ServerName `json:"virtual_host"`
|
2020-06-11 20:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrevEventIDs returns the prev_event IDs of all backwards extremities, de-duplicated in a lexicographically sorted order.
|
|
|
|
func (r *PerformBackfillRequest) PrevEventIDs() []string {
|
|
|
|
var prevEventIDs []string
|
|
|
|
for _, pes := range r.BackwardsExtremities {
|
|
|
|
prevEventIDs = append(prevEventIDs, pes...)
|
|
|
|
}
|
|
|
|
prevEventIDs = util.UniqueStrings(prevEventIDs)
|
|
|
|
return prevEventIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
// PerformBackfillResponse is a response to PerformBackfill.
|
|
|
|
type PerformBackfillResponse struct {
|
|
|
|
// Missing events, arbritrary order.
|
2022-08-19 11:04:26 +02:00
|
|
|
Events []*gomatrixserverlib.HeaderedEvent `json:"events"`
|
|
|
|
HistoryVisibility gomatrixserverlib.HistoryVisibility `json:"history_visibility"`
|
2020-06-11 20:50:40 +02:00
|
|
|
}
|
2020-07-02 16:41:18 +02:00
|
|
|
|
|
|
|
type PerformPublishRequest struct {
|
2022-10-27 14:40:35 +02:00
|
|
|
RoomID string
|
|
|
|
Visibility string
|
|
|
|
AppserviceID string
|
|
|
|
NetworkID string
|
2020-07-02 16:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformPublishResponse struct {
|
|
|
|
// If non-nil, the publish request failed. Contains more information why it failed.
|
|
|
|
Error *PerformError
|
|
|
|
}
|
2020-11-05 11:19:23 +01:00
|
|
|
|
2021-01-22 15:55:08 +01:00
|
|
|
type PerformInboundPeekRequest struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
PeekID string `json:"peek_id"`
|
|
|
|
ServerName gomatrixserverlib.ServerName `json:"server_name"`
|
|
|
|
RenewalInterval int64 `json:"renewal_interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformInboundPeekResponse struct {
|
|
|
|
// Does the room exist on this roomserver?
|
|
|
|
// If the room doesn't exist this will be false and StateEvents will be empty.
|
|
|
|
RoomExists bool `json:"room_exists"`
|
|
|
|
// The room version of the room.
|
|
|
|
RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"`
|
|
|
|
// The current state and auth chain events.
|
|
|
|
// The lists will be in an arbitrary order.
|
|
|
|
StateEvents []*gomatrixserverlib.HeaderedEvent `json:"state_events"`
|
|
|
|
AuthChainEvents []*gomatrixserverlib.HeaderedEvent `json:"auth_chain_events"`
|
|
|
|
// The event at which this state was captured
|
|
|
|
LatestEvent *gomatrixserverlib.HeaderedEvent `json:"latest_event"`
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:19:23 +01:00
|
|
|
// PerformForgetRequest is a request to PerformForget
|
|
|
|
type PerformForgetRequest struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformForgetResponse struct{}
|
2022-04-05 11:04:08 +02:00
|
|
|
|
|
|
|
type PerformRoomUpgradeRequest struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformRoomUpgradeResponse struct {
|
|
|
|
NewRoomID string
|
|
|
|
Error *PerformError
|
|
|
|
}
|
2022-04-28 17:02:30 +02:00
|
|
|
|
|
|
|
type PerformAdminEvacuateRoomRequest struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformAdminEvacuateRoomResponse struct {
|
|
|
|
Affected []string `json:"affected"`
|
|
|
|
Error *PerformError
|
|
|
|
}
|
2022-06-29 16:29:39 +02:00
|
|
|
|
|
|
|
type PerformAdminEvacuateUserRequest struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformAdminEvacuateUserResponse struct {
|
|
|
|
Affected []string `json:"affected"`
|
|
|
|
Error *PerformError
|
|
|
|
}
|
2022-10-31 10:13:28 +01:00
|
|
|
|
2023-01-19 21:02:32 +01:00
|
|
|
type PerformAdminPurgeRoomRequest struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformAdminPurgeRoomResponse struct {
|
|
|
|
Error *PerformError `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-10-31 10:13:28 +01:00
|
|
|
type PerformAdminDownloadStateRequest struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
ServerName gomatrixserverlib.ServerName `json:"server_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformAdminDownloadStateResponse struct {
|
|
|
|
Error *PerformError `json:"error,omitempty"`
|
|
|
|
}
|