Skip to content

Commit f471ca6

Browse files
fixup! feat(contactsmenu): Sort by user status
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent e107a20 commit f471ca6

4 files changed

Lines changed: 26 additions & 130 deletions

File tree

lib/private/Contacts/ContactsMenu/ContactsStore.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,15 @@ public function getContacts(IUser $user, ?string $filter, ?int $limit = null, ?i
7979
if ($offset !== null) {
8080
$options['offset'] = $offset;
8181
}
82-
$recentStatuses = $this->userStatusService?->findAllRecentStatusChanges($limit, $offset) ?? [];
82+
// Status integration only works without pagination and filters
83+
if ($offset === null && ($filter === null || $filter === '')) {
84+
$recentStatuses = $this->userStatusService?->findAllRecentStatusChanges($limit, $offset) ?? [];
85+
} else {
86+
$recentStatuses = [];
87+
}
8388

8489
// Search by status if there is no filter and statuses are available
85-
if (($filter === null || $filter === '') && !empty($recentStatuses)) {
90+
if (!empty($recentStatuses)) {
8691
$allContacts = array_filter(array_map(function(UserStatus $userStatus) use ($options) {
8792
$contact = $this->contactsManager->search(
8893
$userStatus->getUserId(),
@@ -98,7 +103,7 @@ public function getContacts(IUser $user, ?string $filter, ?int $limit = null, ?i
98103
),
99104
)[0] ?? null;
100105
if ($contact !== null) {
101-
$contact['withStatus'] = true;
106+
$contact[Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP] = $userStatus->getStatusMessageTimestamp();
102107
}
103108
return $contact;
104109
}, $recentStatuses));

lib/private/Contacts/ContactsMenu/Entry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
use function array_merge;
3333

3434
class Entry implements IEntry {
35+
public const PROPERTY_STATUS_MESSAGE_TIMESTAMP = 'statusMessageTimestamp';
36+
3537
/** @var string|int|null */
3638
private $id = null;
3739

lib/private/Contacts/ContactsMenu/Manager.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,18 @@ public function findOne(IUser $user, int $shareType, string $shareWith): ?IEntry
8383
*/
8484
private function sortEntries(array $entries): array {
8585
usort($entries, function (Entry $entryA, Entry $entryB) {
86-
$aHasStatus = $entryA->getProperty('withStatus') !== null;
87-
$bHasStatus = $entryB->getProperty('withStatus') !== null;
88-
if (!$aHasStatus && !$bHasStatus) {
86+
$aStatusTimestamp = $entryA->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
87+
$bStatusTimestamp = $entryB->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
88+
if (!$aStatusTimestamp && !$bStatusTimestamp) {
8989
return strcasecmp($entryA->getFullName(), $entryB->getFullName());
9090
}
91-
if ($aHasStatus === null) {
91+
if ($aStatusTimestamp === null) {
9292
return 1;
9393
}
94-
if ($bHasStatus === null) {
94+
if ($bStatusTimestamp === null) {
9595
return -1;
9696
}
97-
return $bHasStatus - $aHasStatus;
97+
return $bStatusTimestamp - $aStatusTimestamp;
9898
});
9999
return $entries;
100100
}

tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php

Lines changed: 10 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,130 +1031,29 @@ public function testPaginateRecentStatus(): void {
10311031
$status2->setUserId('user2');
10321032
$status3 = new UserStatus();
10331033
$status3->setUserId('user3');
1034-
$this->statusService->expects(self::exactly(2))
1035-
->method('findAllRecentStatusChanges')
1036-
->willReturnCallback(function($limit, $offset) use ($status1, $status2, $status3) {
1037-
return match ([$limit, $offset]) {
1038-
[2, null], [2, 0] => [
1039-
$status1,
1040-
$status2,
1041-
],
1042-
[2, 3] => [
1043-
$status3,
1044-
],
1045-
default => [],
1046-
};
1047-
});
1034+
$this->statusService->expects(self::never())
1035+
->method('findAllRecentStatusChanges');
10481036
$this->contactsManager
1049-
->expects(self::exactly(4))
1037+
->expects(self::exactly(2))
10501038
->method('search')
10511039
->willReturnCallback(function($uid, $searchProps, $options) {
1052-
return match ([$uid, $options['limit'] ?? null]) {
1053-
['user1', 1] => [
1054-
[
1055-
'UID' => 'user1',
1056-
'URI' => 'user1.vcf',
1057-
],
1058-
],
1059-
['user2', 1] => [
1060-
[
1061-
'UID' => 'user2',
1062-
'URI' => 'user2.vcf',
1063-
],
1064-
],
1065-
['user3', 1] => [
1066-
[
1067-
'UID' => 'user3',
1068-
'URI' => 'user3.vcf',
1069-
],
1070-
],
1071-
default => [],
1072-
};
1073-
});
1074-
1075-
$page1 = $this->contactsStore->getContacts(
1076-
$user,
1077-
null,
1078-
2,
1079-
);
1080-
$page2 = $this->contactsStore->getContacts(
1081-
$user,
1082-
null,
1083-
2,
1084-
3,
1085-
);
1086-
1087-
self::assertCount(2, $page1);
1088-
self::assertCount(1, $page2);
1089-
}
1090-
1091-
public function testPaginateRecentStatusMixedWithContacts(): void {
1092-
$user = $this->createMock(IUser::class);
1093-
$status1 = new UserStatus();
1094-
$status1->setUserId('user1');
1095-
$status2 = new UserStatus();
1096-
$status2->setUserId('user2');
1097-
$status3 = new UserStatus();
1098-
$status3->setUserId('user3');
1099-
$this->statusService->expects(self::exactly(3))
1100-
->method('findAllRecentStatusChanges')
1101-
->willReturnCallback(function($limit, $offset) use ($status1, $status2, $status3) {
1102-
return match ([$limit, $offset]) {
1103-
[2, null], [2, 0] => [
1104-
$status1,
1105-
$status2,
1106-
],
1107-
[2, 3] => [
1108-
$status3,
1109-
],
1110-
default => [],
1111-
};
1112-
});
1113-
$this->contactsManager
1114-
->expects(self::exactly(5))
1115-
->method('search')
1116-
->willReturnCallback(function($term, $searchProps, $options) {
1117-
return match ([$term, $options['limit'] ?? null, $options['offset'] ?? 0]) {
1118-
['user1', 1, 0] => [
1119-
[
1120-
'UID' => 'user1',
1121-
'URI' => 'user1.vcf',
1122-
],
1123-
],
1124-
['user2', 1, 0] => [
1125-
[
1126-
'UID' => 'user2',
1127-
'URI' => 'user2.vcf',
1128-
],
1129-
],
1130-
['user3', 1, 0] => [
1131-
[
1132-
'UID' => 'user3',
1133-
'URI' => 'user3.vcf',
1134-
],
1135-
],
1136-
['', 1, 0] => [
1040+
return match ([$uid, $options['limit'] ?? null, $options['offset'] ?? null]) {
1041+
['', 2, 0] => [
11371042
[
11381043
'UID' => 'contact1',
11391044
'URI' => 'contact1.vcf',
11401045
],
1141-
],
1142-
['', 2, 3] => [
11431046
[
11441047
'UID' => 'contact2',
11451048
'URI' => 'contact2.vcf',
11461049
],
1050+
],
1051+
['', 2, 3] => [
11471052
[
11481053
'UID' => 'contact3',
11491054
'URI' => 'contact3.vcf',
11501055
],
11511056
],
1152-
['', 2, 5] => [
1153-
[
1154-
'UID' => 'contact4',
1155-
'URI' => 'contact4.vcf',
1156-
],
1157-
],
11581057
default => [],
11591058
};
11601059
});
@@ -1163,26 +1062,16 @@ public function testPaginateRecentStatusMixedWithContacts(): void {
11631062
$user,
11641063
null,
11651064
2,
1065+
0,
11661066
);
11671067
$page2 = $this->contactsStore->getContacts(
11681068
$user,
11691069
null,
11701070
2,
11711071
3,
11721072
);
1173-
$page3 = $this->contactsStore->getContacts(
1174-
$user,
1175-
null,
1176-
2,
1177-
5,
1178-
);
11791073

1180-
self::assertCount(2, $page1); // 2x status
1181-
self::assertEquals('user1', $page1[0]->getProperty('UID'));
1182-
self::assertEquals('user2', $page1[1]->getProperty('UID'));
1183-
self::assertCount(2, $page2); // status+contact
1184-
self::assertEquals('user3', $page2[0]->getProperty('UID'));
1185-
self::assertEquals('contact1', $page2[1]->getProperty('UID'));
1186-
self::assertCount(2, $page3); // 2x contact
1074+
self::assertCount(2, $page1);
1075+
self::assertCount(1, $page2);
11871076
}
11881077
}

0 commit comments

Comments
 (0)