RemoveClassifiedEventActivity.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public function __construct(
  14. private IDBConnection $connection,
  15. ) {
  16. }
  17. /**
  18. * @inheritdoc
  19. */
  20. public function getName() {
  21. return 'Remove activity entries of private events';
  22. }
  23. /**
  24. * @inheritdoc
  25. */
  26. public function run(IOutput $output) {
  27. if (!$this->connection->tableExists('activity')) {
  28. return;
  29. }
  30. $deletedEvents = $this->removePrivateEventActivity();
  31. $deletedEvents += $this->removeConfidentialUncensoredEventActivity();
  32. $output->info("Removed $deletedEvents activity entries");
  33. }
  34. protected function removePrivateEventActivity(): int {
  35. $deletedEvents = 0;
  36. $delete = $this->connection->getQueryBuilder();
  37. $delete->delete('activity')
  38. ->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
  39. ->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
  40. ->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
  41. ->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')));
  42. $query = $this->connection->getQueryBuilder();
  43. $query->select('c.principaluri', 'o.calendarid', 'o.uid')
  44. ->from('calendarobjects', 'o')
  45. ->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
  46. ->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_PRIVATE)));
  47. $result = $query->executeQuery();
  48. while ($row = $result->fetch()) {
  49. if ($row['principaluri'] === null) {
  50. continue;
  51. }
  52. $delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
  53. ->setParameter('type', 'calendar')
  54. ->setParameter('calendar_id', $row['calendarid'])
  55. ->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%');
  56. $deletedEvents += $delete->executeStatement();
  57. }
  58. $result->closeCursor();
  59. return $deletedEvents;
  60. }
  61. protected function removeConfidentialUncensoredEventActivity(): int {
  62. $deletedEvents = 0;
  63. $delete = $this->connection->getQueryBuilder();
  64. $delete->delete('activity')
  65. ->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
  66. ->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
  67. ->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
  68. ->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')))
  69. ->andWhere($delete->expr()->notLike('subjectparams', $delete->createParameter('filtered_name')));
  70. $query = $this->connection->getQueryBuilder();
  71. $query->select('c.principaluri', 'o.calendarid', 'o.uid')
  72. ->from('calendarobjects', 'o')
  73. ->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
  74. ->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_CONFIDENTIAL)));
  75. $result = $query->executeQuery();
  76. while ($row = $result->fetch()) {
  77. if ($row['principaluri'] === null) {
  78. continue;
  79. }
  80. $delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
  81. ->setParameter('type', 'calendar')
  82. ->setParameter('calendar_id', $row['calendarid'])
  83. ->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%')
  84. ->setParameter('filtered_name', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '","name":"Busy"') . '%');
  85. $deletedEvents += $delete->executeStatement();
  86. }
  87. $result->closeCursor();
  88. return $deletedEvents;
  89. }
  90. protected function getPrincipal(string $principalUri): string {
  91. $uri = explode('/', $principalUri);
  92. return array_pop($uri);
  93. }
  94. }