Skip to content

Commit 280b3ce

Browse files
committed
Refactor to be possible idnetify who can mention all
#9464 (comment) Signed-off-by: Vitor Mattos <[email protected]>
1 parent f49037c commit 280b3ce

File tree

11 files changed

+60
-61
lines changed

11 files changed

+60
-61
lines changed

appinfo/routes/routesRoomController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@
109109
/** @see \OCA\Talk\Controller\RoomController::setMessageExpiration() */
110110
['name' => 'Room#setMessageExpiration', 'url' => '/api/{apiVersion}/room/{token}/message-expiration', 'verb' => 'POST', 'requirements' => $requirementsWithToken],
111111
/** @see \OCA\Talk\Controller\RoomController::setCanMentionEveryone() */
112-
['name' => 'Room#setCanMentionEveryone', 'url' => '/api/{apiVersion}/room/{token}/can-mention-everyone', 'verb' => 'POST', 'requirements' => $requirementsWithToken],
113-
/** @see \OCA\Talk\Controller\RoomController::setCanNotMentionEveryone() */
114-
['name' => 'Room#setCanNotMentionEveryone', 'url' => '/api/{apiVersion}/room/{token}/can-mention-everyone', 'verb' => 'DELETE', 'requirements' => $requirementsWithToken],
112+
['name' => 'Room#setCanMentionEveryone', 'url' => '/api/{apiVersion}/room/{token}/can-mention-everyone', 'verb' => 'PUT', 'requirements' => $requirementsWithToken],
115113
],
116114
];

docs/conversation.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -427,23 +427,15 @@ Get all (for moderators and in case of "free selection") or the assigned breakou
427427
+ `403 Forbidden` When the current user is not a moderator/owner or the conversation is not a public conversation
428428
+ `404 Not Found` When the conversation could not be found for the participant
429429

430-
## Allow non moderators to mention everyone using `@all`
430+
## change the permission to mention everyone using `@all`
431431
* Required capability: `can-mention-everyone`
432-
* Method: `POST`
432+
* Method: `PUT`
433433
* Endpoint: `/room/{token}/can-mention-everyone`
434+
* Data:
434435

