|
3 | 3 | import 'package:drift/drift.dart'; |
4 | 4 | import 'package:stream_chat/stream_chat.dart'; |
5 | 5 | import 'package:stream_chat_persistence/src/db/drift_chat_database.dart'; |
| 6 | +import 'package:stream_chat_persistence/src/db/query_utils.dart'; |
6 | 7 | import 'package:stream_chat_persistence/src/entity/locations.dart'; |
7 | 8 | import 'package:stream_chat_persistence/src/mapper/mapper.dart'; |
8 | 9 |
|
@@ -63,6 +64,38 @@ class LocationDao extends DatabaseAccessor<DriftChatDatabase> with _$LocationDao |
63 | 64 | return _locationFromEntity(result); |
64 | 65 | } |
65 | 66 |
|
| 67 | + /// Returns the shared location for every id in [messageIds], keyed by |
| 68 | + /// message id. Messages with no associated location are absent from the |
| 69 | + /// map. |
| 70 | + /// |
| 71 | + /// Returned `Location`s do not populate `Location.message` / |
| 72 | + /// `Location.channel`. The batched method is consumed by |
| 73 | + /// `MessageDao._messagesFromJoinRows`, which attaches the result as |
| 74 | + /// `Message.sharedLocation` on the message it is currently assembling — |
| 75 | + /// the linked fields would just be redundant copies of the parent Message |
| 76 | + /// and Channel. Hydrating them here would also re-enter |
| 77 | + /// `MessageDao.getMessageById` and open a cycle through quoted-message |
| 78 | + /// resolution whenever any link in the chain has a location. Consumers |
| 79 | + /// that need the linked `message` / `channel` should use |
| 80 | + /// [getLocationByMessageId] or [getLocationsByCid]. |
| 81 | + Future<Map<String, Location>> getLocationsByMessageIds( |
| 82 | + List<String> messageIds, |
| 83 | + ) async { |
| 84 | + if (messageIds.isEmpty) return const {}; |
| 85 | + final result = <String, Location>{}; |
| 86 | + for (final chunk in chunked(messageIds)) { |
| 87 | + final query = select(locations)..where((tbl) => tbl.messageId.isIn(chunk)); |
| 88 | + final entities = await query.get(); |
| 89 | + for (final entity in entities) { |
| 90 | + if (entity.messageId case final id?) { |
| 91 | + // Don't enrich message/channel (see method doc) |
| 92 | + result[id] = entity.toLocation(); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
66 | 99 | /// Update multiple locations |
67 | 100 | Future<void> updateLocations(List<Location> locationList) { |
68 | 101 | return batch( |
|
0 commit comments