Skip to content

Commit

Permalink
Extract post publisher from batch publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
dreikanter committed Oct 20, 2024
1 parent 527b872 commit 8e6895b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 21 deletions.
23 changes: 8 additions & 15 deletions app/services/publisher.rb → app/services/batch_publisher.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
60 changes: 60 additions & 0 deletions app/services/post_publisher.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 1 addition & 5 deletions lib/freefeed/downloader.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Freefeed
class Downloader
attr_reader :url
attr_reader :url, :http_client

def initialize(url:, http_client: nil)
@url = url
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/freefeed/v1/attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/services/batch_publisher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe BatchPublisher do
# TBD
end
5 changes: 5 additions & 0 deletions spec/services/post_publisher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe PostPublisher do
# TBD
end

0 comments on commit 8e6895b

Please sign in to comment.