TagManager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OC\Tagging\TagMapper;
  9. use OCP\Db\Exception as DBException;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\EventDispatcher\Event;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\EventDispatcher\IEventListener;
  14. use OCP\IDBConnection;
  15. use OCP\ITagManager;
  16. use OCP\ITags;
  17. use OCP\IUserSession;
  18. use OCP\User\Events\UserDeletedEvent;
  19. use Psr\Log\LoggerInterface;
  20. /**
  21. * @template-implements IEventListener<UserDeletedEvent>
  22. */
  23. class TagManager implements ITagManager, IEventListener {
  24. public function __construct(
  25. private TagMapper $mapper,
  26. private IUserSession $userSession,
  27. private IDBConnection $connection,
  28. private LoggerInterface $logger,
  29. private IEventDispatcher $dispatcher,
  30. ) {
  31. }
  32. /**
  33. * Create a new \OCP\ITags instance and load tags from db.
  34. *
  35. * @see \OCP\ITags
  36. * @param string $type The type identifier e.g. 'contact' or 'event'.
  37. * @param array $defaultTags An array of default tags to be used if none are stored.
  38. * @param boolean $includeShared Whether to include tags for items shared with this user by others.
  39. * @param string $userId user for which to retrieve the tags, defaults to the currently
  40. * logged in user
  41. * @return \OCP\ITags
  42. *
  43. * since 20.0.0 $includeShared isn't used anymore
  44. */
  45. public function load($type, $defaultTags = [], $includeShared = false, $userId = null) {
  46. if (is_null($userId)) {
  47. $user = $this->userSession->getUser();
  48. if ($user === null) {
  49. // nothing we can do without a user
  50. return null;
  51. }
  52. $userId = $this->userSession->getUser()->getUId();
  53. }
  54. return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userSession, $defaultTags);
  55. }
  56. /**
  57. * Get all users who favorited an object
  58. *
  59. * @param string $objectType
  60. * @param int $objectId
  61. * @return array
  62. */
  63. public function getUsersFavoritingObject(string $objectType, int $objectId): array {
  64. $query = $this->connection->getQueryBuilder();
  65. $query->select('uid')
  66. ->from('vcategory_to_object', 'o')
  67. ->innerJoin('o', 'vcategory', 'c', $query->expr()->eq('o.categoryid', 'c.id'))
  68. ->where($query->expr()->eq('objid', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT)))
  69. ->andWhere($query->expr()->eq('c.type', $query->createNamedParameter($objectType)))
  70. ->andWhere($query->expr()->eq('c.category', $query->createNamedParameter(ITags::TAG_FAVORITE)));
  71. $result = $query->execute();
  72. $users = $result->fetchAll(\PDO::FETCH_COLUMN);
  73. $result->closeCursor();
  74. return $users;
  75. }
  76. public function handle(Event $event): void {
  77. if (!($event instanceof UserDeletedEvent)) {
  78. return;
  79. }
  80. // Find all objectid/tagId pairs.
  81. $user = $event->getUser();
  82. $qb = $this->connection->getQueryBuilder();
  83. $qb->select('id')
  84. ->from('vcategory')
  85. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
  86. try {
  87. $result = $qb->executeQuery();
  88. } catch (DBException $e) {
  89. $this->logger->error($e->getMessage(), [
  90. 'app' => 'core',
  91. 'exception' => $e,
  92. ]);
  93. return;
  94. }
  95. $tagsIds = array_map(fn (array $row) => (int)$row['id'], $result->fetchAll());
  96. $result->closeCursor();
  97. if (count($tagsIds) === 0) {
  98. return;
  99. }
  100. // Clean vcategory_to_object table
  101. $qb = $this->connection->getQueryBuilder();
  102. $qb = $qb->delete('vcategory_to_object')
  103. ->where($qb->expr()->in('categoryid', $qb->createParameter('chunk')));
  104. // Clean vcategory
  105. $qb1 = $this->connection->getQueryBuilder();
  106. $qb1 = $qb1->delete('vcategory')
  107. ->where($qb1->expr()->in('uid', $qb1->createParameter('chunk')));
  108. foreach (array_chunk($tagsIds, 1000) as $tagChunk) {
  109. $qb->setParameter('chunk', $tagChunk, IQueryBuilder::PARAM_INT_ARRAY);
  110. $qb1->setParameter('chunk', $tagChunk, IQueryBuilder::PARAM_INT_ARRAY);
  111. try {
  112. $qb->executeStatement();
  113. $qb1->executeStatement();
  114. } catch (DBException $e) {
  115. $this->logger->error($e->getMessage(), [
  116. 'app' => 'core',
  117. 'exception' => $e,
  118. ]);
  119. }
  120. }
  121. }
  122. }