mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-10 16:33:11 -06:00
Move alias API
This commit is contained in:
parent
1ed078ac85
commit
750642c45f
|
|
@ -38,7 +38,7 @@ func DirectoryRoom(
|
|||
roomAlias string,
|
||||
federation *gomatrixserverlib.FederationClient,
|
||||
cfg *config.Dendrite,
|
||||
queryAPI api.RoomserverQueryAPI,
|
||||
aliasAPI api.RoomserverAliasAPI,
|
||||
) util.JSONResponse {
|
||||
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
||||
if err != nil {
|
||||
|
|
@ -53,7 +53,7 @@ func DirectoryRoom(
|
|||
if domain == cfg.Matrix.ServerName {
|
||||
queryReq := api.GetAliasRoomIDRequest{Alias: roomAlias}
|
||||
var queryRes api.GetAliasRoomIDResponse
|
||||
if err = queryAPI.GetAliasRoomID(&queryReq, &queryRes); err != nil {
|
||||
if err = aliasAPI.GetAliasRoomID(&queryReq, &queryRes); err != nil {
|
||||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ func SetLocalAlias(
|
|||
device *authtypes.Device,
|
||||
alias string,
|
||||
cfg *config.Dendrite,
|
||||
queryAPI api.RoomserverQueryAPI,
|
||||
aliasAPI api.RoomserverAliasAPI,
|
||||
) util.JSONResponse {
|
||||
_, domain, err := gomatrixserverlib.SplitID('#', alias)
|
||||
if err != nil {
|
||||
|
|
@ -129,7 +129,7 @@ func SetLocalAlias(
|
|||
Alias: alias,
|
||||
}
|
||||
var queryRes api.SetRoomAliasResponse
|
||||
if err := queryAPI.SetRoomAlias(&queryReq, &queryRes); err != nil {
|
||||
if err := aliasAPI.SetRoomAlias(&queryReq, &queryRes); err != nil {
|
||||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
|
||||
|
|
@ -153,14 +153,14 @@ func RemoveLocalAlias(
|
|||
device *authtypes.Device,
|
||||
alias string,
|
||||
cfg *config.Dendrite,
|
||||
queryAPI api.RoomserverQueryAPI,
|
||||
aliasAPI api.RoomserverAliasAPI,
|
||||
) util.JSONResponse {
|
||||
queryReq := api.RemoveRoomAliasRequest{
|
||||
Alias: alias,
|
||||
UserID: device.UserID,
|
||||
}
|
||||
var queryRes api.RemoveRoomAliasResponse
|
||||
if err := queryAPI.RemoveRoomAlias(&queryReq, &queryRes); err != nil {
|
||||
if err := aliasAPI.RemoveRoomAlias(&queryReq, &queryRes); err != nil {
|
||||
return httputil.LogThenError(req, err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ const pathPrefixUnstable = "/_matrix/client/unstable"
|
|||
func Setup(
|
||||
servMux *http.ServeMux, httpClient *http.Client, cfg config.Dendrite,
|
||||
producer *producers.RoomserverProducer, queryAPI api.RoomserverQueryAPI,
|
||||
aliasAPI api.RoomserverAliasAPI,
|
||||
accountDB *accounts.Database,
|
||||
deviceDB *devices.Database,
|
||||
federation *gomatrixserverlib.FederationClient,
|
||||
|
|
@ -109,21 +110,21 @@ func Setup(
|
|||
r0mux.Handle("/directory/room/{roomAlias}",
|
||||
common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||
vars := mux.Vars(req)
|
||||
return readers.DirectoryRoom(req, device, vars["roomAlias"], federation, &cfg, queryAPI)
|
||||
return readers.DirectoryRoom(req, device, vars["roomAlias"], federation, &cfg, aliasAPI)
|
||||
}),
|
||||
).Methods("GET")
|
||||
|
||||
r0mux.Handle("/directory/room/{roomAlias}",
|
||||
common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||
vars := mux.Vars(req)
|
||||
return readers.SetLocalAlias(req, device, vars["roomAlias"], &cfg, queryAPI)
|
||||
return readers.SetLocalAlias(req, device, vars["roomAlias"], &cfg, aliasAPI)
|
||||
}),
|
||||
).Methods("PUT", "OPTIONS")
|
||||
|
||||
r0mux.Handle("/directory/room/{roomAlias}",
|
||||
common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||
vars := mux.Vars(req)
|
||||
return readers.RemoveLocalAlias(req, device, vars["roomAlias"], &cfg, queryAPI)
|
||||
return readers.RemoveLocalAlias(req, device, vars["roomAlias"], &cfg, aliasAPI)
|
||||
}),
|
||||
).Methods("DELETE")
|
||||
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ func main() {
|
|||
log.Info("config: ", cfg)
|
||||
|
||||
queryAPI := api.NewRoomserverQueryAPIHTTP(cfg.RoomServerURL(), nil)
|
||||
aliasAPI := api.NewRoomserverAliasAPIHTTP(cfg.RoomServerURL(), nil)
|
||||
|
||||
roomserverProducer := producers.NewRoomserverProducer(cfg.RoomServerURL())
|
||||
userUpdateProducer, err := producers.NewUserUpdateProducer(
|
||||
|
|
@ -97,7 +98,8 @@ func main() {
|
|||
log.Info("Starting client API server on ", cfg.Listen.ClientAPI)
|
||||
routing.Setup(
|
||||
http.DefaultServeMux, http.DefaultClient, *cfg, roomserverProducer,
|
||||
queryAPI, accountDB, deviceDB, federation, keyRing, userUpdateProducer,
|
||||
queryAPI, aliasAPI, accountDB, deviceDB, federation, keyRing,
|
||||
userUpdateProducer,
|
||||
)
|
||||
log.Fatal(http.ListenAndServe(string(cfg.Listen.ClientAPI), nil))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import (
|
|||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
"github.com/matrix-org/dendrite/common/config"
|
||||
"github.com/matrix-org/dendrite/roomserver/alias"
|
||||
"github.com/matrix-org/dendrite/roomserver/input"
|
||||
"github.com/matrix-org/dendrite/roomserver/query"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage"
|
||||
|
|
@ -66,13 +67,18 @@ func main() {
|
|||
|
||||
inputAPI.SetupHTTP(http.DefaultServeMux)
|
||||
|
||||
queryAPI := query.RoomserverQueryAPI{
|
||||
queryAPI := query.RoomserverQueryAPI{db}
|
||||
|
||||
queryAPI.SetupHTTP(http.DefaultServeMux)
|
||||
|
||||
aliasAPI := alias.RoomserverAliasAPI{
|
||||
DB: db,
|
||||
Cfg: cfg,
|
||||
InputAPI: inputAPI,
|
||||
QueryAPI: queryAPI,
|
||||
}
|
||||
|
||||
queryAPI.SetupHTTP(http.DefaultServeMux)
|
||||
aliasAPI.SetupHTTP(http.DefaultServeMux)
|
||||
|
||||
http.DefaultServeMux.Handle("/metrics", prometheus.Handler())
|
||||
|
||||
|
|
|
|||
256
src/github.com/matrix-org/dendrite/roomserver/alias/alias.go
Normal file
256
src/github.com/matrix-org/dendrite/roomserver/alias/alias.go
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
// 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 alias
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
"github.com/matrix-org/dendrite/common/config"
|
||||
"github.com/matrix-org/dendrite/roomserver/api"
|
||||
"github.com/matrix-org/dendrite/roomserver/input"
|
||||
"github.com/matrix-org/dendrite/roomserver/query"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
// RoomserverAliasAPIDatabase has the storage APIs needed to implement the alias API.
|
||||
type RoomserverAliasAPIDatabase interface {
|
||||
// Save a given room alias with the room ID it refers to.
|
||||
// Returns an error if there was a problem talking to the database.
|
||||
SetRoomAlias(alias string, roomID string) error
|
||||
// Lookup the room ID a given alias refers to.
|
||||
// Returns an error if there was a problem talking to the database.
|
||||
GetRoomIDFromAlias(alias string) (string, error)
|
||||
// Lookup all aliases referring to a given room ID.
|
||||
// Returns an error if there was a problem talking to the database.
|
||||
GetAliasesFromRoomID(roomID string) ([]string, error)
|
||||
// Remove a given room alias.
|
||||
// Returns an error if there was a problem talking to the database.
|
||||
RemoveRoomAlias(alias string) error
|
||||
}
|
||||
|
||||
// RoomserverAliasAPI is an implementation of api.RoomserverAliasAPI
|
||||
type RoomserverAliasAPI struct {
|
||||
DB RoomserverAliasAPIDatabase
|
||||
Cfg *config.Dendrite
|
||||
InputAPI input.RoomserverInputAPI
|
||||
QueryAPI query.RoomserverQueryAPI
|
||||
}
|
||||
|
||||
// SetRoomAlias implements api.RoomserverAliasAPI
|
||||
func (r *RoomserverAliasAPI) SetRoomAlias(
|
||||
request *api.SetRoomAliasRequest,
|
||||
response *api.SetRoomAliasResponse,
|
||||
) error {
|
||||
// Check if the alias isn't already referring to a room
|
||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(roomID) > 0 {
|
||||
// If the alias already exists, stop the process
|
||||
response.AliasExists = true
|
||||
return nil
|
||||
}
|
||||
response.AliasExists = false
|
||||
|
||||
// Save the new alias
|
||||
if err := r.DB.SetRoomAlias(request.Alias, request.RoomID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Send a m.room.aliases event with the updated list of aliases for this room
|
||||
if err := r.sendUpdatedAliasesEvent(request.UserID, request.RoomID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAliasRoomID implements api.RoomserverAliasAPI
|
||||
func (r *RoomserverAliasAPI) GetAliasRoomID(
|
||||
request *api.GetAliasRoomIDRequest,
|
||||
response *api.GetAliasRoomIDResponse,
|
||||
) error {
|
||||
// Lookup the room ID in the database
|
||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
response.RoomID = roomID
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveRoomAlias implements api.RoomserverAliasAPI
|
||||
func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
||||
request *api.RemoveRoomAliasRequest,
|
||||
response *api.RemoveRoomAliasResponse,
|
||||
) error {
|
||||
// Lookup the room ID in the database
|
||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove the dalias from the database
|
||||
if err := r.DB.RemoveRoomAlias(request.Alias); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Send an updated m.room.aliases event
|
||||
if err := r.sendUpdatedAliasesEvent(request.UserID, roomID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type roomAliasesContent struct {
|
||||
Aliases []string `json:"aliases"`
|
||||
}
|
||||
|
||||
// Build the updated m.room.aliases event to send to the room after addition or
|
||||
// removal of an alias
|
||||
func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(userID string, roomID string) error {
|
||||
serverName := string(r.Cfg.Matrix.ServerName)
|
||||
|
||||
builder := gomatrixserverlib.EventBuilder{
|
||||
Sender: userID,
|
||||
RoomID: roomID,
|
||||
Type: "m.room.aliases",
|
||||
StateKey: &serverName,
|
||||
}
|
||||
|
||||
// Retrieve the updated list of aliases, marhal it and set it as the
|
||||
// event's content
|
||||
aliases, err := r.DB.GetAliasesFromRoomID(roomID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content := roomAliasesContent{Aliases: aliases}
|
||||
rawContent, err := json.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = builder.SetContent(json.RawMessage(rawContent))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get needed state events and depth
|
||||
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(&builder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req := api.QueryLatestEventsAndStateRequest{
|
||||
RoomID: roomID,
|
||||
StateToFetch: eventsNeeded.Tuples(),
|
||||
}
|
||||
var res api.QueryLatestEventsAndStateResponse
|
||||
if err = r.QueryAPI.QueryLatestEventsAndState(&req, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
builder.Depth = res.Depth
|
||||
builder.PrevEvents = res.LatestEvents
|
||||
|
||||
// Add auth events
|
||||
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
||||
for i := range res.StateEvents {
|
||||
authEvents.AddEvent(&res.StateEvents[i])
|
||||
}
|
||||
refs, err := eventsNeeded.AuthEventReferences(&authEvents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
builder.AuthEvents = refs
|
||||
|
||||
// Build the event
|
||||
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), r.Cfg.Matrix.ServerName)
|
||||
now := time.Now()
|
||||
event, err := builder.Build(eventID, now, r.Cfg.Matrix.ServerName, r.Cfg.Matrix.KeyID, r.Cfg.Matrix.PrivateKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create the request
|
||||
ire := api.InputRoomEvent{
|
||||
Kind: api.KindNew,
|
||||
Event: event,
|
||||
AuthEventIDs: event.AuthEventIDs(),
|
||||
SendAsServer: serverName,
|
||||
}
|
||||
inputReq := api.InputRoomEventsRequest{
|
||||
InputRoomEvents: []api.InputRoomEvent{ire},
|
||||
}
|
||||
var inputRes api.InputRoomEventsResponse
|
||||
|
||||
// Send the request
|
||||
if err := r.InputAPI.InputRoomEvents(&inputReq, &inputRes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetupHTTP adds the RoomserverAliasAPI handlers to the http.ServeMux.
|
||||
func (r *RoomserverAliasAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||
servMux.Handle(
|
||||
api.RoomserverSetRoomAliasPath,
|
||||
common.MakeAPI("setRoomAlias", func(req *http.Request) util.JSONResponse {
|
||||
var request api.SetRoomAliasRequest
|
||||
var response api.SetRoomAliasResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
if err := r.SetRoomAlias(&request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: 200, JSON: &response}
|
||||
}),
|
||||
)
|
||||
servMux.Handle(
|
||||
api.RoomserverGetAliasRoomIDPath,
|
||||
common.MakeAPI("getAliasRoomID", func(req *http.Request) util.JSONResponse {
|
||||
var request api.GetAliasRoomIDRequest
|
||||
var response api.GetAliasRoomIDResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
if err := r.GetAliasRoomID(&request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: 200, JSON: &response}
|
||||
}),
|
||||
)
|
||||
servMux.Handle(
|
||||
api.RoomserverRemoveRoomAliasPath,
|
||||
common.MakeAPI("removeRoomAlias", func(req *http.Request) util.JSONResponse {
|
||||
var request api.RemoveRoomAliasRequest
|
||||
var response api.RemoveRoomAliasResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
if err := r.RemoveRoomAlias(&request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: 200, JSON: &response}
|
||||
}),
|
||||
)
|
||||
}
|
||||
129
src/github.com/matrix-org/dendrite/roomserver/api/alias.go
Normal file
129
src/github.com/matrix-org/dendrite/roomserver/api/alias.go
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
// 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 (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// GetAliasRoomIDRequest is a request to GetAliasRoomID
|
||||
type GetAliasRoomIDRequest struct {
|
||||
// Alias we want to lookup
|
||||
Alias string `json:"alias"`
|
||||
}
|
||||
|
||||
// GetAliasRoomIDResponse is a response to GetAliasRoomID
|
||||
type GetAliasRoomIDResponse struct {
|
||||
// The room ID the alias refers to
|
||||
RoomID string `json:"room_id"`
|
||||
}
|
||||
|
||||
// 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(
|
||||
req *SetRoomAliasRequest,
|
||||
response *SetRoomAliasResponse,
|
||||
) error
|
||||
|
||||
// Get the room ID for an alias
|
||||
GetAliasRoomID(
|
||||
req *GetAliasRoomIDRequest,
|
||||
response *GetAliasRoomIDResponse,
|
||||
) error
|
||||
|
||||
// Remove a room alias
|
||||
RemoveRoomAlias(
|
||||
req *RemoveRoomAliasRequest,
|
||||
response *RemoveRoomAliasResponse,
|
||||
) error
|
||||
}
|
||||
|
||||
// RoomserverSetRoomAliasPath is the HTTP path for the SetRoomAlias API.
|
||||
const RoomserverSetRoomAliasPath = "/api/roomserver/setRoomAlias"
|
||||
|
||||
// RoomserverGetAliasRoomIDPath is the HTTP path for the GetAliasRoomID API.
|
||||
const RoomserverGetAliasRoomIDPath = "/api/roomserver/getAliasRoomID"
|
||||
|
||||
// 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.
|
||||
// If httpClient is nil then it uses the http.DefaultClient
|
||||
func NewRoomserverAliasAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverAliasAPI {
|
||||
if httpClient == nil {
|
||||
httpClient = http.DefaultClient
|
||||
}
|
||||
return &httpRoomserverAliasAPI{roomserverURL, httpClient}
|
||||
}
|
||||
|
||||
type httpRoomserverAliasAPI struct {
|
||||
roomserverURL string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// SetRoomAlias implements RoomserverAliasAPI
|
||||
func (h *httpRoomserverAliasAPI) SetRoomAlias(
|
||||
request *SetRoomAliasRequest,
|
||||
response *SetRoomAliasResponse,
|
||||
) error {
|
||||
apiURL := h.roomserverURL + RoomserverSetRoomAliasPath
|
||||
return postJSON(h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
// GetAliasRoomID implements RoomserverAliasAPI
|
||||
func (h *httpRoomserverAliasAPI) GetAliasRoomID(
|
||||
request *GetAliasRoomIDRequest,
|
||||
response *GetAliasRoomIDResponse,
|
||||
) error {
|
||||
// RemoveRoomAlias implements RoomserverAliasAPI
|
||||
apiURL := h.roomserverURL + RoomserverGetAliasRoomIDPath
|
||||
return postJSON(h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
func (h *httpRoomserverAliasAPI) RemoveRoomAlias(
|
||||
request *RemoveRoomAliasRequest,
|
||||
response *RemoveRoomAliasResponse,
|
||||
) error {
|
||||
apiURL := h.roomserverURL + RoomserverRemoveRoomAliasPath
|
||||
return postJSON(h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
|
@ -100,45 +100,6 @@ type QueryEventsByIDResponse struct {
|
|||
Events []gomatrixserverlib.Event `json:"events"`
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// GetAliasRoomIDRequest is a request to GetAliasRoomID
|
||||
type GetAliasRoomIDRequest struct {
|
||||
// Alias we want to lookup
|
||||
Alias string `json:"alias"`
|
||||
}
|
||||
|
||||
// GetAliasRoomIDResponse is a response to GetAliasRoomID
|
||||
type GetAliasRoomIDResponse struct {
|
||||
// The room ID the alias refers to
|
||||
RoomID string `json:"room_id"`
|
||||
}
|
||||
|
||||
// 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{}
|
||||
|
||||
// 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.
|
||||
|
|
@ -158,24 +119,6 @@ type RoomserverQueryAPI interface {
|
|||
request *QueryEventsByIDRequest,
|
||||
response *QueryEventsByIDResponse,
|
||||
) error
|
||||
|
||||
// Set a room alias
|
||||
SetRoomAlias(
|
||||
request *SetRoomAliasRequest,
|
||||
response *SetRoomAliasResponse,
|
||||
) error
|
||||
|
||||
// Get the room ID for an alias
|
||||
GetAliasRoomID(
|
||||
request *GetAliasRoomIDRequest,
|
||||
response *GetAliasRoomIDResponse,
|
||||
) error
|
||||
|
||||
// Remove a room alias
|
||||
RemoveRoomAlias(
|
||||
request *RemoveRoomAliasRequest,
|
||||
response *RemoveRoomAliasResponse,
|
||||
) error
|
||||
}
|
||||
|
||||
// RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API.
|
||||
|
|
@ -187,15 +130,6 @@ const RoomserverQueryStateAfterEventsPath = "/api/roomserver/queryStateAfterEven
|
|||
// RoomserverQueryEventsByIDPath is the HTTP path for the QueryEventsByID API.
|
||||
const RoomserverQueryEventsByIDPath = "/api/roomserver/queryEventsByID"
|
||||
|
||||
// RoomserverSetRoomAliasPath is the HTTP path for the SetRoomAlias API.
|
||||
const RoomserverSetRoomAliasPath = "/api/roomserver/setRoomAlias"
|
||||
|
||||
// RoomserverGetAliasRoomIDPath is the HTTP path for the GetAliasRoomID API.
|
||||
const RoomserverGetAliasRoomIDPath = "/api/roomserver/getAliasRoomID"
|
||||
|
||||
// RoomserverRemoveRoomAliasPath is the HTTP path for the RemoveRoomAlias API.
|
||||
const RoomserverRemoveRoomAliasPath = "/api/roomserver/removeRoomAlias"
|
||||
|
||||
// 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 {
|
||||
|
|
@ -237,33 +171,6 @@ func (h *httpRoomserverQueryAPI) QueryEventsByID(
|
|||
return postJSON(h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
// SetRoomAlias implements RoomserverQueryAPI
|
||||
func (h *httpRoomserverQueryAPI) SetRoomAlias(
|
||||
request *SetRoomAliasRequest,
|
||||
response *SetRoomAliasResponse,
|
||||
) error {
|
||||
apiURL := h.roomserverURL + RoomserverSetRoomAliasPath
|
||||
return postJSON(h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
// GetAliasRoomID implements RoomserverQueryAPI
|
||||
func (h *httpRoomserverQueryAPI) GetAliasRoomID(
|
||||
request *GetAliasRoomIDRequest,
|
||||
response *GetAliasRoomIDResponse,
|
||||
) error {
|
||||
apiURL := h.roomserverURL + RoomserverGetAliasRoomIDPath
|
||||
return postJSON(h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
// RemoveRoomAlias implements RoomserverQueryAPI
|
||||
func (h *httpRoomserverQueryAPI) RemoveRoomAlias(
|
||||
request *RemoveRoomAliasRequest,
|
||||
response *RemoveRoomAliasResponse,
|
||||
) error {
|
||||
apiURL := h.roomserverURL + RoomserverRemoveRoomAliasPath
|
||||
return postJSON(h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
func postJSON(httpClient *http.Client, apiURL string, request, response interface{}) error {
|
||||
jsonBytes, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -16,14 +16,10 @@ package query
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/common"
|
||||
"github.com/matrix-org/dendrite/common/config"
|
||||
"github.com/matrix-org/dendrite/roomserver/api"
|
||||
"github.com/matrix-org/dendrite/roomserver/input"
|
||||
"github.com/matrix-org/dendrite/roomserver/state"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
|
@ -60,9 +56,7 @@ type RoomserverQueryAPIDatabase interface {
|
|||
|
||||
// RoomserverQueryAPI is an implementation of api.RoomserverQueryAPI
|
||||
type RoomserverQueryAPI struct {
|
||||
DB RoomserverQueryAPIDatabase
|
||||
Cfg *config.Dendrite
|
||||
InputAPI input.RoomserverInputAPI
|
||||
DB RoomserverQueryAPIDatabase
|
||||
}
|
||||
|
||||
// QueryLatestEventsAndState implements api.RoomserverQueryAPI
|
||||
|
|
@ -167,162 +161,6 @@ func (r *RoomserverQueryAPI) QueryEventsByID(
|
|||
return nil
|
||||
}
|
||||
|
||||
// SetRoomAlias implements api.RoomserverQueryAPI
|
||||
func (r *RoomserverQueryAPI) SetRoomAlias(
|
||||
request *api.SetRoomAliasRequest,
|
||||
response *api.SetRoomAliasResponse,
|
||||
) error {
|
||||
// Check if the alias isn't already referring to a room
|
||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(roomID) > 0 {
|
||||
// If the alias already exists, stop the process
|
||||
response.AliasExists = true
|
||||
return nil
|
||||
}
|
||||
response.AliasExists = false
|
||||
|
||||
// Save the new alias
|
||||
if err := r.DB.SetRoomAlias(request.Alias, request.RoomID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Send a m.room.aliases event with the updated list of aliases for this room
|
||||
if err := r.sendUpdatedAliasesEvent(request.UserID, request.RoomID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAliasRoomID implements api.RoomserverQueryAPI
|
||||
func (r *RoomserverQueryAPI) GetAliasRoomID(
|
||||
request *api.GetAliasRoomIDRequest,
|
||||
response *api.GetAliasRoomIDResponse,
|
||||
) error {
|
||||
// Lookup the room ID in the database
|
||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
response.RoomID = roomID
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveRoomAlias implements api.RoomserverQueryAPI
|
||||
func (r *RoomserverQueryAPI) RemoveRoomAlias(
|
||||
request *api.RemoveRoomAliasRequest,
|
||||
response *api.RemoveRoomAliasResponse,
|
||||
) error {
|
||||
// Lookup the room ID in the database
|
||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove the dalias from the database
|
||||
if err := r.DB.RemoveRoomAlias(request.Alias); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Send an updated m.room.aliases event
|
||||
if err := r.sendUpdatedAliasesEvent(request.UserID, roomID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type roomAliasesContent struct {
|
||||
Aliases []string `json:"aliases"`
|
||||
}
|
||||
|
||||
// Build the updated m.room.aliases event to send to the room after addition or
|
||||
// removal of an alias
|
||||
func (r *RoomserverQueryAPI) sendUpdatedAliasesEvent(userID string, roomID string) error {
|
||||
serverName := string(r.Cfg.Matrix.ServerName)
|
||||
|
||||
builder := gomatrixserverlib.EventBuilder{
|
||||
Sender: userID,
|
||||
RoomID: roomID,
|
||||
Type: "m.room.aliases",
|
||||
StateKey: &serverName,
|
||||
}
|
||||
|
||||
// Retrieve the updated list of aliases, marhal it and set it as the
|
||||
// event's content
|
||||
aliases, err := r.DB.GetAliasesFromRoomID(roomID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content := roomAliasesContent{Aliases: aliases}
|
||||
rawContent, err := json.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = builder.SetContent(json.RawMessage(rawContent))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get needed state events and depth
|
||||
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(&builder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req := api.QueryLatestEventsAndStateRequest{
|
||||
RoomID: roomID,
|
||||
StateToFetch: eventsNeeded.Tuples(),
|
||||
}
|
||||
var res api.QueryLatestEventsAndStateResponse
|
||||
if err = r.QueryLatestEventsAndState(&req, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
builder.Depth = res.Depth
|
||||
builder.PrevEvents = res.LatestEvents
|
||||
|
||||
// Add auth events
|
||||
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
||||
for i := range res.StateEvents {
|
||||
authEvents.AddEvent(&res.StateEvents[i])
|
||||
}
|
||||
refs, err := eventsNeeded.AuthEventReferences(&authEvents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
builder.AuthEvents = refs
|
||||
|
||||
// Build the event
|
||||
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), r.Cfg.Matrix.ServerName)
|
||||
now := time.Now()
|
||||
event, err := builder.Build(eventID, now, r.Cfg.Matrix.ServerName, r.Cfg.Matrix.KeyID, r.Cfg.Matrix.PrivateKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create the request
|
||||
ire := api.InputRoomEvent{
|
||||
Kind: api.KindNew,
|
||||
Event: event,
|
||||
AuthEventIDs: event.AuthEventIDs(),
|
||||
SendAsServer: serverName,
|
||||
}
|
||||
inputReq := api.InputRoomEventsRequest{
|
||||
InputRoomEvents: []api.InputRoomEvent{ire},
|
||||
}
|
||||
var inputRes api.InputRoomEventsResponse
|
||||
|
||||
// Send the request
|
||||
if err := r.InputAPI.InputRoomEvents(&inputReq, &inputRes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RoomserverQueryAPI) loadStateEvents(stateEntries []types.StateEntry) ([]gomatrixserverlib.Event, error) {
|
||||
eventNIDs := make([]types.EventNID, len(stateEntries))
|
||||
for i := range stateEntries {
|
||||
|
|
@ -388,46 +226,4 @@ func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
|||
return util.JSONResponse{Code: 200, JSON: &response}
|
||||
}),
|
||||
)
|
||||
servMux.Handle(
|
||||
api.RoomserverSetRoomAliasPath,
|
||||
common.MakeAPI("setRoomAlias", func(req *http.Request) util.JSONResponse {
|
||||
var request api.SetRoomAliasRequest
|
||||
var response api.SetRoomAliasResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
if err := r.SetRoomAlias(&request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: 200, JSON: &response}
|
||||
}),
|
||||
)
|
||||
servMux.Handle(
|
||||
api.RoomserverGetAliasRoomIDPath,
|
||||
common.MakeAPI("getAliasRoomID", func(req *http.Request) util.JSONResponse {
|
||||
var request api.GetAliasRoomIDRequest
|
||||
var response api.GetAliasRoomIDResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
if err := r.GetAliasRoomID(&request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: 200, JSON: &response}
|
||||
}),
|
||||
)
|
||||
servMux.Handle(
|
||||
api.RoomserverRemoveRoomAliasPath,
|
||||
common.MakeAPI("removeRoomAlias", func(req *http.Request) util.JSONResponse {
|
||||
var request api.RemoveRoomAliasRequest
|
||||
var response api.RemoveRoomAliasResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
if err := r.RemoveRoomAlias(&request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: 200, JSON: &response}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -355,22 +355,22 @@ func (d *Database) LatestEventIDs(roomNID types.RoomNID) ([]gomatrixserverlib.Ev
|
|||
return references, currentStateSnapshotNID, depth, nil
|
||||
}
|
||||
|
||||
// SetRoomAlias implements query.RoomserverQueryAPIDB
|
||||
// SetRoomAlias implements alias.RoomserverAliasAPIDB
|
||||
func (d *Database) SetRoomAlias(alias string, roomID string) error {
|
||||
return d.statements.insertRoomAlias(alias, roomID)
|
||||
}
|
||||
|
||||
// GetRoomIDFromAlias implements query.RoomserverQueryAPIDB
|
||||
// GetRoomIDFromAlias implements alias.RoomserverAliasAPIDB
|
||||
func (d *Database) GetRoomIDFromAlias(alias string) (string, error) {
|
||||
return d.statements.selectRoomIDFromAlias(alias)
|
||||
}
|
||||
|
||||
// GetAliasesFromRoomID implements query.RoomserverQueryAPIDB
|
||||
// GetAliasesFromRoomID implements alias.RoomserverAliasAPIDB
|
||||
func (d *Database) GetAliasesFromRoomID(roomID string) ([]string, error) {
|
||||
return d.statements.selectAliasesFromRoomID(roomID)
|
||||
}
|
||||
|
||||
// RemoveRoomAlias implements query.RoomserverQueryAPIDB
|
||||
// RemoveRoomAlias implements alias.RoomserverAliasAPIDB
|
||||
func (d *Database) RemoveRoomAlias(alias string) error {
|
||||
return d.statements.deleteRoomAlias(alias)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue