Passed
Push — master ( 5cdc85...37718d )
by Morris
38:53 queued 21:57
created

Provider::parse()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 8
nop 3
dl 0
loc 26
rs 9.1111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Comments\Activity;
25
26
use OCP\Activity\IEvent;
27
use OCP\Activity\IManager;
28
use OCP\Activity\IProvider;
29
use OCP\Comments\ICommentsManager;
30
use OCP\Comments\NotFoundException;
31
use OCP\IL10N;
32
use OCP\IURLGenerator;
33
use OCP\IUser;
34
use OCP\IUserManager;
35
use OCP\L10N\IFactory;
36
37
class Provider implements IProvider {
38
39
	/** @var IFactory */
40
	protected $languageFactory;
41
42
	/** @var IL10N */
43
	protected $l;
44
45
	/** @var IURLGenerator */
46
	protected $url;
47
48
	/** @var ICommentsManager */
49
	protected $commentsManager;
50
51
	/** @var IUserManager */
52
	protected $userManager;
53
54
	/** @var IManager */
55
	protected $activityManager;
56
57
	/** @var string[] */
58
	protected $displayNames = [];
59
60
	/**
61
	 * @param IFactory $languageFactory
62
	 * @param IURLGenerator $url
63
	 * @param ICommentsManager $commentsManager
64
	 * @param IUserManager $userManager
65
	 * @param IManager $activityManager
66
	 */
67
	public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) {
68
		$this->languageFactory = $languageFactory;
69
		$this->url = $url;
70
		$this->commentsManager = $commentsManager;
71
		$this->userManager = $userManager;
72
		$this->activityManager = $activityManager;
73
	}
74
75
	/**
76
	 * @param string $language
77
	 * @param IEvent $event
78
	 * @param IEvent|null $previousEvent
79
	 * @return IEvent
80
	 * @throws \InvalidArgumentException
81
	 * @since 11.0.0
82
	 */
83
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
84
		if ($event->getApp() !== 'comments') {
85
			throw new \InvalidArgumentException();
86
		}
87
88
		$this->l = $this->languageFactory->get('comments', $language);
89
90
		if ($event->getSubject() === 'add_comment_subject') {
91
			$this->parseMessage($event);
92
			if ($this->activityManager->getRequirePNG()) {
93
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
94
			} else {
95
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
96
			}
97
98
			if ($this->activityManager->isFormattingFilteredObject()) {
99
				try {
100
					return $this->parseShortVersion($event);
101
				} catch (\InvalidArgumentException $e) {
102
					// Ignore and simply use the long version...
103
				}
104
			}
105
106
			return $this->parseLongVersion($event);
107
		} else {
108
			throw new \InvalidArgumentException();
109
		}
110
	}
111
112
	/**
113
	 * @param IEvent $event
114
	 * @return IEvent
115
	 * @throws \InvalidArgumentException
116
	 */
117
	protected function parseShortVersion(IEvent $event) {
118
		$subjectParameters = $this->getSubjectParameters($event);
119
120
		if ($event->getSubject() === 'add_comment_subject') {
121
			if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
122
				$event->setParsedSubject($this->l->t('You commented'))
123
					->setRichSubject($this->l->t('You commented'), []);
124
			} else {
125
				$author = $this->generateUserParameter($subjectParameters['actor']);
126
				$event->setParsedSubject($this->l->t('%1$s commented', [$author['name']]))
127
					->setRichSubject($this->l->t('{author} commented'), [
128
						'author' => $author,
129
					]);
130
			}
131
		} else {
132
			throw new \InvalidArgumentException();
133
		}
134
135
		return $event;
136
	}
137
138
	/**
139
	 * @param IEvent $event
140
	 * @return IEvent
141
	 * @throws \InvalidArgumentException
142
	 */
