From 50aab32f0ff4dbd9dfdea126d40a03af698b7db3 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Tue, 2 Jan 2024 23:39:46 +0000 Subject: [PATCH 1/2] Add search param for list subscribers endpoint --- app/config/collections.php | 20 +++++++++++++++++++- app/controllers/api/messaging.php | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/config/collections.php b/app/config/collections.php index b3e3417555..86d2f7ee45 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1810,6 +1810,17 @@ $commonCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('search'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ @@ -1853,7 +1864,14 @@ $commonCollections = [ 'attributes' => ['topicInternalId'], 'lengths' => [], 'orders' => [], - ] + ], + [ + '$id' => ID::custom('_fulltext_search'), + 'type' => Database::INDEX_FULLTEXT, + 'attributes' => ['search'], + 'lengths' => [], + 'orders' => [], + ], ], ], diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 124bc2d219..275a4b1686 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -1955,6 +1955,7 @@ App::post('/v1/messaging/topics/:topicId/subscribers') 'userId' => $user->getId(), 'userInternalId' => $user->getInternalId(), 'providerType' => $target->getAttribute('providerType'), + 'search' => "{$target->getAttribute('providerType')} " ]); try { From 96de1617edaf142ac79dd2f6c1ec25edab1e5789 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Thu, 18 Jan 2024 02:32:32 +0000 Subject: [PATCH 2/2] Update the content in the subscriber's search attribute --- app/controllers/api/messaging.php | 15 +++++-- .../e2e/Services/Messaging/MessagingBase.php | 42 +++++++++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index c348a531bd..19bcfad471 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -1991,20 +1991,27 @@ App::post('/v1/messaging/topics/:topicId/subscribers') $user = Authorization::skip(fn () => $dbForProject->getDocument('users', $target->getAttribute('userId'))); + $userId = $user->getId(); + $subscriber = new Document([ '$id' => $subscriberId, '$permissions' => [ - Permission::read(Role::user($user->getId())), - Permission::delete(Role::user($user->getId())), + Permission::read(Role::user($userId)), + Permission::delete(Role::user($userId)), ], 'topicId' => $topicId, 'topicInternalId' => $topic->getInternalId(), 'targetId' => $targetId, 'targetInternalId' => $target->getInternalId(), - 'userId' => $user->getId(), + 'userId' => $userId, 'userInternalId' => $user->getInternalId(), 'providerType' => $target->getAttribute('providerType'), - 'search' => "{$target->getAttribute('providerType')} " + 'search' => implode(' ', [ + $subscriberId, + $targetId, + $userId, + $target->getAttribute('providerType'), + ]), ]); try { diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index bb09e0a247..06621cc4a0 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -419,6 +419,12 @@ trait MessagingBase */ public function testListSubscribers(array $data) { + $subscriberId = $data['subscriberId']; + $targetId = $data['targetId']; + $userId = $data['userId']; + $providerType = $data['providerType']; + $identifier = $data['identifier']; + $response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'] . '/subscribers', \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -427,11 +433,41 @@ trait MessagingBase $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(1, $response['body']['total']); - $this->assertEquals($data['userId'], $response['body']['subscribers'][0]['target']['userId']); - $this->assertEquals($data['providerType'], $response['body']['subscribers'][0]['target']['providerType']); - $this->assertEquals($data['identifier'], $response['body']['subscribers'][0]['target']['identifier']); + $this->assertEquals($userId, $response['body']['subscribers'][0]['target']['userId']); + $this->assertEquals($providerType, $response['body']['subscribers'][0]['target']['providerType']); + $this->assertEquals($identifier, $response['body']['subscribers'][0]['target']['identifier']); $this->assertEquals(\count($response['body']['subscribers']), $response['body']['total']); + $response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'] . '/subscribers', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]), [ + 'search' => 'DOES_NOT_EXIST', + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(0, $response['body']['total']); + + $searches = [ + $subscriberId, + $targetId, + $userId, + $providerType + ]; + foreach ($searches as $search) { + $response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'] . '/subscribers', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]), [ + 'search' => $search, + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(1, $response['body']['total']); + } + return $data; }