Backend.php 18 KB

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