diff --git a/appinfo/info.xml b/appinfo/info.xml index b3cde7ff1426..6ddd2151433f 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -17,7 +17,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m ]]> - 4.99.0 + 4.99.1 agpl Daniel Calviño Sánchez diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 688a54795eb8..8217c25a433b 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -149,8 +149,8 @@ public function getUnreadMarker(Room $chat, IUser $user): \DateTime { return $marker; } - public function getUnreadCount(Room $chat, \DateTime $unreadSince): int { - return $this->commentsManager->getNumberOfCommentsForObject('chat', $chat->getId(), $unreadSince, 'comment'); + public function getUnreadCount(Room $chat, int $lastReadMessage): int { + return $this->commentsManager->getNumberOfCommentsForObjectSinceComment('chat', $chat->getId(), $lastReadMessage, 'comment'); } /** diff --git a/lib/Chat/CommentsManager.php b/lib/Chat/CommentsManager.php index bbfe9d2c17f5..4b78d366e5fa 100644 --- a/lib/Chat/CommentsManager.php +++ b/lib/Chat/CommentsManager.php @@ -73,4 +73,23 @@ public function getLastCommentDateByActor( return $lastComments; } + + public function getNumberOfCommentsForObjectSinceComment($objectType, $objectId, $lastRead, $verb = ''): int { + $query = $this->dbConn->getQueryBuilder(); + $query->selectAlias($query->createFunction('COUNT(' . $query->getColumnName('id') . ')'), 'num_messages') + ->from('comments') + ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType))) + ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))) + ->andWhere($query->expr()->gt('id', $query->createNamedParameter($lastRead))); + + if ($verb !== '') { + $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); + } + + $result = $query->execute(); + $data = $result->fetch(); + $result->closeCursor(); + + return isset($data['num_messages']) ? (int) $data['num_messages'] : 0; + } } diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index ec598fc0c7c3..773826036704 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -231,12 +231,11 @@ protected function formatRoom(Room $room, Participant $participant = null): arra $currentUser = $this->userManager->get($this->userId); if ($currentUser instanceof IUser) { - $unreadSince = $this->chatManager->getUnreadMarker($room, $currentUser); - if ($participant instanceof Participant) { - $lastMention = $participant->getLastMention(); - $roomData['unreadMention'] = $lastMention !== null && $unreadSince < $lastMention; - } - $roomData['unreadMessages'] = $this->chatManager->getUnreadCount($room, $unreadSince); + $lastReadMessage = $participant->getLastReadMessage(); + $roomData['unreadMessages'] = $this->chatManager->getUnreadCount($room, $lastReadMessage); + + $lastMention = $participant->getLastMentionMessage(); + $roomData['unreadMention'] = $lastMention !== 0 && $lastReadMessage < $lastMention; } // Sort by lastPing diff --git a/lib/Manager.php b/lib/Manager.php index 908b9c3ce58b..f879f81c1b50 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -116,12 +116,7 @@ public function createRoomObject(array $row) { * @return Participant */ public function createParticipantObject(Room $room, array $row) { - $lastMention = null; - if (!empty($row['last_mention'])) { - $lastMention = new \DateTime($row['last_mention']); - } - - return new Participant($this->db, $room, (string) $row['user_id'], (int) $row['participant_type'], (int) $row['last_ping'], (string) $row['session_id'], (bool) $row['in_call'], (bool) $row['favorite'], (int) $row['last_read_message'], $lastMention); + return new Participant($this->db, $room, (string) $row['user_id'], (int) $row['participant_type'], (int) $row['last_ping'], (string) $row['session_id'], (bool) $row['in_call'], (bool) $row['favorite'], (int) $row['last_read_message'], (int) $row['last_mention_message']); } /** diff --git a/lib/Migration/Version4099Date20180831082627.php b/lib/Migration/Version4099Date20180831082627.php index 7d9e1d6a9f2f..e6d524010eb5 100644 --- a/lib/Migration/Version4099Date20180831082627.php +++ b/lib/Migration/Version4099Date20180831082627.php @@ -52,8 +52,12 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op $schema = $schemaClosure(); $table = $schema->getTable('talk_participants'); - if (!$table->hasColumn('last_message_read')) { - $table->addColumn('last_message_read', Type::BIGINT, [ + if (!$table->hasColumn('last_read_message')) { + $table->addColumn('last_read_message', Type::BIGINT, [ + 'default' => 0, + 'notnull' => false, + ]); + $table->addColumn('last_mention_message', Type::BIGINT, [ 'default' => 0, 'notnull' => false, ]); diff --git a/lib/Participant.php b/lib/Participant.php index d26d70a3bfcf..cd67c9d3f5ba 100644 --- a/lib/Participant.php +++ b/lib/Participant.php @@ -24,7 +24,6 @@ namespace OCA\Spreed; -use OCA\Spreed\Exceptions\ParticipantNotFoundException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -58,10 +57,10 @@ class Participant { private $isFavorite; /** @var int */ private $lastReadMessage; - /** @var \DateTime|null */ - private $lastMention; + /** @var int */ + private $lastMentionMessage; - public function __construct(IDBConnection $db, Room $room, string $user, int $participantType, int $lastPing, string $sessionId, int $inCall, bool $isFavorite, int $lastReadMessage, \DateTime $lastMention = null) { + public function __construct(IDBConnection $db, Room $room, string $user, int $participantType, int $lastPing, string $sessionId, int $inCall, bool $isFavorite, int $lastReadMessage, int $lastMentionMessage) { $this->db = $db; $this->room = $room; $this->user = $user; @@ -71,7 +70,7 @@ public function __construct(IDBConnection $db, Room $room, string $user, int $pa $this->inCall = $inCall; $this->isFavorite = $isFavorite; $this->lastReadMessage = $lastReadMessage; - $this->lastMention = $lastMention; + $this->lastMentionMessage = $lastMentionMessage; } public function getUser(): string { @@ -94,13 +93,6 @@ public function getInCallFlags(): int { return $this->inCall; } - /** - * @return \DateTime|null - */ - public function getLastMention() { - return $this->lastMention; - } - public function isFavorite(): bool { return $this->isFavorite; } @@ -140,4 +132,24 @@ public function setLastReadMessage(int $messageId): bool { $this->lastReadMessage = $messageId; return true; } + + public function getLastMentionMessage(): int { + return $this->lastMentionMessage; + } + + public function setLastMentionMessage(int $messageId): bool { + if (!$this->user) { + return false; + } + + $query = $this->db->getQueryBuilder(); + $query->update('talk_participants') + ->set('last_mention_message', $query->createNamedParameter($messageId, IQueryBuilder::PARAM_INT)) + ->where($query->expr()->eq('user_id', $query->createNamedParameter($this->user))) + ->andWhere($query->expr()->eq('room_id', $query->createNamedParameter($this->room->getId()))); + $query->execute(); + + $this->lastMentionMessage = $messageId; + return true; + } }