-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathActivityManager.php
More file actions
165 lines (147 loc) Β· 4.71 KB
/
Copy pathActivityManager.php
File metadata and controls
165 lines (147 loc) Β· 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Forms\Activity;
use OCA\Forms\Db\Form;
use OCA\Forms\Service\CirclesService;
use OCP\Activity\IManager;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\Share\IShare;
class ActivityManager {
public function __construct(
protected string $appName,
private ?string $userId,
private IManager $manager,
private IGroupManager $groupManager,
private CirclesService $circlesService,
) {
}
/**
* Publish a new-Share Activity
*
* @param Form $form The shared form
* @param string $shareeId UserId, the form has been shared to
*/
public function publishNewShare(Form $form, string $shareeId): void {
$event = $this->manager->generateEvent();
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSHARE)
->setAffectedUser($shareeId)
->setAuthor($this->userId)
->setSubject(ActivityConstants::SUBJECT_NEWSHARE, [
'userId' => $this->userId,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
])
->setObject('form', $form->getId());
$this->manager->publish($event);
}
/**
* Publish a new-GroupShare Activity to each affected user
*
* @param Form $form The shared form
* @param string $groupId Group the form has been shared to
*/
public function publishNewGroupShare(Form $form, string $groupId): void {
$affectedUsers = $this->groupManager->get($groupId)->getUsers();
foreach ($affectedUsers as $user) {
$event = $this->manager->generateEvent();
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSHARE)
->setAffectedUser($user->getUID())
->setAuthor($this->userId)
->setSubject(ActivityConstants::SUBJECT_NEWGROUPSHARE, [
'userId' => $this->userId,
'groupId' => $groupId,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
])
->setObject('form', $form->getId());
$this->manager->publish($event);
}
}
/**
* Publish a new-CircleShare Activity to each affected user
*
* @param Form $form The shared form
* @param string $circleId Circle the form has been shared to
*/
public function publishNewCircleShare(Form $form, string $circleId): void {
$users = $this->circlesService->getCircleUsers($circleId);
foreach ($users as $user) {
$event = $this->manager->generateEvent();
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSHARE)
->setAffectedUser($user)
->setAuthor($this->userId)
->setSubject(ActivityConstants::SUBJECT_NEWCIRCLESHARE, [
'userId' => $this->userId,
'circleId' => $circleId,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
])
->setObject('form', $form->getId());
$this->manager->publish($event);
}
}
/**
* Publish a new-Submission Activity
*
* @param Form $form The affected Form
* @param string $submitterID ID of the User who submitted the form. Can also be our 'anon-user-'-ID
*/
public function publishNewSubmission(Form $form, string $submitterID): void {
$event = $this->manager->generateEvent();
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSUBMISSION)
->setAffectedUser($form->getOwnerId())
->setAuthor($submitterID)
->setSubject(ActivityConstants::SUBJECT_NEWSUBMISSION, [
'userId' => $submitterID,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
])
->setObject('form', $form->getId());
$this->manager->publish($event);
}
/**
* Publish a new-Submission Activity for shared forms
*
* @param Form $form The affected Form
* @param string $submitterID ID of the User who submitted the form. Can also be our 'anon-user-'-ID
*/
public function publishNewSharedSubmission(Form $form, int $shareType, string $shareWith, string $submitterID): void {
$users = [];
switch ($shareType) {
case IShare::TYPE_USER:
$users[] = $shareWith;
break;
case IShare::TYPE_GROUP:
$group = $this->groupManager->get($shareWith);
if ($group !== null) {
$users = array_map(fn (IUser $user) => $user->getUID(), $group->getUsers());
}
break;
case IShare::TYPE_CIRCLE:
$users = $this->circlesService->getCircleUsers($shareWith);
break;
}
foreach ($users as $userId) {
$event = $this->manager->generateEvent();
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSHAREDSUBMISSION)
->setAffectedUser($userId)
->setAuthor($submitterID)
->setSubject(ActivityConstants::SUBJECT_NEWSUBMISSION, [
'userId' => $submitterID,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
])
->setObject('form', $form->getId());
$this->manager->publish($event);
}
}
}