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.

137 lines
4.5 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\Spreed\Chat;
  23. use OC\Comments\Comment;
  24. use OC\Comments\Manager;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\Comments\IComment;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\IConfig;
  29. use OCP\IDBConnection;
  30. use OCP\ILogger;
  31. class CommentsManager extends Manager {
  32. /** @var ITimeFactory */
  33. protected $timeFactory;
  34. public function __construct(
  35. IDBConnection $db,
  36. ILogger $logger,
  37. IConfig $config,
  38. ITimeFactory $timeFactory
  39. ) {
  40. parent::__construct($db, $logger, $config);
  41. $this->timeFactory = $timeFactory;
  42. }
  43. /**
  44. * @param array $data
  45. * @return IComment
  46. */
  47. public function getCommentFromData(array $data): IComment {
  48. $message = $data['message'];
  49. unset($data['message']);
  50. $comment = new Comment($this->normalizeDatabaseData($data));
  51. $comment->setMessage($message, ChatManager::MAX_CHAT_LENGTH);
  52. return $comment;
  53. }
  54. /**
  55. * @param string $objectType
  56. * @param string $objectId
  57. * @param string $verb
  58. * @param string $actorType
  59. * @param string[] $actors
  60. * @return array
  61. */
  62. public function getLastCommentDateByActor(
  63. string $objectType,
  64. string $objectId,
  65. string $verb,
  66. string $actorType,
  67. array $actors
  68. ): array {
  69. $lastComments = [];
  70. $query = $this->dbConn->getQueryBuilder();
  71. $query->select('actor_id')
  72. ->selectAlias($query->createFunction('MAX(' . $query->getColumnName('creation_timestamp') . ')'), 'last_comment')
  73. ->from('comments')
  74. ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
  75. ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
  76. ->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)))
  77. ->andWhere($query->expr()->eq('actor_type', $query->createNamedParameter($actorType)))
  78. ->andWhere($query->expr()->in('actor_id', $query->createNamedParameter($actors, IQueryBuilder::PARAM_STR_ARRAY)))
  79. ->groupBy('actor_id');
  80. $result = $query->execute();
  81. while ($row = $result->fetch()) {
  82. $lastComments[$row['actor_id']] = $this->timeFactory->getDateTime($row['last_comment']);
  83. }
  84. $result->closeCursor();
  85. return $lastComments;
  86. }
  87. public function getNumberOfCommentsForObjectSinceComment(string $objectType, string $objectId, int $lastRead, string $verb = ''): int {
  88. $query = $this->dbConn->getQueryBuilder();
  89. $query->selectAlias($query->createFunction('COUNT(' . $query->getColumnName('id') . ')'), 'num_messages')
  90. ->from('comments')
  91. ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
  92. ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
  93. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($lastRead)));
  94. if ($verb !== '') {
  95. $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
  96. }
  97. $result = $query->execute();
  98. $data = $result->fetch();
  99. $result->closeCursor();
  100. return (int) ($data['num_messages'] ?? 0);
  101. }
  102. public function getLastCommentBeforeDate(string $objectType, string $objectId, \DateTime $beforeDate, string $verb = ''): int {
  103. $query = $this->dbConn->getQueryBuilder();
  104. $query->select('id')
  105. ->from('comments')
  106. ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
  107. ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
  108. ->andWhere($query->expr()->lt('creation_timestamp', $query->createNamedParameter($beforeDate, IQueryBuilder::PARAM_DATE)))
  109. ->orderBy('creation_timestamp', 'desc');
  110. if ($verb !== '') {
  111. $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
  112. }
  113. $result = $query->execute();
  114. $data = $result->fetch();
  115. $result->closeCursor();
  116. return (int) ($data['id'] ?? 0);
  117. }
  118. }