Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Manager.php « Activity « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d9b6b3cd2fe5a5c404e3073643371f62ba03224 (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
 *
 * @author Daniel Kesselberg <mail@danielkesselberg.de>
 * @author Joas Schilling <coding@schilljs.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 *
 * @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 OC\Activity;


use OCP\Activity\IConsumer;
use OCP\Activity\IEvent;
use OCP\Activity\IFilter;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\Activity\ISetting;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\RichObjectStrings\IValidator;

class Manager implements IManager {
	/** @var IRequest */
	protected $request;

	/** @var IUserSession */
	protected $session;

	/** @var IConfig */
	protected $config;

	/** @var IValidator */
	protected $validator;

	/** @var string */
	protected $formattingObjectType;

	/** @var int */
	protected $formattingObjectId;

	/** @var bool */
	protected $requirePNG = false;

	/** @var string */
	protected $currentUserId;

	public function __construct(IRequest $request,
								IUserSession $session,
								IConfig $config,
								IValidator $validator) {
		$this->request = $request;
		$this->session = $session;
		$this->config = $config;
		$this->validator = $validator;
	}

	/** @var \Closure[] */
	private $consumersClosures = [];

	/** @var IConsumer[] */
	private $consumers = [];

	/**
	 * @return \OCP\Activity\IConsumer[]
	 */
	protected function getConsumers(): array {
		if (!empty($this->consumers)) {
			return $this->consumers;
		}

		$this->consumers = [];
		foreach($this->consumersClosures as $consumer) {
			$c = $consumer();
			if ($c instanceof IConsumer) {
				$this->consumers[] = $c;
			} else {
				throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
			}
		}

		return $this->consumers;
	}

	/**
	 * Generates a new IEvent object
	 *
	 * Make sure to call at least the following methods before sending it to the
	 * app with via the publish() method:
	 *  - setApp()
	 *  - setType()
	 *  - setAffectedUser()
	 *  - setSubject()
	 *
	 * @return IEvent
	 */
	public function generateEvent(): IEvent {
		return new Event($this->validator);
	}

	/**
	 * Publish an event to the activity consumers
	 *
	 * Make sure to call at least the following methods before sending an Event:
	 *  - setApp()
	 *  - setType()
	 *  - setAffectedUser()
	 *  - setSubject()
	 *
	 * @param IEvent $event
	 * @throws \BadMethodCallException if required values have not been set
	 */
	public function publish(IEvent $event): void {
		if ($event->getAuthor() === '') {
			if ($this->session->getUser() instanceof IUser) {
				$event->setAuthor($this->session->getUser()->getUID());
			}
		}

		if (!$event->getTimestamp()) {
			$event->setTimestamp(time());
		}

		if (!$event->isValid()) {
			throw new \BadMethodCallException('The given event is invalid');
		}

		foreach ($this->getConsumers() as $c) {
			$c->receive($event);
		}
	}

	/**
	 * In order to improve lazy loading a closure can be registered which will be called in case
	 * activity consumers are actually requested
	 *
	 * $callable has to return an instance of OCA\Activity\IConsumer
	 *
	 * @param \Closure $callable
	 */
	public function registerConsumer(\Closure $callable): void {
		$this->consumersClosures[] = $callable;
		$this->consumers = [];
	}

	/** @var string[] */
	protected $filterClasses = [];

	/** @var IFilter[] */
	protected $filters = [];

	/**
	 * @param string $filter Class must implement OCA\Activity\IFilter
	 * @return void
	 */
	public function registerFilter(string $filter): void {
		$this->filterClasses[$filter] = false;
	}

	/**
	 * @return IFilter[]
	 * @throws \InvalidArgumentException
	 */
	public function getFilters(): array {
		foreach ($this->filterClasses as $class => $false) {
			/** @var IFilter $filter */
			$filter = \OC::$server->query($class);

			if (!$filter instanceof IFilter) {
				throw new \InvalidArgumentException('Invalid activity filter registered');
			}

			$this->filters[$filter->getIdentifier()] = $filter;

			unset($this->filterClasses[$class]);
		}
		return $this->filters;
	}

	/**
	 * @param string $id
	 * @return IFilter
	 * @throws \InvalidArgumentException when the filter was not found
	 * @since 11.0.0
	 */
	public function getFilterById(string $id): IFilter {
		$filters = $this->getFilters();

		if (isset($filters[$id])) {
			return $filters[$id];
		}

		throw new \InvalidArgumentException('Requested filter does not exist');
	}

	/** @var string[] */
	protected $providerClasses = [];

	/** @var IProvider[] */
	protected $providers = [];

	/**
	 * @param string $provider Class must implement OCA\Activity\IProvider
	 * @return void
	 */
	public function registerProvider(string $provider): void {
		$this->providerClasses[$provider] = false;
	}

	/**
	 * @return IProvider[]
	 * @throws \InvalidArgumentException
	 */
	public function getProviders(): array {
		foreach ($this->providerClasses as $class => $false) {
			/** @var IProvider $provider */
			$provider = \OC::$server->query($class);

			if (!$provider instanceof IProvider) {
				throw new \InvalidArgumentException('Invalid activity provider registered');
			}

			$this->providers[] = $provider;

			unset($this->providerClasses[$class]);
		}
		return $this->providers;
	}

	/** @var string[] */
	protected $settingsClasses = [];

	/** @var ISetting[] */
	protected $settings = [];

	/**
	 * @param string $setting Class must implement OCA\Activity\ISetting
	 * @return void
	 */
	public function registerSetting(string $setting): void {
		$this->settingsClasses[$setting] = false;
	}

	/**
	 * @return ISetting[]
	 * @throws \InvalidArgumentException
	 */
	public function getSettings(): array {
		foreach ($this->settingsClasses as $class => $false) {
			/** @var ISetting $setting */
			$setting = \OC::$server->query($class);

			if (!$setting instanceof ISetting) {
				throw new \InvalidArgumentException('Invalid activity filter registered');
			}

			$this->settings[$setting->getIdentifier()] = $setting;

			unset($this->settingsClasses[$class]);
		}
		return $this->settings;
	}

	/**
	 * @param string $id
	 * @return ISetting
	 * @throws \InvalidArgumentException when the setting was not found
	 * @since 11.0.0
	 */
	public function getSettingById(string $id): ISetting {
		$settings = $this->getSettings();

		if (isset($settings[$id])) {
			return $settings[$id];
		}

		throw new \InvalidArgumentException('Requested setting does not exist');
	}


	/**
	 * @param string $type
	 * @param int $id
	 */
	public function setFormattingObject(string $type, int $id): void {
		$this->formattingObjectType = $type;
		$this->formattingObjectId = $id;
	}

	/**
	 * @return bool
	 */
	public function isFormattingFilteredObject(): bool {
		return $this->formattingObjectType !== null && $this->formattingObjectId !== null
			&& $this->formattingObjectType === $this->request->getParam('object_type')
			&& $this->formattingObjectId === (int) $this->request->getParam('object_id');
	}

	/**
	 * @param bool $status Set to true, when parsing events should not use SVG icons
	 */
	public function setRequirePNG(bool $status): void {
		$this->requirePNG = $status;
	}

	/**
	 * @return bool
	 */
	public function getRequirePNG(): bool {
		return $this->requirePNG;
	}

	/**
	 * Set the user we need to use
	 *
	 * @param string|null $currentUserId
	 * @throws \UnexpectedValueException If the user is invalid
	 */
	public function setCurrentUserId(string $currentUserId = null): void {
		if (!is_string($currentUserId) && $currentUserId !== null) {
			throw new \UnexpectedValueException('The given current user is invalid');
		}
		$this->currentUserId = $currentUserId;
	}

	/**
	 * Get the user we need to use
	 *
	 * Either the user is logged in, or we try to get it from the token
	 *
	 * @return string
	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
	 */
	public function getCurrentUserId(): string {
		if ($this->currentUserId !== null) {
			return $this->currentUserId;
		}

		if (!$this->session->isLoggedIn()) {
			return $this->getUserFromToken();
		}

		return $this->session->getUser()->getUID();
	}

	/**
	 * Get the user for the token
	 *
	 * @return string
	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
	 */
	protected function getUserFromToken(): string {
		$token = (string) $this->request->getParam('token', '');
		if (strlen($token) !== 30) {
			throw new \UnexpectedValueException('The token is invalid');
		}

		$users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);

		if (count($users) !== 1) {
			// No unique user found
			throw new \UnexpectedValueException('The token is invalid');
		}

		// Token found login as that user
		return array_shift($users);
	}

}