You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

940 lines
34 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  5. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Talk\Controller;
  24. use OCA\Talk\Chat\AutoComplete\SearchPlugin;
  25. use OCA\Talk\Chat\AutoComplete\Sorter;
  26. use OCA\Talk\Chat\ChatManager;
  27. use OCA\Talk\Chat\MessageParser;
  28. use OCA\Talk\Chat\ReactionManager;
  29. use OCA\Talk\GuestManager;
  30. use OCA\Talk\MatterbridgeManager;
  31. use OCA\Talk\Model\Attachment;
  32. use OCA\Talk\Model\Attendee;
  33. use OCA\Talk\Model\Message;
  34. use OCA\Talk\Model\Session;
  35. use OCA\Talk\Participant;
  36. use OCA\Talk\Room;
  37. use OCA\Talk\Service\AttachmentService;
  38. use OCA\Talk\Service\ParticipantService;
  39. use OCA\Talk\Service\SessionService;
  40. use OCA\Talk\Share\RoomShareProvider;
  41. use OCP\App\IAppManager;
  42. use OCP\AppFramework\Http;
  43. use OCP\AppFramework\Http\DataResponse;
  44. use OCP\AppFramework\Utility\ITimeFactory;
  45. use OCP\Collaboration\AutoComplete\IManager;
  46. use OCP\Collaboration\Collaborators\ISearchResult;
  47. use OCP\Comments\IComment;
  48. use OCP\Comments\MessageTooLongException;
  49. use OCP\Comments\NotFoundException;
  50. use OCP\EventDispatcher\IEventDispatcher;
  51. use OCP\IL10N;
  52. use OCP\IRequest;
  53. use OCP\IUserManager;
  54. use OCP\RichObjectStrings\InvalidObjectExeption;
  55. use OCP\RichObjectStrings\IValidator;
  56. use OCP\Security\ITrustedDomainHelper;
  57. use OCP\Share\Exceptions\ShareNotFound;
  58. use OCP\User\Events\UserLiveStatusEvent;
  59. use OCP\UserStatus\IManager as IUserStatusManager;
  60. use OCP\UserStatus\IUserStatus;
  61. class ChatController extends AEnvironmentAwareController {
  62. private ?string $userId;
  63. private IUserManager $userManager;
  64. private IAppManager $appManager;
  65. private ChatManager $chatManager;
  66. private ReactionManager $reactionManager;
  67. private ParticipantService $participantService;
  68. private SessionService $sessionService;
  69. protected AttachmentService $attachmentService;
  70. private GuestManager $guestManager;
  71. /** @var string[] */
  72. protected array $guestNames;
  73. private MessageParser $messageParser;
  74. protected RoomShareProvider $shareProvider;
  75. private IManager $autoCompleteManager;
  76. private IUserStatusManager $statusManager;
  77. protected MatterbridgeManager $matterbridgeManager;
  78. private SearchPlugin $searchPlugin;
  79. private ISearchResult $searchResult;
  80. protected ITimeFactory $timeFactory;
  81. protected IEventDispatcher $eventDispatcher;
  82. protected IValidator $richObjectValidator;
  83. protected ITrustedDomainHelper $trustedDomainHelper;
  84. private IL10N $l;
  85. public function __construct(string $appName,
  86. ?string $UserId,
  87. IRequest $request,
  88. IUserManager $userManager,
  89. IAppManager $appManager,
  90. ChatManager $chatManager,
  91. ReactionManager $reactionManager,
  92. ParticipantService $participantService,
  93. SessionService $sessionService,
  94. AttachmentService $attachmentService,
  95. GuestManager $guestManager,
  96. MessageParser $messageParser,
  97. RoomShareProvider $shareProvider,
  98. IManager $autoCompleteManager,
  99. IUserStatusManager $statusManager,
  100. MatterbridgeManager $matterbridgeManager,
  101. SearchPlugin $searchPlugin,
  102. ISearchResult $searchResult,
  103. ITimeFactory $timeFactory,
  104. IEventDispatcher $eventDispatcher,
  105. IValidator $richObjectValidator,
  106. ITrustedDomainHelper $trustedDomainHelper,
  107. IL10N $l) {
  108. parent::__construct($appName, $request);
  109. $this->userId = $UserId;
  110. $this->userManager = $userManager;
  111. $this->appManager = $appManager;
  112. $this->chatManager = $chatManager;
  113. $this->reactionManager = $reactionManager;
  114. $this->participantService = $participantService;
  115. $this->sessionService = $sessionService;
  116. $this->attachmentService = $attachmentService;
  117. $this->guestManager = $guestManager;
  118. $this->messageParser = $messageParser;
  119. $this->shareProvider = $shareProvider;
  120. $this->autoCompleteManager = $autoCompleteManager;
  121. $this->statusManager = $statusManager;
  122. $this->matterbridgeManager = $matterbridgeManager;
  123. $this->searchPlugin = $searchPlugin;
  124. $this->searchResult = $searchResult;
  125. $this->timeFactory = $timeFactory;
  126. $this->eventDispatcher = $eventDispatcher;
  127. $this->richObjectValidator = $richObjectValidator;
  128. $this->trustedDomainHelper = $trustedDomainHelper;
  129. $this->l = $l;
  130. }
  131. protected function getActorInfo(string $actorDisplayName = ''): array {
  132. if ($this->userId === null) {
  133. $actorType = Attendee::ACTOR_GUESTS;
  134. $actorId = $this->participant->getAttendee()->getActorId();
  135. if ($actorDisplayName) {
  136. $this->guestManager->updateName($this->room, $this->participant, $actorDisplayName);
  137. }
  138. } elseif ($this->userId === MatterbridgeManager::BRIDGE_BOT_USERID && $actorDisplayName) {
  139. $actorType = Attendee::ACTOR_BRIDGED;
  140. $actorId = str_replace(["/", "\""], "", $actorDisplayName);
  141. } else {
  142. $actorType = Attendee::ACTOR_USERS;
  143. $actorId = $this->userId;
  144. }
  145. return [$actorType, $actorId];
  146. }
  147. public function parseCommentToResponse(IComment $comment, Message $parentMessage = null): DataResponse {
  148. $chatMessage = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l);
  149. $this->messageParser->parseMessage($chatMessage);
  150. if (!$chatMessage->getVisibility()) {
  151. $response = new DataResponse([], Http::STATUS_CREATED);
  152. if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) {
  153. $response->addHeader('X-Chat-Last-Common-Read', (string) $this->chatManager->getLastCommonReadMessage($this->room));
  154. }
  155. return $response;
  156. }
  157. $this->participantService->updateLastReadMessage($this->participant, (int) $comment->getId());
  158. $data = $chatMessage->toArray($this->getResponseFormat());
  159. if ($parentMessage instanceof Message) {
  160. $data['parent'] = $parentMessage->toArray($this->getResponseFormat());
  161. }
  162. $response = new DataResponse($data, Http::STATUS_CREATED);
  163. if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) {
  164. $response->addHeader('X-Chat-Last-Common-Read', (string) $this->chatManager->getLastCommonReadMessage($this->room));
  165. }
  166. return $response;
  167. }
  168. /**
  169. * @PublicPage
  170. * @RequireParticipant
  171. * @RequireReadWriteConversation
  172. * @RequirePermissions(permissions=chat)
  173. * @RequireModeratorOrNoLobby
  174. *
  175. * Sends a new chat message to the given room.
  176. *
  177. * The author and timestamp are automatically set to the current user/guest
  178. * and time.
  179. *
  180. * @param string $message the message to send
  181. * @param string $actorDisplayName for guests
  182. * @param string $referenceId for the message to be able to later identify it again
  183. * @param int $replyTo Parent id which this message is a reply to
  184. * @param bool $silent If sent silent the chat message will not create any notifications
  185. * @return DataResponse the status code is "201 Created" if successful, and
  186. * "404 Not found" if the room or session for a guest user was not
  187. * found".
  188. */
  189. public function sendMessage(string $message, string $actorDisplayName = '', string $referenceId = '', int $replyTo = 0, bool $silent = false): DataResponse {
  190. [$actorType, $actorId] = $this->getActorInfo($actorDisplayName);
  191. if (!$actorId) {
  192. return new DataResponse([], Http::STATUS_NOT_FOUND);
  193. }
  194. $parent = $parentMessage = null;
  195. if ($replyTo !== 0) {
  196. try {
  197. $parent = $this->chatManager->getParentComment($this->room, (string) $replyTo);
  198. } catch (NotFoundException $e) {
  199. // Someone is trying to reply cross-rooms or to a non-existing message
  200. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  201. }
  202. $parentMessage = $this->messageParser->createMessage($this->room, $this->participant, $parent, $this->l);
  203. $this->messageParser->parseMessage($parentMessage);
  204. if (!$parentMessage->isReplyable()) {
  205. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  206. }
  207. }
  208. $this->participantService->ensureOneToOneRoomIsFilled($this->room);
  209. $creationDateTime = $this->timeFactory->getDateTime('now', new \DateTimeZone('UTC'));
  210. try {
  211. $comment = $this->chatManager->sendMessage($this->room, $this->participant, $actorType, $actorId, $message, $creationDateTime, $parent, $referenceId, $silent);
  212. } catch (MessageTooLongException $e) {
  213. return new DataResponse([], Http::STATUS_REQUEST_ENTITY_TOO_LARGE);
  214. } catch (\Exception $e) {
  215. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  216. }
  217. return $this->parseCommentToResponse($comment, $parentMessage);
  218. }
  219. /**
  220. * @PublicPage
  221. * @RequireParticipant
  222. * @RequireReadWriteConversation
  223. * @RequirePermissions(permissions=chat)
  224. * @RequireModeratorOrNoLobby
  225. *
  226. * Sends a rich-object to the given room.
  227. *
  228. * The author and timestamp are automatically set to the current user/guest
  229. * and time.
  230. *
  231. * @param string $objectType
  232. * @param string $objectId
  233. * @param string $metaData
  234. * @param string $actorDisplayName
  235. * @param string $referenceId
  236. * @return DataResponse the status code is "201 Created" if successful, and
  237. * "404 Not found" if the room or session for a guest user was not
  238. * found".
  239. */
  240. public function shareObjectToChat(string $objectType, string $objectId, string $metaData = '', string $actorDisplayName = '', string $referenceId = ''): DataResponse {
  241. [$actorType, $actorId] = $this->getActorInfo($actorDisplayName);
  242. if (!$actorId) {
  243. return new DataResponse([], Http::STATUS_NOT_FOUND);
  244. }
  245. $data = $metaData !== '' ? json_decode($metaData, true) : [];
  246. if (!is_array($data)) {
  247. $data = [];
  248. }
  249. $data['type'] = $objectType;
  250. $data['id'] = $objectId;
  251. if (isset($data['link']) && !$this->trustedDomainHelper->isTrustedUrl($data['link'])) {
  252. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  253. }
  254. try {
  255. $this->richObjectValidator->validate('{object}', ['object' => $data]);
  256. } catch (InvalidObjectExeption $e) {
  257. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  258. }
  259. if ($data['type'] === 'geo-location'
  260. && !preg_match(ChatManager::GEO_LOCATION_VALIDATOR, $data['id'])) {
  261. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  262. }
  263. $this->participantService->ensureOneToOneRoomIsFilled($this->room);
  264. $creationDateTime = $this->timeFactory->getDateTime('now', new \DateTimeZone('UTC'));
  265. $message = json_encode([
  266. 'message' => 'object_shared',
  267. 'parameters' => [
  268. 'objectType' => $objectType,
  269. 'objectId' => $objectId,
  270. 'metaData' => $data,
  271. ],
  272. ]);
  273. try {
  274. $comment = $this->chatManager->addSystemMessage($this->room, $actorType, $actorId, $message, $creationDateTime, true, $referenceId);
  275. } catch (MessageTooLongException $e) {
  276. return new DataResponse([], Http::STATUS_REQUEST_ENTITY_TOO_LARGE);
  277. } catch (\Exception $e) {
  278. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  279. }
  280. return $this->parseCommentToResponse($comment);
  281. }
  282. /*
  283. * Gather share IDs from the comments and preload share definitions
  284. * to avoid separate database query for each individual share.
  285. *
  286. * @param IComment[] $comments
  287. */
  288. protected function preloadShares(array $comments): void {
  289. // Scan messages for share IDs
  290. $shareIds = [];
  291. foreach ($comments as $comment) {
  292. $verb = $comment->getVerb();
  293. if ($verb === 'object_shared') {
  294. $message = $comment->getMessage();
  295. $data = json_decode($message, true);
  296. if (isset($data['parameters']['share'])) {
  297. $shareIds[] = $data['parameters']['share'];
  298. }
  299. }
  300. }
  301. if (!empty($shareIds)) {
  302. // Ignore the result for now. Retrieved Share objects will be cached by
  303. // the RoomShareProvider and returned from the cache to
  304. // the Parser\SystemMessage without additional database queries.
  305. $this->shareProvider->getSharesByIds($shareIds);
  306. }
  307. }
  308. /**
  309. * @PublicPage
  310. * @RequireParticipant
  311. * @RequireModeratorOrNoLobby
  312. *
  313. * Receives chat messages from the given room.
  314. *
  315. * - Receiving the history ($lookIntoFuture=0):
  316. * The next $limit messages after $lastKnownMessageId will be returned.
  317. * The new $lastKnownMessageId for the follow up query is available as
  318. * `X-Chat-Last-Given` header.
  319. *
  320. * - Looking into the future ($lookIntoFuture=1):
  321. * If there are currently no messages the response will not be sent
  322. * immediately. Instead, HTTP connection will be kept open waiting for new
  323. * messages to arrive and, when they do, then the response will be sent. The
  324. * connection will not be kept open indefinitely, though; the number of
  325. * seconds to wait for new messages to arrive can be set using the timeout
  326. * parameter; the default timeout is 30 seconds, maximum timeout is 60
  327. * seconds. If the timeout ends a successful but empty response will be
  328. * sent.
  329. * If messages have been returned (status=200) the new $lastKnownMessageId
  330. * for the follow up query is available as `X-Chat-Last-Given` header.
  331. *
  332. * The limit specifies the maximum number of messages that will be returned,
  333. * although the actual number of returned messages could be lower if some
  334. * messages are not visible to the participant. Note that if none of the
  335. * messages are visible to the participant the returned number of messages
  336. * will be 0, yet the status will still be 200. Also note that
  337. * `X-Chat-Last-Given` may reference a message not visible and thus not
  338. * returned, but it should be used nevertheless as the $lastKnownMessageId
  339. * for the follow up query.
  340. *
  341. * @param int $lookIntoFuture Polling for new messages (1) or getting the history of the chat (0)
  342. * @param int $limit Number of chat messages to receive (100 by default, 200 at most)
  343. * @param int $lastKnownMessageId The last known message (serves as offset)
  344. * @param int $lastCommonReadId The last known common read message
  345. * (so the response is 200 instead of 304 when
  346. * it changes even when there are no messages)
  347. * @param int $timeout Number of seconds to wait for new messages (30 by default, 30 at most)
  348. * @param int $setReadMarker Automatically set the last read marker when 1,
  349. * if your client does this itself via chat/{token}/read set to 0
  350. * @param int $includeLastKnown Include the $lastKnownMessageId in the messages when 1 (default 0)
  351. * @param int $noStatusUpdate When the user status should not be automatically set to online set to 1 (default 0)
  352. * @return DataResponse an array of chat messages, "404 Not found" if the
  353. * room token was not valid or "304 Not modified" if there were no messages;
  354. * each chat message is an array with
  355. * fields 'id', 'token', 'actorType', 'actorId',
  356. * 'actorDisplayName', 'timestamp' (in seconds and UTC timezone) and
  357. * 'message'.
  358. */
  359. public function receiveMessages(int $lookIntoFuture,
  360. int $limit = 100,
  361. int $lastKnownMessageId = 0,
  362. int $lastCommonReadId = 0,
  363. int $timeout = 30,
  364. int $setReadMarker = 1,
  365. int $includeLastKnown = 0,
  366. int $noStatusUpdate = 0): DataResponse {
  367. $limit = min(200, $limit);
  368. $timeout = min(30, $timeout);
  369. $session = $this->participant->getSession();
  370. if ($noStatusUpdate === 0 && $session instanceof Session) {
  371. // The mobile apps dont do internal signaling unless in a call
  372. $isMobileApp = $this->request->isUserAgent([
  373. IRequest::USER_AGENT_TALK_ANDROID,
  374. IRequest::USER_AGENT_TALK_IOS,
  375. ]);
  376. if ($isMobileApp && $session->getInCall() === Participant::FLAG_DISCONNECTED) {
  377. $this->sessionService->updateLastPing($session, $this->timeFactory->getTime());
  378. if ($lookIntoFuture) {
  379. $attendee = $this->participant->getAttendee();
  380. if ($attendee->getActorType() === Attendee::ACTOR_USERS) {
  381. // Bump the user status again
  382. $event = new UserLiveStatusEvent(
  383. $this->userManager->get($attendee->getActorId()),
  384. IUserStatus::ONLINE,
  385. $this->timeFactory->getTime()
  386. );
  387. $this->eventDispatcher->dispatchTyped($event);
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * Automatic last read message marking for old clients
  394. * This is pretty dumb and does not give the best and native feeling
  395. * you are used to from other chat apps. The clients should manually
  396. * set the read marker depending on the view port of the set of messages.
  397. *
  398. * We are only setting it automatically here for old clients and the
  399. * web UI, until it can be fixed in Vue. To not use too much broken data,
  400. * we only update the read marker to the last known id, when it is higher
  401. * then the current read marker.
  402. */
  403. $attendee = $this->participant->getAttendee();
  404. if ($lookIntoFuture && $setReadMarker === 1 &&
  405. $lastKnownMessageId > $attendee->getLastReadMessage()) {
  406. $this->participantService->updateLastReadMessage($this->participant, $lastKnownMessageId);
  407. }
  408. $currentUser = $this->userManager->get($this->userId);
  409. if ($lookIntoFuture) {
  410. $comments = $this->chatManager->waitForNewMessages($this->room, $lastKnownMessageId, $limit, $timeout, $currentUser, (bool) $includeLastKnown);
  411. } else {
  412. $comments = $this->chatManager->getHistory($this->room, $lastKnownMessageId, $limit, (bool) $includeLastKnown);
  413. }
  414. if (empty($comments)) {
  415. $response = new DataResponse([], Http::STATUS_NOT_MODIFIED);
  416. if ($lastCommonReadId && $this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) {
  417. $newLastCommonRead = $this->chatManager->getLastCommonReadMessage($this->room);
  418. if ($lastCommonReadId !== $newLastCommonRead) {
  419. // Set the status code to 200 so the header is sent to the client.
  420. // As per "section 10.3.5 of RFC 2616" entity headers shall be
  421. // stripped out on 304: https://stackoverflow.com/a/17822709
  422. $response->setStatus(Http::STATUS_OK);
  423. $response->addHeader('X-Chat-Last-Common-Read', (string) $newLastCommonRead);
  424. }
  425. }
  426. return $response;
  427. }
  428. $this->preloadShares($comments);
  429. $i = 0;
  430. $messages = $commentIdToIndex = $parentIds = [];
  431. foreach ($comments as $comment) {
  432. $id = (int) $comment->getId();
  433. $message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l);
  434. $this->messageParser->parseMessage($message);
  435. if (!$message->getVisibility()) {
  436. $commentIdToIndex[$id] = null;
  437. continue;
  438. }
  439. if ($comment->getParentId() !== '0') {
  440. $parentIds[$id] = $comment->getParentId();
  441. }
  442. $messages[] = $message->toArray($this->getResponseFormat());
  443. $commentIdToIndex[$id] = $i;
  444. $i++;
  445. }
  446. /**
  447. * Set the parent for reply-messages
  448. */
  449. $loadedParents = [];
  450. foreach ($parentIds as $commentId => $parentId) {
  451. $commentKey = $commentIdToIndex[$commentId];
  452. // Parent is already parsed in the message list
  453. if (isset($commentIdToIndex[$parentId])) {
  454. $parentKey = $commentIdToIndex[$parentId];
  455. $messages[$commentKey]['parent'] = $messages[$parentKey];
  456. // We don't show nested parents…
  457. unset($messages[$commentKey]['parent']['parent']);
  458. continue;
  459. }
  460. // Parent was already loaded manually for another comment
  461. if (!empty($loadedParents[$parentId])) {
  462. $messages[$commentKey]['parent'] = $loadedParents[$parentId];
  463. continue;
  464. }
  465. // Parent was not skipped due to visibility, so we need to manually grab it.
  466. if (!isset($commentIdToIndex[$parentId])) {
  467. try {
  468. $comment = $this->chatManager->getParentComment($this->room, $parentId);
  469. $message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l);
  470. $this->messageParser->parseMessage($message);
  471. if ($message->getVisibility()) {
  472. $loadedParents[$parentId] = $message->toArray($this->getResponseFormat());
  473. $messages[$commentKey]['parent'] = $loadedParents[$parentId];
  474. continue;
  475. }
  476. $loadedParents[$parentId] = [
  477. 'id' => (int) $parentId,
  478. 'deleted' => true,
  479. ];
  480. } catch (NotFoundException $e) {
  481. }
  482. }
  483. // Message is not visible to the user
  484. $messages[$commentKey]['parent'] = [
  485. 'id' => (int) $parentId,
  486. 'deleted' => true,
  487. ];
  488. }
  489. $messages = $this->loadSelfReactions($messages, $commentIdToIndex);
  490. $response = new DataResponse($messages, Http::STATUS_OK);
  491. $newLastKnown = end($comments);
  492. if ($newLastKnown instanceof IComment) {
  493. $response->addHeader('X-Chat-Last-Given', (string) $newLastKnown->getId());
  494. /**
  495. * This falsely set the read marker on new messages, although you
  496. * navigated away to a different chat already. So we removed this
  497. * and instead update the read marker before your next waiting.
  498. * So when you are still there, it will just have a wrong read
  499. * marker for the time until your next request starts, while it will
  500. * not update the value, when you actually left the chat already.
  501. * if ($setReadMarker === 1 && $lookIntoFuture) {
  502. * $this->participantService->updateLastReadMessage($this->participant, (int) $newLastKnown->getId());
  503. * }
  504. */
  505. if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) {
  506. $response->addHeader('X-Chat-Last-Common-Read', (string) $this->chatManager->getLastCommonReadMessage($this->room));
  507. }
  508. }
  509. return $response;
  510. }
  511. protected function loadSelfReactions(array $messages, array $commentIdToIndex): array {
  512. // Get message ids with reactions
  513. $messageIdsWithReactions = array_map(
  514. static fn (array $message) => $message['id'],
  515. array_filter($messages, static fn (array $message) => !empty($message['reactions']))
  516. );
  517. // Get parents with reactions
  518. $parentsWithReactions = array_map(
  519. static fn (array $message) => ['parent' => $message['parent']['id'], 'message' => $message['id']],
  520. array_filter($messages, static fn (array $message) => !empty($message['parent']['reactions']))
  521. );
  522. // Create a map, so we can translate the parent's $messageId to the correct child entries
  523. $parentMap = $parentIdsWithReactions = [];
  524. foreach ($parentsWithReactions as $entry) {
  525. $parentMap[(int) $entry['parent']] ??= [];
  526. $parentMap[(int) $entry['parent']][] = (int) $entry['message'];
  527. $parentIdsWithReactions[] = (int) $entry['parent'];
  528. }
  529. // Unique list for the query
  530. $idsWithReactions = array_unique(array_merge($messageIdsWithReactions, $parentIdsWithReactions));
  531. $reactionsById = $this->reactionManager->getReactionsByActorForMessages($this->participant, $idsWithReactions);
  532. // Inject the reactions self into the $messages array
  533. foreach ($reactionsById as $messageId => $reactions) {
  534. if (isset($commentIdToIndex[$messageId]) && isset($messages[$commentIdToIndex[$messageId]])) {
  535. $messages[$commentIdToIndex[$messageId]]['reactionsSelf'] = $reactions;
  536. }
  537. // Add the self part also to potential parent elements
  538. if (isset($parentMap[$messageId])) {
  539. foreach ($parentMap[$messageId] as $mid) {
  540. if (isset($messages[$commentIdToIndex[$mid]])) {
  541. $messages[$commentIdToIndex[$mid]]['parent']['reactionsSelf'] = $reactions;
  542. }
  543. }
  544. }
  545. }
  546. return $messages;
  547. }
  548. /**
  549. * @NoAdminRequired
  550. * @RequireParticipant
  551. * @RequireReadWriteConversation
  552. * @RequirePermissions(permissions=chat)
  553. * @RequireModeratorOrNoLobby
  554. *
  555. * @param int $messageId
  556. * @return DataResponse
  557. */
  558. public function deleteMessage(int $messageId): DataResponse {
  559. try {
  560. $message = $this->chatManager->getComment($this->room, (string) $messageId);
  561. } catch (NotFoundException $e) {
  562. return new DataResponse([], Http::STATUS_NOT_FOUND);
  563. }
  564. $attendee = $this->participant->getAttendee();
  565. $isOwnMessage = $message->getActorType() === $attendee->getActorType()
  566. && $message->getActorId() === $attendee->getActorId();
  567. // Special case for if the message is a bridged message, then the message is the bridge bot's message.
  568. $isOwnMessage = $isOwnMessage || ($message->getActorType() === Attendee::ACTOR_BRIDGED && $attendee->getActorId() === MatterbridgeManager::BRIDGE_BOT_USERID);
  569. if (!$isOwnMessage
  570. && (!$this->participant->hasModeratorPermissions(false)
  571. || $this->room->getType() === Room::TYPE_ONE_TO_ONE)) {
  572. // Actor is not a moderator or not the owner of the message
  573. return new DataResponse([], Http::STATUS_FORBIDDEN);
  574. }
  575. if ($message->getVerb() !== ChatManager::VERB_MESSAGE && $message->getVerb() !== ChatManager::VERB_OBJECT_SHARED) {
  576. // System message (since the message is not parsed, it has type "system")
  577. return new DataResponse([], Http::STATUS_METHOD_NOT_ALLOWED);
  578. }
  579. $maxDeleteAge = $this->timeFactory->getDateTime();
  580. $maxDeleteAge->sub(new \DateInterval('PT6H'));
  581. if ($message->getCreationDateTime() < $maxDeleteAge) {
  582. // Message is too old
  583. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  584. }
  585. try {
  586. $systemMessageComment = $this->chatManager->deleteMessage(
  587. $this->room,
  588. $message,
  589. $this->participant,
  590. $this->timeFactory->getDateTime()
  591. );
  592. } catch (ShareNotFound $e) {
  593. return new DataResponse([], Http::STATUS_NOT_FOUND);
  594. }
  595. $systemMessage = $this->messageParser->createMessage($this->room, $this->participant, $systemMessageComment, $this->l);
  596. $this->messageParser->parseMessage($systemMessage);
  597. $comment = $this->chatManager->getComment($this->room, (string) $messageId);
  598. $message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l);
  599. $this->messageParser->parseMessage($message);
  600. $data = $systemMessage->toArray($this->getResponseFormat());
  601. $data['parent'] = $message->toArray($this->getResponseFormat());
  602. $bridge = $this->matterbridgeManager->getBridgeOfRoom($this->room);
  603. $response = new DataResponse($data, $bridge['enabled'] ? Http::STATUS_ACCEPTED : Http::STATUS_OK);
  604. if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) {
  605. $response->addHeader('X-Chat-Last-Common-Read', (string) $this->chatManager->getLastCommonReadMessage($this->room));
  606. }
  607. return $response;
  608. }
  609. /**
  610. * @NoAdminRequired
  611. * @RequireModeratorParticipant
  612. * @RequireReadWriteConversation
  613. *
  614. * @return DataResponse
  615. */
  616. public function clearHistory(): DataResponse {
  617. $attendee = $this->participant->getAttendee();
  618. if (!$this->participant->hasModeratorPermissions(false)
  619. || $this->room->getType() === Room::TYPE_ONE_TO_ONE) {
  620. // Actor is not a moderator or not the owner of the message
  621. return new DataResponse([], Http::STATUS_FORBIDDEN);
  622. }
  623. $systemMessageComment = $this->chatManager->clearHistory(
  624. $this->room,
  625. $attendee->getActorType(),
  626. $attendee->getActorId()
  627. );
  628. $systemMessage = $this->messageParser->createMessage($this->room, $this->participant, $systemMessageComment, $this->l);
  629. $this->messageParser->parseMessage($systemMessage);
  630. $data = $systemMessage->toArray($this->getResponseFormat());
  631. $bridge = $this->matterbridgeManager->getBridgeOfRoom($this->room);
  632. $response = new DataResponse($data, $bridge['enabled'] ? Http::STATUS_ACCEPTED : Http::STATUS_OK);
  633. if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) {
  634. $response->addHeader('X-Chat-Last-Common-Read', (string) $this->chatManager->getLastCommonReadMessage($this->room));
  635. }
  636. return $response;
  637. }
  638. /**
  639. * @NoAdminRequired
  640. * @RequireParticipant
  641. *
  642. * @param int $lastReadMessage
  643. * @return DataResponse
  644. */
  645. public function setReadMarker(int $lastReadMessage): DataResponse {
  646. $this->participantService->updateLastReadMessage($this->participant, $lastReadMessage);
  647. $response = new DataResponse();
  648. if ($this->participant->getAttendee()->getReadPrivacy() === Participant::PRIVACY_PUBLIC) {
  649. $response->addHeader('X-Chat-Last-Common-Read', (string) $this->chatManager->getLastCommonReadMessage($this->room));
  650. }
  651. return $response;
  652. }
  653. /**
  654. * @NoAdminRequired
  655. * @RequireParticipant
  656. *
  657. * @return DataResponse
  658. */
  659. public function markUnread(): DataResponse {
  660. $message = $this->room->getLastMessage();
  661. $unreadId = 0;
  662. if ($message instanceof IComment) {
  663. try {
  664. $previousMessage = $this->chatManager->getPreviousMessageWithVerb(
  665. $this->room,
  666. (int)$message->getId(),
  667. [ChatManager::VERB_MESSAGE],
  668. $message->getVerb() === ChatManager::VERB_MESSAGE
  669. );
  670. $unreadId = (int) $previousMessage->getId();
  671. } catch (NotFoundException $e) {
  672. // No chat message found, only system messages.
  673. // Marking unread from beginning
  674. }
  675. }
  676. return $this->setReadMarker($unreadId);
  677. }
  678. /**
  679. * @PublicPage
  680. * @RequireParticipant
  681. * @RequireModeratorOrNoLobby
  682. *
  683. * @param int $limit
  684. * @return DataResponse
  685. */
  686. public function getObjectsSharedInRoomOverview(int $limit = 7): DataResponse {
  687. $limit = min(20, $limit);
  688. $objectTypes = [
  689. Attachment::TYPE_AUDIO,
  690. Attachment::TYPE_DECK_CARD,
  691. Attachment::TYPE_FILE,
  692. Attachment::TYPE_LOCATION,
  693. Attachment::TYPE_MEDIA,
  694. Attachment::TYPE_OTHER,
  695. Attachment::TYPE_POLL,
  696. Attachment::TYPE_VOICE,
  697. ];
  698. $messages = [];
  699. $messageIdsByType = [];
  700. foreach ($objectTypes as $objectType) {
  701. $attachments = $this->attachmentService->getAttachmentsByType($this->room, $objectType, 0, $limit);
  702. $messageIdsByType[$objectType] = array_map(static fn (Attachment $attachment): int => $attachment->getMessageId(), $attachments);
  703. }
  704. $comments = $this->chatManager->getMessagesById($this->room, array_merge(...array_values($messageIdsByType)));
  705. foreach ($comments as $comment) {
  706. $message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l);
  707. $this->messageParser->parseMessage($message);
  708. if (!$message->getVisibility()) {
  709. continue;
  710. }
  711. $messages[(int) $comment->getId()] = $message->toArray($this->getResponseFormat());
  712. }
  713. $messagesByType = [];
  714. foreach ($objectTypes as $objectType) {
  715. $messagesByType[$objectType] = [];
  716. foreach ($messageIdsByType[$objectType] as $messageId) {
  717. $messagesByType[$objectType][] = $messages[$messageId];
  718. }
  719. }
  720. return new DataResponse($messagesByType, Http::STATUS_OK);
  721. }
  722. /**
  723. * @PublicPage
  724. * @RequireParticipant
  725. * @RequireModeratorOrNoLobby
  726. *
  727. * @param string $objectType
  728. * @param int $lastKnownMessageId
  729. * @param int $limit
  730. * @return DataResponse
  731. */
  732. public function getObjectsSharedInRoom(string $objectType, int $lastKnownMessageId = 0, int $limit = 100): DataResponse {
  733. $offset = max(0, $lastKnownMessageId);
  734. $limit = min(200, $limit);
  735. $attachments = $this->attachmentService->getAttachmentsByType($this->room, $objectType, $offset, $limit);
  736. $messageIds = array_map(static fn (Attachment $attachment): int => $attachment->getMessageId(), $attachments);
  737. $messages = $this->getMessagesForRoom($this->room, $messageIds);
  738. $response = new DataResponse($messages, Http::STATUS_OK);
  739. if (!empty($messages)) {
  740. $newLastKnown = min(array_keys($messages));
  741. $response->addHeader('X-Chat-Last-Given', $newLastKnown);
  742. }
  743. return $response;
  744. }
  745. protected function getMessagesForRoom(Room $room, array $messageIds): array {
  746. $comments = $this->chatManager->getMessagesById($room, $messageIds);
  747. $messages = [];
  748. foreach ($comments as $comment) {
  749. $message = $this->messageParser->createMessage($room, $this->participant, $comment, $this->l);
  750. $this->messageParser->parseMessage($message);
  751. if (!$message->getVisibility()) {
  752. continue;
  753. }
  754. $messages[(int) $comment->getId()] = $message->toArray($this->getResponseFormat());
  755. }
  756. return $messages;
  757. }
  758. /**
  759. * @PublicPage
  760. * @RequireParticipant
  761. * @RequireReadWriteConversation
  762. * @RequirePermissions(permissions=chat)
  763. * @RequireModeratorOrNoLobby
  764. *
  765. * @param string $search
  766. * @param int $limit
  767. * @param bool $includeStatus
  768. * @return DataResponse
  769. */
  770. public function mentions(string $search, int $limit = 20, bool $includeStatus = false): DataResponse {
  771. $this->searchPlugin->setContext([
  772. 'itemType' => 'chat',
  773. 'itemId' => $this->room->getId(),
  774. 'room' => $this->room,
  775. ]);
  776. $this->searchPlugin->search($search, $limit, 0, $this->searchResult);
  777. $results = $this->searchResult->asArray();
  778. $exactMatches = $results['exact'];
  779. unset($results['exact']);
  780. $results = array_merge_recursive($exactMatches, $results);
  781. $this->autoCompleteManager->registerSorter(Sorter::class);
  782. $this->autoCompleteManager->runSorters(['talk_chat_participants'], $results, [
  783. 'itemType' => 'chat',
  784. 'itemId' => (string) $this->room->getId(),
  785. 'search' => $search,
  786. ]);
  787. $statuses = [];
  788. if ($this->userId !== null
  789. && $includeStatus
  790. && $this->appManager->isEnabledForUser('user_status')) {
  791. $userIds = array_filter(array_map(static function (array $userResult) {
  792. return $userResult['value']['shareWith'];
  793. }, $results['users']));
  794. $statuses = $this->statusManager->getUserStatuses($userIds);
  795. }
  796. $results = $this->prepareResultArray($results, $statuses);
  797. $results = $this->chatManager->addConversationNotify($results, $search, $this->room, $this->participant);
  798. return new DataResponse($results);
  799. }
  800. /**
  801. * @param array $results
  802. * @param IUserStatus[] $statuses
  803. * @return array
  804. */
  805. protected function prepareResultArray(array $results, array $statuses): array {
  806. $output = [];
  807. foreach ($results as $type => $subResult) {
  808. foreach ($subResult as $result) {
  809. $data = [
  810. 'id' => $result['value']['shareWith'],
  811. 'label' => $result['label'],
  812. 'source' => $type,
  813. ];
  814. if ($type === Attendee::ACTOR_USERS && isset($statuses[$data['id']])) {
  815. $data['status'] = $statuses[$data['id']]->getStatus();
  816. $data['statusIcon'] = $statuses[$data['id']]->getIcon();
  817. $data['statusMessage'] = $statuses[$data['id']]->getMessage();
  818. $data['statusClearAt'] = $statuses[$data['id']]->getClearAt();
  819. }
  820. $output[] = $data;
  821. }
  822. }
  823. return $output;
  824. }
  825. }