8000 Add Mastodon Notifier · symfony/symfony@d097a63 · GitHub
[go: up one dir, main page]

Skip to content

Commit d097a63

Browse files
qdequippenicolas-grekas
authored andcommitted
Add Mastodon Notifier
1 parent 46df55e commit d097a63

19 files changed

+565
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
use Symfony\Component\Notifier\Bridge\LineNotify\LineNotifyTransportFactory;
153153
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
154154
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory as MailjetNotifierTransportFactory;
155+
use Symfony\Component\Notifier\Bridge\Mastodon\MastodonTransportFactory;
155156
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
156157
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
157158
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransport;
@@ -2580,6 +2581,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25802581
LineNotifyTransportFactory::class => 'notifier.transport_factory.line-notify',
25812582
LinkedInTransportFactory::class => 'notifier.transport_factory.linked-in',
25822583
MailjetNotifierTransportFactory::class => 'notifier.transport_factory.mailjet',
2584+
MastodonTransportFactory::class => 'notifier.transport_factory.mastodon',
25832585
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
25842586
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
25852587
MessageBirdTransport::class => 'notifier.transport_factory.message-bird',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Component\Notifier\Bridge\LineNotify\LineNotifyTransportFactory;
3838
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
3939
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTr 9E88 ansportFactory;
40+
use Symfony\Component\Notifier\Bridge\Mastodon\MastodonTransportFactory;
4041
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
4142
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
4243
use Symfony\Component\Notifier\Bridge\MessageBird\MessageBirdTransportFactory;
@@ -321,5 +322,9 @@
321322
->set('notifier.transport_factory.line-notify', LineNotifyTransportFactory::class)
322323
->parent('notifier.transport_factory.abstract')
323324
->tag('chatter.transport_factory')
325+
326+
->set('notifier.transport_factory.mastodon', MastodonTransportFactory::class)
327+
->parent('notifier.transport_factory.abstract')
328+
->tag('chatter.transport_factory')
324329
;
325330
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.3
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Mastodon;
13+
14+
use Symfony\Component\Mime\Part\File;
15+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
16+
17+
final class MastodonOptions implements MessageOptionsInterface
18+
{
19+
public function __construct(
20+
private array $options = [],
21+
) {
22+
}
23+
24+
public function toArray(): array
25+
{
26+
return $this->options;
27+
}
28+
29+
public function getRecipientId(): ?string
30+
{
31+
return null;
32+
}
33+
34+
/**
35+
* @param string[] $choices
36+
*/
37+
public function poll(array $choices, int $expiresIn): self
38+
{
39+
$this->options['poll'] = [
40+
'options' => $choices,
41+
'expires_in' => $expiresIn,
42+
];
43+
44+
return $this;
45+
}
46+
47+
public function attachMedia(File $file, File $thumbnail = null, string $description = null, string $focus = null): self
48+
{
49+
$this->options['attach'][] = [
50+
'file' => $file,
51+
'thumbnail' => $thumbnail,
52+
'description' => $description,
53+
'focus' => $focus,
54+
];
55+
56+
return $this;
57+
}
58+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Mastodon;
13+
14+
use Symfony\Component\Mime\Part\DataPart;
15+
use Symfony\Component\Mime\Part\File;
16+
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
17+
use Symfony\Component\Notifier\Exception\RuntimeException;
18+
use Symfony\Component\Notifier\Exception\TransportException;
19+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
20+
use Symfony\Component\Notifier\Message\ChatMessage;
21+
use Symfony\Component\Notifier\Message\MessageInterface;
22+
use Symfony\Component\Notifier\Message\SentMessage;
23+
use Symfony\Component\Notifier\Transport\AbstractTransport;
24+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
25+
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
26+
use Symfony\Contracts\HttpClient\HttpClientInterface;
27+
use Symfony\Contracts\HttpClient\ResponseInterface;
28+
29+
/**
30+
* @author Quentin Dequippe <quentin@dequippe.tech>
31+
*
32+
* @see https://docs.joinmastodon.org
33+
*/
34+
final class MastodonTransport extends AbstractTransport
35+
{
36+
public function __construct(#[\SensitiveParameter] private readonly string $accessToken, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
37+
{
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('mastodon://%s', $this->getEndpoint());
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof MastodonOptions);
49+
}
50+
51+
public function request(string $method, string $url, array $options): ResponseInterface
52+
{
53+
$url = sprintf('https://%s%s', $this->getEndpoint(), $url);
54+
55+
$options['auth_bearer'] = $this->accessToken;
56+
57+
return $this->client->request($method, $url, $options);
58+
}
59+
60+
/**
61+
* @see https://docs.joinmastodon.org/methods/statuses/
62+
*/
63+
protected function doSend(MessageInterface $message): SentMessage
64+
{
65+
if (!$message instanceof ChatMessage) {
66+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
67+
}
68+
69+
$options = $message->getOptions()?->toArray() ?? [];
70+
$options['status'] = $message->getSubject();
71+
$response = null;
72+
73+
try {
74+
if (isset($options['attach'])) {
75+
$options['media_ids'] = $this->uploadMedia($options['attach']);
76+
unset($options['attach']);
77+
}
78+
79+
$response = $this->request('POST', '/api/v1/statuses', ['json' => $options]);
80+
$statusCode = $response->getStatusCode();
81+
$result = $response->toArray(false);
82+
} catch (ExceptionInterface $e) {
83+
if (null !== $response) {
84+
throw new TransportException($e->getMessage(), $response, 0, $e);
85+
}
86+
87+
throw new RuntimeException($e->getMessage(), 0, $e);
88+
}
89+
90+
if (200 !== $statusCode) {
91+
throw new TransportException(sprintf('Unable to post the Mastodon message: "%s" (%s).', $result['error_description'], $result['error']), $response);
92+
}
93+
94+
$sentMessage = new SentMessage($message, (string) $this);
95+
$sentMessage->setMessageId($result['id']);
96+
97+
return $sentMessage;
98+
}
99+
100+
/**
101+
* @param array<array{file: File, thumbnail: File|null, description: string|null, focus: string}> $media
102+
*/
103+
private function uploadMedia(array $media): array
104+
{
105+
$responses = [];
106+
107+
foreach ($media as [
108+
'file' => $file,
109+
'thumbnail' => $thumbnail,
110+
'description' => $description,
111+
'focus' => $focus,
112+
]) {
113+
$formDataPart = new FormDataPart(array_filter([
114+
'file' => new DataPart($file),
115+
'thumbnail' => $thumbnail ? new DataPart($thumbnail) : null,
116+
'description' => $description,
117+
'focus' => $focus,
118+
]));
119+
120+
$headers = [];
121+
foreach ($formDataPart->getPreparedHeaders()->all() as $header) {
122+
$headers[] = $header->toString();
123+
}
124+
125+
$responses[] = $this->request('POST', '/api/v2/media', [
126+
'headers' => $headers,
127+
'body' => $formDataPart->bodyToIterable(),
128+
]);
129+
}
130+
131+
$mediaIds = [];
132+
133+
try {
134+
foreach ($responses as $i => $response) {
135+
unset($responses[$i]);
136+
$result = $response->toArray(false);
137+
138+
if (300 <= $response->getStatusCode()) {
139+
throw new TransportException(sprintf('Unable to upload media as attachment: "%s" (%s).', $result['error_description'], $result['error']), $response);
140+
}
141+
142+
$mediaIds[] = $result['id'];
143+
}
144+
} finally {
145+
foreach ($responses as $response) {
146+
$response->cancel();
147+
}
148+
}
149+
150+
return $mediaIds;
151+
}
152+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Mastodon;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author Quentin Dequippe <quentin@dequippe.tech>
20+
*/
21+
final class MastodonTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): MastodonTransport
24+
{
25+
$scheme = $dsn->getScheme();
26+
27+
if ('mastodon' !== $scheme) {
28+
throw new UnsupportedSchemeException($dsn, 'mastodon', $this->getSupportedSchemes());
29+
}
30+
31+
$token = $this->getUser($dsn);
32+
$host = $dsn->getHost();
33+
$port = $dsn->getPort();
34+
35+
return (new MastodonTransport($token, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
}
37+
38+
protected function getSupportedSchemes(): array
39+
{
40+
return ['mastodon'];
41+
}
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Mastodon Notifier
2+
=================
3+
4+
Provides Mastodon integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
MASTODON_DSN=mastodon://ACCESS_TOKEN@HOST
11+
```
12+
13+
where:
14+
- `ACCESS_TOKEN` is your Mastodon access token
15+
- `HOST` is your Mastodon host
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)
0