have setting room aliases use sender IDs

- deleting already used them, but since setting still used user IDs this caused bugs
- also, should we even link pseudo IDs and room aliases? Question for future portable IDs work
This commit is contained in:
Sam Wedgwood 2023-07-26 13:46:26 +01:00
parent 79d4a0e399
commit 4b4fab209c
6 changed files with 37 additions and 11 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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