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 <serra@allgood.dev>
This commit is contained in:
Serra Allgood 2019-05-20 22:56:58 -07:00
parent bb87f792e9
commit 248d44cc4a
2 changed files with 98 additions and 6 deletions

View file

@ -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

View file

@ -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)
}
})
}