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"
|
2022-03-07 09:37:04 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2020-09-02 09:02:48 +00:00
|
|
|
"fmt"
|
2022-03-07 09:37:04 +00:00
|
|
|
"time"
|
2017-07-28 10:31:43 +00:00
|
|
|
|
2022-03-07 09:37:04 +00:00
|
|
|
asAPI "github.com/matrix-org/dendrite/appservice/api"
|
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2020-05-01 09:48:17 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2022-03-07 09:37:04 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2023-04-27 11:54:20 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2017-07-28 10:31:43 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-03-07 09:37:04 +00:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/tidwall/sjson"
|
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,
|
2023-07-31 13:39:41 +00:00
|
|
|
senderID spec.SenderID,
|
|
|
|
roomID spec.RoomID,
|
|
|
|
alias string,
|
|
|
|
) (aliasAlreadyUsed bool, err error) {
|
2017-07-28 10:31:43 +00:00
|
|
|
// Check if the alias isn't already referring to a room
|
2023-07-31 13:39:41 +00:00
|
|
|
existingRoomID, err := r.DB.GetRoomIDForAlias(ctx, alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return false, err
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
2023-07-31 13:39:41 +00:00
|
|
|
|
|
|
|
if len(existingRoomID) > 0 {
|
2017-07-28 10:31:43 +00:00
|
|
|
// If the alias already exists, stop the process
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, nil
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the new alias
|
2023-07-31 13:39:41 +00:00
|
|
|
if err := r.DB.SetRoomAlias(ctx, alias, roomID.String(), string(senderID)); err != nil {
|
|
|
|
return false, err
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 13:39:41 +00:00
|
|
|
return false, nil
|
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)
|
2021-03-03 17:00:31 +00:00
|
|
|
if err == nil && roomID != "" {
|
|
|
|
response.RoomID = roomID
|
|
|
|
return nil
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 17:00:31 +00: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 09:48:17 +00:00
|
|
|
|
2021-03-03 17:00:31 +00:00
|
|
|
if aliasRes.AliasExists {
|
|
|
|
roomID, err = r.DB.GetRoomIDForAlias(ctx, request.Alias)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-01 09:48:17 +00:00
|
|
|
}
|
2021-03-03 17:00:31 +00:00
|
|
|
response.RoomID = roomID
|
|
|
|
return nil
|
2019-06-25 12:43:18 +00:00
|
|
|
}
|
2020-12-18 13:33:28 +00:00
|
|
|
}
|
2018-08-08 15:17:09 +00:00
|
|
|
|
2021-03-03 17:00:31 +00:00
|
|
|
return err
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-14 14:23:46 +00:00
|
|
|
// nolint:gocyclo
|
2020-05-01 09:48:17 +00:00
|
|
|
// RemoveRoomAlias implements alias.RoomserverInternalAPI
|
2023-06-28 18:29:49 +00:00
|
|
|
// nolint: gocyclo
|
2023-07-31 13:39:41 +00:00
|
|
|
func (r *RoomserverInternalAPI) RemoveRoomAlias(ctx context.Context, senderID spec.SenderID, alias string) (aliasFound bool, aliasRemoved bool, err error) {
|
|
|
|
roomID, err := r.DB.GetRoomIDForAlias(ctx, alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return false, false, fmt.Errorf("r.DB.GetRoomIDForAlias: %w", err)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
2021-07-21 15:53:50 +00:00
|
|
|
if roomID == "" {
|
2023-07-31 13:39:41 +00:00
|
|
|
return false, false, nil
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 14:23:46 +00:00
|
|
|
validRoomID, err := spec.NewRoomID(roomID)
|
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2023-06-14 14:23:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 13:39:41 +00:00
|
|
|
sender, err := r.QueryUserIDForSender(ctx, *validRoomID, senderID)
|
2023-06-07 17:14:35 +00:00
|
|
|
if err != nil || sender == nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, fmt.Errorf("r.QueryUserIDForSender: %w", err)
|
2023-06-06 20:55:18 +00:00
|
|
|
}
|
|
|
|
virtualHost := sender.Domain()
|
|
|
|
|
2023-07-31 13:39:41 +00:00
|
|
|
creatorID, err := r.DB.GetCreatorIDForAlias(ctx, alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, fmt.Errorf("r.DB.GetCreatorIDForAlias: %w", err)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 13:39:41 +00:00
|
|
|
if spec.SenderID(creatorID) != senderID {
|
2023-04-27 11:54:20 +00:00
|
|
|
var plEvent *types.HeaderedEvent
|
2022-03-24 10:03:22 +00:00
|
|
|
var pls *gomatrixserverlib.PowerLevelContent
|
|
|
|
|
2023-04-19 14:50:33 +00:00
|
|
|
plEvent, err = r.DB.GetStateEvent(ctx, roomID, spec.MRoomPowerLevels, "")
|
2021-07-21 15:53:50 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, fmt.Errorf("r.DB.GetStateEvent: %w", err)
|
2021-07-21 15:53:50 +00:00
|
|
|
}
|
2017-07-28 10:31:43 +00:00
|
|
|
|
2022-03-24 10:03:22 +00:00
|
|
|
pls, err = plEvent.PowerLevels()
|
2017-09-20 09:59:19 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, fmt.Errorf("plEvent.PowerLevels: %w", err)
|
2017-09-20 09:59:19 +00:00
|
|
|
}
|
2017-07-28 10:31:43 +00:00
|
|
|
|
2023-07-31 13:39:41 +00:00
|
|
|
if pls.UserLevel(senderID) < pls.EventLevel(spec.MRoomCanonicalAlias, true) {
|
|
|
|
return true, false, nil
|
2021-07-21 15:53:50 +00:00
|
|
|
}
|
2020-09-02 09:02:48 +00:00
|
|
|
}
|
2020-03-17 18:00:10 +00:00
|
|
|
|
2023-04-19 14:50:33 +00:00
|
|
|
ev, err := r.DB.GetStateEvent(ctx, roomID, spec.MRoomCanonicalAlias, "")
|
2022-03-07 09:37:04 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2022-03-07 09:37:04 +00:00
|
|
|
} else if ev != nil {
|
|
|
|
stateAlias := gjson.GetBytes(ev.Content(), "alias").Str
|
|
|
|
// the alias to remove is currently set as the canonical alias, remove it
|
2023-07-31 13:39:41 +00:00
|
|
|
if stateAlias == alias {
|
2022-03-07 09:37:04 +00:00
|
|
|
res, err := sjson.DeleteBytes(ev.Content(), "alias")
|
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 13:39:41 +00:00
|
|
|
canonicalSenderID := ev.SenderID()
|
|
|
|
canonicalSender, err := r.QueryUserIDForSender(ctx, *validRoomID, canonicalSenderID)
|
|
|
|
if err != nil || canonicalSender == nil {
|
|
|
|
return true, false, err
|
2022-11-15 15:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 18:29:49 +00:00
|
|
|
validRoomID, err := spec.NewRoomID(roomID)
|
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2023-06-28 18:29:49 +00:00
|
|
|
}
|
2023-07-31 13:39:41 +00:00
|
|
|
identity, err := r.SigningIdentityFor(ctx, *validRoomID, *canonicalSender)
|
2022-11-15 15:05:23 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2022-11-15 15:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 10:17:42 +00:00
|
|
|
proto := &gomatrixserverlib.ProtoEvent{
|
2023-07-31 13:39:41 +00:00
|
|
|
SenderID: string(canonicalSenderID),
|
2023-09-15 14:39:06 +00:00
|
|
|
RoomID: ev.RoomID().String(),
|
2022-03-07 09:37:04 +00:00
|
|
|
Type: ev.Type(),
|
|
|
|
StateKey: ev.StateKey(),
|
|
|
|
Content: res,
|
|
|
|
}
|
|
|
|
|
2023-05-04 10:17:42 +00:00
|
|
|
eventsNeeded, err := gomatrixserverlib.StateNeededForProtoEvent(proto)
|
2022-03-07 09:37:04 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, fmt.Errorf("gomatrixserverlib.StateNeededForEventBuilder: %w", err)
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
if len(eventsNeeded.Tuples()) == 0 {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, errors.New("expecting state tuples for event builder, got none")
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stateRes := &api.QueryLatestEventsAndStateResponse{}
|
2023-06-14 14:23:46 +00:00
|
|
|
if err = helpers.QueryLatestEventsAndState(ctx, r.DB, r, &api.QueryLatestEventsAndStateRequest{RoomID: roomID, StateToFetch: eventsNeeded.Tuples()}, stateRes); err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 18:29:49 +00:00
|
|
|
newEvent, err := eventutil.BuildEvent(ctx, proto, &identity, time.Now(), &eventsNeeded, stateRes)
|
2022-03-07 09:37:04 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 11:54:20 +00:00
|
|
|
err = api.SendEvents(ctx, r, api.KindNew, []*types.HeaderedEvent{newEvent}, virtualHost, r.ServerName, r.ServerName, nil, false)
|
2022-03-07 09:37:04 +00:00
|
|
|
if err != nil {
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, false, err
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 15:53:50 +00:00
|
|
|
// Remove the alias from the database
|
2023-07-31 13:39:41 +00:00
|
|
|
if err := r.DB.RemoveRoomAlias(ctx, alias); err != nil {
|
|
|
|
return true, false, err
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 13:39:41 +00:00
|
|
|
return true, true, nil
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|