Skip to content

Commit e107a20

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

2 files changed

Lines changed: 220 additions & 2 deletions

File tree

lib/private/Contacts/ContactsMenu/ContactsStore.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ 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) ?? [];
82+
$recentStatuses = $this->userStatusService?->findAllRecentStatusChanges($limit, $offset) ?? [];
8383

8484
// Search by status if there is no filter and statuses are available
85-
if (($filter === null || $filter === '') && $offset === null && !empty($recentStatuses)) {
85+
if (($filter === null || $filter === '') && !empty($recentStatuses)) {
8686
$allContacts = array_filter(array_map(function(UserStatus $userStatus) use ($options) {
8787
$contact = $this->contactsManager->search(
8888
$userStatus->getUserId(),
@@ -93,6 +93,7 @@ public function getContacts(IUser $user, ?string $filter, ?int $limit = null, ?i
9393
$options,
9494
[
9595
'limit' => 1,
96+
'offset' => 0,
9697
],
9798
),
9899
)[0] ?? null;

tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
47
* @copyright 2017 Lukas Reschke <lukas@statuscode.ch>
@@ -28,6 +31,7 @@
2831
use OC\Contacts\ContactsMenu\ContactsStore;
2932
use OC\KnownUser\KnownUserService;
3033
use OC\Profile\ProfileManager;
34+
use OCA\UserStatus\Db\UserStatus;
3135
use OCA\UserStatus\Service\StatusService;
3236
use OCP\Contacts\IManager;
3337
use OCP\IConfig;
@@ -968,4 +972,217 @@ public function testFindOneNoMatches() {
968972

969973
$this->assertEquals(null, $entry);
970974
}
975+
976+
public function testGetRecentStatusFirst(): void {
977+
$user = $this->createMock(IUser::class);
978+
$status1 = new UserStatus();
979+
$status1->setUserId('user1');
980+
$status2 = new UserStatus();
981+
$status2->setUserId('user2');
982+
$this->statusService->expects(self::once())
983+
->method('findAllRecentStatusChanges')
984+
->willReturn([
985+
$status1,
986+
$status2,
987+
]);
988+
$this->contactsManager
989+
->expects(self::exactly(3))
990+
->method('search')
991+
->willReturnCallback(function($uid, $searchProps, $options) {
992+
return match ([$uid, $options['limit'] ?? null]) {
993+
['user1', 1] => [
994+
[
995+
'UID' => 'user1',
996+
'URI' => 'user1.vcf',
997+
],
998+
],
999+
['user2' => [], 1], // Simulate not found
1000+
['', 4] => [
1001+
[
1002+
'UID' => 'contact1',
1003+
'URI' => 'contact1.vcf',
1004+
],
1005+
[
1006+
'UID' => 'contact2',
1007+
'URI' => 'contact2.vcf',
1008+
],
1009+
],
1010+
default => [],
1011+
};
1012+
});
1013+
1014+
$contacts = $this->contactsStore->getContacts(
1015+
$user,
1016+
null,
1017+
5,
1018+
);
1019+
1020+
self::assertCount(3, $contacts);
1021+
self::assertEquals('user1', $contacts[0]->getProperty('UID'));
1022+
self::assertEquals('contact1', $contacts[1]->getProperty('UID'));
1023+
self::assertEquals('contact2', $contacts[2]->getProperty('UID'));
1024+
}
1025+
1026+
public function testPaginateRecentStatus(): void {
1027+
$user = $this->createMock(IUser::class);
1028+
$status1 = new UserStatus();
1029+
$status1->setUserId('user1');
1030+
$status2 = new UserStatus();
1031+
$status2->setUserId('user2');
1032+
$status3 = new UserStatus();
1033+
$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+
});
1048+
$this->contactsManager
1049+
->expects(self::exactly(4))
1050+
->method('search')
1051+
->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] => [
1137+
[
1138+
'UID' => 'contact1',
1139+
'URI' => 'contact1.vcf',
1140+
],
1141+
],
1142+
['', 2, 3] => [
1143+
[
1144+
'UID' => 'contact2',
1145+
'URI' => 'contact2.vcf',
1146+
],
1147+
[
1148+
'UID' => 'contact3',
1149+
'URI' => 'contact3.vcf',
1150+
],
1151+
],
1152+
['', 2, 5] => [
1153+
[
1154+
'UID' => 'contact4',
1155+
'URI' => 'contact4.vcf',
1156+
],
1157+
],
1158+
default => [],
1159+
};
1160+
});
1161+
1162+
$page1 = $this->contactsStore->getContacts(
1163+
$user,
1164+
null,
1165+
2,
1166+
);
1167+
$page2 = $this->contactsStore->getContacts(
1168+
$user,
1169+
null,
1170+
2,
1171+
3,
1172+
);
1173+
$page3 = $this->contactsStore->getContacts(
1174+
$user,
1175+
null,
1176+
2,
1177+
5,
1178+
);
1179+
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
1187+
}
9711188
}

0 commit comments

Comments
 (0)