Skip to content

Commit

Permalink
fixup! feat(contactsmenu): Sort by user status
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Nov 8, 2023
1 parent e107a20 commit f471ca6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 130 deletions.
11 changes: 8 additions & 3 deletions lib/private/Contacts/ContactsMenu/ContactsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ public function getContacts(IUser $user, ?string $filter, ?int $limit = null, ?i
if ($offset !== null) {
$options['offset'] = $offset;
}
$recentStatuses = $this->userStatusService?->findAllRecentStatusChanges($limit, $offset) ?? [];
// Status integration only works without pagination and filters
if ($offset === null && ($filter === null || $filter === '')) {
$recentStatuses = $this->userStatusService?->findAllRecentStatusChanges($limit, $offset) ?? [];
} else {
$recentStatuses = [];
}

// Search by status if there is no filter and statuses are available
if (($filter === null || $filter === '') && !empty($recentStatuses)) {
if (!empty($recentStatuses)) {
$allContacts = array_filter(array_map(function(UserStatus $userStatus) use ($options) {
$contact = $this->contactsManager->search(
$userStatus->getUserId(),
Expand All @@ -98,7 +103,7 @@ public function getContacts(IUser $user, ?string $filter, ?int $limit = null, ?i
),
)[0] ?? null;
if ($contact !== null) {
$contact['withStatus'] = true;
$contact[Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP] = $userStatus->getStatusMessageTimestamp();
}
return $contact;
}, $recentStatuses));
Expand Down
2 changes: 2 additions & 0 deletions lib/private/Contacts/ContactsMenu/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use function array_merge;

class Entry implements IEntry {
public const PROPERTY_STATUS_MESSAGE_TIMESTAMP = 'statusMessageTimestamp';

/** @var string|int|null */
private $id = null;

Expand Down
12 changes: 6 additions & 6 deletions lib/private/Contacts/ContactsMenu/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ public function findOne(IUser $user, int $shareType, string $shareWith): ?IEntry
*/
private function sortEntries(array $entries): array {
usort($entries, function (Entry $entryA, Entry $entryB) {
$aHasStatus = $entryA->getProperty('withStatus') !== null;
$bHasStatus = $entryB->getProperty('withStatus') !== null;
if (!$aHasStatus && !$bHasStatus) {
$aStatusTimestamp = $entryA->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
$bStatusTimestamp = $entryB->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
if (!$aStatusTimestamp && !$bStatusTimestamp) {
return strcasecmp($entryA->getFullName(), $entryB->getFullName());
}
if ($aHasStatus === null) {
if ($aStatusTimestamp === null) {
return 1;
}
if ($bHasStatus === null) {
if ($bStatusTimestamp === null) {
return -1;
}
return $bHasStatus - $aHasStatus;
return $bStatusTimestamp - $aStatusTimestamp;
});
return $entries;
}
Expand Down
131 changes: 10 additions & 121 deletions tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,130 +1031,29 @@ public function testPaginateRecentStatus(): void {
$status2->setUserId('user2');
$status3 = new UserStatus();
$status3->setUserId('user3');
$this->statusService->expects(self::exactly(2))
->method('findAllRecentStatusChanges')
->willReturnCallback(function($limit, $offset) use ($status1, $status2, $status3) {
return match ([$limit, $offset]) {
[2, null], [2, 0] => [
$status1,
$status2,
],
[2, 3] => [
$status3,
],
default => [],
};
});
$this->statusService->expects(self::never())
->method('findAllRecentStatusChanges');
$this->contactsManager
->expects(self::exactly(4))
->expects(self::exactly(2))
->method('search')
->willReturnCallback(function($uid, $searchProps, $options) {
return match ([$uid, $options['limit'] ?? null]) {
['user1', 1] => [
[
'UID' => 'user1',
'URI' => 'user1.vcf',
],
],
['user2', 1] => [
[
'UID' => 'user2',
'URI' => 'user2.vcf',
],
],
['user3', 1] => [
[
'UID' => 'user3',
'URI' => 'user3.vcf',
],
],
default => [],
};
});

$page1 = $this->contactsStore->getContacts(
$user,
null,
2,
);
$page2 = $this->contactsStore->getContacts(
$user,
null,
2,
3,
);

self::assertCount(2, $page1);
self::assertCount(1, $page2);
}

public function testPaginateRecentStatusMixedWithContacts(): void {
$user = $this->createMock(IUser::class);
$status1 = new UserStatus();
$status1->setUserId('user1');
$status2 = new UserStatus();
$status2->setUserId('user2');
$status3 = new UserStatus();
$status3->setUserId('user3');
$this->statusService->expects(self::exactly(3))
->method('findAllRecentStatusChanges')
->willReturnCallback(function($limit, $offset) use ($status1, $status2, $status3) {
return match ([$limit, $offset]) {
[2, null], [2, 0] => [
$status1,
$status2,
],
[2, 3] => [
$status3,
],
default => [],
};
});
$this->contactsManager
->expects(self::exactly(5))
->method('search')
->willReturnCallback(function($term, $searchProps, $options) {
return match ([$term, $options['limit'] ?? null, $options['offset'] ?? 0]) {
['user1', 1, 0] => [
[
'UID' => 'user1',
'URI' => 'user1.vcf',
],
],
['user2', 1, 0] => [
[
'UID' => 'user2',
'URI' => 'user2.vcf',
],
],
['user3', 1, 0] => [
[
'UID' => 'user3',
'URI' => 'user3.vcf',
],
],
['', 1, 0] => [
return match ([$uid, $options['limit'] ?? null, $options['offset'] ?? null]) {
['', 2, 0] => [
[
'UID' => 'contact1',
'URI' => 'contact1.vcf',
],
],
['', 2, 3] => [
[
'UID' => 'contact2',
'URI' => 'contact2.vcf',
],
],
['', 2, 3] => [
[
'UID' => 'contact3',
'URI' => 'contact3.vcf',
],
],
['', 2, 5] => [
[
'UID' => 'contact4',
'URI' => 'contact4.vcf',
],
],
default => [],
};
});
Expand All @@ -1163,26 +1062,16 @@ public function testPaginateRecentStatusMixedWithContacts(): void {
$user,
null,
2,
0,
);
$page2 = $this->contactsStore->getContacts(
$user,
null,
2,
3,
);
$page3 = $this->contactsStore->getContacts(
$user,
null,
2,
5,
);

self::assertCount(2, $page1); // 2x status
self::assertEquals('user1', $page1[0]->getProperty('UID'));
self::assertEquals('user2', $page1[1]->getProperty('UID'));
self::assertCount(2, $page2); // status+contact
self::assertEquals('user3', $page2[0]->getProperty('UID'));
self::assertEquals('contact1', $page2[1]->getProperty('UID'));
self::assertCount(2, $page3); // 2x contact
self::assertCount(2, $page1);
self::assertCount(1, $page2);
}
}

0 comments on commit f471ca6

Please sign in to comment.