1<?php
2/**
3 * @copyright Copyright (c) 2021 Jonas Rittershofer <jotoeri@users.noreply.github.com>
4 *
5 * @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
6 *
7 * @license GNU AGPL version 3 or any later version
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24namespace OCA\Forms\Activity;
25
26use OCA\Forms\Db\Form;
27
28use OCP\Activity\IManager;
29use OCP\IGroupManager;
30use OCP\ILogger;
31use OCP\IUser;
32use OCP\IUserSession;
33
34class ActivityManager {
35	protected $appName;
36
37	/** @var IManager */
38	private $manager;
39
40	/** @var IGroupManager */
41	private $groupManager;
42
43	/** @var ILogger */
44	private $logger;
45
46	/** @var IUser */
47	private $currentUser;
48
49	public function __construct(string $appName,
50								IManager $manager,
51								IGroupManager $groupManager,
52								ILogger $logger,
53								IUserSession $userSession) {
54		$this->appName = $appName;
55		$this->manager = $manager;
56		$this->groupManager = $groupManager;
57		$this->logger = $logger;
58		$this->currentUser = $userSession->getUser();
59	}
60
61	/**
62	 * Publish a new-Share Activity
63	 * @param Form $form The shared form
64	 * @param string $shareeId UserId, the form has been shared to
65	 */
66	public function publishNewShare(Form $form, string $shareeId) {
67		$event = $this->manager->generateEvent();
68		$event->setApp($this->appName)
69			->setType(ActivityConstants::TYPE_NEWSHARE)
70			->setAffectedUser($shareeId)
71			->setAuthor($this->currentUser->getUID())
72			->setSubject(ActivityConstants::SUBJECT_NEWSHARE, [
73				'userId' => $this->currentUser->getUID(),
74				'formTitle' => $form->getTitle(),
75				'formHash' => $form->getHash()
76			])
77			->setObject('form', $form->getId());
78
79		$this->manager->publish($event);
80	}
81
82	/**
83	 * Publish a new-GroupShare Activity to each affected user
84	 * @param Form $form The shared form
85	 * @param string $groupId Group the form has been shared to
86	 */
87	public function publishNewGroupShare(Form $form, string $groupId) {
88		$affectedUsers = $this->groupManager->get($groupId)->getUsers();
89
90		foreach ($affectedUsers as $user) {
91			$event = $this->manager->generateEvent();
92			$event->setApp($this->appName)
93				->setType(ActivityConstants::TYPE_NEWSHARE)
94				->setAffectedUser($user->getUID())
95				->setAuthor($this->currentUser->getUID())
96				->setSubject(ActivityConstants::SUBJECT_NEWGROUPSHARE, [
97					'userId' => $this->currentUser->getUID(),
98					'groupId' => $groupId,
99					'formTitle' => $form->getTitle(),
100					'formHash' => $form->getHash()
101				])
102				->setObject('form', $form->getId());
103
104			$this->manager->publish($event);
105		}
106	}
107
108	/**
109	 * Publish a new-Submission Activity
110	 * @param Form $form The affected Form
111	 * @param string $submittorId ID of the User who submitted the form. Can also be our 'anon-user-'-ID
112	 */
113	public function publishNewSubmission(Form $form, string $submittorId) {
114		$event = $this->manager->generateEvent();
115		$event->setApp($this->appName)
116			->setType(ActivityConstants::TYPE_NEWSUBMISSION)
117			->setAffectedUser($form->getOwnerId())
118			->setAuthor($submittorId)
119			->setSubject(ActivityConstants::SUBJECT_NEWSUBMISSION, [
120				'userId' => $submittorId,
121				'formTitle' => $form->getTitle(),
122				'formHash' => $form->getHash()
123			])
124			->setObject('form', $form->getId());
125
126		$this->manager->publish($event);
127	}
128}
129