feat(thread): Track last activity of a thread

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling
2025-07-10 15:07:35 +02:00
parent 33dd6eaa02
commit 8181840a98
5 changed files with 70 additions and 3 deletions

View File

@ -18,7 +18,7 @@
* 🌉 **Sync with other chat solutions** With [Matterbridge](https://github.com/42wim/matterbridge/) being integrated in Talk, you can easily sync a lot of other chat solutions to Nextcloud Talk and vice-versa.
]]></description>
<version>22.0.0-dev.9</version>
<version>22.0.0-dev.10</version>
<licence>agpl</licence>
<author>Anna Larch</author>

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Override;
/**
* Add the last activity (for sorting) and thread name for future feature to name a thread
*/
class Version22000Date20250710124258 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
#[Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$table = $schema->getTable('talk_threads');
if (!$table->hasColumn('last_activity')) {
$table->addColumn('last_activity', Types::DATETIME, [
'notnull' => false,
]);
$table->addColumn('name', Types::STRING, [
'notnull' => false,
'length' => 255,
'default' => '',
]);
$table->addIndex(['last_activity'], 'talkthread_lastactive');
}
return $schema;
}
}

View File

@ -20,6 +20,10 @@ use OCP\DB\Types;
* @method int getLastMessageId()
* @method void setNumReplies(int $numReplies)
* @method int getNumReplies()
* @method void setLastActivity(\DateTime $lastActivity)
* @method \DateTime|null getLastActivity()
* @method void setName(string $name)
* @method string getName()
*
* @psalm-import-type TalkThread from ResponseDefinitions
*/
@ -27,11 +31,15 @@ class Thread extends Entity implements \JsonSerializable {
protected int $roomId = 0;
protected int $lastMessageId = 0;
protected int $numReplies = 0;
protected ?\DateTime $lastActivity = null;
protected string $name = '';
public function __construct() {
$this->addType('roomId', Types::BIGINT);
$this->addType('lastMessageId', Types::BIGINT);
$this->addType('numReplies', Types::BIGINT);
$this->addType('lastActivity', Types::DATETIME);
$this->addType('name', Types::STRING);
}
/**
@ -41,9 +49,11 @@ class Thread extends Entity implements \JsonSerializable {
public function jsonSerialize(): array {
return [
'id' => max(1, $this->getId()),
'roomId' => max(1, $this->getRoomId()),
// 'roomId' => max(1, $this->getRoomId()),
'lastMessageId' => max(0, $this->getLastMessageId()),
'numReplies' => max(0, $this->getNumReplies()),
'lastActivity' => max(0, $this->getLastActivity()?->getTimestamp() ?? 0),
// 'name' => $this->getName(),
];
}
}

View File

@ -449,8 +449,8 @@ namespace OCA\Talk;
*
* @psalm-type TalkThread = array{
* id: positive-int,
* roomId: positive-int,
* lastMessageId: non-negative-int,
* lastActivity: non-negative-int,
* numReplies: non-negative-int,
* }
*

View File

@ -16,6 +16,7 @@ use OCA\Talk\Model\ThreadAttendeeMapper;
use OCA\Talk\Model\ThreadMapper;
use OCA\Talk\Room;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@ -26,6 +27,7 @@ class ThreadService {
protected IDBConnection $connection,
protected ThreadMapper $threadMapper,
protected ThreadAttendeeMapper $threadAttendeeMapper,
protected ITimeFactory $timeFactory,
) {
}
@ -37,6 +39,7 @@ class ThreadService {
$thread->setNumReplies($info['num_replies']);
$thread->setLastMessageId($info['last_message_id']);
$thread->setRoomId($room->getId());
$thread->setLastActivity($this->timeFactory->getDateTime());
try {
$this->threadMapper->insert($thread);
@ -125,15 +128,19 @@ class ThreadService {
}
public function updateLastMessageInfoAfterReply(Thread $thread, int $lastMessageId): void {
$dateTime = $this->timeFactory->getDateTime();
$query = $this->connection->getQueryBuilder();
$query->update('talk_threads')
->set('num_replies', $query->func()->add('num_replies', $query->expr()->literal(1)))
->set('last_message_id', $query->createNamedParameter($lastMessageId))
->set('last_activity', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATETIME_MUTABLE))
->where($query->expr()->eq('id', $query->createNamedParameter($thread->getId())));
$query->executeStatement();
$thread->setNumReplies($thread->getNumReplies() + 1);
$thread->setLastMessageId($lastMessageId);
$thread->setLastActivity($dateTime);
}
/**