435-
* Response:
436-
- Status code:
437-
+ `200 OK`
438-
+ `400 Bad Request` Error: `room`: When the conversation is a one-to-one conversation
439-
+ `403 Forbidden` When the current user is not a moderator/owner or the conversation is not a one-to-one conversation
440-
+ `404 Not Found` When the conversation could not be found for the participant
441-
442-
443-
## Disallow non moderators to mention everyone using `@all`
444-
* Required capability: `can-mention-everyone`
445-
* Method: `DELETE`
446-
* Endpoint: `/room/{token}/can-mention-everyone`
436+
| field | type | Description |
437+
|--------------|------|---------------------------------------------------------------------------|
438+
| `permission` | int | See [Constants - Can mention everyone](constants.md#can-mention-everyone) |
447439

448440
* Response:
449441
- Status code:

lib/Chat/ChatManager.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -756,16 +756,20 @@ public function addConversationNotify(array $results, string $search, Room $room
756756
} else {
757757
$roomDisplayName = $room->getDisplayName('');
758758
}
759-
if ($room->getCanMentionEveryone() === Room::CAN_MENTION_EVERYONE
760-
|| $participant->hasModeratorPermissions(true)
761-
) {
762-
if ($search === '' || $this->searchIsPartOfConversationNameOrAtAll($search, $roomDisplayName)) {
763-
array_unshift($results, [
764-
'id' => 'all',
765-
'label' => $roomDisplayName,
766-
'source' => 'calls',
767-
]);
768-
}
759+
switch ($room->getCanMentionEveryone()) {
760+
case Room::CAN_MENTION_EVERYONE_ALL:
761+
break;
762+
case Room::CAN_MENTION_EVERYONE_MODERATORS:
763+
if (!$participant->hasModeratorPermissions(true)) {
764+
return $results;
765+
}
766+
}
767+
if ($search === '' || $this->searchIsPartOfConversationNameOrAtAll($search, $roomDisplayName)) {
768+
array_unshift($results, [
769+
'id' => 'all',
770+
'label' => $roomDisplayName,
771+
'source' => 'calls',
772+
]);
769773
}
770774
return $results;
771775
}

lib/Chat/Notifier.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,14 @@ private function addMentionAllToList(Room $chat, array $list, Participant $parti
196196
if (count($list) === count($usersToNotify)) {
197197
return $usersToNotify;
198198
}
199-
if (!$participant->hasModeratorPermissions(true) && !$chat->getCanMentionEveryone()) {
200-
return $usersToNotify;
199+
200+
switch ($chat->getCanMentionEveryone()) {
201+
case Room::CAN_MENTION_EVERYONE_ALL:
202+
break;
203+
case Room::CAN_MENTION_EVERYONE_MODERATORS:
204+
if (!$participant->hasModeratorPermissions(true)) {
205+
return $usersToNotify;
206+
}
201207
}
202208

203209
$attendees = $this->participantService->getActorsByType($chat, Attendee::ACTOR_USERS);

lib/Controller/RoomController.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,20 +1494,9 @@ public function setMessageExpiration(int $seconds): DataResponse {
14941494

14951495
#[NoAdminRequired]
14961496
#[RequireLoggedInModeratorParticipant]
1497-
public function setCanMentionEveryone(): DataResponse {
1497+
public function setCanMentionEveryone(int $permission = Room::CAN_MENTION_EVERYONE_ALL): DataResponse {
14981498
try {
1499-
$this->roomService->setCanMentionEveryone($this->room, Room::CAN_MENTION_EVERYONE);
1500-
} catch (\InvalidArgumentException $e) {
1501-
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
1502-
}
1503-
return new DataResponse();
1504-
}
1505-
1506-
#[NoAdminRequired]
1507-
#[RequireLoggedInModeratorParticipant]
1508-
public function setCanNotMentionEveryone(): DataResponse {
1509-
try {
1510-
$this->roomService->setCanMentionEveryone($this->room, Room::CAN_NOT_MENTION_EVERYONE);
1499+
$this->roomService->setCanMentionEveryone($this->room, $permission);
15111500
} catch (\InvalidArgumentException $e) {
15121501
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
15131502
}

lib/Room.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class Room {
101101
public const PARTICIPANT_REMOVED_ALL = 'remove_all';
102102
public const PARTICIPANT_LEFT = 'leave';
103103

104-
public const CAN_NOT_MENTION_EVERYONE = 0;
105-
public const CAN_MENTION_EVERYONE = 1;
104+
public const CAN_MENTION_EVERYONE_MODERATORS = 0;
105+
public const CAN_MENTION_EVERYONE_ALL = 1;
106106

107107
public const EVENT_AFTER_ROOM_CREATE = self::class . '::createdRoom';
108108
public const EVENT_BEFORE_ROOM_DELETE = self::class . '::preDeleteRoom';

lib/Service/RoomFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function formatRoomV4(
139139
'isCustomAvatar' => $this->avatarService->isCustomAvatar($room),
140140
'breakoutRoomMode' => BreakoutRoom::MODE_NOT_CONFIGURED,
141141
'breakoutRoomStatus' => BreakoutRoom::STATUS_STOPPED,
142-
'canMentionEveryone' => Room::CAN_MENTION_EVERYONE,
142+
'canMentionEveryone' => Room::CAN_MENTION_EVERYONE_ALL,
143143
];
144144

145145
$lastActivity = $room->getLastActivity();

lib/Service/RoomService.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,12 @@ public function deleteRoom(Room $room): void {
852852
}
853853
}
854854

855-
public function setCanMentionEveryone(Room $room, int $canMentionEveryone): void {
856-
if (!in_array($canMentionEveryone, [Room::CAN_NOT_MENTION_EVERYONE, Room::CAN_MENTION_EVERYONE])) {
855+
public function setCanMentionEveryone(Room $room, int $permission): void {
856+
$allowedValues = [
857+
Room::CAN_MENTION_EVERYONE_MODERATORS,
858+
Room::CAN_MENTION_EVERYONE_ALL
859+
];
860+
if (!in_array($permission, $allowedValues)) {
857861
throw new InvalidArgumentException('config');
858862
}
859863
if ($room->getType() !== Room::TYPE_GROUP
@@ -863,20 +867,20 @@ public function setCanMentionEveryone(Room $room, int $canMentionEveryone): void
863867
}
864868

865869
$old = $room->getCanMentionEveryone();
866-
if ($old === $canMentionEveryone) {
870+
if ($old === $permission) {
867871
return;
868872
}
869873

870-
$event = new ModifyRoomEvent($room, 'canMentionEveryone', $canMentionEveryone, $old);
874+
$event = new ModifyRoomEvent($room, 'canMentionEveryone', $permission, $old);
871875
$this->dispatcher->dispatch(Room::EVENT_BEFORE_SET_CAN_MENTION_EVERYONE, $event);
872876

873877
$update = $this->db->getQueryBuilder();
874878
$update->update('talk_rooms')
875-
->set('can_mention_everyone', $update->createNamedParameter($canMentionEveryone, IQueryBuilder::PARAM_INT))
879+
->set('can_mention_everyone', $update->createNamedParameter($permission, IQueryBuilder::PARAM_INT))
876880
->where($update->expr()->eq('id', $update->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT)));
877881
$update->executeStatement();
878882

879-
$room->setCanMentionEveryone($canMentionEveryone);
883+
$room->setCanMentionEveryone($permission);
880884

881885
$this->dispatcher->dispatch(Room::EVENT_AFTER_SET_CAN_MENTION_EVERYONE, $event);
882886
}

tests/integration/features/bootstrap/FeatureContext.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3155,12 +3155,18 @@ public function userSetTheMessageExpirationToXWithStatusCode(string $user, int $
31553155
}
31563156

31573157
/**
3158-
* @Given /^user "([^"]*)" set (can|can not) mention everyone of room "([^"]*)" with (\d+)(?: \((v4)\))?$/
3158+
* @Given /^user "([^"]*)" set can mention everyone to (all|moderators) of room "([^"]*)" with (\d+)(?: \((v4)\))?$/
31593159
*/
3160-
public function userSetMentionEveryoneOfRoomWithStatus(string $user, string $canMentionEveryone, string $identifier, int $statusCode, string $apiVersion = 'v1'): void {
3161-
$verb = $canMentionEveryone === 'can' ? 'POST' : 'DELETE';
3160+
public function userSetMentionEveryoneOfRoomWithStatus(string $user, string $who, string $identifier, int $statusCode, string $apiVersion = 'v1'): void {
3161+
if ($who === 'all') {
3162+
$permission = 1;
3163+
} elseif ($who === 'moderators') {
3164+
$permission = 0;
3165+
}
31623166
$this->setCurrentUser($user);
3163-
$this->sendRequest($verb, '/apps/spreed/api/' . $apiVersion . '/room/' . self::$identifierToToken[$identifier] . '/can-mention-everyone');
3167+
$this->sendRequest('PUT', '/apps/spreed/api/' . $apiVersion . '/room/' . self::$identifierToToken[$identifier] . '/can-mention-everyone', [
3168+
'permission' => $permission,
3169+
]);
31643170
$this->assertStatusCode($this->response, $statusCode);
31653171
}
31663172

tests/integration/features/chat/mentions.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,11 @@ Feature: chat/mentions
634634
Then user "participant2" gets the following candidate mentions in room "room" for "all" with 200
635635
| id | label | source |
636636
| all | room | calls |
637-
# can not
638-
When user "participant1" set can not mention everyone of room "room" with 200 (v4)
637+
# moderators
638+
When user "participant1" set can mention everyone to moderators of room "room" with 200 (v4)
639639
Then user "participant2" gets the following candidate mentions in room "room" for "all" with 200
640-
# can
641-
When user "participant1" set can mention everyone of room "room" with 200 (v4)
640+
# all
641+
When user "participant1" set can mention everyone to all of room "room" with 200 (v4)
642642
Then user "participant2" gets the following candidate mentions in room "room" for "all" with 200
643643
| id | label | source |
644644
| all | room | calls |

0 commit comments

Comments
 (0)