Completed
Push — master ( b95e87...fafc07 )
by
unknown
30:06
created
lib/private/Activity/Manager.php 1 patch
Indentation   +349 added lines, -349 removed lines patch added patch discarded remove patch
@@ -29,353 +29,353 @@
 block discarded – undo
29 29
 
30 30
 class Manager implements IManager {
31 31
 
32
-	/** @var string */
33
-	protected $formattingObjectType;
34
-
35
-	/** @var int */
36
-	protected $formattingObjectId;
37
-
38
-	/** @var bool */
39
-	protected $requirePNG = false;
40
-
41
-	/** @var string */
42
-	protected $currentUserId;
43
-
44
-	public function __construct(
45
-		protected IRequest $request,
46
-		protected IUserSession $session,
47
-		protected IConfig $config,
48
-		protected IValidator $validator,
49
-		protected IRichTextFormatter $richTextFormatter,
50
-		protected IL10N $l10n,
51
-		protected ITimeFactory $timeFactory,
52
-	) {
53
-	}
54
-
55
-	/** @var \Closure[] */
56
-	private $consumersClosures = [];
57
-
58
-	/** @var IConsumer[] */
59
-	private $consumers = [];
60
-
61
-	/**
62
-	 * @return \OCP\Activity\IConsumer[]
63
-	 */
64
-	protected function getConsumers(): array {
65
-		if (!empty($this->consumers)) {
66
-			return $this->consumers;
67
-		}
68
-
69
-		$this->consumers = [];
70
-		foreach ($this->consumersClosures as $consumer) {
71
-			$c = $consumer();
72
-			if ($c instanceof IConsumer) {
73
-				$this->consumers[] = $c;
74
-			} else {
75
-				throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
76
-			}
77
-		}
78
-
79
-		return $this->consumers;
80
-	}
81
-
82
-	/**
83
-	 * Generates a new IEvent object
84
-	 *
85
-	 * Make sure to call at least the following methods before sending it to the
86
-	 * app with via the publish() method:
87
-	 *  - setApp()
88
-	 *  - setType()
89
-	 *  - setAffectedUser()
90
-	 *  - setSubject()
91
-	 *
92
-	 * @return IEvent
93
-	 */
94
-	public function generateEvent(): IEvent {
95
-		return new Event($this->validator, $this->richTextFormatter);
96
-	}
97
-
98
-	/**
99
-	 * {@inheritDoc}
100
-	 */
101
-	public function publish(IEvent $event): void {
102
-		if ($event->getAuthor() === '' && $this->session->getUser() instanceof IUser) {
103
-			$event->setAuthor($this->session->getUser()->getUID());
104
-		}
105
-
106
-		if (!$event->getTimestamp()) {
107
-			$event->setTimestamp($this->timeFactory->getTime());
108
-		}
109
-
110
-		if ($event->getAffectedUser() === '' || !$event->isValid()) {
111
-			throw new IncompleteActivityException('The given event is invalid');
112
-		}
113
-
114
-		foreach ($this->getConsumers() as $c) {
115
-			$c->receive($event);
116
-		}
117
-	}
118
-
119
-	/**
120
-	 * {@inheritDoc}
121
-	 */
122
-	public function bulkPublish(IEvent $event, array $affectedUserIds, ISetting $setting): void {
123
-		if (empty($affectedUserIds)) {
124
-			throw new IncompleteActivityException('The given event is invalid');
125
-		}
126
-
127
-		if (($event->getAuthor() === '') && $this->session->getUser() instanceof IUser) {
128
-			$event->setAuthor($this->session->getUser()->getUID());
129
-		}
130
-
131
-		if (!$event->getTimestamp()) {
132
-			$event->setTimestamp($this->timeFactory->getTime());
133
-		}
134
-
135
-		if (!$event->isValid()) {
136
-			throw new IncompleteActivityException('The given event is invalid');
137
-		}
138
-
139
-		foreach ($this->getConsumers() as $c) {
140
-			if ($c instanceof IBulkConsumer) {
141
-				$c->bulkReceive($event, $affectedUserIds, $setting);
142
-				continue;
143
-			}
144
-			foreach ($affectedUserIds as $affectedUserId) {
145
-				$event->setAffectedUser($affectedUserId);
146
-				$c->receive($event);
147
-			}
148
-		}
149
-	}
150
-
151
-
152
-	/**
153
-	 * In order to improve lazy loading a closure can be registered which will be called in case
154
-	 * activity consumers are actually requested
155
-	 *
156
-	 * $callable has to return an instance of OCA\Activity\IConsumer
157
-	 *
158
-	 * @param \Closure $callable
159
-	 */
160
-	public function registerConsumer(\Closure $callable): void {
161
-		$this->consumersClosures[] = $callable;
162
-		$this->consumers = [];
163
-	}
164
-
165
-	/** @var string[] */
166
-	protected $filterClasses = [];
167
-
168
-	/** @var IFilter[] */
169
-	protected $filters = [];
170
-
171
-	/**
172
-	 * @param string $filter Class must implement OCA\Activity\IFilter
173
-	 * @return void
174
-	 */
175
-	public function registerFilter(string $filter): void {
176
-		$this->filterClasses[$filter] = false;
177
-	}
178
-
179
-	/**
180
-	 * @return IFilter[]
181
-	 * @throws \InvalidArgumentException
182
-	 */
183
-	public function getFilters(): array {
184
-		foreach ($this->filterClasses as $class => $false) {
185
-			/** @var IFilter $filter */
186
-			$filter = \OCP\Server::get($class);
187
-
188
-			if (!$filter instanceof IFilter) {
189
-				throw new \InvalidArgumentException('Invalid activity filter registered');
190
-			}
191
-
192
-			$this->filters[$filter->getIdentifier()] = $filter;
193
-
194
-			unset($this->filterClasses[$class]);
195
-		}
196
-		return $this->filters;
197
-	}
198
-
199
-	/**
200
-	 * {@inheritDoc}
201
-	 */
202
-	public function getFilterById(string $id): IFilter {
203
-		$filters = $this->getFilters();
204
-
205
-		if (isset($filters[$id])) {
206
-			return $filters[$id];
207
-		}
208
-
209
-		throw new FilterNotFoundException($id);
210
-	}
211
-
212
-	/** @var string[] */
213
-	protected $providerClasses = [];
214
-
215
-	/** @var IProvider[] */
216
-	protected $providers = [];
217
-
218
-	/**
219
-	 * @param string $provider Class must implement OCA\Activity\IProvider
220
-	 * @return void
221
-	 */
222
-	public function registerProvider(string $provider): void {
223
-		$this->providerClasses[$provider] = false;
224
-	}
225
-
226
-	/**
227
-	 * @return IProvider[]
228
-	 * @throws \InvalidArgumentException
229
-	 */
230
-	public function getProviders(): array {
231
-		foreach ($this->providerClasses as $class => $false) {
232
-			/** @var IProvider $provider */
233
-			$provider = \OCP\Server::get($class);
234
-
235
-			if (!$provider instanceof IProvider) {
236
-				throw new \InvalidArgumentException('Invalid activity provider registered');
237
-			}
238
-
239
-			$this->providers[] = $provider;
240
-
241
-			unset($this->providerClasses[$class]);
242
-		}
243
-		return $this->providers;
244
-	}
245
-
246
-	/** @var string[] */
247
-	protected $settingsClasses = [];
248
-
249
-	/** @var ISetting[] */
250
-	protected $settings = [];
251
-
252
-	/**
253
-	 * @param string $setting Class must implement OCA\Activity\ISetting
254
-	 * @return void
255
-	 */
256
-	public function registerSetting(string $setting): void {
257
-		$this->settingsClasses[$setting] = false;
258
-	}
259
-
260
-	/**
261
-	 * @return ActivitySettings[]
262
-	 * @throws \InvalidArgumentException
263
-	 */
264
-	public function getSettings(): array {
265
-		foreach ($this->settingsClasses as $class => $false) {
266
-			/** @var ISetting $setting */
267
-			$setting = \OCP\Server::get($class);
268
-
269
-			if ($setting instanceof ISetting) {
270
-				if (!$setting instanceof ActivitySettings) {
271
-					$setting = new ActivitySettingsAdapter($setting, $this->l10n);
272
-				}
273
-			} else {
274
-				throw new \InvalidArgumentException('Invalid activity filter registered');
275
-			}
276
-
277
-			$this->settings[$setting->getIdentifier()] = $setting;
278
-
279
-			unset($this->settingsClasses[$class]);
280
-		}
281
-		return $this->settings;
282
-	}
283
-
284
-	/**
285
-	 * {@inheritDoc}
286
-	 */
287
-	public function getSettingById(string $id): ActivitySettings {
288
-		$settings = $this->getSettings();
289
-
290
-		if (isset($settings[$id])) {
291
-			return $settings[$id];
292
-		}
293
-
294
-		throw new SettingNotFoundException($id);
295
-	}
296
-
297
-
298
-	/**
299
-	 * @param string $type
300
-	 * @param int $id
301
-	 */
302
-	public function setFormattingObject(string $type, int $id): void {
303
-		$this->formattingObjectType = $type;
304
-		$this->formattingObjectId = $id;
305
-	}
306
-
307
-	/**
308
-	 * @return bool
309
-	 */
310
-	public function isFormattingFilteredObject(): bool {
311
-		return $this->formattingObjectType !== null && $this->formattingObjectId !== null
312
-			&& $this->formattingObjectType === $this->request->getParam('object_type')
313
-			&& $this->formattingObjectId === (int)$this->request->getParam('object_id');
314
-	}
315
-
316
-	/**
317
-	 * @param bool $status Set to true, when parsing events should not use SVG icons
318
-	 */
319
-	public function setRequirePNG(bool $status): void {
320
-		$this->requirePNG = $status;
321
-	}
322
-
323
-	/**
324
-	 * @return bool
325
-	 */
326
-	public function getRequirePNG(): bool {
327
-		return $this->requirePNG;
328
-	}
329
-
330
-	/**
331
-	 * Set the user we need to use
332
-	 *
333
-	 * @param string|null $currentUserId
334
-	 */
335
-	public function setCurrentUserId(?string $currentUserId = null): void {
336
-		$this->currentUserId = $currentUserId;
337
-	}
338
-
339
-	/**
340
-	 * Get the user we need to use
341
-	 *
342
-	 * Either the user is logged in, or we try to get it from the token
343
-	 *
344
-	 * @return string
345
-	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
346
-	 */
347
-	public function getCurrentUserId(): string {
348
-		if ($this->currentUserId !== null) {
349
-			return $this->currentUserId;
350
-		}
351
-
352
-		if (!$this->session->isLoggedIn()) {
353
-			return $this->getUserFromToken();
354
-		}
355
-
356
-		return $this->session->getUser()->getUID();
357
-	}
358
-
359
-	/**
360
-	 * Get the user for the token
361
-	 *
362
-	 * @return string
363
-	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
364
-	 */
365
-	protected function getUserFromToken(): string {
366
-		$token = (string)$this->request->getParam('token', '');
367
-		if (strlen($token) !== 30) {
368
-			throw new \UnexpectedValueException('The token is invalid');
369
-		}
370
-
371
-		$users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
372
-
373
-		if (count($users) !== 1) {
374
-			// No unique user found
375
-			throw new \UnexpectedValueException('The token is invalid');
376
-		}
377
-
378
-		// Token found login as that user
379
-		return array_shift($users);
380
-	}
32
+    /** @var string */
33
+    protected $formattingObjectType;
34
+
35
+    /** @var int */
36
+    protected $formattingObjectId;
37
+
38
+    /** @var bool */
39
+    protected $requirePNG = false;
40
+
41
+    /** @var string */
42
+    protected $currentUserId;
43
+
44
+    public function __construct(
45
+        protected IRequest $request,
46
+        protected IUserSession $session,
47
+        protected IConfig $config,
48
+        protected IValidator $validator,
49
+        protected IRichTextFormatter $richTextFormatter,
50
+        protected IL10N $l10n,
51
+        protected ITimeFactory $timeFactory,
52
+    ) {
53
+    }
54
+
55
+    /** @var \Closure[] */
56
+    private $consumersClosures = [];
57
+
58
+    /** @var IConsumer[] */
59
+    private $consumers = [];
60
+
61
+    /**
62
+     * @return \OCP\Activity\IConsumer[]
63
+     */
64
+    protected function getConsumers(): array {
65
+        if (!empty($this->consumers)) {
66
+            return $this->consumers;
67
+        }
68
+
69
+        $this->consumers = [];
70
+        foreach ($this->consumersClosures as $consumer) {
71
+            $c = $consumer();
72
+            if ($c instanceof IConsumer) {
73
+                $this->consumers[] = $c;
74
+            } else {
75
+                throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
76
+            }
77
+        }
78
+
79
+        return $this->consumers;
80
+    }
81
+
82
+    /**
83
+     * Generates a new IEvent object
84
+     *
85
+     * Make sure to call at least the following methods before sending it to the
86
+     * app with via the publish() method:
87
+     *  - setApp()
88
+     *  - setType()
89
+     *  - setAffectedUser()
90
+     *  - setSubject()
91
+     *
92
+     * @return IEvent
93
+     */
94
+    public function generateEvent(): IEvent {
95
+        return new Event($this->validator, $this->richTextFormatter);
96
+    }
97
+
98
+    /**
99
+     * {@inheritDoc}
100
+     */
101
+    public function publish(IEvent $event): void {
102
+        if ($event->getAuthor() === '' && $this->session->getUser() instanceof IUser) {
103
+            $event->setAuthor($this->session->getUser()->getUID());
104
+        }
105
+
106
+        if (!$event->getTimestamp()) {
107
+            $event->setTimestamp($this->timeFactory->getTime());
108
+        }
109
+
110
+        if ($event->getAffectedUser() === '' || !$event->isValid()) {
111
+            throw new IncompleteActivityException('The given event is invalid');
112
+        }
113
+
114
+        foreach ($this->getConsumers() as $c) {
115
+            $c->receive($event);
116
+        }
117
+    }
118
+
119
+    /**
120
+     * {@inheritDoc}
121
+     */
122
+    public function bulkPublish(IEvent $event, array $affectedUserIds, ISetting $setting): void {
123
+        if (empty($affectedUserIds)) {
124
+            throw new IncompleteActivityException('The given event is invalid');
125
+        }
126
+
127
+        if (($event->getAuthor() === '') && $this->session->getUser() instanceof IUser) {
128
+            $event->setAuthor($this->session->getUser()->getUID());
129
+        }
130
+
131
+        if (!$event->getTimestamp()) {
132
+            $event->setTimestamp($this->timeFactory->getTime());
133
+        }
134
+
135
+        if (!$event->isValid()) {
136
+            throw new IncompleteActivityException('The given event is invalid');
137
+        }
138
+
139
+        foreach ($this->getConsumers() as $c) {
140
+            if ($c instanceof IBulkConsumer) {
141
+                $c->bulkReceive($event, $affectedUserIds, $setting);
142
+                continue;
143
+            }
144
+            foreach ($affectedUserIds as $affectedUserId) {
145
+                $event->setAffectedUser($affectedUserId);
146
+                $c->receive($event);
147
+            }
148
+        }
149
+    }
150
+
151
+
152
+    /**
153
+     * In order to improve lazy loading a closure can be registered which will be called in case
154
+     * activity consumers are actually requested
155
+     *
156
+     * $callable has to return an instance of OCA\Activity\IConsumer
157
+     *
158
+     * @param \Closure $callable
159
+     */
160
+    public function registerConsumer(\Closure $callable): void {
161
+        $this->consumersClosures[] = $callable;
162
+        $this->consumers = [];
163
+    }
164
+
165
+    /** @var string[] */
166
+    protected $filterClasses = [];
167
+
168
+    /** @var IFilter[] */
169
+    protected $filters = [];
170
+
171
+    /**
172
+     * @param string $filter Class must implement OCA\Activity\IFilter
173
+     * @return void
174
+     */
175
+    public function registerFilter(string $filter): void {
176
+        $this->filterClasses[$filter] = false;
177
+    }
178
+
179
+    /**
180
+     * @return IFilter[]
181
+     * @throws \InvalidArgumentException
182
+     */
183
+    public function getFilters(): array {
184
+        foreach ($this->filterClasses as $class => $false) {
185
+            /** @var IFilter $filter */
186
+            $filter = \OCP\Server::get($class);
187
+
188
+            if (!$filter instanceof IFilter) {
189
+                throw new \InvalidArgumentException('Invalid activity filter registered');
190
+            }
191
+
192
+            $this->filters[$filter->getIdentifier()] = $filter;
193
+
194
+            unset($this->filterClasses[$class]);
195
+        }
196
+        return $this->filters;
197
+    }
198
+
199
+    /**
200
+     * {@inheritDoc}
201
+     */
202
+    public function getFilterById(string $id): IFilter {
203
+        $filters = $this->getFilters();
204
+
205
+        if (isset($filters[$id])) {
206
+            return $filters[$id];
207
+        }
208
+
209
+        throw new FilterNotFoundException($id);
210
+    }
211
+
212
+    /** @var string[] */
213
+    protected $providerClasses = [];
214
+
215
+    /** @var IProvider[] */
216
+    protected $providers = [];
217
+
218
+    /**
219
+     * @param string $provider Class must implement OCA\Activity\IProvider
220
+     * @return void
221
+     */
222
+    public function registerProvider(string $provider): void {
223
+        $this->providerClasses[$provider] = false;
224
+    }
225
+
226
+    /**
227
+     * @return IProvider[]
228
+     * @throws \InvalidArgumentException
229
+     */
230
+    public function getProviders(): array {
231
+        foreach ($this->providerClasses as $class => $false) {
232
+            /** @var IProvider $provider */
233
+            $provider = \OCP\Server::get($class);
234
+
235
+            if (!$provider instanceof IProvider) {
236
+                throw new \InvalidArgumentException('Invalid activity provider registered');
237
+            }
238
+
239
+            $this->providers[] = $provider;
240
+
241
+            unset($this->providerClasses[$class]);
242
+        }
243
+        return $this->providers;
244
+    }
245
+
246
+    /** @var string[] */
247
+    protected $settingsClasses = [];
248
+
249
+    /** @var ISetting[] */
250
+    protected $settings = [];
251
+
252
+    /**
253
+     * @param string $setting Class must implement OCA\Activity\ISetting
254
+     * @return void
255
+     */
256
+    public function registerSetting(string $setting): void {
257
+        $this->settingsClasses[$setting] = false;
258
+    }
259
+
260
+    /**
261
+     * @return ActivitySettings[]
262
+     * @throws \InvalidArgumentException
263
+     */
264
+    public function getSettings(): array {
265
+        foreach ($this->settingsClasses as $class => $false) {
266
+            /** @var ISetting $setting */
267
+            $setting = \OCP\Server::get($class);
268
+
269
+            if ($setting instanceof ISetting) {
270
+                if (!$setting instanceof ActivitySettings) {
271
+                    $setting = new ActivitySettingsAdapter($setting, $this->l10n);
272
+                }
273
+            } else {
274
+                throw new \InvalidArgumentException('Invalid activity filter registered');
275
+            }
276
+
277
+            $this->settings[$setting->getIdentifier()] = $setting;
278
+
279
+            unset($this->settingsClasses[$class]);
280
+        }
281
+        return $this->settings;
282
+    }
283
+
284
+    /**
285
+     * {@inheritDoc}
286
+     */
287
+    public function getSettingById(string $id): ActivitySettings {
288
+        $settings = $this->getSettings();
289
+
290
+        if (isset($settings[$id])) {
291
+            return $settings[$id];
292
+        }
293
+
294
+        throw new SettingNotFoundException($id);
295
+    }
296
+
297
+
298
+    /**
299
+     * @param string $type
300
+     * @param int $id
301
+     */
302
+    public function setFormattingObject(string $type, int $id): void {
303
+        $this->formattingObjectType = $type;
304
+        $this->formattingObjectId = $id;
305
+    }
306
+
307
+    /**
308
+     * @return bool
309
+     */
310
+    public function isFormattingFilteredObject(): bool {
311
+        return $this->formattingObjectType !== null && $this->formattingObjectId !== null
312
+            && $this->formattingObjectType === $this->request->getParam('object_type')
313
+            && $this->formattingObjectId === (int)$this->request->getParam('object_id');
314
+    }
315
+
316
+    /**
317
+     * @param bool $status Set to true, when parsing events should not use SVG icons
318
+     */
319
+    public function setRequirePNG(bool $status): void {
320
+        $this->requirePNG = $status;
321
+    }
322
+
323
+    /**
324
+     * @return bool
325
+     */
326
+    public function getRequirePNG(): bool {
327
+        return $this->requirePNG;
328
+    }
329
+
330
+    /**
331
+     * Set the user we need to use
332
+     *
333
+     * @param string|null $currentUserId
334
+     */
335
+    public function setCurrentUserId(?string $currentUserId = null): void {
336
+        $this->currentUserId = $currentUserId;
337
+    }
338
+
339
+    /**
340
+     * Get the user we need to use
341
+     *
342
+     * Either the user is logged in, or we try to get it from the token
343
+     *
344
+     * @return string
345
+     * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
346
+     */
347
+    public function getCurrentUserId(): string {
348
+        if ($this->currentUserId !== null) {
349
+            return $this->currentUserId;
350
+        }
351
+
352
+        if (!$this->session->isLoggedIn()) {
353
+            return $this->getUserFromToken();
354
+        }
355
+
356
+        return $this->session->getUser()->getUID();
357
+    }
358
+
359
+    /**
360
+     * Get the user for the token
361
+     *
362
+     * @return string
363
+     * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
364
+     */
365
+    protected function getUserFromToken(): string {
366
+        $token = (string)$this->request->getParam('token', '');
367
+        if (strlen($token) !== 30) {
368
+            throw new \UnexpectedValueException('The token is invalid');
369
+        }
370
+
371
+        $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
372
+
373
+        if (count($users) !== 1) {
374
+            // No unique user found
375
+            throw new \UnexpectedValueException('The token is invalid');
376
+        }
377
+
378
+        // Token found login as that user
379
+        return array_shift($users);
380
+    }
381 381
 }
Please login to merge, or discard this patch.