RemoveClassifiedEventActivity.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Migration;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCP\IDBConnection;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. class RemoveClassifiedEventActivity implements IRepairStep {
  13. /** @var IDBConnection */
  14. private $connection;
  15. public function __construct(IDBConnection $connection) {
  16. $this->connection = $connection;
  17. }
  18. /**
  19. * @inheritdoc
  20. */
  21. public function getName() {
  22. return 'Remove activity entries of private events';
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function run(IOutput $output) {
  28. if (!$this->connection->tableExists('activity')) {
  29. return;
  30. }
  31. $deletedEvents = $this->removePrivateEventActivity();
  32. $deletedEvents += $this->removeConfidentialUncensoredEventActivity();
  33. $output->info("Removed $deletedEvents activity entries");
  34. }
  35. protected function removePrivateEventActivity(): int {
  36. $deletedEvents = 0;
  37. $delete = $this->connection->getQueryBuilder();
  38. $delete->delete('activity')
  39. ->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
  40. ->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
  41. ->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
  42. ->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')));
  43. $query = $this->connection->getQueryBuilder();
  44. $query->select('c.principaluri', 'o.calendarid', 'o.uid')
  45. ->from('calendarobjects', 'o')
  46. ->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
  47. ->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_PRIVATE)));
  48. $result = $query->execute();
  49. while ($row = $result->fetch()) {
  50. if ($row['principaluri'] === null) {
  51. continue;
  52. }
  53. $delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
  54. ->setParameter('type', 'calendar')
  55. ->setParameter('calendar_id', $row['calendarid'])
  56. ->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%');
  57. $deletedEvents += $delete->execute();
  58. }
  59. $result->closeCursor();
  60. return $deletedEvents;
  61. }
  62. protected function removeConfidentialUncensoredEventActivity(): int {
  63. $deletedEvents = 0;
  64. $delete = $this->connection->getQueryBuilder();
  65. $delete->delete('activity')
  66. ->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
  67. ->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
  68. ->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
  69. ->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')))
  70. ->andWhere($delete->expr()->notLike('subjectparams', $delete->createParameter('filtered_name')));
  71. $query = $this->connection->getQueryBuilder();
  72. $query->select('c.principaluri', 'o.calendarid', 'o.uid')
  73. ->from('calendarobjects', 'o')
  74. ->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
  75. ->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_CONFIDENTIAL)));
  76. $result = $query->execute();
  77. while ($row = $result->fetch()) {
  78. if ($row['principaluri'] === null) {
  79. continue;
  80. }
  81. $delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
  82. ->setParameter('type', 'calendar')
  83. ->setParameter('calendar_id', $row['calendarid'])
  84. ->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%')
  85. ->setParameter('filtered_name', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '","name":"Busy"') . '%');
  86. $deletedEvents += $delete->execute();
  87. }
  88. $result->closeCursor();
  89. return $deletedEvents;
  90. }
  91. protected function getPrincipal(string $principalUri): string {
  92. $uri = explode('/', $principalUri);
  93. return array_pop($uri);
  94. }
  95. }