diff --git a/clientapi/routing/directory.go b/clientapi/routing/directory.go index d9129d1bd..e914b2905 100644 --- a/clientapi/routing/directory.go +++ b/clientapi/routing/directory.go @@ -181,10 +181,37 @@ func SetLocalAlias( return *resErr } + maybeRoomID, err := spec.NewRoomID(r.RoomID) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: spec.InvalidParam("invalid room ID"), + } + } + roomID := *maybeRoomID // should be safe due to error check + + maybeUserID, err := spec.NewUserID(device.UserID, true) + if err != nil { + return util.JSONResponse{ + Code: http.StatusInternalServerError, + JSON: spec.Unknown("internal server error"), + } + } + userID := *maybeUserID + + senderID, err := rsAPI.QuerySenderIDForUser(req.Context(), roomID, userID) + if err != nil { + util.GetLogger(req.Context()).WithError(err).Error("QuerySenderIDForUser failed") + return util.JSONResponse{ + Code: http.StatusInternalServerError, + JSON: spec.Unknown("internal server error"), + } + } + queryReq := roomserverAPI.SetRoomAliasRequest{ - UserID: device.UserID, - RoomID: r.RoomID, - Alias: alias, + SenderID: senderID, + RoomID: r.RoomID, + Alias: alias, } var queryRes roomserverAPI.SetRoomAliasResponse if err := rsAPI.SetRoomAlias(req.Context(), &queryReq, &queryRes); err != nil { diff --git a/roomserver/api/alias.go b/roomserver/api/alias.go index c091cf6a3..5a6d48000 100644 --- a/roomserver/api/alias.go +++ b/roomserver/api/alias.go @@ -23,7 +23,7 @@ import ( // SetRoomAliasRequest is a request to SetRoomAlias type SetRoomAliasRequest struct { // ID of the user setting the alias - UserID string `json:"user_id"` + SenderID spec.SenderID `json:"sender"` // New alias for the room Alias string `json:"alias"` // The room ID the alias is referring to diff --git a/roomserver/internal/alias.go b/roomserver/internal/alias.go index b04a56fe8..a3db9e97f 100644 --- a/roomserver/internal/alias.go +++ b/roomserver/internal/alias.go @@ -51,7 +51,7 @@ func (r *RoomserverInternalAPI) SetRoomAlias( response.AliasExists = false // Save the new alias - if err := r.DB.SetRoomAlias(ctx, request.Alias, request.RoomID, request.UserID); err != nil { + if err := r.DB.SetRoomAlias(ctx, request.Alias, request.RoomID, string(request.SenderID)); err != nil { return err } diff --git a/roomserver/internal/perform/perform_create_room.go b/roomserver/internal/perform/perform_create_room.go index 12e756c2e..247a5bcd4 100644 --- a/roomserver/internal/perform/perform_create_room.go +++ b/roomserver/internal/perform/perform_create_room.go @@ -434,9 +434,9 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo // been taken. if roomAlias != "" { aliasReq := api.SetRoomAliasRequest{ - Alias: roomAlias, - RoomID: roomID.String(), - UserID: userID.String(), + SenderID: senderID, + Alias: roomAlias, + RoomID: roomID.String(), } var aliasResp api.SetRoomAliasResponse diff --git a/roomserver/internal/perform/perform_upgrade.go b/roomserver/internal/perform/perform_upgrade.go index 32f547dc1..ff0464c58 100644 --- a/roomserver/internal/perform/perform_upgrade.go +++ b/roomserver/internal/perform/perform_upgrade.go @@ -188,7 +188,7 @@ func moveLocalAliases(ctx context.Context, return fmt.Errorf("Failed to remove old room alias: %w", err) } - setAliasReq := api.SetRoomAliasRequest{UserID: userID.String(), Alias: alias, RoomID: newRoomID} + setAliasReq := api.SetRoomAliasRequest{SenderID: senderID, Alias: alias, RoomID: newRoomID} setAliasRes := api.SetRoomAliasResponse{} if err = URSAPI.SetRoomAlias(ctx, &setAliasReq, &setAliasRes); err != nil { return fmt.Errorf("Failed to set new room alias: %w", err) diff --git a/roomserver/roomserver_test.go b/roomserver/roomserver_test.go index ce0721bea..d24a31f61 100644 --- a/roomserver/roomserver_test.go +++ b/roomserver/roomserver_test.go @@ -274,9 +274,8 @@ func TestPurgeRoom(t *testing.T) { if !isPublished { t.Fatalf("room should be published before purging") } - aliasResp := &api.SetRoomAliasResponse{} - if err = rsAPI.SetRoomAlias(ctx, &api.SetRoomAliasRequest{RoomID: room.ID, Alias: "myalias", UserID: alice.ID}, aliasResp); err != nil { + if err = rsAPI.SetRoomAlias(ctx, &api.SetRoomAliasRequest{RoomID: room.ID, Alias: "myalias", SenderID: spec.SenderID(alice.ID)}, aliasResp); err != nil { t.Fatal(err) } // check the alias is actually there