Completed
Push — master ( 9de15f...4a54fd )
by Maxence
02:21
created

BaseProvider::generateParsedSubject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
/**
4
 * Circles - Bring cloud-users closer together.
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Activity;
29
30
use OCA\Circles\Api\v1\Circles;
31
use OCA\Circles\Model\Circle;
32
use OCA\Circles\Model\FederatedLink;
33
use OCA\Circles\Model\Member;
34
use OCA\Circles\Service\MiscService;
35
use OCP\Activity\IEvent;
36
use OCP\Activity\IManager;
37
use OCP\IL10N;
38
use OCP\IURLGenerator;
39
40
class BaseProvider {
41
42
43
	/** @var MiscService */
44
	protected $miscService;
45
46
	/** @var IL10N */
47
	protected $l10n;
48
49
	/** @var IURLGenerator */
50
	protected $url;
51
52
	/** @var IManager */
53
	protected $activityManager;
54
55
	public function __construct(
56
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
57
	) {
58
		$this->url = $url;
59
		$this->activityManager = $activityManager;
60
		$this->l10n = $l10n;
61
		$this->miscService = $miscService;
62
	}
63
64
65
	/**
66
	 * general function to generate Circle event.
67
	 *
68
	 * @param IEvent $event
69
	 * @param Circle $circle
70
	 * @param FederatedLink $remote
71
	 * @param string $ownEvent
72
	 * @param string $othersEvent
73
	 *
74
	 * @return IEvent
75
	 */
76
	protected function parseCircleEvent(
77
		IEvent &$event, Circle $circle, $remote, $ownEvent, $othersEvent
78
	) {
79
		$data = [
80
			'author' => $this->generateViewerParameter($circle),
81
			'circle' => $this->generateCircleParameter($circle),
82
			'remote' => ($remote === null) ? '' : $this->generateRemoteCircleParameter($remote)
83
		];
84
85
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
86
			return $event->setRichSubject($ownEvent, $data);
87
		}
88
89
		return $event->setRichSubject($othersEvent, $data);
90
	}
91
92
93
	/**
94
	 * general function to generate Member event.
95
	 *
96
	 * @param Circle $circle
97
	 * @param $member
98
	 * @param IEvent $event
99
	 * @param $ownEvent
100
	 * @param $othersEvent
101
	 *
102
	 * @return IEvent
103
	 */
104
	protected function parseMemberEvent(
105
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
106
	) {
107
		$data = [
108
			'circle' => $this->generateCircleParameter($circle),
109
			'member' => $this->generateUserParameter($member)
110
		];
111
112
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
113
		) {
114
			return $event->setRichSubject($ownEvent, $data);
115
		}
116
117
		return $event->setRichSubject($othersEvent, $data);
118
	}
119
120
121
	/**
122
	 * general function to generate Link event.
123
	 *
124
	 * @param Circle $circle
125
	 * @param FederatedLink $remote
126
	 * @param IEvent $event
127
	 * @param string $line
128
	 *
129
	 * @return IEvent
130
	 */
131
	protected function parseLinkEvent(IEvent &$event, Circle $circle, FederatedLink $remote, $line
132
	) {
133
		$data = [
134
			'circle' => $this->generateCircleParameter($circle),
135
			'remote' => $this->generateRemoteCircleParameter($remote)
136
		];
137
138
		return $event->setRichSubject($line, $data);
139
	}
140
141
142
	/**
143
	 * general function to generate Circle+Member event.
144
	 *
145
	 * @param Circle $circle
146
	 * @param Member $member
147
	 * @param IEvent $event
148
	 * @param string $ownEvent
149
	 * @param string $othersEvent
150
	 *
151
	 * @return IEvent
152
	 */
153
	protected function parseCircleMemberEvent(
154
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
155
	) {
156
		$data = [
157
			'author' => $this->generateViewerParameter($circle),
158
			'circle' => $this->generateCircleParameter($circle),
159
			'member' => $this->generateUserParameter($member),
160
			'group'  => $this->generateGroupParameter($member),
161
		];
162
163
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
164
			return $event->setRichSubject($ownEvent, $data);
165
		}
166
167
		return $event->setRichSubject($othersEvent, $data);
