From 4b12b69044bad4f2a4edbaa8ce148b0182899a6d Mon Sep 17 00:00:00 2001 From: Devon Hudson Date: Tue, 13 Jun 2023 16:10:35 +0100 Subject: [PATCH] Switch to using new db table for room userID lookup --- roomserver/internal/query/query.go | 22 ++++++++++++++++++++-- roomserver/storage/interface.go | 4 ++-- roomserver/storage/shared/storage.go | 5 ----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/roomserver/internal/query/query.go b/roomserver/internal/query/query.go index 3e91b77b3..19fd456b5 100644 --- a/roomserver/internal/query/query.go +++ b/roomserver/internal/query/query.go @@ -16,6 +16,7 @@ package query import ( "context" + "crypto/ed25519" "database/sql" "errors" "fmt" @@ -1012,6 +1013,23 @@ func (r *Queryer) QueryUserIDForSender(ctx context.Context, roomID spec.RoomID, if err == nil { return userID, nil } - // TODO: pseudoIDs - return r.DB.GetUserIDForSender(ctx, roomID.String(), senderID) + + bytes := spec.Base64Bytes{} + err = bytes.Decode(string(senderID)) + if err != nil { + return nil, err + } + queryMap := map[spec.RoomID][]ed25519.PublicKey{roomID: {ed25519.PublicKey(bytes)}} + result, err := r.DB.SelectUserIDsForPublicKeys(ctx, queryMap) + if err != nil { + return nil, err + } + + if userKeys, ok := result[roomID]; ok { + if userID, ok := userKeys[string(senderID)]; ok { + return spec.NewUserID(userID, true) + } + } + + return nil, nil } diff --git a/roomserver/storage/interface.go b/roomserver/storage/interface.go index 19cbdb346..447b90552 100644 --- a/roomserver/storage/interface.go +++ b/roomserver/storage/interface.go @@ -168,8 +168,6 @@ type Database interface { GetServerInRoom(ctx context.Context, roomNID types.RoomNID, serverName spec.ServerName) (bool, error) // GetKnownUsers searches all users that userID knows about. GetKnownUsers(ctx context.Context, userID, searchString string, limit int) ([]string, error) - // GetKnownUsers tries to obtain the current mxid for a given user. - GetUserIDForSender(ctx context.Context, roomID string, senderID spec.SenderID) (*spec.UserID, error) // GetKnownRooms returns a list of all rooms we know about. GetKnownRooms(ctx context.Context) ([]string, error) // ForgetRoom sets a flag in the membership table, that the user wishes to forget a specific room @@ -207,6 +205,8 @@ type UserRoomKeys interface { SelectUserRoomPublicKey(ctx context.Context, userID spec.UserID, roomID spec.RoomID) (key ed25519.PublicKey, err error) // SelectUserIDsForPublicKeys selects all userIDs for the requested senderKeys. Returns a map from roomID -> map from publicKey to userID. // If a senderKey can't be found, it is omitted in the result. + // TODO: Why is the result map indexed by string not public key? + // TODO: Shouldn't the input & result map be changed to be indexed by string instead of the RoomID struct? SelectUserIDsForPublicKeys(ctx context.Context, publicKeys map[spec.RoomID][]ed25519.PublicKey) (map[spec.RoomID]map[string]string, error) } diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index da8732f64..1cd0f23a2 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -1546,11 +1546,6 @@ func (d *Database) GetKnownUsers(ctx context.Context, userID, searchString strin return d.MembershipTable.SelectKnownUsers(ctx, nil, stateKeyNID, searchString, limit) } -func (d *Database) GetUserIDForSender(ctx context.Context, roomID string, senderID spec.SenderID) (*spec.UserID, error) { - // TODO: Use real logic once DB for pseudoIDs is in place - return spec.NewUserID(string(senderID), true) -} - // GetKnownRooms returns a list of all rooms we know about. func (d *Database) GetKnownRooms(ctx context.Context) ([]string, error) { return d.RoomsTable.SelectRoomIDsWithEvents(ctx, nil)