revolution/laravel-notification-mastodon

Mastodon 的 Laravel 通知

3.2.2 2024-09-16 08:52 UTC

This package is auto-updated.

Last update: 2024-09-16 08:54:19 UTC


README

要求

  • PHP >= 8.1
  • Laravel >= 10.0

安装

Composer

composer require revolution/laravel-notification-mastodon

配置

设置默认 domaintoken

config/services.php

    'mastodon' => [
        'domain'        => env('MASTODON_DOMAIN'),
        'token'         => env('MASTODON_TOKEN'),
    ],

.env

MASTODON_DOMAIN=https://example.com
MASTODON_TOKEN=

TOKEN?

前往你的 Mastodon 偏好设置页面。

使用方法

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

use Revolution\Laravel\Notification\Mastodon\MastodonChannel;
use Revolution\Laravel\Notification\Mastodon\MastodonMessage;

class MastodonNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $status;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($status)
    {
        $this->status = $status;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     *
     * @return array
     */
    public function via($notifiable)
    {
        return [MastodonChannel::class];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed $notifiable
     *
     * @return MastodonMessage
     */
    public function toMastodon($notifiable)
    {
        return MastodonMessage::create($this->status);
    }
}

发送到特定账户

use Illuminate\Support\Facades\Notification;
use Revolution\Laravel\Notification\Mastodon\MastodonRoute;

Notification::route('mastodon', MastodonRoute::to(config('services.mastodon.domain'), config('services.mastodon.token')))
            ->notify(new MastodonNotification('test'));

发送到用户的账户

通过 https://github.com/kawax/socialite-mastodon 获取 token

use Illuminate\Notifications\Notifiable;
use Revolution\Laravel\Notification\Mastodon\MastodonRoute;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForMastodon($notification): MastodonRoute
    {
        return MastodonRoute::to(domain: $this->domain, token: $this->token);
    }
}
$user->notify(new MastodonNotification('test'));

设置选项

https://docs.joinmastodon.org/methods/statuses/

    public function toMastodon($notifiable)
    {
        $options = [
            'visibility' => 'unlisted',
        ];

        return MastodonMessage::create($this->status)
                              ->options($options);
    }

许可证

MIT
版权所有 kawax