143
	protected function parseLongVersion(IEvent $event) {
144
		$subjectParameters = $this->getSubjectParameters($event);
145
146
		if ($event->getSubject() === 'add_comment_subject') {
147
			if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
148
				$event->setParsedSubject($this->l->t('You commented on %1$s', [
149
						$subjectParameters['filePath'],
150
					]))
151
					->setRichSubject($this->l->t('You commented on {file}'), [
152
						'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
153
					]);
154
			} else {
155
				$author = $this->generateUserParameter($subjectParameters['actor']);
156
				$event->setParsedSubject($this->l->t('%1$s commented on %2$s', [
157
						$author['name'],
158
						$subjectParameters['filePath'],
159
					]))
160
					->setRichSubject($this->l->t('{author} commented on {file}'), [
161
						'author' => $author,
162
						'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
163
					]);
164
			}
165
		} else {
166
			throw new \InvalidArgumentException();
167
		}
168
169
		return $event;
170
	}
171
172
	protected function getSubjectParameters(IEvent $event) {
173
		$subjectParameters = $event->getSubjectParameters();
174
		if (isset($subjectParameters['fileId'])) {
175
			return $subjectParameters;
176
		}
177
178
		// Fix subjects from 12.0.3 and older
179
		//
180
		// Do NOT Remove unless necessary
181
		// Removing this will break parsing of activities that were created on
182
		// Nextcloud 12, so we should keep this as long as it's acceptable.
183
		// Otherwise if people upgrade over multiple releases in a short period,
184
		// they will get the dead entries in their stream.
185
		return [
186
			'actor' => $subjectParameters[0],
187
			'fileId' => (int) $event->getObjectId(),
188
			'filePath' => trim($subjectParameters[1], '/'),
189
		];
190
	}
191
192
	/**
193
	 * @param IEvent $event
194
	 */
195
	protected function parseMessage(IEvent $event) {
196
		$messageParameters = $event->getMessageParameters();
197
		if (empty($messageParameters)) {
198
			// Email
199
			return;
200
		}
201
202
		$commentId = isset($messageParameters['commentId']) ? $messageParameters['commentId'] : $messageParameters[0];
203
204
		try {
205
			$comment = $this->commentsManager->get((string) $commentId);
206
			$message = $comment->getMessage();
207
			$message = str_replace("\n", '<br />', str_replace(['<', '>'], ['&lt;', '&gt;'], $message));
208
209
			$mentionCount = 1;
210
			$mentions = [];
211
			foreach ($comment->getMentions() as $mention) {
212
				if ($mention['type'] !== 'user') {
213
					continue;
214
				}
215
216
				$message = preg_replace(
217
					'/(^|\s)(' . '@' . $mention['id'] . ')(\b)/',
218
					//'${1}' . $this->regexSafeUser($mention['id'], $displayName) . '${3}',
219
					'${1}' . '{mention' . $mentionCount . '}' . '${3}',
220
					$message
221
				);
222
				$mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
223
				$mentionCount++;
224
			}
225
226
			$event->setParsedMessage($comment->getMessage())
227
				->setRichMessage($message, $mentions);
228
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
229
		}
230
	}
231
232
	/**
233
	 * @param int $id
234
	 * @param string $path
235
	 * @return array
236
	 */
237
	protected function generateFileParameter($id, $path) {
238
		return [
239
			'type' => 'file',
240
			'id' => $id,
241
			'name' => basename($path),
242
			'path' => $path,
243
			'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
244
		];
245
	}
246
247
	/**
248
	 * @param string $uid
249
	 * @return array
250
	 */
251
	protected function generateUserParameter($uid) {
252
		if (!isset($this->displayNames[$uid])) {
253
			$this->displayNames[$uid] = $this->getDisplayName($uid);
254
		}
255
256
		return [
257
			'type' => 'user',
258
			'id' => $uid,
259
			'name' => $this->displayNames[$uid],
260
		];
261
	}
262
263
	/**
264
	 * @param string $uid
265
	 * @return string
266
	 */
267
	protected function getDisplayName($uid) {
268
		$user = $this->userManager->get($uid);
269
		if ($user instanceof IUser) {
270
			return $user->getDisplayName();
271
		} else {
272
			return $uid;
273
		}
274
	}
275
}
276