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.

95 lines
2.8 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Talk\Chat;
  23. use OC\Comments\Comment;
  24. use OC\Comments\Manager;
  25. use OCP\Comments\IComment;
  26. use OCP\DB\Exception;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. class CommentsManager extends Manager {
  29. /**
  30. * @param array $data
  31. * @return IComment
  32. */
  33. public function getCommentFromData(array $data): IComment {
  34. $message = $data['message'];
  35. unset($data['message']);
  36. $comment = new Comment($this->normalizeDatabaseData($data));
  37. $comment->setMessage($message, ChatManager::MAX_CHAT_LENGTH);
  38. return $comment;
  39. }
  40. /**
  41. * @param string[] $ids
  42. * @return IComment[]
  43. * @throws Exception
  44. */
  45. public function getCommentsById(array $ids): array {
  46. $commentIds = array_map('intval', $ids);
  47. $query = $this->dbConn->getQueryBuilder();
  48. $query->select('*')
  49. ->from('comments')
  50. ->where($query->expr()->in('id', $query->createNamedParameter($commentIds, IQueryBuilder::PARAM_INT_ARRAY)));
  51. $comments = [];
  52. $result = $query->execute();
  53. while ($row = $result->fetch()) {
  54. $comments[(int) $row['id']] = $this->getCommentFromData($row);
  55. }
  56. $result->closeCursor();
  57. return $comments;
  58. }
  59. /**
  60. * @param string $actorType
  61. * @param string $actorId
  62. * @param string[] $messageIds
  63. * @return array
  64. * @psalm-return array<int, string[]>
  65. */
  66. public function retrieveReactionsByActor(string $actorType, string $actorId, array $messageIds): array {
  67. $commentIds = array_map('intval', $messageIds);
  68. $query = $this->dbConn->getQueryBuilder();
  69. $query->select('*')
  70. ->from('reactions')
  71. ->where($query->expr()->eq('actor_type', $query->createNamedParameter($actorType)))
  72. ->andWhere($query->expr()->eq('actor_id', $query->createNamedParameter($actorId)))
  73. ->andWhere($query->expr()->in('parent_id', $query->createNamedParameter($commentIds, IQueryBuilder::PARAM_INT_ARRAY)));
  74. $reactions = [];
  75. $result = $query->executeQuery();
  76. while ($row = $result->fetch()) {
  77. $reactions[(int) $row['parent_id']] ??= [];
  78. $reactions[(int) $row['parent_id']][] = $row['reaction'];
  79. }
  80. $result->closeCursor();
  81. return $reactions;
  82. }
  83. }