1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
7 *
8 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
9 * @author John Molakvoæ <skjnldsv@protonmail.com>
10 *
11 * @license GNU AGPL version 3 or any later version
12 *
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License as
15 * published by the Free Software Foundation, either version 3 of the
16 * License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Affero General Public License for more details.
22 *
23 * You should have received a copy of the GNU Affero General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27namespace OCA\Files_Sharing\Listener;
28
29use OCP\Contacts\Events\ContactInteractedWithEvent;
30use OCP\EventDispatcher\Event;
31use OCP\EventDispatcher\IEventDispatcher;
32use OCP\EventDispatcher\IEventListener;
33use OCP\ILogger;
34use OCP\IUserManager;
35use OCP\Share\Events\ShareCreatedEvent;
36use OCP\Share\IShare;
37use function in_array;
38
39class ShareInteractionListener implements IEventListener {
40	private const SUPPORTED_SHARE_TYPES = [
41		IShare::TYPE_USER,
42		IShare::TYPE_EMAIL,
43		IShare::TYPE_REMOTE,
44	];
45
46	/** @var IEventDispatcher */
47	private $dispatcher;
48
49	/** @var IUserManager */
50	private $userManager;
51
52	/** @var ILogger */
53	private $logger;
54
55	public function __construct(IEventDispatcher $dispatcher,
56								IUserManager $userManager,
57								ILogger $logger) {
58		$this->dispatcher = $dispatcher;
59		$this->userManager = $userManager;
60		$this->logger = $logger;
61	}
62
63	public function handle(Event $event): void {
64		if (!($event instanceof ShareCreatedEvent)) {
65			// Unrelated
66			return;
67		}
68
69		$share = $event->getShare();
70		if (!in_array($share->getShareType(), self::SUPPORTED_SHARE_TYPES, true)) {
71			$this->logger->debug('Share type does not allow to emit interaction event');
72			return;
73		}
74		$actor = $this->userManager->get($share->getSharedBy());
75		$sharedWith = $this->userManager->get($share->getSharedWith());
76		if ($actor === null) {
77			$this->logger->warning('Share was not created by a user, can\'t emit interaction event');
78			return;
79		}
80		$interactionEvent = new ContactInteractedWithEvent($actor);
81		switch ($share->getShareType()) {
82			case IShare::TYPE_USER:
83				$interactionEvent->setUid($share->getSharedWith());
84				if ($sharedWith !== null) {
85					$interactionEvent->setFederatedCloudId($sharedWith->getCloudId());
86				}
87				break;
88			case IShare::TYPE_EMAIL:
89				$interactionEvent->setEmail($share->getSharedWith());
90				break;
91			case IShare::TYPE_REMOTE:
92				$interactionEvent->setFederatedCloudId($share->getSharedWith());
93				break;
94		}
95
96		$this->dispatcher->dispatchTyped($interactionEvent);
97	}
98}
99