issue_2715_fix_broken_twitter_urls_job.rb 1.1 KB

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