2017-07-28 10:31:43 +00: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.
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
package internal
|
2017-07-28 10:31:43 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 12:37:50 +00:00
|
|
|
"context"
|
2017-07-28 10:31:43 +00:00
|
|
|
"encoding/json"
|
2020-04-14 17:36:08 +00:00
|
|
|
"errors"
|
2020-09-02 09:02:48 +00:00
|
|
|
"fmt"
|
2017-07-28 10:31:43 +00:00
|
|
|
"time"
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-07-28 10:31:43 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
// RoomserverInternalAPIDatabase has the storage APIs needed to implement the alias API.
|
|
|
|
type RoomserverInternalAPIDatabase interface {
|
2017-07-28 10:31:43 +00:00
|
|
|
// Save a given room alias with the room ID it refers to.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2019-08-07 03:00:58 +00:00
|
|
|
SetRoomAlias(ctx context.Context, alias string, roomID string, creatorUserID string) error
|
2017-08-23 14:08:48 +00:00
|
|
|
// Look up the room ID a given alias refers to.
|
2017-07-28 10:31:43 +00:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
2018-05-30 12:43:13 +00:00
|
|
|
GetRoomIDForAlias(ctx context.Context, alias string) (string, error)
|
2017-08-23 14:08:48 +00:00
|
|
|
// Look up all aliases referring to a given room ID.
|
2017-07-28 10:31:43 +00:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
2018-05-30 12:43:13 +00:00
|
|
|
GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error)
|
2019-08-07 03:00:58 +00:00
|
|
|
// Get the user ID of the creator of an alias.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
|
|
|
GetCreatorIDForAlias(ctx context.Context, alias string) (string, error)
|
2017-07-28 10:31:43 +00:00
|
|
|
// Remove a given room alias.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
RemoveRoomAlias(ctx context.Context, alias string) error
|
2020-03-17 18:00:10 +00:00
|
|
|
// Look up the room version for a given room.
|
|
|
|
GetRoomVersionForRoom(
|
|
|
|
ctx context.Context, roomID string,
|
|
|
|
) (gomatrixserverlib.RoomVersion, error)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
// SetRoomAlias implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) SetRoomAlias(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx context.Context,
|
2020-05-01 09:48:17 +00:00
|
|
|
request *api.SetRoomAliasRequest,
|
|
|
|
response *api.SetRoomAliasResponse,
|
2017-07-28 10:31:43 +00:00
|
|
|
) error {
|
|
|
|
// Check if the alias isn't already referring to a room
|
2018-05-30 12:43:13 +00:00
|
|
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
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
|
2019-08-07 03:00:58 +00:00
|
|
|
if err := r.DB.SetRoomAlias(ctx, request.Alias, request.RoomID, request.UserID); err != nil {
|
2017-07-28 10:31:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a m.room.aliases event with the updated list of aliases for this room
|
2017-09-13 12:37:50 +00:00
|
|
|
// At this point we've already committed the alias to the database so we
|
|
|
|
// shouldn't cancel this request.
|
|
|
|
// TODO: Ensure that we send unsent events when if server restarts.
|
2017-11-15 11:13:09 +00:00
|
|
|
return r.sendUpdatedAliasesEvent(context.TODO(), request.UserID, request.RoomID)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
// GetRoomIDForAlias implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) GetRoomIDForAlias(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx context.Context,
|
2020-05-01 09:48:17 +00:00
|
|
|
request *api.GetRoomIDForAliasRequest,
|
|
|
|
response *api.GetRoomIDForAliasResponse,
|
2017-07-28 10:31:43 +00:00
|
|
|
) error {
|
2017-08-23 14:08:48 +00:00
|
|
|
// Look up the room ID in the database
|
2018-05-30 12:43:13 +00:00
|
|
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
/*
|
|
|
|
TODO: Why is this here? It creates an unnecessary dependency
|
|
|
|
from the roomserver to the appservice component, which should be
|
|
|
|
altogether optional.
|
2019-06-25 12:43:18 +00:00
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
if roomID == "" {
|
|
|
|
// No room found locally, try our application services by making a call to
|
|
|
|
// the appservice component
|
|
|
|
aliasReq := appserviceAPI.RoomAliasExistsRequest{Alias: request.Alias}
|
|
|
|
var aliasResp appserviceAPI.RoomAliasExistsResponse
|
|
|
|
if err = r.AppserviceAPI.RoomAliasExists(ctx, &aliasReq, &aliasResp); err != nil {
|
2019-06-25 12:43:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-01 09:48:17 +00:00
|
|
|
|
|
|
|
if aliasResp.AliasExists {
|
|
|
|
roomID, err = r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 12:43:18 +00:00
|
|
|
}
|
2020-05-01 09:48:17 +00:00
|
|
|
*/
|
2018-08-08 15:17:09 +00:00
|
|
|
|
2017-07-28 10:31:43 +00:00
|
|
|
response.RoomID = roomID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
// GetAliasesForRoomID implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) GetAliasesForRoomID(
|
2018-05-30 12:43:13 +00:00
|
|
|
ctx context.Context,
|
2020-05-01 09:48:17 +00:00
|
|
|
request *api.GetAliasesForRoomIDRequest,
|
|
|
|
response *api.GetAliasesForRoomIDResponse,
|
2018-05-30 12:43:13 +00:00
|
|
|
) error {
|
|
|
|
// Look up the aliases in the database for the given RoomID
|
|
|
|
aliases, err := r.DB.GetAliasesForRoomID(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Aliases = aliases
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
// GetCreatorIDForAlias implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) GetCreatorIDForAlias(
|
2019-08-07 03:00:58 +00:00
|
|
|
ctx context.Context,
|
2020-05-01 09:48:17 +00:00
|
|
|
request *api.GetCreatorIDForAliasRequest,
|
|
|
|
response *api.GetCreatorIDForAliasResponse,
|
2019-08-07 03:00:58 +00:00
|
|
|
) error {
|
|
|
|
// Look up the aliases in the database for the given RoomID
|
|
|
|
creatorID, err := r.DB.GetCreatorIDForAlias(ctx, request.Alias)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.UserID = creatorID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 09:48:17 +00:00
|
|
|
// RemoveRoomAlias implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) RemoveRoomAlias(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx context.Context,
|
2020-05-01 09:48:17 +00:00
|
|
|
request *api.RemoveRoomAliasRequest,
|
|
|
|
response *api.RemoveRoomAliasResponse,
|
2017-07-28 10:31:43 +00:00
|
|
|
) error {
|
2017-08-23 14:08:48 +00:00
|
|
|
// Look up the room ID in the database
|
2018-05-30 12:43:13 +00:00
|
|
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the dalias from the database
|
2017-09-13 15:30:19 +00:00
|
|
|
if err := r.DB.RemoveRoomAlias(ctx, request.Alias); err != nil {
|
2017-07-28 10:31:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an updated m.room.aliases event
|
2017-09-13 15:30:19 +00:00
|
|
|
// At this point we've already committed the alias to the database so we
|
|
|
|
// shouldn't cancel this request.
|
|
|
|
// TODO: Ensure that we send unsent events when if server restarts.
|
2017-11-15 11:13:09 +00:00
|
|
|
return r.sendUpdatedAliasesEvent(context.TODO(), request.UserID, roomID)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-05-01 09:48:17 +00:00
|
|
|
func (r *RoomserverInternalAPI) sendUpdatedAliasesEvent(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx context.Context, userID string, roomID string,
|
|
|
|
) error {
|
2017-07-28 10:31:43 +00:00
|
|
|
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
|
2018-05-30 12:43:13 +00:00
|
|
|
aliases, err := r.DB.GetAliasesForRoomID(ctx, roomID)
|
2017-07-28 10:31:43 +00:00
|
|
|
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
|
|
|
|
}
|
2020-04-14 17:36:08 +00:00
|
|
|
if len(eventsNeeded.Tuples()) == 0 {
|
|
|
|
return errors.New("expecting state tuples for event builder, got none")
|
|
|
|
}
|
2020-05-01 09:48:17 +00:00
|
|
|
req := api.QueryLatestEventsAndStateRequest{
|
2017-07-28 10:31:43 +00:00
|
|
|
RoomID: roomID,
|
|
|
|
StateToFetch: eventsNeeded.Tuples(),
|
|
|
|
}
|
2020-05-01 09:48:17 +00:00
|
|
|
var res api.QueryLatestEventsAndStateResponse
|
|
|
|
if err = r.QueryLatestEventsAndState(ctx, &req, &res); err != nil {
|
2017-07-28 10:31:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
builder.Depth = res.Depth
|
|
|
|
builder.PrevEvents = res.LatestEvents
|
|
|
|
|
|
|
|
// Add auth events
|
|
|
|
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
|
|
|
for i := range res.StateEvents {
|
2020-03-16 17:29:52 +00:00
|
|
|
err = authEvents.AddEvent(&res.StateEvents[i].Event)
|
2017-09-20 09:59:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
refs, err := eventsNeeded.AuthEventReferences(&authEvents)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
builder.AuthEvents = refs
|
|
|
|
|
2020-09-02 09:02:48 +00:00
|
|
|
roomInfo, err := r.DB.RoomInfo(ctx, roomID)
|
2020-03-17 18:00:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-02 09:02:48 +00:00
|
|
|
if roomInfo == nil {
|
|
|
|
return fmt.Errorf("room %s does not exist", roomID)
|
|
|
|
}
|
2020-03-17 18:00:10 +00:00
|
|
|
|
2017-07-28 10:31:43 +00:00
|
|
|
// Build the event
|
|
|
|
now := time.Now()
|
2017-09-13 12:37:50 +00:00
|
|
|
event, err := builder.Build(
|
2020-03-27 16:28:22 +00:00
|
|
|
now, r.Cfg.Matrix.ServerName, r.Cfg.Matrix.KeyID,
|
2020-09-02 09:02:48 +00:00
|
|
|
r.Cfg.Matrix.PrivateKey, roomInfo.RoomVersion,
|
2017-09-13 12:37:50 +00:00
|
|
|
)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the request
|
2020-05-01 09:48:17 +00:00
|
|
|
ire := api.InputRoomEvent{
|
|
|
|
Kind: api.KindNew,
|
2020-09-02 09:02:48 +00:00
|
|
|
Event: event.Headered(roomInfo.RoomVersion),
|
2017-07-28 10:31:43 +00:00
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
SendAsServer: serverName,
|
|
|
|
}
|
2020-05-01 09:48:17 +00:00
|
|
|
inputReq := api.InputRoomEventsRequest{
|
|
|
|
InputRoomEvents: []api.InputRoomEvent{ire},
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
2020-05-01 09:48:17 +00:00
|
|
|
var inputRes api.InputRoomEventsResponse
|
2017-07-28 10:31:43 +00:00
|
|
|
|
|
|
|
// Send the request
|
2020-09-16 12:00:52 +00:00
|
|
|
r.InputRoomEvents(ctx, &inputReq, &inputRes)
|
|
|
|
return inputRes.Err()
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|