Skip to content

Commit

Permalink
Use the last read message id
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Sep 26, 2018
1 parent 1a58b84 commit d379cd6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
]]></description>

<version>4.99.0</version>
<version>4.99.1</version>
<licence>agpl</licence>

<author>Daniel Calviño Sánchez</author>
Expand Down
4 changes: 2 additions & 2 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
19 changes: 19 additions & 0 deletions lib/Chat/CommentsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 5 additions & 6 deletions lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/Migration/Version4099Date20180831082627.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand Down
36 changes: 24 additions & 12 deletions lib/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

namespace OCA\Spreed;

use OCA\Spreed\Exceptions\ParticipantNotFoundException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit d379cd6

Please sign in to comment.