168
	}
169
170
171
	/**
172
	 * general function to generate Circle+Member advanced event.
173
	 *
174
	 * @param Circle $circle
175
	 * @param Member $member
176
	 * @param IEvent $event
177
	 *\
178
	 * @param $ownEvent
179
	 * @param $targetEvent
180
	 * @param $othersEvent
181
	 *
182
	 * @return IEvent
183
	 */
184
	protected function parseCircleMemberAdvancedEvent(
185
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $targetEvent, $othersEvent
186
	) {
187
		$data = [
188
			'author' => $this->generateViewerParameter($circle),
189
			'circle' => $this->generateCircleParameter($circle),
190
			'member' => $this->generateUserParameter($member)
191
		];
192
193
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
194
			return $event->setRichSubject($ownEvent, $data);
195
		}
196
197
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
198
			return $event->setRichSubject($targetEvent, $data);
199
		}
200
201
		return $event->setRichSubject($othersEvent, $data);
202
	}
203
204
205
	/**
206
	 * @param IEvent $event
207
	 */
208
	protected function generateParsedSubject(IEvent &$event) {
209
		$subject = $event->getRichSubject();
210
		$params = $event->getRichSubjectParameters();
211
		$ak = array_keys($params);
212
		foreach ($ak as $k) {
213
			if (is_array($params[$k])) {
214
				$subject = str_replace('{' . $k . '}', $params[$k]['parsed'], $subject);
215
			}
216
		}
217
218
		$event->setParsedSubject($subject);
219
	}
220
221
222
	/**
223
	 * @param Circle $circle
224
	 * @param string $userId
225
	 *
226
	 * @return bool
227
	 */
228
	protected function isViewerTheAuthor(Circle $circle, $userId) {
229
		if ($circle->getViewer() === null) {
230
			return false;
231
		}
232
233
		if ($circle->getViewer()
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $circle->getViewe...etUserId() === $userId;.
Loading history...
234
				   ->getUserId() === $userId) {
235
			return true;
236
		}
237
238
		return false;
239
	}
240
241
242
	/**
243
	 * @param Circle $circle
244
	 *
245
	 * @return string|array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
246
	 */
247
	protected function generateViewerParameter(Circle $circle) {
248
		if ($circle->getViewer() === null) {
249
			return '';
250
		}
251
252
		return $this->generateUserParameter($circle->getViewer());
253
	}
254
255
256
	/**
257
	 * @param Circle $circle
258
	 *
259
	 * @return array<string,string|integer>
260
	 */
261
	protected function generateCircleParameter(Circle $circle) {
262
		return [
263
			'type'   => 'circle',
264
			'id'     => $circle->getId(),
265
			'name'   => $circle->getName(),
266
			'parsed' => $circle->getName(),
267
			'link'   => Circles::generateLink($circle->getUniqueId())
268
		];
269
	}
270
271
272
	/**
273
	 * @param FederatedLink $link
274
	 *
275
	 * @return array<string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
276
	 */
277
	protected function generateRemoteCircleParameter(FederatedLink $link) {
278
		return [
279
			'type'   => 'circle',
280
			'id'     => $link->getUniqueId(),
281
			'name'   => $link->getToken() . '@' . $link->getAddress(),
282
			'parsed' => $link->getToken() . '@' . $link->getAddress()
283
		];
284
	}
285
286
287
	/**
288
	 * @param Member $member
289
	 *
290
	 * @return array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
291
	 */
292
	protected function generateUserParameter(Member $member) {
293
		return [
294
			'type'   => 'user',
295
			'id'     => $member->getUserId(),
296
			'name'   => $this->miscService->getDisplayName($member->getUserId(), true),
297
			'parsed' => $this->miscService->getDisplayName($member->getUserId(), true)
298
		];
299
	}
300
301
302
	/**
303
	 * @param Member $group
304
	 *
305
	 * @return array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
306
	 */
307
	protected function generateGroupParameter($group) {
308
		return [
309
			'type'   => 'group',
310
			'id'     => $group->getUserId(),
311
			'name'   => $group->getUserId(),
312
			'parsed' => $group->getUserId()
313
		];
314
	}
315
316
}