Skip to content

Commit

Permalink
feat(chat): Outline the participant count when trying to mention ever…
Browse files Browse the repository at this point in the history
…yone

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jul 23, 2024
1 parent ba177ec commit e75a811
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob
| `status` | string | Optional: Only available with `includeStatus=true` and for users with a set status |
| `statusIcon` | string | Optional: Only available with `includeStatus=true` and for users with a set status |
| `statusMessage` | string | Optional: Only available with `includeStatus=true` and for users with a set status |
| `details` | string | Optional: Only provided for the "Everyone" option and can be used as a subline directly |

## System messages

Expand Down
8 changes: 6 additions & 2 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\Notification\IManager as INotificationManager;
Expand Down Expand Up @@ -97,6 +98,7 @@ public function __construct(
protected IReferenceManager $referenceManager,
protected ILimiter $rateLimiter,
protected IRequest $request,
protected IL10N $l,
protected LoggerInterface $logger,
) {
$this->cache = $cacheFactory->createDistributed(CachePrefix::CHAT_LAST_MESSAGE_ID);
Expand Down Expand Up @@ -961,12 +963,14 @@ public function addConversationNotify(array $results, string $search, Room $room
$roomDisplayName = $room->getDisplayName('');
}
if ($search === '' || $this->searchIsPartOfConversationNameOrAtAll($search, $roomDisplayName)) {
array_unshift($results, [
$participantCount = $this->participantService->getNumberOfUsers($room);
$results[] = [
'id' => 'all',
'label' => $roomDisplayName,
'details' => $this->l->n('All %n participant', 'All %n participants', $participantCount),
'source' => 'calls',
'mentionId' => 'all',
]);
];
}
return $results;
}
Expand Down
1 change: 1 addition & 0 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* label: string,
* source: string,
* mentionId: string,
* details: ?string,
* status: ?string,
* statusClearAt: ?int,
* statusIcon: ?string,
Expand Down
5 changes: 5 additions & 0 deletions openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@
"label",
"source",
"mentionId",
"details",
"status",
"statusClearAt",
"statusIcon",
Expand All @@ -455,6 +456,10 @@
"mentionId": {
"type": "string"
},
"details": {
"type": "string",
"nullable": true
},
"status": {
"type": "string",
"nullable": true
Expand Down
5 changes: 5 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
"label",
"source",
"mentionId",
"details",
"status",
"statusClearAt",
"statusIcon",
Expand All @@ -396,6 +397,10 @@
"mentionId": {
"type": "string"
},
"details": {
"type": "string",
"nullable": true
},
"status": {
"type": "string",
"nullable": true
Expand Down
2 changes: 1 addition & 1 deletion src/components/NewMessage/NewMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ export default {
if (possibleMention.source === 'calls') {
possibleMention.icon = 'icon-user-forced-white'
possibleMention.iconUrl = getConversationAvatarOcsUrl(this.token, isDarkTheme)
possibleMention.subline = t('spreed', 'Everyone')
possibleMention.subline = possibleMention?.details ? possibleMention.details : t('spreed', 'Everyone')
} else if (possibleMention.source === ATTENDEE.ACTOR_TYPE.GROUPS) {
possibleMention.icon = 'icon-group-forced-white'
possibleMention.subline = t('spreed', 'Group')
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,7 @@ export type components = {
label: string;
source: string;
mentionId: string;
details: string | null;
status: string | null;
/** Format: int64 */
statusClearAt: number | null;
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ export type components = {
label: string;
source: string;
mentionId: string;
details: string | null;
status: string | null;
/** Format: int64 */
statusClearAt: number | null;
Expand Down
15 changes: 13 additions & 2 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3080,8 +3080,13 @@ public function userGetsTheFollowingCandidateMentionsInRoomFor($user, $identifie
Assert::assertEmpty($mentions);
return;
}
$expected = $formData->getHash();
if (empty($expected)) {
Assert::assertEmpty($mentions);
return;
}

Assert::assertCount(count($formData->getHash()), $mentions, 'Mentions count does not match' . "\n" . json_encode($mentions, JSON_PRETTY_PRINT));
Assert::assertCount(count($expected), $mentions, 'Mentions count does not match' . "\n" . json_encode($mentions, JSON_PRETTY_PRINT));

usort($mentions, function ($a, $b) {
if ($a['source'] === $b['source']) {
Expand All @@ -3090,14 +3095,15 @@ public function userGetsTheFollowingCandidateMentionsInRoomFor($user, $identifie
return $a['source'] <=> $b['source'];
});

$expected = $formData->getHash();
usort($expected, function ($a, $b) {
if ($a['source'] === $b['source']) {
return $a['label'] <=> $b['label'];
}
return $a['source'] <=> $b['source'];
});

$checkDetails = array_key_exists('details', $expected[0]);

foreach ($expected as $key => $row) {
if ($row['id'] === 'GUEST_ID') {
Assert::assertMatchesRegularExpression('/^guest\/[0-9a-f]{40}$/', $mentions[$key]['id']);
Expand All @@ -3124,6 +3130,11 @@ public function userGetsTheFollowingCandidateMentionsInRoomFor($user, $identifie
unset($row['avatar']);
}
unset($mentions[$key]['avatar']);
if (!$checkDetails) {
unset($mentions[$key]['details']);
} elseif (empty($row['details'])) {
unset($row['details']);
}
Assert::assertEquals($row, $mentions[$key]);
}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/features/chat-1/mentions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Feature: chat/mentions
| roomType | 2 |
| roomName | room |
Then user "participant1" gets the following candidate mentions in room "group room" for "" with 200
| id | label | source | mentionId |
| all | room | calls | all |
| id | label | source | mentionId | details |
| all | room | calls | all | All 1 participant |

Scenario: get mentions in a group room
When user "participant1" creates room "group room" (v4)
Expand Down Expand Up @@ -223,7 +223,6 @@ Feature: chat/mentions
| id | label | source | mentionId |
| GUEST_ID | FooBar | guests | GUEST_ID |
And user "guest1" gets the following candidate mentions in room "public room" for "oob" with 200
| id | label | source | mentionId |
And user "guest2" gets the following candidate mentions in room "public room" for "oob" with 200
| id | label | source | mentionId |
| GUEST_ID | FooBar | guests | GUEST_ID |
Expand Down
36 changes: 29 additions & 7 deletions tests/php/Chat/ChatManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\Notification\IManager as INotificationManager;
Expand Down Expand Up @@ -58,6 +59,7 @@ class ChatManagerTest extends TestCase {
protected ILimiter&MockObject $rateLimiter;
protected IRequest&MockObject $request;
protected LoggerInterface&MockObject $logger;
protected IL10N&MockObject $l;
protected ?ChatManager $chatManager = null;

public function setUp(): void {
Expand All @@ -77,8 +79,15 @@ public function setUp(): void {
$this->referenceManager = $this->createMock(IReferenceManager::class);
$this->rateLimiter = $this->createMock(ILimiter::class);
$this->request = $this->createMock(IRequest::class);
$this->l = $this->createMock(IL10N::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->l->method('n')
->willReturnCallback(function (string $singular, string $plural, int $count, array $parameters = []) {
$text = $count === 1 ? $singular : $plural;
return vsprintf(str_replace('%n', (string) $count, $text), $parameters);
});

$this->chatManager = $this->getManager();
}

Expand Down Expand Up @@ -108,6 +117,7 @@ protected function getManager(array $methods = []): ChatManager {
$this->referenceManager,
$this->rateLimiter,
$this->request,
$this->l,
$this->logger,
])
->onlyMethods($methods)
Expand All @@ -131,6 +141,7 @@ protected function getManager(array $methods = []): ChatManager {
$this->referenceManager,
$this->rateLimiter,
$this->request,
$this->l,
$this->logger,
);
}
Expand Down Expand Up @@ -690,7 +701,8 @@ public static function dataAddConversationNotify(): array {
'',
['getType' => Room::TYPE_ONE_TO_ONE],
[],
[]
null,
[],
],
[
'',
Expand All @@ -699,13 +711,15 @@ public static function dataAddConversationNotify(): array {
'actor_type' => Attendee::ACTOR_USERS,
'actor_id' => 'user',
])],
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all']]
324,
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all', 'details' => 'All 324 participants']]
],
[
'',
['getMentionPermissions' => 1],
['hasModeratorPermissions' => false],
[]
null,
[],
],
[
'all',
Expand All @@ -714,7 +728,8 @@ public static function dataAddConversationNotify(): array {
'actor_type' => Attendee::ACTOR_USERS,
'actor_id' => 'user',
])],
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all']]
1,
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all', 'details' => 'All 1 participant']],
],
[
'all',
Expand All @@ -726,7 +741,8 @@ public static function dataAddConversationNotify(): array {
]),
'hasModeratorPermissions' => true,
],
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all']]
8,
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all', 'details' => 'All 8 participants']],
],
[
'here',
Expand All @@ -735,15 +751,16 @@ public static function dataAddConversationNotify(): array {
'actor_type' => Attendee::ACTOR_GUESTS,
'actor_id' => 'guest',
])],
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all']]
12,
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'mentionId' => 'all', 'details' => 'All 12 participants']],
],
];
}

/**
* @dataProvider dataAddConversationNotify
*/
public function testAddConversationNotify(string $search, array $roomMocks, array $participantMocks, array $expected): void {
public function testAddConversationNotify(string $search, array $roomMocks, array $participantMocks, ?int $totalCount, array $expected): void {
$room = $this->createMock(Room::class);
foreach ($roomMocks as $method => $return) {
$room->expects($this->once())
Expand All @@ -758,6 +775,11 @@ public function testAddConversationNotify(string $search, array $roomMocks, arra
->willReturn($return);
}

if ($totalCount !== null) {
$this->participantService->method('getNumberOfUsers')
->willReturn($totalCount);
}

$actual = $this->chatManager->addConversationNotify([], $search, $room, $participant);
$this->assertEquals($expected, $actual);
}
Expand Down

0 comments on commit e75a811

Please sign in to comment.