Backend.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\Activity;
  7. use OCA\DAV\CalDAV\Activity\Provider\Calendar;
  8. use OCA\DAV\CalDAV\Activity\Provider\Event;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use OCP\Activity\IEvent;
  11. use OCP\Activity\IManager as IActivityManager;
  12. use OCP\App\IAppManager;
  13. use OCP\IGroup;
  14. use OCP\IGroupManager;
  15. use OCP\IUser;
  16. use OCP\IUserManager;
  17. use OCP\IUserSession;
  18. use Sabre\VObject\Reader;
  19. /**
  20. * Class Backend
  21. *
  22. * @package OCA\DAV\CalDAV\Activity
  23. */
  24. class Backend {
  25. public function __construct(
  26. protected IActivityManager $activityManager,
  27. protected IGroupManager $groupManager,
  28. protected IUserSession $userSession,
  29. protected IAppManager $appManager,
  30. protected IUserManager $userManager,
  31. ) {
  32. }
  33. /**
  34. * Creates activities when a calendar was creates
  35. *
  36. * @param array $calendarData
  37. */
  38. public function onCalendarAdd(array $calendarData) {
  39. $this->triggerCalendarActivity(Calendar::SUBJECT_ADD, $calendarData);
  40. }
  41. /**
  42. * Creates activities when a calendar was updated
  43. *
  44. * @param array $calendarData
  45. * @param array $shares
  46. * @param array $properties
  47. */
  48. public function onCalendarUpdate(array $calendarData, array $shares, array $properties) {
  49. $this->triggerCalendarActivity(Calendar::SUBJECT_UPDATE, $calendarData, $shares, $properties);
  50. }
  51. /**
  52. * Creates activities when a calendar was moved to trash
  53. *
  54. * @param array $calendarData
  55. * @param array $shares
  56. */
  57. public function onCalendarMovedToTrash(array $calendarData, array $shares): void {
  58. $this->triggerCalendarActivity(Calendar::SUBJECT_MOVE_TO_TRASH, $calendarData, $shares);
  59. }
  60. /**
  61. * Creates activities when a calendar was restored
  62. *
  63. * @param array $calendarData
  64. * @param array $shares
  65. */
  66. public function onCalendarRestored(array $calendarData, array $shares): void {
  67. $this->triggerCalendarActivity(Calendar::SUBJECT_RESTORE, $calendarData, $shares);
  68. }
  69. /**
  70. * Creates activities when a calendar was deleted
  71. *
  72. * @param array $calendarData
  73. * @param array $shares
  74. */
  75. public function onCalendarDelete(array $calendarData, array $shares): void {
  76. $this->triggerCalendarActivity(Calendar::SUBJECT_DELETE, $calendarData, $shares);
  77. }
  78. /**
  79. * Creates activities when a calendar was (un)published
  80. *
  81. * @param array $calendarData
  82. * @param bool $publishStatus
  83. */
  84. public function onCalendarPublication(array $calendarData, bool $publishStatus): void {
  85. $this->triggerCalendarActivity($publishStatus ? Calendar::SUBJECT_PUBLISH : Calendar::SUBJECT_UNPUBLISH, $calendarData);
  86. }
  87. /**
  88. * Creates activities for all related users when a calendar was touched
  89. *
  90. * @param string $action
  91. * @param array $calendarData
  92. * @param array $shares
  93. * @param array $changedProperties
  94. */
  95. protected function triggerCalendarActivity($action, array $calendarData, array $shares = [], array $changedProperties = []) {
  96. if (!isset($calendarData['principaluri'])) {
  97. return;
  98. }
  99. $principal = explode('/', $calendarData['principaluri']);
  100. $owner = array_pop($principal);
  101. $currentUser = $this->userSession->getUser();
  102. if ($currentUser instanceof IUser) {
  103. $currentUser = $currentUser->getUID();
  104. } else {
  105. $currentUser = $owner;
  106. }
  107. $event = $this->activityManager->generateEvent();
  108. $event->setApp('dav')
  109. ->setObject('calendar', (int)$calendarData['id'])
  110. ->setType('calendar')
  111. ->setAuthor($currentUser);
  112. $changedVisibleInformation = array_intersect([
  113. '{DAV:}displayname',
  114. '{http://apple.com/ns/ical/}calendar-color'
  115. ], array_keys($changedProperties));
  116. if (empty($shares) || ($action === Calendar::SUBJECT_UPDATE && empty($changedVisibleInformation))) {
  117. $users = [$owner];
  118. } else {
  119. $users = $this->getUsersForShares($shares);
  120. $users[] = $owner;
  121. }
  122. foreach ($users as $user) {
  123. if ($action === Calendar::SUBJECT_DELETE && !$this->userManager->userExists($user)) {
  124. // Avoid creating calendar_delete activities for deleted users
  125. continue;
  126. }
  127. $event->setAffectedUser($user)
  128. ->setSubject(
  129. $user === $currentUser ? $action . '_self' : $action,
  130. [
  131. 'actor' => $currentUser,
  132. 'calendar' => [
  133. 'id' => (int)$calendarData['id'],
  134. 'uri' => $calendarData['uri'],
  135. 'name' => $calendarData['{DAV:}displayname'],
  136. ],
  137. ]
  138. );
  139. $this->activityManager->publish($event);
  140. }
  141. }
  142. /**
  143. * Creates activities for all related users when a calendar was (un-)shared
  144. *
  145. * @param array $calendarData
  146. * @param array $shares
  147. * @param array $add
  148. * @param array $remove
  149. */
  150. public function onCalendarUpdateShares(array $calendarData, array $shares, array $add, array $remove) {
  151. $principal = explode('/', $calendarData['principaluri']);
  152. $owner = $principal[2];
  153. $currentUser = $this->userSession->getUser();
  154. if ($currentUser instanceof IUser) {
  155. $currentUser = $currentUser->getUID();
  156. } else {
  157. $currentUser = $owner;
  158. }
  159. $event = $this->activityManager->generateEvent();
  160. $event->setApp('dav')
  161. ->setObject('calendar', (int)$calendarData['id'])
  162. ->setType('calendar')
  163. ->setAuthor($currentUser);
  164. foreach ($remove as $principal) {
  165. // principal:principals/users/test
  166. $parts = explode(':', $principal, 2);
  167. if ($parts[0] !== 'principal') {
  168. continue;
  169. }
  170. $principal = explode('/', $parts[1]);
  171. if ($principal[1] === 'users') {
  172. $this->triggerActivityUser(
  173. $principal[2],
  174. $event,
  175. $calendarData,
  176. Calendar::SUBJECT_UNSHARE_USER,
  177. Calendar::SUBJECT_DELETE . '_self'
  178. );
  179. if ($owner !== $principal[2]) {
  180. $parameters = [
  181. 'actor' => $event->getAuthor(),
  182. 'calendar' => [
  183. 'id' => (int)$calendarData['id'],
  184. 'uri' => $calendarData['uri'],
  185. 'name' => $calendarData['{DAV:}displayname'],
  186. ],
  187. 'user' => $principal[2],
  188. ];
  189. if ($owner === $event->getAuthor()) {
  190. $subject = Calendar::SUBJECT_UNSHARE_USER . '_you';
  191. } elseif ($principal[2] === $event->getAuthor()) {
  192. $subject = Calendar::SUBJECT_UNSHARE_USER . '_self';
  193. } else {
  194. $event->setAffectedUser($event->getAuthor())
  195. ->setSubject(Calendar::SUBJECT_UNSHARE_USER . '_you', $parameters);
  196. $this->activityManager->publish($event);
  197. $subject = Calendar::SUBJECT_UNSHARE_USER . '_by';
  198. }
  199. $event->setAffectedUser($owner)
  200. ->setSubject($subject, $parameters);
  201. $this->activityManager->publish($event);
  202. }
  203. } elseif ($principal[1] === 'groups') {
  204. $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_UNSHARE_USER);
  205. $parameters = [
  206. 'actor' => $event->getAuthor(),
  207. 'calendar' => [
  208. 'id' => (int)$calendarData['id'],
  209. 'uri' => $calendarData['uri'],
  210. 'name' => $calendarData['{DAV:}displayname'],
  211. ],
  212. 'group' => $principal[2],
  213. ];
  214. if ($owner === $event->getAuthor()) {
  215. $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_you';
  216. } else {
  217. $event->setAffectedUser($event->getAuthor())
  218. ->setSubject(Calendar::SUBJECT_UNSHARE_GROUP . '_you', $parameters);
  219. $this->activityManager->publish($event);
  220. $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_by';
  221. }
  222. $event->setAffectedUser($owner)
  223. ->setSubject($subject, $parameters);
  224. $this->activityManager->publish($event);
  225. }
  226. }
  227. foreach ($add as $share) {
  228. if ($this->isAlreadyShared($share['href'], $shares)) {
  229. continue;
  230. }
  231. // principal:principals/users/test
  232. $parts = explode(':', $share['href'], 2);
  233. if ($parts[0] !== 'principal') {
  234. continue;
  235. }
  236. $principal = explode('/', $parts[1]);
  237. if ($principal[1] === 'users') {
  238. $this->triggerActivityUser($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER);
  239. if ($owner !== $principal[2]) {
  240. $parameters = [
  241. 'actor' => $event->getAuthor(),
  242. 'calendar' => [
  243. 'id' => (int)$calendarData['id'],
  244. 'uri' => $calendarData['uri'],
  245. 'name' => $calendarData['{DAV:}displayname'],
  246. ],
  247. 'user' => $principal[2],
  248. ];
  249. if ($owner === $event->getAuthor()) {
  250. $subject = Calendar::SUBJECT_SHARE_USER . '_you';
  251. } else {
  252. $event->setAffectedUser($event->getAuthor())
  253. ->setSubject(Calendar::SUBJECT_SHARE_USER . '_you', $parameters);
  254. $this->activityManager->publish($event);
  255. $subject = Calendar::SUBJECT_SHARE_USER . '_by';
  256. }
  257. $event->setAffectedUser($owner)
  258. ->setSubject($subject, $parameters);
  259. $this->activityManager->publish($event);
  260. }
  261. } elseif ($principal[1] === 'groups') {
  262. $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER);
  263. $parameters = [
  264. 'actor' => $event->getAuthor(),
  265. 'calendar' => [
  266. 'id' => (int)$calendarData['id'],
  267. 'uri' => $calendarData['uri'],
  268. 'name' => $calendarData['{DAV:}displayname'],
  269. ],
  270. 'group' => $principal[2],
  271. ];
  272. if ($owner === $event->getAuthor()) {
  273. $subject = Calendar::SUBJECT_SHARE_GROUP . '_you';
  274. } else {
  275. $event->setAffectedUser($event->getAuthor())
  276. ->setSubject(Calendar::SUBJECT_SHARE_GROUP . '_you', $parameters);
  277. $this->activityManager->publish($event);
  278. $subject = Calendar::SUBJECT_SHARE_GROUP . '_by';
  279. }
  280. $event->setAffectedUser($owner)
  281. ->setSubject($subject, $parameters);
  282. $this->activityManager->publish($event);
  283. }
  284. }
  285. }
  286. /**
  287. * Checks if a calendar is already shared with a principal
  288. *
  289. * @param string $principal
  290. * @param array[] $shares
  291. * @return bool
  292. */
  293. protected function isAlreadyShared($principal, $shares) {
  294. foreach ($shares as $share) {
  295. if ($principal === $share['href']) {
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. /**
  302. * Creates the given activity for all members of the given group
  303. *
  304. * @param string $gid
  305. * @param IEvent $event
  306. * @param array $properties
  307. * @param string $subject
  308. */
  309. protected function triggerActivityGroup($gid, IEvent $event, array $properties, $subject) {
  310. $group = $this->groupManager->get($gid);
  311. if ($group instanceof IGroup) {
  312. foreach ($group->getUsers() as $user) {
  313. // Exclude current user
  314. if ($user->getUID() !== $event->getAuthor()) {
  315. $this->triggerActivityUser($user->getUID(), $event, $properties, $subject);
  316. }
  317. }
  318. }
  319. }
  320. /**
  321. * Creates the given activity for the given user
  322. *
  323. * @param string $user
  324. * @param IEvent $event
  325. * @param array $properties
  326. * @param string $subject
  327. * @param string $subjectSelf
  328. */
  329. protected function triggerActivityUser($user, IEvent $event, array $properties, $subject, $subjectSelf = '') {
  330. $event->setAffectedUser($user)
  331. ->setSubject(
  332. $user === $event->getAuthor() && $subjectSelf ? $subjectSelf : $subject,
  333. [
  334. 'actor' => $event->getAuthor(),
  335. 'calendar' => [
  336. 'id' => (int)$properties['id'],
  337. 'uri' => $properties['uri'],
  338. 'name' => $properties['{DAV:}displayname'],
  339. ],
  340. ]
  341. );
  342. $this->activityManager->publish($event);
  343. }
  344. /**
  345. * Creates activities when a calendar object was created/updated/deleted
  346. *
  347. * @param string $action
  348. * @param array $calendarData
  349. * @param array $shares
  350. * @param array $objectData
  351. */
  352. public function onTouchCalendarObject($action, array $calendarData, array $shares, array $objectData) {
  353. if (!isset($calendarData['principaluri'])) {
  354. return;
  355. }
  356. $principal = explode('/', $calendarData['principaluri']);
  357. $owner = array_pop($principal);
  358. $currentUser = $this->userSession->getUser();
  359. if ($currentUser instanceof IUser) {
  360. $currentUser = $currentUser->getUID();
  361. } else {
  362. $currentUser = $owner;
  363. }
  364. $classification = $objectData['classification'] ?? CalDavBackend::CLASSIFICATION_PUBLIC;
  365. $object = $this->getObjectNameAndType($objectData);
  366. if (!$object) {
  367. return;
  368. }
  369. $action = $action . '_' . $object['type'];
  370. if ($object['type'] === 'todo' && str_starts_with($action, Event::SUBJECT_OBJECT_UPDATE) && $object['status'] === 'COMPLETED') {
  371. $action .= '_completed';
  372. } elseif ($object['type'] === 'todo' && str_starts_with($action, Event::SUBJECT_OBJECT_UPDATE) && $object['status'] === 'NEEDS-ACTION') {
  373. $action .= '_needs_action';
  374. }
  375. $event = $this->activityManager->generateEvent();
  376. $event->setApp('dav')
  377. ->setObject('calendar', (int)$calendarData['id'])
  378. ->setType($object['type'] === 'event' ? 'calendar_event' : 'calendar_todo')
  379. ->setAuthor($currentUser);
  380. $users = $this->getUsersForShares($shares);
  381. $users[] = $owner;
  382. // Users for share can return the owner itself if the calendar is published
  383. foreach (array_unique($users) as $user) {
  384. if ($classification === CalDavBackend::CLASSIFICATION_PRIVATE && $user !== $owner) {
  385. // Private events are only shown to the owner
  386. continue;
  387. }
  388. $params = [
  389. 'actor' => $event->getAuthor(),
  390. 'calendar' => [
  391. 'id' => (int)$calendarData['id'],
  392. 'uri' => $calendarData['uri'],
  393. 'name' => $calendarData['{DAV:}displayname'],
  394. ],
  395. 'object' => [
  396. 'id' => $object['id'],
  397. 'name' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner ? 'Busy' : $object['name'],
  398. 'classified' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner,
  399. ],
  400. ];
  401. if ($object['type'] === 'event' && !str_contains($action, Event::SUBJECT_OBJECT_DELETE) && $this->appManager->isEnabledForUser('calendar')) {
  402. $params['object']['link']['object_uri'] = $objectData['uri'];
  403. $params['object']['link']['calendar_uri'] = $calendarData['uri'];
  404. $params['object']['link']['owner'] = $owner;
  405. }
  406. $event->setAffectedUser($user)
  407. ->setSubject(
  408. $user === $currentUser ? $action . '_self' : $action,
  409. $params
  410. );
  411. $this->activityManager->publish($event);
  412. }
  413. }
  414. /**
  415. * Creates activities when a calendar object was moved
  416. */
  417. public function onMovedCalendarObject(array $sourceCalendarData, array $targetCalendarData, array $sourceShares, array $targetShares, array $objectData): void {
  418. if (!isset($targetCalendarData['principaluri'])) {
  419. return;
  420. }
  421. $sourcePrincipal = explode('/', $sourceCalendarData['principaluri']);
  422. $sourceOwner = array_pop($sourcePrincipal);
  423. $targetPrincipal = explode('/', $targetCalendarData['principaluri']);
  424. $targetOwner = array_pop($targetPrincipal);
  425. if ($sourceOwner !== $targetOwner) {
  426. $this->onTouchCalendarObject(
  427. Event::SUBJECT_OBJECT_DELETE,
  428. $sourceCalendarData,
  429. $sourceShares,
  430. $objectData
  431. );
  432. $this->onTouchCalendarObject(
  433. Event::SUBJECT_OBJECT_ADD,
  434. $targetCalendarData,
  435. $targetShares,
  436. $objectData
  437. );
  438. return;
  439. }
  440. $currentUser = $this->userSession->getUser();
  441. if ($currentUser instanceof IUser) {
  442. $currentUser = $currentUser->getUID();
  443. } else {
  444. $currentUser = $targetOwner;
  445. }
  446. $classification = $objectData['classification'] ?? CalDavBackend::CLASSIFICATION_PUBLIC;
  447. $object = $this->getObjectNameAndType($objectData);
  448. if (!$object) {
  449. return;
  450. }
  451. $event = $this->activityManager->generateEvent();
  452. $event->setApp('dav')
  453. ->setObject('calendar', (int)$targetCalendarData['id'])
  454. ->setType($object['type'] === 'event' ? 'calendar_event' : 'calendar_todo')
  455. ->setAuthor($currentUser);
  456. $users = $this->getUsersForShares(array_intersect($sourceShares, $targetShares));
  457. $users[] = $targetOwner;
  458. // Users for share can return the owner itself if the calendar is published
  459. foreach (array_unique($users) as $user) {
  460. if ($classification === CalDavBackend::CLASSIFICATION_PRIVATE && $user !== $targetOwner) {
  461. // Private events are only shown to the owner
  462. continue;
  463. }
  464. $params = [
  465. 'actor' => $event->getAuthor(),
  466. 'sourceCalendar' => [
  467. 'id' => (int)$sourceCalendarData['id'],
  468. 'uri' => $sourceCalendarData['uri'],
  469. 'name' => $sourceCalendarData['{DAV:}displayname'],
  470. ],
  471. 'targetCalendar' => [
  472. 'id' => (int)$targetCalendarData['id'],
  473. 'uri' => $targetCalendarData['uri'],
  474. 'name' => $targetCalendarData['{DAV:}displayname'],
  475. ],
  476. 'object' => [
  477. 'id' => $object['id'],
  478. 'name' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $targetOwner ? 'Busy' : $object['name'],
  479. 'classified' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $targetOwner,
  480. ],
  481. ];
  482. if ($object['type'] === 'event' && $this->appManager->isEnabledForUser('calendar')) {
  483. $params['object']['link']['object_uri'] = $objectData['uri'];
  484. $params['object']['link']['calendar_uri'] = $targetCalendarData['uri'];
  485. $params['object']['link']['owner'] = $targetOwner;
  486. }
  487. $event->setAffectedUser($user)
  488. ->setSubject(
  489. $user === $currentUser ? Event::SUBJECT_OBJECT_MOVE . '_' . $object['type'] . '_self' : Event::SUBJECT_OBJECT_MOVE . '_' . $object['type'],
  490. $params
  491. );
  492. $this->activityManager->publish($event);
  493. }
  494. }
  495. /**
  496. * @param array $objectData
  497. * @return string[]|false
  498. */
  499. protected function getObjectNameAndType(array $objectData) {
  500. $vObject = Reader::read($objectData['calendardata']);
  501. $component = $componentType = null;
  502. foreach ($vObject->getComponents() as $component) {
  503. if (in_array($component->name, ['VEVENT', 'VTODO'])) {
  504. $componentType = $component->name;
  505. break;
  506. }
  507. }
  508. if (!$componentType) {
  509. // Calendar objects must have a VEVENT or VTODO component
  510. return false;
  511. }
  512. if ($componentType === 'VEVENT') {
  513. return ['id' => (string)$component->UID, 'name' => (string)$component->SUMMARY, 'type' => 'event'];
  514. }
  515. return ['id' => (string)$component->UID, 'name' => (string)$component->SUMMARY, 'type' => 'todo', 'status' => (string)$component->STATUS];
  516. }
  517. /**
  518. * Get all users that have access to a given calendar
  519. *
  520. * @param array $shares
  521. * @return string[]
  522. */
  523. protected function getUsersForShares(array $shares) {
  524. $users = $groups = [];
  525. foreach ($shares as $share) {
  526. $principal = explode('/', $share['{http://owncloud.org/ns}principal']);
  527. if ($principal[1] === 'users') {
  528. $users[] = $principal[2];
  529. } elseif ($principal[1] === 'groups') {
  530. $groups[] = $principal[2];
  531. }
  532. }
  533. if (!empty($groups)) {
  534. foreach ($groups as $gid) {
  535. $group = $this->groupManager->get($gid);
  536. if ($group instanceof IGroup) {
  537. foreach ($group->getUsers() as $user) {
  538. $users[] = $user->getUID();
  539. }
  540. }
  541. }
  542. }
  543. return array_unique($users);
  544. }
  545. }