diff --git a/README.md b/README.md index 2daff87..19781cc 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Delivery methods we officially support: Bulk delivery methods we support: +* [Bluesky](docs/bulk_delivery_methods/bluesky.md) * [Discord](docs/bulk_delivery_methods/discord.md) * [Slack](docs/bulk_delivery_methods/slack.md) * [Webhook](docs/bulk_delivery_methods/webhook.md) @@ -427,8 +428,8 @@ Recipients can also be computed inside a notifier: class NewCommentNotifier < ApplicationNotifier recipients ->{ params[:record].thread.all_authors } - # or - recipients do + # or + recipients do params[:record].thread.all_authors end @@ -526,6 +527,7 @@ Individual delivery methods: Bulk delivery methods: +* [Bluesky](docs/bulk_delivery_methods/bluesky.md) * [Discord](docs/bulk_delivery_methods/discord.md) * [Slack](docs/bulk_delivery_methods/slack.md) * [Webhook](docs/bulk_delivery_methods/webhook.md) diff --git a/docs/bulk_delivery_methods/bluesky.md b/docs/bulk_delivery_methods/bluesky.md new file mode 100644 index 0000000..e3790ae --- /dev/null +++ b/docs/bulk_delivery_methods/bluesky.md @@ -0,0 +1,21 @@ +# Bluesky Bulk Delivery Method + +Create a Bluesky post. + +## Usage + +```ruby +class CommentNotification + bulk_deliver_by :bluesky do |config| + config.identifier = "username" + config.password = "password" + config.json = -> { + { + text: "Hello world!", + createdAt: Time.current.iso8601 + # ... + } + } + end +end +``` diff --git a/lib/noticed.rb b/lib/noticed.rb index 5cff19a..18c8d8e 100644 --- a/lib/noticed.rb +++ b/lib/noticed.rb @@ -20,6 +20,7 @@ def self.deprecator # :nodoc: autoload :Translation, "noticed/translation" module BulkDeliveryMethods + autoload :Bluesky, "noticed/bulk_delivery_methods/bluesky" autoload :Discord, "noticed/bulk_delivery_methods/discord" autoload :Slack, "noticed/bulk_delivery_methods/slack" autoload :Test, "noticed/bulk_delivery_methods/test" diff --git a/lib/noticed/bulk_delivery_methods/bluesky.rb b/lib/noticed/bulk_delivery_methods/bluesky.rb new file mode 100644 index 0000000..06c1b52 --- /dev/null +++ b/lib/noticed/bulk_delivery_methods/bluesky.rb @@ -0,0 +1,49 @@ +module Noticed + module BulkDeliveryMethods + class Bluesky < BulkDeliveryMethod + required_options :identifier, :password, :json + + # bulk_deliver_by :bluesky do |config| + # config.identifier = ENV["BLUESKY_ID"] + # config.password = ENV["BLUESKY_PASSWORD"] + # config.json = {text: "...", createdAt: "..."} + # end + + def deliver + Rails.logger.debug(evaluate_option(:json)) + post_request( + "https://#{host}/xrpc/com.atproto.repo.createRecord", + headers: {"Authorization" => "Bearer #{token}"}, + json: { + repo: identifier, + collection: "app.bsky.feed.post", + record: evaluate_option(:json) + }, + ) + end + + def token + start_session.dig("accessJwt") + end + + def start_session + response = post_request( + "https://#{host}/xrpc/com.atproto.server.createSession", + json: { + identifier: identifier, + password: evaluate_option(:password) + } + ) + JSON.parse(response.body) + end + + def host + @host ||= evaluate_option(:host) || "bsky.social" + end + + def identifier + @identifier ||= evaluate_option(:identifier) + end + end + end +end