diff options
author | Joas Schilling <coding@schilljs.com> | 2020-12-08 16:46:54 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-12-10 13:47:04 +0300 |
commit | bc716c7199a1ed315e63d84e3f5f4501800af68b (patch) | |
tree | e4669f0343be5b4e0972717cf5726c4d0e17a63d /lib/Service | |
parent | 6eefd9c3fae1d66d3a277f0b1e3d2a8e3a02418c (diff) |
Add the last common read message id as header on chat requests and to the conversation list
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/ParticipantService.php | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/Service/ParticipantService.php b/lib/Service/ParticipantService.php index c0f41fc03..af731d060 100644 --- a/lib/Service/ParticipantService.php +++ b/lib/Service/ParticipantService.php @@ -491,6 +491,46 @@ class ParticipantService { $query->execute(); } + public function getLastCommonReadChatMessage(Room $room): int { + $query = $this->connection->getQueryBuilder(); + $query->selectAlias($query->func()->min('last_read_message'), 'last_common_read_message') + ->from('talk_attendees') + ->where($query->expr()->eq('actor_type', $query->createNamedParameter(Attendee::ACTOR_USERS))) + ->andWhere($query->expr()->eq('room_id', $query->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->eq('read_privacy', $query->createNamedParameter(Participant::PRIVACY_PUBLIC, IQueryBuilder::PARAM_INT))); + + $result = $query->execute(); + $row = $result->fetch(); + $result->closeCursor(); + + return (int) ($row['last_common_read_message'] ?? 0); + } + + /** + * @param int[] $roomIds + * @return array A map of roomId => "last common read message id" + * @psalm-return array<int, int> + */ + public function getLastCommonReadChatMessageForMultipleRooms(array $roomIds): array { + $query = $this->connection->getQueryBuilder(); + $query->select('room_id') + ->selectAlias($query->func()->min('last_read_message'), 'last_common_read_message') + ->from('talk_attendees') + ->where($query->expr()->eq('actor_type', $query->createNamedParameter(Attendee::ACTOR_USERS))) + ->andWhere($query->expr()->in('room_id', $query->createNamedParameter($roomIds, IQueryBuilder::PARAM_INT_ARRAY))) + ->andWhere($query->expr()->eq('read_privacy', $query->createNamedParameter(Participant::PRIVACY_PUBLIC, IQueryBuilder::PARAM_INT))) + ->groupBy('room_id'); + + $commonReads = array_fill_keys($roomIds, 0); + $result = $query->execute(); + while ($row = $result->fetch()) { + $commonReads[(int) $row['room_id']] = (int) $row['last_common_read_message']; + } + $result->closeCursor(); + + return $commonReads; + } + /** * @param Room $room * @return Participant[] |