|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2020 Julius Härtl <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Julius Härtl <[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\Deck\Service; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
use OCA\Deck\AppInfo\Application; |
|
28
|
|
|
use OCA\Deck\BadRequestException; |
|
29
|
|
|
use OCA\Deck\NoPermissionException; |
|
30
|
|
|
use OCA\Deck\NotFoundException; |
|
31
|
|
|
use OCA\Deck\StatusException; |
|
32
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
33
|
|
|
use OCP\Comments\IComment; |
|
34
|
|
|
use OCP\Comments\ICommentsManager; |
|
35
|
|
|
use OCP\Comments\MessageTooLongException; |
|
36
|
|
|
use OCP\Comments\NotFoundException as CommentNotFoundException; |
|
37
|
|
|
use OCP\ILogger; |
|
38
|
|
|
use OCP\IUserManager; |
|
39
|
|
|
use OutOfBoundsException; |
|
40
|
|
|
use Sabre\DAV\Exception\Forbidden; |
|
41
|
|
|
use function is_numeric; |
|
42
|
|
|
|
|
43
|
|
|
class CommentService { |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ICommentsManager |
|
47
|
|
|
*/ |
|
48
|
|
|
private $commentsManager; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var IUserManager |
|
51
|
|
|
*/ |
|
52
|
|
|
private $userManager; |
|
53
|
|
|
/** @var ILogger */ |
|
54
|
|
|
private $logger; |
|
55
|
|
|
private $userId; |
|
56
|
|
|
|
|
57
|
|
|
public function __construct(ICommentsManager $commentsManager, IUserManager $userManager, ILogger $logger, $userId) { |
|
58
|
|
|
$this->commentsManager = $commentsManager; |
|
59
|
|
|
$this->userManager = $userManager; |
|
60
|
|
|
$this->logger = $logger; |
|
61
|
|
|
$this->userId = $userId; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function list(string $cardId, int $limit = 20, int $offset = 0): DataResponse { |
|
65
|
|
|
if (!is_numeric($cardId)) { |
|
66
|
|
|
throw new BadRequestException('A valid card id must be provided'); |
|
67
|
|
|
} |
|
68
|
|
|
$comments = $this->commentsManager->getForObject(Application::COMMENT_ENTITY_TYPE, $cardId, $limit, $offset); |
|
69
|
|
|
$result = []; |
|
70
|
|
|
foreach ($comments as $comment) { |
|
71
|
|
|
$formattedComment = $this->formatComment($comment); |
|
72
|
|
|
try { |
|
73
|
|
|
if ($comment->getParentId() !== '0' && $replyTo = $this->commentsManager->get($comment->getParentId())) { |
|
74
|
|
|
$formattedComment['replyTo'] = $this->formatComment($replyTo); |
|
75
|
|
|
} |
|
76
|
|
|
} catch (CommentNotFoundException $e) { |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
$result[] = $formattedComment; |
|
79
|
|
|
} |
|
80
|
|
|
return new DataResponse($result); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $cardId |
|
85
|
|
|
* @param string $message |
|
86
|
|
|
* @param string $replyTo |
|
87
|
|
|
* @return DataResponse |
|
88
|
|
|
* @throws BadRequestException |
|
89
|
|
|
* @throws NotFoundException |
|
90
|
|
|
*/ |
|
91
|
|
|
public function create(string $cardId, string $message, string $replyTo = '0'): DataResponse { |
|
92
|
|
|
if (!is_numeric($cardId)) { |
|
93
|
|
|
throw new BadRequestException('A valid card id must be provided'); |
|
94
|
|
|
} |
|
95
|
|
|
try { |
|
96
|
|
|
$comment = $this->commentsManager->create('users', $this->userId, Application::COMMENT_ENTITY_TYPE, $cardId); |
|
97
|
|
|
$comment->setMessage($message); |
|
98
|
|
|
$comment->setVerb('comment'); |
|
99
|
|
|
$comment->setParentId($replyTo); |
|
100
|
|
|
$this->commentsManager->save($comment); |
|
101
|
|
|
return new DataResponse($this->formatComment($comment)); |
|
102
|
|
|
} catch (\InvalidArgumentException $e) { |
|
103
|
|
|
throw new BadRequestException('Invalid input values'); |
|
104
|
|
|
} catch (MessageTooLongException $e) { |
|
105
|
|
|
$msg = 'Message exceeds allowed character limit of '; |
|
106
|
|
|
throw new BadRequestException($msg . IComment::MAX_MESSAGE_LENGTH); |
|
107
|
|
|
} catch (CommentNotFoundException $e) { |
|
108
|
|
|
throw new NotFoundException('Could not create comment.'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function update(string $cardId, string $commentId, string $message): DataResponse { |
|
113
|
|
|
if (!is_numeric($cardId)) { |
|
114
|
|
|
throw new BadRequestException('A valid card id must be provided'); |
|
115
|
|
|
} |
|
116
|
|
|
if (!is_numeric($commentId)) { |
|
117
|
|
|
throw new BadRequestException('A valid comment id must be provided'); |
|
118
|
|
|
} |
|
119
|
|
|
try { |
|
120
|
|
|
$comment = $this->commentsManager->get($commentId); |
|
121
|
|
|
} catch (CommentNotFoundException $e) { |
|
122
|
|
|
throw new NotFoundException('No comment found.'); |
|
123
|
|
|
} |
|
124
|
|
|
if ($comment->getActorType() !== 'users' || $comment->getActorId() !== $this->userId) { |
|
125
|
|
|
throw new NoPermissionException('Only authors are allowed to edit their comment.'); |
|
126
|
|
|
} |
|
127
|
|
|
$comment->setMessage($message); |
|
128
|
|
|
$this->commentsManager->save($comment); |
|
129
|
|
|
return new DataResponse($this->formatComment($comment)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function delete(string $cardId, string $commentId): DataResponse { |
|
133
|
|
|
if (!is_numeric($cardId)) { |
|
134
|
|
|
throw new BadRequestException('A valid card id must be provided'); |
|
135
|
|
|
} |
|
136
|
|
|
if (!is_numeric($commentId)) { |
|
137
|
|
|
throw new BadRequestException('A valid comment id must be provided'); |
|
138
|
|
|
} |
|
139
|
|
|
$this->commentsManager->delete($commentId); |
|
140
|
|
|
return new DataResponse([]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function formatComment(IComment $comment): array { |
|
144
|
|
|
$user = $this->userManager->get($comment->getActorId()); |
|
145
|
|
|
$actorDisplayName = $user !== null ? $user->getDisplayName() : $comment->getActorId(); |
|
146
|
|
|
|
|
147
|
|
|
return [ |
|
148
|
|
|
'id' => $comment->getId(), |
|
149
|
|
|
'objectId' => $comment->getObjectId(), |
|
150
|
|
|
'message' => $comment->getMessage(), |
|
151
|
|
|
'actorId' => $comment->getActorId(), |
|
152
|
|
|
'actorType' => $comment->getActorType(), |
|
153
|
|
|
'actorDisplayName' => $actorDisplayName, |
|
154
|
|
|
'mentions' => array_map(function($mention) { |
|
155
|
|
|
try { |
|
156
|
|
|
$displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); |
|
157
|
|
|
} catch (OutOfBoundsException $e) { |
|
158
|
|
|
$this->logger->logException($e); |
|
|
|
|
|
|
159
|
|
|
// No displayname, upon client's discretion what to display. |
|
160
|
|
|
$displayName = ''; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return [ |
|
164
|
|
|
'mentionId' => $mention['id'], |
|
165
|
|
|
'mentionType' => $mention['type'], |
|
166
|
|
|
'mentionDisplayName' => $displayName |
|
167
|
|
|
]; |
|
168
|
|
|
}, $comment->getMentions()), |
|
169
|
|
|
]; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|