1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Arthur Schiwon <[email protected]> |
6
|
|
|
* @author Joas Schilling <[email protected]> |
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license AGPL-3.0 |
10
|
|
|
* |
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
13
|
|
|
* as published by the Free Software Foundation. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\Comments\Notification; |
26
|
|
|
|
27
|
|
|
use OCP\Comments\ICommentsManager; |
28
|
|
|
use OCP\Comments\NotFoundException; |
29
|
|
|
use OCP\Files\IRootFolder; |
30
|
|
|
use OCP\IURLGenerator; |
31
|
|
|
use OCP\IUserManager; |
32
|
|
|
use OCP\L10N\IFactory; |
33
|
|
|
use OCP\Notification\INotification; |
34
|
|
|
use OCP\Notification\INotifier; |
35
|
|
|
|
36
|
|
|
class Notifier implements INotifier { |
37
|
|
|
|
38
|
|
|
/** @var IFactory */ |
39
|
|
|
protected $l10nFactory; |
40
|
|
|
|
41
|
|
|
/** @var IRootFolder */ |
42
|
|
|
protected $rootFolder; |
43
|
|
|
|
44
|
|
|
/** @var ICommentsManager */ |
45
|
|
|
protected $commentsManager; |
46
|
|
|
|
47
|
|
|
/** @var IURLGenerator */ |
48
|
|
|
protected $url; |
49
|
|
|
|
50
|
|
|
/** @var IUserManager */ |
51
|
|
|
protected $userManager; |
52
|
|
|
|
53
|
|
|
public function __construct( |
54
|
|
|
IFactory $l10nFactory, |
55
|
|
|
IRootFolder $rootFolder, |
56
|
|
|
ICommentsManager $commentsManager, |
57
|
|
|
IURLGenerator $url, |
58
|
|
|
IUserManager $userManager |
59
|
|
|
) { |
60
|
|
|
$this->l10nFactory = $l10nFactory; |
61
|
|
|
$this->rootFolder = $rootFolder; |
62
|
|
|
$this->commentsManager = $commentsManager; |
63
|
|
|
$this->url = $url; |
64
|
|
|
$this->userManager = $userManager; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param INotification $notification |
69
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification |
70
|
|
|
* @return INotification |
71
|
|
|
* @throws \InvalidArgumentException When the notification was not prepared by a notifier |
72
|
|
|
*/ |
73
|
|
|
public function prepare(INotification $notification, $languageCode) { |
74
|
|
|
if($notification->getApp() !== 'comments') { |
75
|
|
|
throw new \InvalidArgumentException(); |
76
|
|
|
} |
77
|
|
|
try { |
78
|
|
|
$comment = $this->commentsManager->get($notification->getObjectId()); |
79
|
|
|
} catch(NotFoundException $e) { |
80
|
|
|
// needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
81
|
|
|
throw new \InvalidArgumentException('Comment not found', 0, $e); |
82
|
|
|
} |
83
|
|
|
$l = $this->l10nFactory->get('comments', $languageCode); |
84
|
|
|
$displayName = $comment->getActorId(); |
85
|
|
|
$isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; |
86
|
|
|
if($comment->getActorType() === 'users') { |
87
|
|
|
$commenter = $this->userManager->get($comment->getActorId()); |
88
|
|
|
if(!is_null($commenter)) { |
89
|
|
|
$displayName = $commenter->getDisplayName(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
switch($notification->getSubject()) { |
94
|
|
|
case 'mention': |
95
|
|
|
$parameters = $notification->getSubjectParameters(); |
96
|
|
|
if($parameters[0] !== 'files') { |
97
|
|
|
throw new \InvalidArgumentException('Unsupported comment object'); |
98
|
|
|
} |
99
|
|
|
$userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
100
|
|
|
$nodes = $userFolder->getById((int)$parameters[1]); |
101
|
|
|
if(empty($nodes)) { |
102
|
|
|
throw new \InvalidArgumentException('Cannot resolve file ID to node instance'); |
103
|
|
|
} |
104
|
|
|
$node = $nodes[0]; |
105
|
|
|
|
106
|
|
|
if ($isDeletedActor) { |
107
|
|
|
$notification->setParsedSubject($l->t( |
108
|
|
|
'You were mentioned on “%s”, in a comment by a user that has since been deleted', |
109
|
|
|
[$node->getName()] |
110
|
|
|
)) |
111
|
|
|
->setRichSubject( |
112
|
|
|
$l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'), |
113
|
|
|
[ |
114
|
|
|
'file' => [ |
115
|
|
|
'type' => 'file', |
116
|
|
|
'id' => $comment->getObjectId(), |
117
|
|
|
'name' => $node->getName(), |
118
|
|
|
'path' => $node->getPath(), |
119
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
120
|
|
|
], |
121
|
|
|
] |
122
|
|
|
); |
123
|
|
|
} else { |
124
|
|
|
$notification->setParsedSubject($l->t( |
125
|
|
|
'%1$s mentioned you in a comment on “%2$s”', |
126
|
|
|
[$displayName, $node->getName()] |
127
|
|
|
)) |
128
|
|
|
->setRichSubject( |
129
|
|
|
$l->t('{user} mentioned you in a comment on “{file}”'), |
130
|
|
|
[ |
131
|
|
|
'user' => [ |
132
|
|
|
'type' => 'user', |
133
|
|
|
'id' => $comment->getActorId(), |
134
|
|
|
'name' => $displayName, |
135
|
|
|
], |
136
|
|
|
'file' => [ |
137
|
|
|
'type' => 'file', |
138
|
|
|
'id' => $comment->getObjectId(), |
139
|
|
|
'name' => $node->getName(), |
140
|
|
|
'path' => $node->getPath(), |
141
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
142
|
|
|
], |
143
|
|
|
] |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) |
147
|
|
|
->setLink($this->url->linkToRouteAbsolute( |
148
|
|
|
'comments.Notifications.view', |
149
|
|
|
['id' => $comment->getId()]) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
return $notification; |
153
|
|
|
break; |
|
|
|
|
154
|
|
|
|
155
|
|
|
default: |
156
|
|
|
throw new \InvalidArgumentException('Invalid subject'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.