|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Talk\BackgroundJob; |
| 11 | + |
| 12 | +use OCA\Talk\AppInfo\Application; |
| 13 | +use OCA\Talk\Chat\ChatManager; |
| 14 | +use OCA\Talk\Chat\MessageParser; |
| 15 | +use OCA\Talk\Exceptions\ParticipantNotFoundException; |
| 16 | +use OCA\Talk\Exceptions\RoomNotFoundException; |
| 17 | +use OCA\Talk\Manager; |
| 18 | +use OCA\Talk\Model\Attendee; |
| 19 | +use OCA\Talk\Model\ScheduledMessage; |
| 20 | +use OCA\Talk\Model\Thread; |
| 21 | +use OCA\Talk\Participant; |
| 22 | +use OCA\Talk\Service\ParticipantService; |
| 23 | +use OCA\Talk\Service\ScheduledMessageService; |
| 24 | +use OCA\Talk\Service\ThreadService; |
| 25 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 26 | +use OCP\BackgroundJob\TimedJob; |
| 27 | +use OCP\Comments\MessageTooLongException; |
| 28 | +use OCP\Comments\NotFoundException; |
| 29 | +use OCP\IL10N; |
| 30 | +use OCP\L10N\IFactory; |
| 31 | +use Psr\Log\LoggerInterface; |
| 32 | + |
| 33 | +/** |
| 34 | + * Class SendScheduledMessages |
| 35 | + * |
| 36 | + * @package OCA\Talk\BackgroundJob |
| 37 | + */ |
| 38 | +class SendScheduledMessages extends TimedJob { |
| 39 | + private readonly IL10N $l10n; |
| 40 | + public function __construct( |
| 41 | + private ScheduledMessageService $scheduledMessageService, |
| 42 | + private ParticipantService $participantService, |
| 43 | + private Manager $manager, |
| 44 | + private ChatManager $chatManager, |
| 45 | + private MessageParser $messageParser, |
| 46 | + private ThreadService $threadService, |
| 47 | + private IFactory $l10nFactory, |
| 48 | + private LoggerInterface $logger, |
| 49 | + ITimeFactory $time, |
| 50 | + ) { |
| 51 | + // Every minute |
| 52 | + $this->setInterval(60); |
| 53 | + $this->l10n = $this->l10nFactory->get(Application::APP_ID); |
| 54 | + parent::__construct($time); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritDoc |
| 59 | + */ |
| 60 | + #[\Override] |
| 61 | + protected function run($argument): void { |
| 62 | + $messages = $this->scheduledMessageService->getDue($this->time->getDateTime()); |
| 63 | + if (empty($messages)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $rooms = []; |
| 68 | + /** @var ScheduledMessage $message */ |
| 69 | + foreach ($messages as $message) { |
| 70 | + if (!isset($rooms[$message->getRoomId()])) { |
| 71 | + try { |
| 72 | + $rooms[$message->getRoomId()] = $this->manager->getRoomById($message->getRoomId()); |
| 73 | + } catch (RoomNotFoundException) { |
| 74 | + // This shouldn't happen |
| 75 | + // What to do, what to do |
| 76 | + continue; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $room = $rooms[$message->getRoomId()]; |
| 81 | + try { |
| 82 | + $participant = $this->participantService->getParticipantByActor($room, $message->getActorType(), $message->getActorId()); |
| 83 | + } catch (ParticipantNotFoundException) { |
| 84 | + $this->scheduledMessageService->deleteMessage($room, (int)$message->getId(), $message->getActorType(), $message->getActorId()); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if ($room->isFederatedConversation()) { |
| 89 | + // skip for now |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (($participant->getPermissions() & Attendee::PERMISSIONS_CHAT) === 0) { |
| 94 | + $this->scheduledMessageService->deleteMessage($room, (int)$message->getId(), $message->getActorType(), $message->getActorId()); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if ($room->getReadOnly()) { |
| 99 | + $this->scheduledMessageService->deleteMessage($room, (int)$message->getId(), $message->getActorType(), $message->getActorId()); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + $parent = $parentMessage = null; |
| 104 | + if ($message->getParentId() !== 0) { |
| 105 | + try { |
| 106 | + $parent = $this->chatManager->getParentComment($room, (string)$message->getParentId()); |
| 107 | + } catch (NotFoundException $e) { |
| 108 | + // Log and continue or delete? |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + $parentMessage = $this->messageParser->createMessage($room, $participant, $parent, $this->l10n); |
| 113 | + $this->messageParser->parseMessage($parentMessage); |
| 114 | + if (!$parentMessage->isReplyable()) { |
| 115 | + // Log and continue or delete? |
| 116 | + continue; |
| 117 | + } |
| 118 | + } elseif ($message->getThreadId() > 0) { |
| 119 | + if (!$this->threadService->validateThread($room->getId(), $message->getThreadId())) { |
| 120 | + $message->setThreadId(0); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + $this->participantService->ensureOneToOneRoomIsFilled($room); |
| 125 | + |
| 126 | + try { |
| 127 | + $metaData = $message->getDecodedMetaData(); |
| 128 | + $threadId = $message->getThreadId(); |
| 129 | + $threadTitle = $metaData[ScheduledMessage::METADATA_THREAD_TITLE]; |
| 130 | + $comment = $this->chatManager->sendMessage($room, |
| 131 | + $participant, |
| 132 | + $message->getActorType(), |
| 133 | + $message->getActorId(), |
| 134 | + $message->getMessage(), |
| 135 | + $this->time->getDateTime(), |
| 136 | + $parent, |
| 137 | + '', |
| 138 | + $metaData[ScheduledMessage::METADATA_SILENT] ?? false, |
| 139 | + threadId: $threadId); |
| 140 | + if ($threadId === Thread::THREAD_CREATE && $threadTitle !== '') { |
| 141 | + $thread = $this->threadService->createThread($room, (int)$comment->getId(), $threadTitle); |
| 142 | + // Add to subscribed threads list |
| 143 | + $this->threadService->setNotificationLevel($participant->getAttendee(), $thread->getId(), Participant::NOTIFY_DEFAULT); |
| 144 | + |
| 145 | + $this->chatManager->addSystemMessage( |
| 146 | + $room, |
| 147 | + $participant, |
| 148 | + $participant->getAttendee()->getActorType(), |
| 149 | + $participant->getAttendee()->getActorId(), |
| 150 | + json_encode(['message' => 'thread_created', 'parameters' => ['thread' => (int)$comment->getId(), 'title' => $thread->getName()]]), |
| 151 | + $this->time->getDateTime(), |
| 152 | + false, |
| 153 | + null, |
| 154 | + $comment, |
| 155 | + true, |
| 156 | + true |
| 157 | + ); |
| 158 | + } |
| 159 | + } catch (MessageTooLongException) { |
| 160 | + continue; |
| 161 | + } catch (\Exception $e) { |
| 162 | + $this->logger->warning($e->getMessage()); |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + $this->scheduledMessageService->deleteMessage($room, (int)$message->getId(), $message->getActorType(), $message->getActorId()); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments