Completed
Push — master ( a4086a...810fb7 )
by Björn
66:13 queued 48:44
created

Activity::getParsedParameters()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 21

Duplication

Lines 16
Ratio 57.14 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 5
nop 1
dl 16
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\ShareByMail;
23
24
use OCP\Activity\IEvent;
25
use OCP\Activity\IManager;
26
use OCP\Activity\IProvider;
27
use OCP\Contacts\IManager as IContactsManager;
28
use OCP\IL10N;
29
use OCP\IURLGenerator;
30
use OCP\IUser;
31
use OCP\IUserManager;
32
use OCP\L10N\IFactory;
33
34
class Activity implements IProvider {
35
36
	/** @var IFactory */
37
	protected $languageFactory;
38
39
	/** @var IL10N */
40
	protected $l;
41
42
	/** @var IURLGenerator */
43
	protected $url;
44
45
	/** @var IManager */
46
	protected $activityManager;
47
48
	/** @var IUserManager */
49
	protected $userManager;
50
	/** @var IContactsManager */
51
	protected $contactsManager;
52
53
	/** @var array */
54
	protected $displayNames = [];
55
56
	/** @var array */
57
	protected $contactNames = [];
58
59
	const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self';
60
	const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by';
61
	const SUBJECT_SHARED_EMAIL_PASSWORD_SEND = 'shared_with_email_password_send';
62
	const SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF = 'shared_with_email_password_send_self';
63
64
	/**
65
	 * @param IFactory $languageFactory
66
	 * @param IURLGenerator $url
67
	 * @param IManager $activityManager
68
	 * @param IUserManager $userManager
69
	 * @param IContactsManager $contactsManager
70
	 */
71 View Code Duplication
	public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
		$this->languageFactory = $languageFactory;
73
		$this->url = $url;
74
		$this->activityManager = $activityManager;
75
		$this->userManager = $userManager;
76
		$this->contactsManager = $contactsManager;
77
	}
78
79
	/**
80
	 * @param string $language
81
	 * @param IEvent $event
82
	 * @param IEvent|null $previousEvent
83
	 * @return IEvent
84
	 * @throws \InvalidArgumentException
85
	 * @since 11.0.0
86
	 */
87 View Code Duplication
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
		if ($event->getApp() !== 'sharebymail') {
89
			throw new \InvalidArgumentException();
90
		}
91
92
		$this->l = $this->languageFactory->get('sharebymail', $language);
93
94
		if ($this->activityManager->isFormattingFilteredObject()) {
95
			try {
96
				return $this->parseShortVersion($event);
97
			} catch (\InvalidArgumentException $e) {
98
				// Ignore and simply use the long version...
99
			}
100
		}
101
102
		return $this->parseLongVersion($event);
103
	}
104
105
	/**
106
	 * @param IEvent $event
107
	 * @return IEvent
108
	 * @throws \InvalidArgumentException
109
	 * @since 11.0.0
110
	 */
111
	public function parseShortVersion(IEvent $event) {
112
		$parsedParameters = $this->getParsedParameters($event);
113
114
		if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
115
			$event->setParsedSubject($this->l->t('Shared with %1$s', [
116
					$parsedParameters['email']['name'],
117
				]))
118
				->setRichSubject($this->l->t('Shared with {email}'), [
119
					'email' => $parsedParameters['email'],
120
				])
121
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
122
		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
123
			$event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [
124
				$parsedParameters['email']['name'],
125
				$parsedParameters['actor']['name'],
126
			]))
127
				->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
128
					'email' => $parsedParameters['email'],
129
					'actor' => $parsedParameters['actor'],
130
				])
131
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
132
		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
133
			$event->setParsedSubject($this->l->t('Password for mail share send to %1$s', [
134
				$parsedParameters['email']['name']
135
			]))
136
				->setRichSubject($this->l->t('Password for mail share send to {email}'), [
137
					'email' => $parsedParameters['email']
138
				])
139
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
140 View Code Duplication
		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
141
			$event->setParsedSubject($this->l->t('Password for mail share send to you'))
142
				->setRichSubject($this->l->t('Password for mail share send to you'))
143
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
144
		} else {
145
			throw new \InvalidArgumentException();
146
		}
147
148
		return $event;
149
	}
150
151
	/**
152
	 * @param IEvent $event
153
	 * @return IEvent
154
	 * @throws \InvalidArgumentException
155
	 * @since 11.0.0
156
	 */
157
	public function parseLongVersion(IEvent $event) {
158
		$parsedParameters = $this->getParsedParameters($event);
159
160
		if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
161
			$event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [
162
					$parsedParameters['file']['path'],
163
					$parsedParameters['email']['name'],
164
				]))
165
				->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters)
166
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
167
		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
168
			$event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [
169
				$parsedParameters['file']['path'],
170
				$parsedParameters['email']['name'],
171
				$parsedParameters['actor']['name'],
172
			]))
173
				->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters)
174
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
175
		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
176
			$event->setParsedSubject($this->l->t('Password to access %1$s was send to %2s', [
177
				$parsedParameters['file']['path'],
178
				$parsedParameters['email']['name']
179
			]))
180
				->setRichSubject($this->l->t('Password to access {file} was send to {email}'), $parsedParameters)
181
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
182
		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
183
			$event->setParsedSubject(
184
				$this->l->t('Password to access %1$s was send to you',
185
					[$parsedParameters['file']['path']]))
186
				->setRichSubject($this->l->t('Password to access {file} was send to you'), $parsedParameters)
187
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
188
189
		} else {
190
			throw new \InvalidArgumentException();
191
		}
192
193
		return $event;
194
	}
195
196
	protected function getParsedParameters(IEvent $event) {
197
		$subject = $event->getSubject();
198
		$parameters = $event->getSubjectParameters();
199
200
		switch ($subject) {
201 View Code Duplication
			case self::SUBJECT_SHARED_EMAIL_SELF:
202
				return [
203
					'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
204
					'email' => $this->generateEmailParameter($parameters[1]),
205
				];
206 View Code Duplication
			case self::SUBJECT_SHARED_EMAIL_BY:
207
				return [
208
					'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
209
					'email' => $this->generateEmailParameter($parameters[1]),
210
					'actor' => $this->generateUserParameter($parameters[2]),
211
				];
212 View Code Duplication
			case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND:
213
				return [
214
					'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
215
					'email' => $this->generateEmailParameter($parameters[1]),
216
				];
217
			case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF:
218
				return [
219
					'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
220
				];
221
		}
222
		throw new \InvalidArgumentException();
223
	}
224
225
	/**
226
	 * @param int $id
227
	 * @param string $path
228
	 * @return array
229
	 */
230 View Code Duplication
	protected function generateFileParameter($id, $path) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
		return [
232
			'type' => 'file',
233
			'id' => $id,
234
			'name' => basename($path),
235
			'path' => trim($path, '/'),
236
			'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
237
		];
238
	}
239
240
	/**
241
	 * @param string $email
242
	 * @return array
243
	 */
244
	protected function generateEmailParameter($email) {
245
		if (!isset($this->contactNames[$email])) {
246
			$this->contactNames[$email] = $this->getContactName($email);
247
		}
248
249
		return [
250
			'type' => 'email',
251
			'id' => $email,
252
			'name' => $this->contactNames[$email],
253
		];
254
	}
255
256
	/**
257
	 * @param string $uid
258
	 * @return array
259
	 */
260 View Code Duplication
	protected function generateUserParameter($uid) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
		if (!isset($this->displayNames[$uid])) {
262
			$this->displayNames[$uid] = $this->getDisplayName($uid);
263
		}
264
265
		return [
266
			'type' => 'user',
267
			'id' => $uid,
268
			'name' => $this->displayNames[$uid],
269
		];
270
	}
271
272
	/**
273
	 * @param string $email
274
	 * @return string
275
	 */
276
	protected function getContactName($email) {
277
		$addressBookContacts = $this->contactsManager->search($email, ['EMAIL']);
278
279
		foreach ($addressBookContacts as $contact) {
280
			if (isset($contact['isLocalSystemBook'])) {
281
				continue;
282
			}
283
284
			if (in_array($email, $contact['EMAIL'])) {
285
				return $contact['FN'];
286
			}
287
		}
288
289
		return $email;
290
	}
291
292
	/**
293
	 * @param string $uid
294
	 * @return string
295
	 */
296
	protected function getDisplayName($uid) {
297
		$user = $this->userManager->get($uid);
298
		if ($user instanceof IUser) {
299
			return $user->getDisplayName();
300
		} else {
301
			return $uid;
302
		}
303
	}
304
}
305