fix(pinned): Handle message expiration

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2025-11-03 16:21:39 +01:00
parent 3e32fbb0dd
commit 8851d2b545
No known key found for this signature in database
GPG key ID: F72FA5B49FFA96B0
2 changed files with 22 additions and 0 deletions

View file

@ -12,6 +12,9 @@ namespace OCA\Talk\BackgroundJob;
use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attachment;
use OCA\Talk\Service\AttachmentService;
use OCA\Talk\Service\RoomService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\Comments\NotFoundException;
@ -21,6 +24,8 @@ class UnpinMessage extends QueuedJob {
ITimeFactory $time,
protected Manager $manager,
protected ChatManager $chatManager,
protected AttachmentService $attachmentService,
protected RoomService $roomService,
) {
parent::__construct($time);
}
@ -42,6 +47,15 @@ class UnpinMessage extends QueuedJob {
try {
$comment = $this->chatManager->getComment($room, (string)$messageId);
} catch (NotFoundException) {
// Message most likely expired, reset the last_pinned_id if matching
if ($room->getLastPinnedId() === $messageId) {
$newLastPinned = 0;
$attachments = $this->attachmentService->getAttachmentsByType($room, Attachment::TYPE_PINNED, 0, 1);
if (isset($attachments[0])) {
$newLastPinned = $attachments[0]->getMessageId();
}
$this->roomService->setLastPinnedId($room, $newLastPinned);
}
return;
}

View file

@ -775,6 +775,14 @@ class ChatManager {
$comment,
);
if ($pinUntil === 0) {
// Pinned without expiration, pin until message expiration, if applicable
$pinUntil = $message->getExpireDate()?->getTimestamp() ?? 0;
} elseif ($message->getExpireDate()) {
// When the message expires, expire the pin latest at that time
$pinUntil = min($pinUntil, $message->getExpireDate()->getTimestamp());
}
$metaData[Message::METADATA_PINNED_MESSAGE_ID] = (int)$message->getId();
$metaData[Message::METADATA_PINNED_BY_TYPE] = $participant->getAttendee()->getActorType();
$metaData[Message::METADATA_PINNED_BY_ID] = $participant->getAttendee()->getActorId();