communicate_facebook_job.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class CommunicateFacebookJob < ApplicationJob
  3. retry_on StandardError, attempts: 4, wait: lambda { |executions|
  4. executions * 120.seconds
  5. }
  6. def perform(article_id)
  7. article = Ticket::Article.find(article_id)
  8. # set retry count
  9. article.preferences['delivery_retry'] ||= 0
  10. article.preferences['delivery_retry'] += 1
  11. ticket = Ticket.lookup(id: article.ticket_id)
  12. log_error(article, "Can't find ticket.preferences for Ticket.find(#{article.ticket_id})") if !ticket.preferences
  13. log_error(article, "Can't find ticket.preferences['channel_id'] for Ticket.find(#{article.ticket_id})") if !ticket.preferences['channel_id']
  14. channel = Channel.lookup(id: ticket.preferences['channel_id'])
  15. log_error(article, "Channel.find(#{channel.id}) isn't a twitter channel!") if !channel.options[:adapter].match?(%r{\Afacebook}i)
  16. # check source object id
  17. if !ticket.preferences['channel_fb_object_id']
  18. log_error(article, "fb object id is missing in ticket.preferences['channel_fb_object_id'] for Ticket.find(#{ticket.id})")
  19. end
  20. # fill in_reply_to
  21. if article.in_reply_to.blank?
  22. article.in_reply_to = ticket.articles.first.message_id
  23. end
  24. begin
  25. facebook = Channel::Driver::Facebook.new
  26. post = facebook.send(
  27. channel.options,
  28. ticket.preferences[:channel_fb_object_id],
  29. {
  30. type: article.type.name,
  31. to: article.to,
  32. body: article.body,
  33. in_reply_to: article.in_reply_to,
  34. }
  35. )
  36. rescue => e
  37. log_error(article, e.message)
  38. return
  39. end
  40. if !post
  41. log_error(article, 'Got no post!')
  42. return
  43. end
  44. # fill article with post info
  45. article.from = post['from']['name']
  46. article.message_id = post['id']
  47. # set delivery status
  48. article.preferences['delivery_status_message'] = nil
  49. article.preferences['delivery_status'] = 'success'
  50. article.preferences['delivery_status_date'] = Time.zone.now
  51. article.save!
  52. Rails.logger.info "Send facebook to: '#{article.to}' (from #{article.from})"
  53. article
  54. end
  55. def log_error(local_record, message)
  56. local_record.preferences['delivery_status'] = 'fail'
  57. local_record.preferences['delivery_status_message'] = message.encode!('UTF-8', 'UTF-8', invalid: :replace, replace: '?')
  58. local_record.preferences['delivery_status_date'] = Time.zone.now
  59. local_record.save
  60. Rails.logger.error message
  61. if local_record.preferences['delivery_retry'] > 3
  62. Ticket::Article.create(
  63. ticket_id: local_record.ticket_id,
  64. content_type: 'text/plain',
  65. body: "Unable to send post: #{message}",
  66. internal: true,
  67. sender: Ticket::Article::Sender.find_by(name: 'System'),
  68. type: Ticket::Article::Type.find_by(name: 'note'),
  69. preferences: {
  70. delivery_article_id_related: local_record.id,
  71. delivery_message: true,
  72. },
  73. updated_by_id: 1,
  74. created_by_id: 1,
  75. )
  76. end
  77. raise message
  78. end
  79. end