trabajo-afectivo/app/jobs/issue_2715_fix_broken_twitter_urls_job.rb

30 lines
1,012 B
Ruby
Raw Normal View History

class Issue2715FixBrokenTwitterUrlsJob < ApplicationJob
STATUS_TEMPLATE = 'https://twitter.com/_/status/%<message_id>s'.freeze
DM_TEMPLATE = 'https://twitter.com/messages/%<recipient_id>s-%<sender_id>s'.freeze
def perform
Ticket::Article.joins(:type)
.where(ticket_article_types: { name: ['twitter status', 'twitter direct-message'] })
.order(created_at: :desc)
.limit(10_000)
.find_each { |article| fix_broken_links(article) }
end
private
def fix_broken_links(article)
type = Ticket::Article::Type.lookup(id: article.type_id).name
article.preferences[:links]&.each do |link|
link[:url] = case type
when 'twitter status'
STATUS_TEMPLATE % article.attributes.symbolize_keys
when 'twitter direct-message'
DM_TEMPLATE % article.preferences[:twitter].symbolize_keys
end
end
article.save!
end
end