2017-07-28 05:31:43 -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.
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
package internal
|
2017-07-28 05:31:43 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-13 07:37:50 -05:00
|
|
|
"context"
|
2022-03-07 03:37:04 -06:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2020-09-02 04:02:48 -05:00
|
|
|
"fmt"
|
2022-03-07 03:37:04 -06:00
|
|
|
"time"
|
2017-07-28 05:31:43 -05:00
|
|
|
|
2022-03-07 03:37:04 -06:00
|
|
|
asAPI "github.com/matrix-org/dendrite/appservice/api"
|
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2020-05-01 04:48:17 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2022-03-07 03:37:04 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2017-07-28 05:31:43 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-03-07 03:37:04 -06:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/tidwall/sjson"
|
2017-07-28 05:31:43 -05:00
|
|
|
)
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
// RoomserverInternalAPIDatabase has the storage APIs needed to implement the alias API.
|
|
|
|
type RoomserverInternalAPIDatabase interface {
|
2017-07-28 05:31:43 -05: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-06 22:00:58 -05:00
|
|
|
SetRoomAlias(ctx context.Context, alias string, roomID string, creatorUserID string) error
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the room ID a given alias refers to.
|
2017-07-28 05:31:43 -05:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
2018-05-30 07:43:13 -05:00
|
|
|
GetRoomIDForAlias(ctx context.Context, alias string) (string, error)
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up all aliases referring to a given room ID.
|
2017-07-28 05:31:43 -05:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
2018-05-30 07:43:13 -05:00
|
|
|
GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error)
|
2017-07-28 05:31:43 -05:00
|
|
|
// Remove a given room alias.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 10:30:19 -05:00
|
|
|
RemoveRoomAlias(ctx context.Context, alias string) error
|
2020-03-17 13:00:10 -05:00
|
|
|
// Look up the room version for a given room.
|
|
|
|
GetRoomVersionForRoom(
|
|
|
|
ctx context.Context, roomID string,
|
|
|
|
) (gomatrixserverlib.RoomVersion, error)
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
// SetRoomAlias implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) SetRoomAlias(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2020-05-01 04:48:17 -05:00
|
|
|
request *api.SetRoomAliasRequest,
|
|
|
|
response *api.SetRoomAliasResponse,
|
2017-07-28 05:31:43 -05:00
|
|
|
) error {
|
|
|
|
// Check if the alias isn't already referring to a room
|
2018-05-30 07:43:13 -05:00
|
|
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
2017-07-28 05:31:43 -05: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-06 22:00:58 -05:00
|
|
|
if err := r.DB.SetRoomAlias(ctx, request.Alias, request.RoomID, request.UserID); err != nil {
|
2017-07-28 05:31:43 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
return nil
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
// GetRoomIDForAlias implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) GetRoomIDForAlias(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2020-05-01 04:48:17 -05:00
|
|
|
request *api.GetRoomIDForAliasRequest,
|
|
|
|
response *api.GetRoomIDForAliasResponse,
|
2017-07-28 05:31:43 -05:00
|
|
|
) error {
|
2017-08-23 09:08:48 -05:00
|
|
|
// Look up the room ID in the database
|
2018-05-30 07:43:13 -05:00
|
|
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
2021-03-03 11:00:31 -06:00
|
|
|
if err == nil && roomID != "" {
|
|
|
|
response.RoomID = roomID
|
|
|
|
return nil
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
2021-03-03 11:00:31 -06:00
|
|
|
// Check appservice on err, but only if the appservice API is
|
|
|
|
// wired in and no room ID was found.
|
|
|
|
if r.asAPI != nil && request.IncludeAppservices && roomID == "" {
|
|
|
|
// No room found locally, try our application services by making a call to
|
|
|
|
// the appservice component
|
|
|
|
aliasReq := &asAPI.RoomAliasExistsRequest{
|
|
|
|
Alias: request.Alias,
|
|
|
|
}
|
|
|
|
aliasRes := &asAPI.RoomAliasExistsResponse{}
|
|
|
|
if err = r.asAPI.RoomAliasExists(ctx, aliasReq, aliasRes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-01 04:48:17 -05:00
|
|
|
|
2021-03-03 11:00:31 -06:00
|
|
|
if aliasRes.AliasExists {
|
|
|
|
roomID, err = r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-01 04:48:17 -05:00
|
|
|
}
|
2021-03-03 11:00:31 -06:00
|
|
|
response.RoomID = roomID
|
|
|
|
return nil
|
2019-06-25 07:43:18 -05:00
|
|
|
}
|
2020-12-18 07:33:28 -06:00
|
|
|
}
|
2018-08-08 10:17:09 -05:00
|
|
|
|
2021-03-03 11:00:31 -06:00
|
|
|
return err
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
// GetAliasesForRoomID implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) GetAliasesForRoomID(
|
2018-05-30 07:43:13 -05:00
|
|
|
ctx context.Context,
|
2020-05-01 04:48:17 -05:00
|
|
|
request *api.GetAliasesForRoomIDRequest,
|
|
|
|
response *api.GetAliasesForRoomIDResponse,
|
2018-05-30 07:43:13 -05: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 04:48:17 -05:00
|
|
|
// RemoveRoomAlias implements alias.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) RemoveRoomAlias(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2020-05-01 04:48:17 -05:00
|
|
|
request *api.RemoveRoomAliasRequest,
|
|
|
|
response *api.RemoveRoomAliasResponse,
|
2017-07-28 05:31:43 -05:00
|
|
|
) error {
|
2022-11-14 06:30:20 -06:00
|
|
|
_, virtualHost, err := r.Cfg.Matrix.SplitLocalID('@', request.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-30 07:43:13 -05:00
|
|
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
2017-07-28 05:31:43 -05:00
|
|
|
if err != nil {
|
2021-07-21 10:53:50 -05:00
|
|
|
return fmt.Errorf("r.DB.GetRoomIDForAlias: %w", err)
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
2021-07-21 10:53:50 -05:00
|
|
|
if roomID == "" {
|
|
|
|
response.Found = false
|
|
|
|
response.Removed = false
|
|
|
|
return nil
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
response.Found = true
|
|
|
|
creatorID, err := r.DB.GetCreatorIDForAlias(ctx, request.Alias)
|
2017-07-28 05:31:43 -05:00
|
|
|
if err != nil {
|
2021-07-21 10:53:50 -05:00
|
|
|
return fmt.Errorf("r.DB.GetCreatorIDForAlias: %w", err)
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
if creatorID != request.UserID {
|
2022-03-24 05:03:22 -05:00
|
|
|
var plEvent *gomatrixserverlib.HeaderedEvent
|
|
|
|
var pls *gomatrixserverlib.PowerLevelContent
|
|
|
|
|
|
|
|
plEvent, err = r.DB.GetStateEvent(ctx, roomID, gomatrixserverlib.MRoomPowerLevels, "")
|
2021-07-21 10:53:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.GetStateEvent: %w", err)
|
|
|
|
}
|
2017-07-28 05:31:43 -05:00
|
|
|
|
2022-03-24 05:03:22 -05:00
|
|
|
pls, err = plEvent.PowerLevels()
|
2017-09-20 04:59:19 -05:00
|
|
|
if err != nil {
|
2021-07-21 10:53:50 -05:00
|
|
|
return fmt.Errorf("plEvent.PowerLevels: %w", err)
|
2017-09-20 04:59:19 -05:00
|
|
|
}
|
2017-07-28 05:31:43 -05:00
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
if pls.UserLevel(request.UserID) < pls.EventLevel(gomatrixserverlib.MRoomCanonicalAlias, true) {
|
|
|
|
response.Removed = false
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-02 04:02:48 -05:00
|
|
|
}
|
2020-03-17 13:00:10 -05:00
|
|
|
|
2022-03-07 03:37:04 -06:00
|
|
|
ev, err := r.DB.GetStateEvent(ctx, roomID, gomatrixserverlib.MRoomCanonicalAlias, "")
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
} else if ev != nil {
|
|
|
|
stateAlias := gjson.GetBytes(ev.Content(), "alias").Str
|
|
|
|
// the alias to remove is currently set as the canonical alias, remove it
|
|
|
|
if stateAlias == request.Alias {
|
|
|
|
res, err := sjson.DeleteBytes(ev.Content(), "alias")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sender := request.UserID
|
|
|
|
if request.UserID != ev.Sender() {
|
|
|
|
sender = ev.Sender()
|
|
|
|
}
|
|
|
|
|
2022-11-14 11:06:46 -06:00
|
|
|
_, senderDomain, err := r.Cfg.Matrix.SplitLocalID('@', sender)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
identity, err := r.Cfg.Matrix.SigningIdentityFor(senderDomain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-07 03:37:04 -06:00
|
|
|
builder := &gomatrixserverlib.EventBuilder{
|
|
|
|
Sender: sender,
|
|
|
|
RoomID: ev.RoomID(),
|
|
|
|
Type: ev.Type(),
|
|
|
|
StateKey: ev.StateKey(),
|
|
|
|
Content: res,
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gomatrixserverlib.StateNeededForEventBuilder: %w", err)
|
|
|
|
}
|
|
|
|
if len(eventsNeeded.Tuples()) == 0 {
|
|
|
|
return errors.New("expecting state tuples for event builder, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
stateRes := &api.QueryLatestEventsAndStateResponse{}
|
2022-03-24 05:03:22 -05:00
|
|
|
if err = helpers.QueryLatestEventsAndState(ctx, r.DB, &api.QueryLatestEventsAndStateRequest{RoomID: roomID, StateToFetch: eventsNeeded.Tuples()}, stateRes); err != nil {
|
2022-03-07 03:37:04 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-14 11:06:46 -06:00
|
|
|
newEvent, err := eventutil.BuildEvent(ctx, builder, r.Cfg.Matrix, identity, time.Now(), &eventsNeeded, stateRes)
|
2022-03-07 03:37:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-14 06:30:20 -06:00
|
|
|
err = api.SendEvents(ctx, r, api.KindNew, []*gomatrixserverlib.HeaderedEvent{newEvent}, virtualHost, r.ServerName, r.ServerName, nil, false)
|
2022-03-07 03:37:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
// Remove the alias from the database
|
|
|
|
if err := r.DB.RemoveRoomAlias(ctx, request.Alias); err != nil {
|
2017-07-28 05:31:43 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:53:50 -05:00
|
|
|
response.Removed = true
|
|
|
|
return nil
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|