From 2bb8854f616b8bd3188349dc3d807e904ed22343 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 28 Sep 2022 18:45:07 +1300 Subject: [PATCH 1/5] WIP integrate messaging library (cherry picked from commit 9a25f77e3e7e76f6c93cc3ce1985f43426a0debf) # Conflicts: # app/workers/messaging.php # src/Appwrite/SMS/Adapter/Mock.php # tests/e2e/Services/Account/AccountCustomClientTest.php --- app/init.php | 12 +-- app/workers/messaging.php | 24 +++--- composer.json | 21 ++++-- composer.lock | 65 +++++++++++++++- src/Appwrite/SMS/Adapter.php | 75 ------------------- src/Appwrite/SMS/Adapter/Mock.php | 24 ------ src/Appwrite/SMS/Adapter/Msg91.php | 46 ------------ src/Appwrite/SMS/Adapter/Telesign.php | 39 ---------- src/Appwrite/SMS/Adapter/TextMagic.php | 42 ----------- src/Appwrite/SMS/Adapter/Twilio.php | 36 --------- src/Appwrite/SMS/Adapter/Vonage.php | 41 ---------- .../Account/AccountCustomClientTest.php | 14 +++- 12 files changed, 109 insertions(+), 330 deletions(-) delete mode 100644 src/Appwrite/SMS/Adapter.php delete mode 100644 src/Appwrite/SMS/Adapter/Mock.php delete mode 100644 src/Appwrite/SMS/Adapter/Msg91.php delete mode 100644 src/Appwrite/SMS/Adapter/Telesign.php delete mode 100644 src/Appwrite/SMS/Adapter/TextMagic.php delete mode 100644 src/Appwrite/SMS/Adapter/Twilio.php delete mode 100644 src/Appwrite/SMS/Adapter/Vonage.php diff --git a/app/init.php b/app/init.php index df7763707d..8a945cf43c 100644 --- a/app/init.php +++ b/app/init.php @@ -23,12 +23,6 @@ use Ahc\Jwt\JWT; use Ahc\Jwt\JWTException; use Appwrite\Extend\Exception; use Appwrite\Auth\Auth; -use Appwrite\SMS\Adapter\Mock; -use Appwrite\SMS\Adapter\Telesign; -use Appwrite\SMS\Adapter\TextMagic; -use Appwrite\SMS\Adapter\Twilio; -use Appwrite\SMS\Adapter\Msg91; -use Appwrite\SMS\Adapter\Vonage; use Appwrite\DSN\DSN; use Appwrite\Event\Audit; use Appwrite\Event\Database as EventDatabase; @@ -47,6 +41,12 @@ use Utopia\Database\ID; use Utopia\Logger\Logger; use Utopia\Config\Config; use Utopia\Locale\Locale; +use Utopia\Messaging\Adapters\SMS\Mock; +use Utopia\Messaging\Adapters\SMS\Msg91; +use Utopia\Messaging\Adapters\SMS\Telesign; +use Utopia\Messaging\Adapters\SMS\TextMagic; +use Utopia\Messaging\Adapters\SMS\Twilio; +use Utopia\Messaging\Adapters\SMS\Vonage; use Utopia\Registry\Registry; use MaxMind\Db\Reader; use PHPMailer\PHPMailer\PHPMailer; diff --git a/app/workers/messaging.php b/app/workers/messaging.php index 1fc5deaf44..d1173a8860 100644 --- a/app/workers/messaging.php +++ b/app/workers/messaging.php @@ -1,16 +1,17 @@ args['recipient']; - $message = $this->args['message']; + $message = new SMS( + to: [$this->args['recipient']], + content: $this->args['message'], + from: $this->from, + ); try { - $this->sms->send($this->from, $recipient, $message); + $this->sms->send($message); } catch (\Exception $error) { throw new Exception('Error sending message: ' . $error->getMessage(), 500); } diff --git a/composer.json b/composer.json index 973aec8539..cf46060070 100644 --- a/composer.json +++ b/composer.json @@ -43,8 +43,6 @@ "ext-sockets": "*", "appwrite/php-clamav": "1.1.*", "appwrite/php-runtimes": "0.11.*", - "utopia-php/framework": "0.21.*", - "utopia-php/logger": "0.3.*", "utopia-php/abuse": "0.13.*", "utopia-php/analytics": "0.2.*", "utopia-php/audit": "0.14.*", @@ -52,15 +50,18 @@ "utopia-php/cli": "0.13.*", "utopia-php/config": "0.2.*", "utopia-php/database": "0.25.*", - "utopia-php/locale": "0.4.*", - "utopia-php/registry": "0.5.*", - "utopia-php/preloader": "0.2.*", "utopia-php/domains": "1.1.*", - "utopia-php/swoole": "0.3.*", - "utopia-php/storage": "0.11.*", - "utopia-php/websocket": "0.1.0", + "utopia-php/framework": "0.21.*", "utopia-php/image": "0.5.*", + "utopia-php/locale": "0.4.*", + "utopia-php/logger": "0.3.*", + "utopia-php/messaging": "dev-feat-base", "utopia-php/orchestration": "0.6.*", + "utopia-php/preloader": "0.2.*", + "utopia-php/registry": "0.5.*", + "utopia-php/storage": "0.11.*", + "utopia-php/swoole": "0.3.*", + "utopia-php/websocket": "0.1.0", "resque/php-resque": "1.3.6", "matomo/device-detector": "6.0.0", "dragonmantank/cron-expression": "3.3.1", @@ -74,6 +75,10 @@ { "url": "https://github.com/appwrite/runtimes.git", "type": "git" + }, + { + "url": "https://github.com/utopia-php/messaging", + "type": "git" } ], "require-dev": { diff --git a/composer.lock b/composer.lock index 9ec0ea3197..c43fb24826 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "568151395a8877f87d9bdce048adc2dc", + "content-hash": "3322a1fdc38f00e20390df83e7e41a40", "packages": [ { "name": "adhocore/jwt", @@ -2394,6 +2394,65 @@ }, "time": "2022-03-18T10:56:57+00:00" }, + { + "name": "utopia-php/messaging", + "version": "dev-feat-base", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/messaging", + "reference": "52bc71093f0f689a4d5ab1081645ceff07e9c21d" + }, + "require": { + "php": ">=8.0.0" + }, + "require-dev": { + "phpmailer/phpmailer": "6.6.*", + "phpunit/phpunit": "9.5.*", + "squizlabs/php_codesniffer": "^3.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Messaging\\": "src/Utopia/Messaging" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\E2E\\": "tests/e2e", + "Tests\\Unit\\": "tests/unit" + } + }, + "scripts": { + "test": [ + "vendor/bin/phpunit" + ], + "lint": [ + "vendor/bin/phpcs" + ], + "format": [ + "vendor/bin/phpcbf" + ] + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jake Barnby", + "email": "jake@appwrite.io" + } + ], + "description": "A simple, light and advanced PHP messaging library", + "keywords": [ + "library", + "messaging", + "php", + "upf", + "utopia", + "utopia-php" + ], + "time": "2022-09-28T02:51:05+00:00" + }, { "name": "utopia-php/orchestration", "version": "0.6.0", @@ -5360,7 +5419,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "utopia-php/messaging": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/SMS/Adapter.php b/src/Appwrite/SMS/Adapter.php deleted file mode 100644 index 9f029bca69..0000000000 --- a/src/Appwrite/SMS/Adapter.php +++ /dev/null @@ -1,75 +0,0 @@ -user = $user; - $this->secret = $secret; - } - - /** - * Send Message to phone. - * @param string $from - * @param string $to - * @param string $message - * @return void - */ - abstract public function send(string $from, string $to, string $message): void; - - /** - * @param string $method - * @param string $url - * @param array $headers - * @param string $payload - * - * @return string - */ - protected function request(string $method, string $url, array $headers = [], ?string $payload = null, ?string $userpwd = null): string - { - $ch = \curl_init($url); - - \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - \curl_setopt($ch, CURLOPT_HEADER, 0); - \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - \curl_setopt($ch, CURLOPT_USERAGENT, 'Appwrite Phone Authentication'); - - if (!is_null($payload)) { - \curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); - } - - if (!is_null($userpwd)) { - \curl_setopt($ch, CURLOPT_USERPWD, $userpwd); - } - - $headers[] = 'Content-length: ' . \strlen($payload); - - \curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - - $response = (string) \curl_exec($ch); - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - - \curl_close($ch); - - if ($code >= 400) { - throw new \Exception($response); - } - - return $response; - } -} diff --git a/src/Appwrite/SMS/Adapter/Mock.php b/src/Appwrite/SMS/Adapter/Mock.php deleted file mode 100644 index 147f7ec9b2..0000000000 --- a/src/Appwrite/SMS/Adapter/Mock.php +++ /dev/null @@ -1,24 +0,0 @@ - utilized from for flow id - * @param string $to - * @param string $message - * @return void - */ - public function send(string $from, string $to, string $message): void - { - $to = ltrim($to, '+'); - $this->request( - method: 'POST', - url: $this->endpoint, - payload: json_encode([ - 'sender' => $this->user, - 'otp' => $message, - 'flow_id' => $from, - 'mobiles' => $to - ]), - headers: [ - "content-type: application/JSON", - "authkey: {$this->secret}", - ] - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/Telesign.php b/src/Appwrite/SMS/Adapter/Telesign.php deleted file mode 100644 index 1ac6c1b284..0000000000 --- a/src/Appwrite/SMS/Adapter/Telesign.php +++ /dev/null @@ -1,39 +0,0 @@ -request( - method: 'POST', - url: $this->endpoint, - payload: \http_build_query([ - 'message' => $message, - 'message_type' => 'otp', - 'phone_number' => $to - ]), - userpwd: "{$this->user}:{$this->secret}" - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/TextMagic.php b/src/Appwrite/SMS/Adapter/TextMagic.php deleted file mode 100644 index a7f391b76c..0000000000 --- a/src/Appwrite/SMS/Adapter/TextMagic.php +++ /dev/null @@ -1,42 +0,0 @@ -request( - method: 'POST', - url: $this->endpoint . '/messages', - payload: \http_build_query([ - 'text' => $message, - 'from' => $from, - 'phones' => $to - ]), - headers: [ - "X-TM-Username: {$this->user}", - "X-TM-Key: {$this->secret}", - ] - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/Twilio.php b/src/Appwrite/SMS/Adapter/Twilio.php deleted file mode 100644 index 4988c720ad..0000000000 --- a/src/Appwrite/SMS/Adapter/Twilio.php +++ /dev/null @@ -1,36 +0,0 @@ -request( - method: 'POST', - url: "{$this->endpoint}/Accounts/{$this->user}/Messages.json", - payload: \http_build_query([ - 'Body' => $message, - 'From' => $from, - 'To' => $to - ]), - userpwd: "{$this->user}:{$this->secret}" - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/Vonage.php b/src/Appwrite/SMS/Adapter/Vonage.php deleted file mode 100644 index cd7ef3c5bf..0000000000 --- a/src/Appwrite/SMS/Adapter/Vonage.php +++ /dev/null @@ -1,41 +0,0 @@ -request( - method: 'POST', - url: $this->endpoint, - headers: $headers, - payload: \http_build_query([ - 'text' => $message, - 'from' => $from, - 'to' => $to, - 'api_key' => $this->user, - 'api_secret' => $this->secret - ]) - ); - } -} diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 541e8e79f4..82cc6d4fc0 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -716,7 +716,19 @@ class AccountCustomClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); - $data['token'] = Mock::$digits; + \sleep(2); + + $smsRequest = $this->getLastRequest(); + + $this->assertEquals('http://request-catcher:5000/mock-sms', $smsRequest['url']); + $this->assertEquals('Appwrite Mock Message Sender', $smsRequest['headers']['User-Agent']); + $this->assertEquals('username', $smsRequest['headers']['X-Username']); + $this->assertEquals('password', $smsRequest['headers']['X-Key']); + $this->assertEquals('POST', $smsRequest['method']); + $this->assertEquals('+123456789', $smsRequest['data']['from']); + $this->assertEquals($number, $smsRequest['data']['to']); + + $data['token'] = $smsRequest['data']['message']; $data['id'] = $userId; $data['number'] = $number; From 5c6caf5603b468a9b0b4c6218d3b8c38f63f2ffa Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 3 Oct 2022 13:20:53 +1300 Subject: [PATCH 2/5] Update to messaging lib to tagged release (cherry picked from commit 580eafffd2a90fb9dccc4554ac9ce388864f1e8d) # Conflicts: # composer.lock --- composer.json | 6 +----- composer.lock | 54 ++++++++++++++++++++++----------------------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index cf46060070..1583b51d04 100644 --- a/composer.json +++ b/composer.json @@ -55,7 +55,7 @@ "utopia-php/image": "0.5.*", "utopia-php/locale": "0.4.*", "utopia-php/logger": "0.3.*", - "utopia-php/messaging": "dev-feat-base", + "utopia-php/messaging": "0.1.*", "utopia-php/orchestration": "0.6.*", "utopia-php/preloader": "0.2.*", "utopia-php/registry": "0.5.*", @@ -75,10 +75,6 @@ { "url": "https://github.com/appwrite/runtimes.git", "type": "git" - }, - { - "url": "https://github.com/utopia-php/messaging", - "type": "git" } ], "require-dev": { diff --git a/composer.lock b/composer.lock index c43fb24826..02a11ebd96 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3322a1fdc38f00e20390df83e7e41a40", + "content-hash": "e81c5582d74cc867fbf80e9bdf4f4872", "packages": [ { "name": "adhocore/jwt", @@ -2396,11 +2396,17 @@ }, { "name": "utopia-php/messaging", - "version": "dev-feat-base", + "version": "0.1.0", "source": { "type": "git", - "url": "https://github.com/utopia-php/messaging", - "reference": "52bc71093f0f689a4d5ab1081645ceff07e9c21d" + "url": "https://github.com/utopia-php/messaging.git", + "reference": "501272fad666f06bec8f130076862e7981a73f8c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/messaging/zipball/501272fad666f06bec8f130076862e7981a73f8c", + "reference": "501272fad666f06bec8f130076862e7981a73f8c", + "shasum": "" }, "require": { "php": ">=8.0.0" @@ -2416,23 +2422,7 @@ "Utopia\\Messaging\\": "src/Utopia/Messaging" } }, - "autoload-dev": { - "psr-4": { - "Tests\\E2E\\": "tests/e2e", - "Tests\\Unit\\": "tests/unit" - } - }, - "scripts": { - "test": [ - "vendor/bin/phpunit" - ], - "lint": [ - "vendor/bin/phpcs" - ], - "format": [ - "vendor/bin/phpcbf" - ] - }, + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2451,7 +2441,11 @@ "utopia", "utopia-php" ], - "time": "2022-09-28T02:51:05+00:00" + "support": { + "issues": "https://github.com/utopia-php/messaging/issues", + "source": "https://github.com/utopia-php/messaging/tree/0.1.0" + }, + "time": "2022-09-29T11:22:48+00:00" }, { "name": "utopia-php/orchestration", @@ -5342,16 +5336,16 @@ }, { "name": "twig/twig", - "version": "v3.4.2", + "version": "v3.4.3", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077" + "reference": "c38fd6b0b7f370c198db91ffd02e23b517426b58" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077", - "reference": "e07cdd3d430cd7e453c31b36eb5ad6c0c5e43077", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/c38fd6b0b7f370c198db91ffd02e23b517426b58", + "reference": "c38fd6b0b7f370c198db91ffd02e23b517426b58", "shasum": "" }, "require": { @@ -5402,7 +5396,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.4.2" + "source": "https://github.com/twigphp/Twig/tree/v3.4.3" }, "funding": [ { @@ -5414,14 +5408,12 @@ "type": "tidelift" } ], - "time": "2022-08-12T06:47:24+00:00" + "time": "2022-09-28T08:42:51+00:00" } ], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "utopia-php/messaging": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { From 2fbc40e966189b2936f0bb813be36ef01c4f0f9e Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 3 Oct 2022 13:42:53 +1300 Subject: [PATCH 3/5] Merge fixes --- .env | 2 +- app/init.php | 2 +- app/workers/messaging.php | 2 +- composer.lock | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 227ea10676..65fb54cb04 100644 --- a/.env +++ b/.env @@ -56,7 +56,7 @@ _APP_SMTP_PORT=1025 _APP_SMTP_SECURE= _APP_SMTP_USERNAME= _APP_SMTP_PASSWORD= -_APP_SMS_PROVIDER=sms://mock +_APP_SMS_PROVIDER=sms://username:password@mock _APP_SMS_FROM=+123456789 _APP_STORAGE_LIMIT=30000000 _APP_STORAGE_PREVIEW_LIMIT=20000000 diff --git a/app/init.php b/app/init.php index 8a945cf43c..dc5c874018 100644 --- a/app/init.php +++ b/app/init.php @@ -1029,7 +1029,7 @@ App::setResource('sms', function () { $secret = $dsn->getPassword(); return match ($dsn->getHost()) { - 'mock' => new Mock('', ''), // used for tests + 'mock' => new Mock($user, $secret), // used for tests 'twilio' => new Twilio($user, $secret), 'text-magic' => new TextMagic($user, $secret), 'telesign' => new Telesign($user, $secret), diff --git a/app/workers/messaging.php b/app/workers/messaging.php index d1173a8860..790ea00a74 100644 --- a/app/workers/messaging.php +++ b/app/workers/messaging.php @@ -35,7 +35,7 @@ class MessagingV1 extends Worker $secret = $dsn->getPassword(); $this->sms = match ($dsn->getHost()) { - 'mock' => new Mock('', ''), // used for tests + 'mock' => new Mock($user, $secret), // used for tests 'twilio' => new Twilio($user, $secret), 'text-magic' => new TextMagic($user, $secret), 'telesign' => new Telesign($user, $secret), diff --git a/composer.lock b/composer.lock index 02a11ebd96..0649639266 100644 --- a/composer.lock +++ b/composer.lock @@ -2409,6 +2409,7 @@ "shasum": "" }, "require": { + "ext-curl": "*", "php": ">=8.0.0" }, "require-dev": { From a247cc614f49dcd35624bda2b284460e229e759e Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 10 Oct 2022 11:28:26 +1300 Subject: [PATCH 4/5] Increase sleep --- tests/e2e/Services/Account/AccountCustomClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 82cc6d4fc0..932e535d81 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -716,7 +716,7 @@ class AccountCustomClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); - \sleep(2); + \sleep(5); $smsRequest = $this->getLastRequest(); From 702710aab141c874b8f97676ca59b8ed581c4647 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 10 Oct 2022 16:20:46 +1300 Subject: [PATCH 5/5] Fix import --- app/workers/messaging.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/workers/messaging.php b/app/workers/messaging.php index 8693c3da6f..8373d07ba3 100644 --- a/app/workers/messaging.php +++ b/app/workers/messaging.php @@ -2,7 +2,6 @@ use Appwrite\DSN\DSN; use Appwrite\Resque\Worker; -use Appwrite\SMS\Adapter; use Utopia\App; use Utopia\CLI\Console; use Utopia\Messaging\Adapter;