Refactor syndication clients for testability
This commit is contained in:
@@ -2,7 +2,7 @@ require "securerandom"
|
||||
|
||||
module Adamantium
|
||||
class MicropubRequestParser
|
||||
include Deps["logger", "post_utilities.slugify", "repos.post_repo"]
|
||||
include Deps["post_utilities.slugify", "repos.post_repo"]
|
||||
|
||||
def call(params:)
|
||||
return nil if params.key?(:action)
|
||||
@@ -83,8 +83,6 @@ module Adamantium
|
||||
new_params[:url] = params[:"bookmark-of"]
|
||||
new_params[:slug] = slug(name: new_params[:name], default_slug: params[:slug])
|
||||
|
||||
logger.info(new_params)
|
||||
|
||||
new_params
|
||||
end
|
||||
end
|
||||
|
@@ -1,72 +1,23 @@
|
||||
require "digest"
|
||||
require "dry/monads"
|
||||
require "httparty"
|
||||
require "tempfile"
|
||||
require "open-uri"
|
||||
|
||||
module Adamantium
|
||||
module Syndication
|
||||
class Mastodon
|
||||
include Dry::Monads[:result]
|
||||
include Deps["settings", "logger"]
|
||||
include Deps[mastodon_client: "clients.mastodon"]
|
||||
|
||||
def call(post:)
|
||||
unless settings.mastodon_token && settings.mastodon_server
|
||||
logger.info("No Mastodon credentials")
|
||||
return Failure(:no_mastodon_credentials)
|
||||
end
|
||||
|
||||
content = if post[:name]
|
||||
"#{post[:name]} — #{settings.micropub_site_url}/post/#{post[:slug]}"
|
||||
else
|
||||
post[:content]
|
||||
end
|
||||
|
||||
tags = post[:category].map { |tag| "##{tag}" }.join(" ")
|
||||
text_with_tags = "#{content} #{tags}"
|
||||
|
||||
key = Digest::MD5.hexdigest text_with_tags
|
||||
mastodon_token = settings.mastodon_token
|
||||
mastodon_server = settings.mastodon_server.split("@").first
|
||||
logger.info("Photos: #{post[:photos].inspect}")
|
||||
media_ids = post[:photos]&.map do |photo|
|
||||
file = Tempfile.new(SecureRandom.uuid)
|
||||
file.write(URI.open(photo[:value]).read)
|
||||
file.rewind
|
||||
response = HTTParty.post("#{mastodon_server}api/v2/media", {
|
||||
headers: {
|
||||
Authorization: "Bearer #{mastodon_token}"
|
||||
},
|
||||
multipart: true,
|
||||
body: {
|
||||
file: file,
|
||||
description: photo[:alt]
|
||||
}
|
||||
})
|
||||
file.close
|
||||
file.unlink
|
||||
logger.info(response.code)
|
||||
|
||||
JSON.parse(response.body, symbolize_names: true).fetch(:id, nil)
|
||||
mastodon_client.upload_media(photo: photo)
|
||||
end&.compact
|
||||
|
||||
response = HTTParty.post("#{mastodon_server}api/v1/statuses", {
|
||||
headers: {
|
||||
"Idempotency-Key": key,
|
||||
Authorization: "Bearer #{mastodon_token}"
|
||||
},
|
||||
body: {
|
||||
status: text_with_tags,
|
||||
media_ids: media_ids
|
||||
}
|
||||
})
|
||||
response = mastodon_client.create_post(post: post, media_ids: media_ids)
|
||||
|
||||
if response.code >= 200 && response.code < 300
|
||||
status = JSON.parse(response.body, symbolize_names: true)
|
||||
Success("#{mastodon_server}/#{status[:id]}")
|
||||
if response.success?
|
||||
Success(response.value!)
|
||||
else
|
||||
logger.info("Failed to syndicate to Mastodon: #{response.message}")
|
||||
Failure(response.message)
|
||||
Failure(:failed_to_syndicate)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user