diff --git a/app/services/publisher.rb b/app/services/batch_publisher.rb similarity index 59% rename from app/services/publisher.rb rename to app/services/batch_publisher.rb index efc9ad8b..74eb406b 100644 --- a/app/services/publisher.rb +++ b/app/services/batch_publisher.rb @@ -1,7 +1,7 @@ # Takes a collection of posts, publishes each, updates post status. # Does not interrupt on publication error. # -class Publisher +class BatchPublisher include Logging attr_reader :posts, :freefeed_client @@ -14,20 +14,13 @@ def initialize(posts:, freefeed_client:) def publish logger.info("publishing #{TextHelpers.pluralize(pending_posts.count, "posts")}") - pending_posts.each do |post| - publish_post(post) - end - end - - private + posts.each do |post| + logger.info("publishing post: #{post.id}") - def pending_posts - @pending_posts ||= posts.filter(&:pending?) - end - - # :reek:UnusedParameters - def publish_post(post) - logger.info("publishing post: #{post.id}") - # TBD + post.with_lock do + next unless post.reload.enqueued? + PostPublisher.new(post: post, freefeed_client: freefeed_client).publish + end + end end end diff --git a/app/services/post_publisher.rb b/app/services/post_publisher.rb new file mode 100644 index 00000000..b40eacdd --- /dev/null +++ b/app/services/post_publisher.rb @@ -0,0 +1,60 @@ +class PostPublisher + include Logging + + attr_reader :post, :freefeed_client + + def initialize(post:, freefeed_client:) + @post = post + @freefeed_client = freefeed_client + end + + def publish + attachment_ids = create_attachments + post_id = create_post(attachment_ids) + post.update(freefeed_post_id: post_id) + create_comments(post_id) + post.success! + logger.info("---> new post URL: #{post.permalink}") + rescue StandardError + post.fail! + # TBD: Report error + end + + private + + def create_post(attachment_ids) + response = freefeed_client.create_post( + post: { + body: post.text, + attachments: attachment_ids + }, + meta: { + feeds: [post.feed.name] + } + ) + + response.parse.dig("posts", "id") + end + + def create_comments(post_id) + post.comments.each do |comment| + freefeed_client.create_comment( + comment: { + body: comment, + postId: post_id + } + ) + end + end + + def create_attachments + post.attachments.map { |url| create_attachment(url) } + end + + def create_attachment(url) + Downloader.call(url) do |io, content_type| + response = freefeed_client.create_attachment(io, content_type: content_type) + response.parse.fetch("attachments").fetch("id") + end + end +end diff --git a/lib/freefeed/downloader.rb b/lib/freefeed/downloader.rb index 75fc94df..88115002 100644 --- a/lib/freefeed/downloader.rb +++ b/lib/freefeed/downloader.rb @@ -1,6 +1,6 @@ module Freefeed class Downloader - attr_reader :url + attr_reader :url, :http_client def initialize(url:, http_client: nil) @url = url @@ -30,9 +30,5 @@ def fetch_url # TBD: Report download error nil end - - def http_client - @http_client ||= HTTP.follow(max_hops: max_hops).timeout(timeout_seconds) - end end end diff --git a/lib/freefeed/v1/attachments.rb b/lib/freefeed/v1/attachments.rb index 4de3bdb8..1a6662c3 100644 --- a/lib/freefeed/v1/attachments.rb +++ b/lib/freefeed/v1/attachments.rb @@ -9,7 +9,7 @@ def create_attachment(source, content_type: nil) end def create_attachment_from(url:, **) - ::Freefeed::Downloader.new(url: url, **).call do |io, content_type| + ::Freefeed::Downloader.new(url: url, http_client: http_client, **).call do |io, content_type| response = create_attachment(io, content_type: content_type) response.parse.fetch("attachments").fetch("id") end diff --git a/spec/services/batch_publisher_spec.rb b/spec/services/batch_publisher_spec.rb new file mode 100644 index 00000000..1c689ea1 --- /dev/null +++ b/spec/services/batch_publisher_spec.rb @@ -0,0 +1,5 @@ +require "rails_helper" + +RSpec.describe BatchPublisher do + # TBD +end diff --git a/spec/services/post_publisher_spec.rb b/spec/services/post_publisher_spec.rb new file mode 100644 index 00000000..2cc33cb6 --- /dev/null +++ b/spec/services/post_publisher_spec.rb @@ -0,0 +1,5 @@ +require "rails_helper" + +RSpec.describe PostPublisher do + # TBD +end