Skip to content

[Notifier] Add new Pushy notifier bridge #53927

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

Merged
merged 1 commit into from
Feb 15, 2024
Merged
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
Add new Pushy notifier bridge
  • Loading branch information
stloyd committed Feb 14, 2024
commit b8f666df0e76a0120d5c05958c0fc8a72ae55322
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\PagerDuty\PagerDutyTransportFactory::class => 'notifier.transport_factory.pager-duty',
NotifierBridge\Plivo\PlivoTransportFactory::class => 'notifier.transport_factory.plivo',
NotifierBridge\Pushover\PushoverTransportFactory::class => 'notifier.transport_factory.pushover',
NotifierBridge\Pushy\PushyTransportFactory::class => 'notifier.transport_factory.pushy',
NotifierBridge\Redlink\RedlinkTransportFactory::class => 'notifier.transport_factory.redlink',
NotifierBridge\RingCentral\RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
NotifierBridge\RocketChat\RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
'ovh-cloud' => Bridge\OvhCloud\OvhCloudTransportFactory::class,
'plivo' => Bridge\Plivo\PlivoTransportFactory::class,
'pushover' => Bridge\Pushover\PushoverTransportFactory::class,
'pushy' => Bridge\Pushy\PushyTransportFactory::class,
'redlink' => Bridge\Redlink\RedlinkTransportFactory::class,
'ring-central' => Bridge\RingCentral\RingCentralTransportFactory::class,
'sendberry' => Bridge\Sendberry\SendberryTransportFactory::class,
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pushy/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pushy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pushy/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.1
---

* Add the bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Pushy\Enum;

/**
* @author Joseph Bielawski <stloyd@gmail.com>
*/
enum InterruptionLevel: string
{
case ACTIVE = 'active';
case CRITICAL = 'critical';
case PASSIVE = 'passive';
case TIME_SENSITIVE = 'time-sensitive';
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pushy/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
191 changes: 191 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pushy/PushyOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Pushy;

use Symfony\Component\Notifier\Bridge\Pushy\Enum\InterruptionLevel;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
use Symfony\Component\Notifier\Notification\Notification;

/**
* @author Joseph Bielawski <stloyd@gmail.com>
*
* @see https://pushy.me/docs/api/send-notifications
*/
final class PushyOptions implements MessageOptionsInterface
{
public function __construct(
private array $options = [],
) {
}

public static function fromNotification(Notification $notification): self
{
$options = new self();
$options->interruptionLevel(
match ($notification->getImportance()) {
Notification::IMPORTANCE_URGENT => InterruptionLevel::CRITICAL,
Notification::IMPORTANCE_HIGH => InterruptionLevel::TIME_SENSITIVE,
Notification::IMPORTANCE_MEDIUM => InterruptionLevel::ACTIVE,
Notification::IMPORTANCE_LOW => InterruptionLevel::PASSIVE,
}
);

return $options;
}

public function toArray(): array
{
return $this->options;
}

public function getRecipientId(): ?string
{
return $this->options['to'] ?? null;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @param string|string[] $to
*
* @return $this
*/
public function to(string|array $to): static
{
$this->options['to'] = $to;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function contentAvailable(bool $bool): static
{
$this->options['content_available'] = $bool;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function mutableContent(bool $bool): static
{
$this->options['mutable_content'] = $bool;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function ttl(int $seconds): static
{
if ($seconds > (86400 * 365)) {
throw new InvalidArgumentException('Pushy notification time to live cannot exceed 365 days.');
}

$this->options['time_to_live'] = $seconds;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function schedule(int $seconds): static
{
if (false === \DateTime::createFromFormat('U', $seconds)) {
throw new InvalidArgumentException('Pushy notification schedule time must be correct Unix timestamp.');
}

if (\DateTime::createFromFormat('U', $seconds) >= new \DateTime('+1 year')) {
throw new InvalidArgumentException('Pushy notification schedule time cannot exceed 1 year.');
}

$this->options['schedule'] = $seconds;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function collapseKey(string $collapseKey): static
{
if (32 < \strlen($collapseKey)) {
throw new InvalidArgumentException('Pushy notification collapse key cannot be longer than 32 characters.');
}

$this->options['collapse_key'] = $collapseKey;

return $this;
}

/**
* @return $this
*/
public function body(string $body): static
{
$this->options['notification']['body'] = $body;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function badge(int $badge): static
{
$this->options['notification']['badge'] = $badge;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function threadId(int $threadId): static
{
$this->options['notification']['thread_id'] = $threadId;

return $this;
}

/**
* @see https://pushy.me/docs/api/send-notifications#request-schema
*
* @return $this
*/
public function interruptionLevel(InterruptionLevel $interruptionLevel): static
{
$this->options['notification']['interruption_level'] = $interruptionLevel->value;

return $this;
}
}
95 changes: 95 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pushy/PushyTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Pushy;

use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Joseph Bielawski <stloyd@gmail.com>
*/
final class PushyTransport extends AbstractTransport
{
protected const HOST = 'api.pushy.me';

public function __construct(
#[\SensitiveParameter] private readonly string $apiKey,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof PushyOptions);
}

public function __toString(): string
{
return sprintf('pushy://%s', $this->getEndpoint());
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof PushMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
}

$options = $message->getOptions()?->toArray() ?? [];
$options['data'] = $message->getContent();
$options['notification']['title'] = $message->getSubject();
$options['to'] ??= $message->getRecipientId();

if (!$options['to']) {
throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__));
}

$endpoint = sprintf('https://%s?api_key=%s', $this->getEndpoint(), $this->apiKey);
$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => array_filter($options),
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Pushy server.', $response, 0, $e);
}

if (200 !== $statusCode) {
throw new TransportException(sprintf('Unable to send the Pushy push notification: "%s".', $response->getContent(false)), $response);
}

$result = $response->toArray(false);

if (!isset($result['id'])) {
throw new TransportException(sprintf('Unable to find the message ID within the Pushy response: "%s".', $response->getContent(false)), $response);
}

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($result['id']);

return $sentMessage;
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy