Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat push #19

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.idea
vendor
.phpunit.result.cache
Makefile
.envrc
.env
*.p8
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "9.5.*",
"phpmailer/phpmailer": "6.6.*",
"phpunit/phpunit": "^9.6",
"phpmailer/phpmailer": "^6.8",
"laravel/pint": "^1.2"
},
"config": {
Expand Down
49 changes: 26 additions & 23 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
- ./src:/usr/local/src/src
- ./tests:/usr/local/src/tests
- ./phpunit.xml:/usr/local/src/phpunit.xml
env_file:
- .env

maildev:
image: appwrite/mailcatcher:1.0.0
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnFailure="true"
>
<testsuites>
<testsuite name="unit">
Expand Down
6 changes: 4 additions & 2 deletions src/Utopia/Messaging/Adapters/Email/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ public function getMaxMessagesPerRequest(): int
*/
protected function process(Email $message): string
{
return $this->request(
$response = $this->request(
method: 'POST',
url: "https://api.mailgun.net/v3/{$this->domain}/messages",
headers: [
'Authorization: Basic '.base64_encode('api:'.$this->apiKey),
],
body: \http_build_query([
'from' => $message->getFrom(),
'to' => \implode(',', $message->getTo()),
'from' => $message->getFrom(),
'subject' => $message->getSubject(),
'text' => $message->isHtml() ? null : $message->getContent(),
'html' => $message->isHtml() ? $message->getContent() : null,
]),
);

return $response;
}
}
4 changes: 1 addition & 3 deletions src/Utopia/Messaging/Adapters/Email/Sendgrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

class Sendgrid extends EmailAdapter
{
public function __construct(
private string $apiKey,
) {
public function __construct(private string $apiKey) {
}

public function getName(): string
Expand Down
95 changes: 95 additions & 0 deletions src/Utopia/Messaging/Adapters/Push/APNS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Utopia\Messaging\Adapters\Push;

use Utopia\Messaging\Adapters\Push as PushAdapter;
use Utopia\Messaging\Messages\Push;

class APNS extends PushAdapter
{
private $authKey;
private $authKeyId;
private $teamId;
private $bundleId;
private $endpoint;

public function __construct(string $authKey, string $authKeyId, string $teamId, string $bundleId, string $endpoint)
{
$this->authKey = $authKey;
$this->authKeyId = $authKeyId;
$this->teamId = $teamId;
$this->bundleId = $bundleId;
$this->endpoint = $endpoint;
}

public function getName(): string
{
return 'APNS';
}


public function getMaxMessagesPerRequest(): int
{
return 1000;
}

public function process(Push $message): string
{
$headers = [
'authorization: bearer ' . $this->generateJwt(),
'apns-topic: ' . $this->bundleId,
];

$payload = json_encode([
'aps' => [
'alert' => [
'title' => $message->getTitle(),
'body' => $message->getBody(),
],
'badge' => $message->getBadge(),
'sound' => $message->getSound(),
'data' => $message->getData(),
],
]);

// Assuming the 'to' array contains device tokens for the push notification recipients.
foreach ($message->getTo() as $to) {
$url = $this->endpoint . '/3/device/' . $to;
$response = $this->request('POST', $url, $headers, $payload);

// You might want to handle each response here, for instance, logging failures
}

// This example simply returns the last response, adjust as needed
return $response;
}

private function generateJwt(): string
{
$header = json_encode(['alg' => 'ES256', 'kid' => $this->authKeyId]);
$claims = json_encode([
'iss' => $this->teamId,
'iat' => time(),
]);

$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header));
$base64UrlClaims = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($claims));

$privateKeyResource = openssl_pkey_get_private(file_get_contents($this->authKey));
if (!$privateKeyResource) {
throw new \Exception('Invalid private key');
}

$signature = '';
$success = openssl_sign("$base64UrlHeader.$base64UrlClaims", $signature, $privateKeyResource, OPENSSL_ALGO_SHA256);

if (!$success) {
throw new \Exception('Failed to sign JWT');
}

$base64UrlSignature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature));

return "$base64UrlHeader.$base64UrlClaims.$base64UrlSignature";
}

}
53 changes: 0 additions & 53 deletions src/Utopia/Messaging/Adapters/SMS/TwilioNotify.php

This file was deleted.

7 changes: 7 additions & 0 deletions src/Utopia/Messaging/Entities/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Utopia\Messaging\Adapters\Push\Entities;

abstract class Entity {
protected $id;
}
Loading