From 6569060ba033fee4ae999a2bc8967c063e371526 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Tue, 30 Jan 2024 20:29:51 +1100 Subject: [PATCH] De-syndicate posts --- lib/adamantium/client/mastodon.rb | 21 +++++++++++++++ lib/adamantium/syndication/mastodon.rb | 10 +++++++ slices/admin/actions/posts/de_syndicate.rb | 15 +++++++++++ slices/admin/commands/posts/de_syndicate.rb | 29 +++++++++++++++++++++ slices/admin/config/routes.rb | 1 + slices/admin/templates/posts/show.html.slim | 2 ++ 6 files changed, 78 insertions(+) create mode 100644 slices/admin/actions/posts/de_syndicate.rb create mode 100644 slices/admin/commands/posts/de_syndicate.rb diff --git a/lib/adamantium/client/mastodon.rb b/lib/adamantium/client/mastodon.rb index 1d94d24..9f204cc 100644 --- a/lib/adamantium/client/mastodon.rb +++ b/lib/adamantium/client/mastodon.rb @@ -78,6 +78,27 @@ module Adamantium JSON.parse(response.body, symbolize_names: true).fetch(:id, nil) end + def remove_post(post_id:) + unless settings.mastodon_token && settings.mastodon_server + return Failure(:no_mastodon_credentials) + end + + mastodon_token = settings.mastodon_token + mastodon_server = settings.mastodon_server.split("@").first + response = HTTParty.delete("#{mastodon_server}api/v1/statuses/#{post_id}", { + headers: { + "Idempotency-Key": key, + Authorization: "Bearer #{mastodon_token}" + } + }) + + if response.code >= 200 && response.code < 300 + Success(post_id) + else + Failure(response.message) + end + end + private def sanitze_post(content) diff --git a/lib/adamantium/syndication/mastodon.rb b/lib/adamantium/syndication/mastodon.rb index ab9fda3..0975842 100644 --- a/lib/adamantium/syndication/mastodon.rb +++ b/lib/adamantium/syndication/mastodon.rb @@ -20,6 +20,16 @@ module Adamantium Failure(:failed_to_syndicate) end end + + def de_syndicate(post_id:) + response = mastodon_client.remove_post(post_id: post_id) + + if response.success? + Success(response.value!) + else + Failure(:failed_to_de_syndicate) + end + end end end end diff --git a/slices/admin/actions/posts/de_syndicate.rb b/slices/admin/actions/posts/de_syndicate.rb new file mode 100644 index 0000000..c446853 --- /dev/null +++ b/slices/admin/actions/posts/de_syndicate.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Admin + module Actions + module Posts + class DeSyndicate < Admin::Action + include Deps["commands.posts.de_syndicate"] + + def handle(req, res) + de_syndicate.call(post_id: req.params[:id], target: req.params[:target]) + end + end + end + end +end diff --git a/slices/admin/commands/posts/de_syndicate.rb b/slices/admin/commands/posts/de_syndicate.rb new file mode 100644 index 0000000..6fb1ee1 --- /dev/null +++ b/slices/admin/commands/posts/de_syndicate.rb @@ -0,0 +1,29 @@ +require "readability" +require "down" + +module Admin + module Commands + module Posts + class DeSyndicate + include Dry::Monads[:result] + include Deps["repos.post_repo", "syndication.mastodon"] + + def call(post_id:, target:) + post = post_repo.find(id: post_id) + + request = if target.to_sym == :mastodon + masto_id = post.syndication_sources[target].split("/").last + mastodon.de_syndicate(post_id: masto_id) + end + + if request.success? + post.syndication_sources.delete(target) + post_repo.update(post_id, post) + end + + Success() + end + end + end + end +end diff --git a/slices/admin/config/routes.rb b/slices/admin/config/routes.rb index 832226f..ce5ea7f 100644 --- a/slices/admin/config/routes.rb +++ b/slices/admin/config/routes.rb @@ -49,6 +49,7 @@ module Admin post "/posts/:id/publish", to: Auth.call(action: "posts.publish") get "/posts/:id", to: Auth.call(action: "posts.show") post "/posts/:id/syndicate/add_source", to: Auth.call(action: "posts.add_syndication_source") + post "/posts/:id/de_syndicate/:target", to: Auth.call(action: "posts.de_syndicate") post "/posts/:id/syndicate/:target", to: Auth.call(action: "posts.syndicate") post "/post/:id/update", to: Auth.call(action: "posts.update") diff --git a/slices/admin/templates/posts/show.html.slim b/slices/admin/templates/posts/show.html.slim index 95be94f..a1af05c 100644 --- a/slices/admin/templates/posts/show.html.slim +++ b/slices/admin/templates/posts/show.html.slim @@ -6,6 +6,8 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark: li a href=url = name + = " — " + button hx-swap="afterend" hx-post="/admin/posts/#{post.id}/de_syndicate/#{name}" de-syndicate form hx-post="/admin/posts/#{post.id}/syndicate/add_source" span class="mr-2" label for="syndication_source_name"