diff --git a/roomserver/storage/interface.go b/roomserver/storage/interface.go index 149d5849b..c048ac3d6 100644 --- a/roomserver/storage/interface.go +++ b/roomserver/storage/interface.go @@ -204,7 +204,8 @@ type UserRoomKeys interface { InsertUserRoomPublicKey(ctx context.Context, userID spec.UserID, roomID spec.RoomID, key ed25519.PublicKey) (result ed25519.PublicKey, err error) // SelectUserRoomPrivateKey selects the private key for the given user and room combination SelectUserRoomPrivateKey(ctx context.Context, userID spec.UserID, roomID spec.RoomID) (key ed25519.PrivateKey, err error) - // SelectUserIDsForPublicKeys selects all userIDs for the requested senderKeys. Returns a map from roomID -> map from publicKey to userID + // 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. SelectUserIDsForPublicKeys(ctx context.Context, publicKeys map[spec.RoomID][]ed25519.PublicKey) (map[spec.RoomID]map[string]string, error) } diff --git a/roomserver/storage/postgres/user_room_keys_table.go b/roomserver/storage/postgres/user_room_keys_table.go index 630f5afba..22f978bf0 100644 --- a/roomserver/storage/postgres/user_room_keys_table.go +++ b/roomserver/storage/postgres/user_room_keys_table.go @@ -51,7 +51,7 @@ const insertUserRoomPublicKeySQL = ` const selectUserRoomKeySQL = `SELECT pseudo_id_key FROM roomserver_user_room_keys WHERE user_nid = $1 AND room_nid = $2` -const selectUserNIDsSQL = `SELECT user_nid, room_nid, pseudo_id_pub_key FROM roomserver_user_room_keys WHERE pseudo_id_pub_key = ANY($1)` +const selectUserNIDsSQL = `SELECT user_nid, room_nid, pseudo_id_pub_key FROM roomserver_user_room_keys WHERE room_nid = ANY($1) AND pseudo_id_pub_key = ANY($2)` type userRoomKeysStatements struct { insertUserRoomPrivateKeyStmt *sql.Stmt @@ -113,7 +113,7 @@ func (s *userRoomKeysStatements) BulkSelectUserNIDs(ctx context.Context, txn *sq senders = append(senders, key) } } - rows, err := stmt.QueryContext(ctx, pq.Array(senders)) + rows, err := stmt.QueryContext(ctx, pq.Array(roomNIDs), pq.Array(senders)) if err != nil { return nil, err } diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index 4466b5b28..a01d393d9 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -1733,14 +1733,14 @@ func (d *Database) SelectUserIDsForPublicKeys(ctx context.Context, publicKeys ma nids = append(nids, nid.EventStateKeyNID) } // get the userIDs - nidMAP, seErr := d.EventStateKeys(ctx, nids) + nidMap, seErr := d.EventStateKeys(ctx, nids) if seErr != nil { return seErr } // build the result map (roomID -> map publicKey -> userID) for publicKey, userRoomKeyPair := range userRoomKeyPairMap { - userID := nidMAP[userRoomKeyPair.EventStateKeyNID] + userID := nidMap[userRoomKeyPair.EventStateKeyNID] roomID := rooms[userRoomKeyPair.RoomNID] resMap, exists := result[roomID] if !exists { diff --git a/roomserver/storage/tables/interface.go b/roomserver/storage/tables/interface.go index 8f2422a32..cd0e51686 100644 --- a/roomserver/storage/tables/interface.go +++ b/roomserver/storage/tables/interface.go @@ -194,6 +194,7 @@ type UserRoomKeys interface { // SelectUserRoomPrivateKey selects the private key for the given user and room combination SelectUserRoomPrivateKey(ctx context.Context, txn *sql.Tx, userNID types.EventStateKeyNID, roomNID types.RoomNID) (ed25519.PrivateKey, error) // BulkSelectUserNIDs selects all userIDs for the requested senderKeys. Returns a map from publicKey -> types.UserRoomKeyPair. + // If a senderKey can't be found, it is omitted in the result. BulkSelectUserNIDs(ctx context.Context, txn *sql.Tx, senderKeys map[types.RoomNID][]ed25519.PublicKey) (map[string]types.UserRoomKeyPair, error) }