issue_2715_fix_broken_twitter_urls_job.rb 1012 B

1234567891011121314151617181920212223242526272829
  1. class Issue2715FixBrokenTwitterUrlsJob < ApplicationJob
  2. STATUS_TEMPLATE = 'https://twitter.com/_/status/%<message_id>s'.freeze
  3. DM_TEMPLATE = 'https://twitter.com/messages/%<recipient_id>s-%<sender_id>s'.freeze
  4. def perform
  5. Ticket::Article.joins(:type)
  6. .where(ticket_article_types: { name: ['twitter status', 'twitter direct-message'] })
  7. .order(created_at: :desc)
  8. .limit(10_000)
  9. .find_each { |article| fix_broken_links(article) }
  10. end
  11. private
  12. def fix_broken_links(article)
  13. type = Ticket::Article::Type.lookup(id: article.type_id).name
  14. article.preferences[:links]&.each do |link|
  15. link[:url] = case type
  16. when 'twitter status'
  17. STATUS_TEMPLATE % article.attributes.symbolize_keys
  18. when 'twitter direct-message'
  19. DM_TEMPLATE % article.preferences[:twitter].symbolize_keys
  20. end
  21. end
  22. article.save!
  23. end
  24. end