2017-07-28 12:31:43 +02: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 api
|
|
|
|
|
|
|
|
import (
|
2017-09-13 14:37:50 +02:00
|
|
|
"context"
|
2020-04-03 12:40:50 +02:00
|
|
|
"errors"
|
2017-07-28 12:31:43 +02:00
|
|
|
"net/http"
|
2017-09-28 15:50:40 +02:00
|
|
|
|
2018-07-17 16:36:04 +02:00
|
|
|
commonHTTP "github.com/matrix-org/dendrite/common/http"
|
2017-09-28 15:50:40 +02:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-07-28 12:31:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetRoomAliasRequest is a request to SetRoomAlias
|
|
|
|
type SetRoomAliasRequest struct {
|
|
|
|
// ID of the user setting the alias
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
// New alias for the room
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
// The room ID the alias is referring to
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRoomAliasResponse is a response to SetRoomAlias
|
|
|
|
type SetRoomAliasResponse struct {
|
|
|
|
// Does the alias already refer to a room?
|
|
|
|
AliasExists bool `json:"alias_exists"`
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// GetRoomIDForAliasRequest is a request to GetRoomIDForAlias
|
|
|
|
type GetRoomIDForAliasRequest struct {
|
2017-07-28 12:31:43 +02:00
|
|
|
// Alias we want to lookup
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// GetRoomIDForAliasResponse is a response to GetRoomIDForAlias
|
|
|
|
type GetRoomIDForAliasResponse struct {
|
2017-07-28 12:31:43 +02:00
|
|
|
// The room ID the alias refers to
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// GetAliasesForRoomIDRequest is a request to GetAliasesForRoomID
|
|
|
|
type GetAliasesForRoomIDRequest struct {
|
|
|
|
// The room ID we want to find aliases for
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAliasesForRoomIDResponse is a response to GetAliasesForRoomID
|
|
|
|
type GetAliasesForRoomIDResponse struct {
|
|
|
|
// The aliases the alias refers to
|
|
|
|
Aliases []string `json:"aliases"`
|
|
|
|
}
|
|
|
|
|
2019-08-07 05:00:58 +02:00
|
|
|
// GetCreatorIDForAliasRequest is a request to GetCreatorIDForAlias
|
|
|
|
type GetCreatorIDForAliasRequest struct {
|
|
|
|
// The alias we want to find the creator of
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCreatorIDForAliasResponse is a response to GetCreatorIDForAlias
|
|
|
|
type GetCreatorIDForAliasResponse struct {
|
|
|
|
// The user ID of the alias creator
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:31:43 +02:00
|
|
|
// RemoveRoomAliasRequest is a request to RemoveRoomAlias
|
|
|
|
type RemoveRoomAliasRequest struct {
|
|
|
|
// ID of the user removing the alias
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
// The room alias to remove
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRoomAliasResponse is a response to RemoveRoomAlias
|
|
|
|
type RemoveRoomAliasResponse struct{}
|
|
|
|
|
|
|
|
// RoomserverAliasAPI is used to save, lookup or remove a room alias
|
|
|
|
type RoomserverAliasAPI interface {
|
|
|
|
// Set a room alias
|
|
|
|
SetRoomAlias(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-07-28 12:31:43 +02:00
|
|
|
req *SetRoomAliasRequest,
|
|
|
|
response *SetRoomAliasResponse,
|
|
|
|
) error
|
|
|
|
|
|
|
|
// Get the room ID for an alias
|
2018-05-30 14:43:13 +02:00
|
|
|
GetRoomIDForAlias(
|
|
|
|
ctx context.Context,
|
|
|
|
req *GetRoomIDForAliasRequest,
|
|
|
|
response *GetRoomIDForAliasResponse,
|
|
|
|
) error
|
|
|
|
|
|
|
|
// Get all known aliases for a room ID
|
|
|
|
GetAliasesForRoomID(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2018-05-30 14:43:13 +02:00
|
|
|
req *GetAliasesForRoomIDRequest,
|
|
|
|
response *GetAliasesForRoomIDResponse,
|
2017-07-28 12:31:43 +02:00
|
|
|
) error
|
|
|
|
|
2019-08-07 05:00:58 +02:00
|
|
|
// Get the user ID of the creator of an alias
|
|
|
|
GetCreatorIDForAlias(
|
|
|
|
ctx context.Context,
|
|
|
|
req *GetCreatorIDForAliasRequest,
|
|
|
|
response *GetCreatorIDForAliasResponse,
|
|
|
|
) error
|
|
|
|
|
2017-07-28 12:31:43 +02:00
|
|
|
// Remove a room alias
|
|
|
|
RemoveRoomAlias(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-07-28 12:31:43 +02:00
|
|
|
req *RemoveRoomAliasRequest,
|
|
|
|
response *RemoveRoomAliasResponse,
|
|
|
|
) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoomserverSetRoomAliasPath is the HTTP path for the SetRoomAlias API.
|
|
|
|
const RoomserverSetRoomAliasPath = "/api/roomserver/setRoomAlias"
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// RoomserverGetRoomIDForAliasPath is the HTTP path for the GetRoomIDForAlias API.
|
|
|
|
const RoomserverGetRoomIDForAliasPath = "/api/roomserver/GetRoomIDForAlias"
|
|
|
|
|
|
|
|
// RoomserverGetAliasesForRoomIDPath is the HTTP path for the GetAliasesForRoomID API.
|
|
|
|
const RoomserverGetAliasesForRoomIDPath = "/api/roomserver/GetAliasesForRoomID"
|
2017-07-28 12:31:43 +02:00
|
|
|
|
2019-08-07 05:00:58 +02:00
|
|
|
// RoomserverGetCreatorIDForAliasPath is the HTTP path for the GetCreatorIDForAlias API.
|
|
|
|
const RoomserverGetCreatorIDForAliasPath = "/api/roomserver/GetCreatorIDForAlias"
|
|
|
|
|
2017-07-28 12:31:43 +02:00
|
|
|
// RoomserverRemoveRoomAliasPath is the HTTP path for the RemoveRoomAlias API.
|
|
|
|
const RoomserverRemoveRoomAliasPath = "/api/roomserver/removeRoomAlias"
|
|
|
|
|
|
|
|
// NewRoomserverAliasAPIHTTP creates a RoomserverAliasAPI implemented by talking to a HTTP POST API.
|
2020-04-03 12:40:50 +02:00
|
|
|
// If httpClient is nil an error is returned
|
|
|
|
func NewRoomserverAliasAPIHTTP(roomserverURL string, httpClient *http.Client) (RoomserverAliasAPI, error) {
|
2017-07-28 12:31:43 +02:00
|
|
|
if httpClient == nil {
|
2020-04-03 12:40:50 +02:00
|
|
|
return nil, errors.New("NewRoomserverAliasAPIHTTP: httpClient is <nil>")
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|
2020-04-03 12:40:50 +02:00
|
|
|
return &httpRoomserverAliasAPI{roomserverURL, httpClient}, nil
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpRoomserverAliasAPI struct {
|
|
|
|
roomserverURL string
|
|
|
|
httpClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRoomAlias implements RoomserverAliasAPI
|
|
|
|
func (h *httpRoomserverAliasAPI) SetRoomAlias(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-07-28 12:31:43 +02:00
|
|
|
request *SetRoomAliasRequest,
|
|
|
|
response *SetRoomAliasResponse,
|
|
|
|
) error {
|
2017-09-28 15:50:40 +02:00
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "SetRoomAlias")
|
|
|
|
defer span.Finish()
|
|
|
|
|
2017-07-28 12:31:43 +02:00
|
|
|
apiURL := h.roomserverURL + RoomserverSetRoomAliasPath
|
2018-07-17 16:36:04 +02:00
|
|
|
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
// GetRoomIDForAlias implements RoomserverAliasAPI
|
|
|
|
func (h *httpRoomserverAliasAPI) GetRoomIDForAlias(
|
|
|
|
ctx context.Context,
|
|
|
|
request *GetRoomIDForAliasRequest,
|
|
|
|
response *GetRoomIDForAliasResponse,
|
|
|
|
) error {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "GetRoomIDForAlias")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
apiURL := h.roomserverURL + RoomserverGetRoomIDForAliasPath
|
2018-07-17 16:36:04 +02:00
|
|
|
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
2018-05-30 14:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAliasesForRoomID implements RoomserverAliasAPI
|
|
|
|
func (h *httpRoomserverAliasAPI) GetAliasesForRoomID(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2018-05-30 14:43:13 +02:00
|
|
|
request *GetAliasesForRoomIDRequest,
|
|
|
|
response *GetAliasesForRoomIDResponse,
|
2017-07-28 12:31:43 +02:00
|
|
|
) error {
|
2018-05-30 14:43:13 +02:00
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "GetAliasesForRoomID")
|
2017-09-28 15:50:40 +02:00
|
|
|
defer span.Finish()
|
|
|
|
|
2018-05-30 14:43:13 +02:00
|
|
|
apiURL := h.roomserverURL + RoomserverGetAliasesForRoomIDPath
|
2018-07-17 16:36:04 +02:00
|
|
|
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 05:00:58 +02:00
|
|
|
// GetCreatorIDForAlias implements RoomserverAliasAPI
|
|
|
|
func (h *httpRoomserverAliasAPI) GetCreatorIDForAlias(
|
|
|
|
ctx context.Context,
|
|
|
|
request *GetCreatorIDForAliasRequest,
|
|
|
|
response *GetCreatorIDForAliasResponse,
|
|
|
|
) error {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "GetCreatorIDForAlias")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
apiURL := h.roomserverURL + RoomserverGetCreatorIDForAliasPath
|
|
|
|
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:37:50 +02:00
|
|
|
// RemoveRoomAlias implements RoomserverAliasAPI
|
2017-07-28 12:31:43 +02:00
|
|
|
func (h *httpRoomserverAliasAPI) RemoveRoomAlias(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-07-28 12:31:43 +02:00
|
|
|
request *RemoveRoomAliasRequest,
|
|
|
|
response *RemoveRoomAliasResponse,
|
|
|
|
) error {
|
2017-09-28 15:50:40 +02:00
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "RemoveRoomAlias")
|
|
|
|
defer span.Finish()
|
|
|
|
|
2017-07-28 12:31:43 +02:00
|
|
|
apiURL := h.roomserverURL + RoomserverRemoveRoomAliasPath
|
2018-07-17 16:36:04 +02:00
|
|
|
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
2017-07-28 12:31:43 +02:00
|
|
|
}
|