1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Controller;
use OCA\Talk\Chat\ReactionManager;
use OCA\Talk\Exceptions\ReactionAlreadyExistsException;
use OCA\Talk\Exceptions\ReactionNotSupportedException;
use OCA\Talk\Exceptions\ReactionOutOfContextException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Comments\NotFoundException;
use OCP\IRequest;
class ReactionController extends AEnvironmentAwareController {
/** @var ReactionManager */
private $reactionManager;
public function __construct(string $appName,
IRequest $request,
ReactionManager $reactionManager) {
parent::__construct($appName, $request);
$this->reactionManager = $reactionManager;
}
/**
* @NoAdminRequired
* @RequireParticipant
* @RequireReadWriteConversation
* @RequireModeratorOrNoLobby
*
* @param int $messageId for reaction
* @param string $reaction the reaction emoji
* @return DataResponse
*/
public function react(int $messageId, string $reaction): DataResponse {
try {
$chat = $this->getRoom();
$participant = $this->getParticipant();
$parentMessage = $this->reactionManager->getCommentToReact($chat, (string) $messageId);
$this->reactionManager->addReactionMessage($chat, $participant, $parentMessage, $reaction);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (ReactionAlreadyExistsException $e) {
return new DataResponse([], Http::STATUS_OK);
} catch (ReactionNotSupportedException | ReactionOutOfContextException | \Exception $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
return new DataResponse([], Http::STATUS_CREATED);
}
/**
* @NoAdminRequired
* @RequireParticipant
* @RequireReadWriteConversation
* @RequireModeratorOrNoLobby
*
* @param int $messageId for reaction
* @param string $reaction the reaction emoji
* @return DataResponse
*/
public function delete(int $messageId, string $reaction): DataResponse {
$participant = $this->getParticipant();
try {
// Verify that messageId is part of the room
$this->reactionManager->getCommentToReact($this->getRoom(), (string) $messageId);
} catch (ReactionNotSupportedException | ReactionOutOfContextException | NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
try {
$this->reactionManager->deleteReactionMessage(
$participant,
$messageId,
$reaction
);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
return new DataResponse([], Http::STATUS_OK);
}
/**
* @NoAdminRequired
* @RequireParticipant
* @RequireReadWriteConversation
* @RequireModeratorOrNoLobby
*
* @param int $messageId for reaction
* @param string|null $reaction the reaction emoji
* @return DataResponse
*/
public function getReactions(int $messageId, ?string $reaction): DataResponse {
try {
// Verify that messageId is part of the room
$this->reactionManager->getCommentToReact($this->getRoom(), (string) $messageId);
} catch (ReactionNotSupportedException | ReactionOutOfContextException | NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$reactions = $this->reactionManager->retrieveReactionMessages($this->getRoom(), $this->getParticipant(), $messageId, $reaction);
return new DataResponse($reactions, Http::STATUS_OK);
}
}
|