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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Vincent Petry <pvince81@owncloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\SystemTags\Activity;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IShareHelper;
use OCP\SystemTag\ISystemTag;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ManagerEvent;
use OCP\SystemTag\MapperEvent;
use OCP\SystemTag\TagNotFoundException;
class Listener {
/** @var IGroupManager */
protected $groupManager;
/** @var IManager */
protected $activityManager;
/** @var IUserSession */
protected $session;
/** @var IConfig */
protected $config;
/** @var \OCP\SystemTag\ISystemTagManager */
protected $tagManager;
/** @var \OCP\App\IAppManager */
protected $appManager;
/** @var \OCP\Files\Config\IMountProviderCollection */
protected $mountCollection;
/** @var \OCP\Files\IRootFolder */
protected $rootFolder;
/** @var IShareHelper */
protected $shareHelper;
/**
* Listener constructor.
*
* @param IGroupManager $groupManager
* @param IManager $activityManager
* @param IUserSession $session
* @param IConfig $config
* @param ISystemTagManager $tagManager
* @param IAppManager $appManager
* @param IMountProviderCollection $mountCollection
* @param IRootFolder $rootFolder
* @param IShareHelper $shareHelper
*/
public function __construct(IGroupManager $groupManager,
IManager $activityManager,
IUserSession $session,
IConfig $config,
ISystemTagManager $tagManager,
IAppManager $appManager,
IMountProviderCollection $mountCollection,
IRootFolder $rootFolder,
IShareHelper $shareHelper) {
$this->groupManager = $groupManager;
$this->activityManager = $activityManager;
$this->session = $session;
$this->config = $config;
$this->tagManager = $tagManager;
$this->appManager = $appManager;
$this->mountCollection = $mountCollection;
$this->rootFolder = $rootFolder;
$this->shareHelper = $shareHelper;
}
/**
* @param ManagerEvent $event
*/
public function event(ManagerEvent $event) {
$actor = $this->session->getUser();
if ($actor instanceof IUser) {
$actor = $actor->getUID();
} else {
$actor = '';
}
$tag = $event->getTag();
$activity = $this->activityManager->generateEvent();
$activity->setApp('systemtags')
->setType('systemtags')
->setAuthor($actor)
->setObject('systemtag', (int)$tag->getId(), $tag->getName());
if ($event->getEvent() === ManagerEvent::EVENT_CREATE) {
$activity->setSubject(Provider::CREATE_TAG, [
$actor,
$this->prepareTagAsParameter($event->getTag()),
]);
} elseif ($event->getEvent() === ManagerEvent::EVENT_UPDATE) {
$activity->setSubject(Provider::UPDATE_TAG, [
$actor,
$this->prepareTagAsParameter($event->getTag()),
$this->prepareTagAsParameter($event->getTagBefore()),
]);
} elseif ($event->getEvent() === ManagerEvent::EVENT_DELETE) {
$activity->setSubject(Provider::DELETE_TAG, [
$actor,
$this->prepareTagAsParameter($event->getTag()),
]);
} else {
return;
}
$group = $this->groupManager->get('admin');
if ($group instanceof IGroup) {
foreach ($group->getUsers() as $user) {
$activity->setAffectedUser($user->getUID());
$this->activityManager->publish($activity);
}
}
if ($actor !== '' && ($event->getEvent() === ManagerEvent::EVENT_CREATE || $event->getEvent() === ManagerEvent::EVENT_UPDATE)) {
$this->updateLastUsedTags($actor, $event->getTag());
}
}
/**
* @param MapperEvent $event
*/
public function mapperEvent(MapperEvent $event) {
$tagIds = $event->getTags();
if ($event->getObjectType() !== 'files' ||empty($tagIds)
|| !in_array($event->getEvent(), [MapperEvent::EVENT_ASSIGN, MapperEvent::EVENT_UNASSIGN])
|| !$this->appManager->isInstalled('activity')) {
// System tags not for files, no tags, not (un-)assigning or no activity-app enabled (save the energy)
return;
}
try {
$tags = $this->tagManager->getTagsByIds($tagIds);
} catch (TagNotFoundException $e) {
// User assigned/unassigned a non-existing tag, ignore...
return;
}
if (empty($tags)) {
return;
}
// Get all mount point owners
$cache = $this->mountCollection->getMountCache();
$mounts = $cache->getMountsForFileId($event->getObjectId());
if (empty($mounts)) {
return;
}
$users = [];
foreach ($mounts as $mount) {
$owner = $mount->getUser()->getUID();
$ownerFolder = $this->rootFolder->getUserFolder($owner);
$nodes = $ownerFolder->getById($event->getObjectId());
if (!empty($nodes)) {
/** @var Node $node */
$node = array_shift($nodes);
$al = $this->shareHelper->getPathsForAccessList($node);
$users += $al['users'];
}
}
$actor = $this->session->getUser();
if ($actor instanceof IUser) {
$actor = $actor->getUID();
} else {
$actor = '';
}
$activity = $this->activityManager->generateEvent();
$activity->setApp('systemtags')
->setType('systemtags')
->setAuthor($actor)
->setObject($event->getObjectType(), (int) $event->getObjectId());
foreach ($users as $user => $path) {
$user = (string)$user; // numerical ids could be ints which are not accepted everywhere
$activity->setAffectedUser($user);
foreach ($tags as $tag) {
// don't publish activity for non-admins if tag is invisible
if (!$tag->isUserVisible() && !$this->groupManager->isAdmin($user)) {
continue;
}
if ($event->getEvent() === MapperEvent::EVENT_ASSIGN) {
$activity->setSubject(Provider::ASSIGN_TAG, [
$actor,
$path,
$this->prepareTagAsParameter($tag),
]);
} elseif ($event->getEvent() === MapperEvent::EVENT_UNASSIGN) {
$activity->setSubject(Provider::UNASSIGN_TAG, [
$actor,
$path,
$this->prepareTagAsParameter($tag),
]);
}
$this->activityManager->publish($activity);
}
}
if ($actor !== '' && $event->getEvent() === MapperEvent::EVENT_ASSIGN) {
foreach ($tags as $tag) {
$this->updateLastUsedTags($actor, $tag);
}
}
}
/**
* @param string $actor
* @param ISystemTag $tag
*/
protected function updateLastUsedTags($actor, ISystemTag $tag) {
$lastUsedTags = $this->config->getUserValue($actor, 'systemtags', 'last_used', '[]');
$lastUsedTags = json_decode($lastUsedTags, true);
array_unshift($lastUsedTags, $tag->getId());
array_unique($lastUsedTags);
$lastUsedTags = array_slice($lastUsedTags, 0, 10);
$this->config->setUserValue($actor, 'systemtags', 'last_used', json_encode($lastUsedTags));
}
/**
* @param ISystemTag $tag
* @return string
*/
protected function prepareTagAsParameter(ISystemTag $tag) {
return json_encode([
'id' => $tag->getId(),
'name' => $tag->getName(),
'assignable' => $tag->isUserAssignable(),
'visible' => $tag->isUserVisible(),
]);
}
}
|