From 248d44cc4a47edcafb4aff86978b800973fd3a74 Mon Sep 17 00:00:00 2001 From: Serra Allgood Date: Mon, 20 May 2019 22:56:58 -0700 Subject: [PATCH] roomserver/alias: don't call appserviceAPI in GetRoomIDForAlias if local alias found The existing implementation will always make a call to appserviceAPI, regardless of whether a local alias is found in the database. Fixes #631 Signed-off-by: Serra Allgood --- .../dendrite/roomserver/alias/alias.go | 14 +-- .../dendrite/roomserver/alias/alias_test.go | 90 +++++++++++++++++++ 2 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/roomserver/alias/alias_test.go diff --git a/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go b/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go index 27279aad8..e6602c743 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go +++ b/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go @@ -96,12 +96,14 @@ func (r *RoomserverAliasAPI) GetRoomIDForAlias( return err } - // No rooms 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 { - return err + if len(roomID) == 0 { + // No rooms 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 { + return err + } } response.RoomID = roomID diff --git a/src/github.com/matrix-org/dendrite/roomserver/alias/alias_test.go b/src/github.com/matrix-org/dendrite/roomserver/alias/alias_test.go new file mode 100644 index 000000000..4bbff9846 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/roomserver/alias/alias_test.go @@ -0,0 +1,90 @@ +// 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 ( + "context" + "fmt" + "testing" + + appserviceAPI "github.com/matrix-org/dendrite/appservice/api" + roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" +) + +type MockRoomserverAliasAPIDatabase struct{} + +func (db MockRoomserverAliasAPIDatabase) SetRoomAlias(ctx context.Context, alias string, roomID string) error { + return nil +} + +func (db MockRoomserverAliasAPIDatabase) GetRoomIDForAlias(ctx context.Context, alias string) (string, error) { + return "123", nil +} + +func (db MockRoomserverAliasAPIDatabase) GetAliasesForRoomID(ctx context.Context, roomID string) ([]string, error) { + aliases := make([]string, 0) + return aliases, nil +} + +func (db MockRoomserverAliasAPIDatabase) RemoveRoomAlias(ctx context.Context, alias string) error { + return nil +} + +type MockAppServiceQueryAPI struct{} + +func (q MockAppServiceQueryAPI) UserIDExists( + ctx context.Context, + req *appserviceAPI.UserIDExistsRequest, + resp *appserviceAPI.UserIDExistsResponse, +) error { + return nil +} + +func (q MockAppServiceQueryAPI) RoomAliasExists( + ctx context.Context, + req *appserviceAPI.RoomAliasExistsRequest, + resp *appserviceAPI.RoomAliasExistsResponse, +) error { + return fmt.Errorf("Should not have called this") +} + +func TestGetRoomIDForAlias(t *testing.T) { + type args struct { + ctx context.Context + request *roomserverAPI.GetRoomIDForAliasRequest + response *roomserverAPI.GetRoomIDForAliasResponse + } + + t.Run("Found local alias", func(t *testing.T) { + aliasAPI := &RoomserverAliasAPI{ + DB: MockRoomserverAliasAPIDatabase{}, + AppserviceAPI: MockAppServiceQueryAPI{}, + } + args := args{ + context.Background(), + &roomserverAPI.GetRoomIDForAliasRequest{}, + &roomserverAPI.GetRoomIDForAliasResponse{}, + } + + err := aliasAPI.GetRoomIDForAlias(args.ctx, args.request, args.response) + if err != nil { + t.Errorf("Got %s; wanted no error", err) + } + + if args.response.RoomID != "123" { + t.Errorf("Got %s; wanted 123", args.response.RoomID) + } + }) +}