communicate_facebook_job.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2012-2024 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(#{ticket.preferences['channel_id']}) does not exist anymore!") if channel.blank?
  16. log_error(article, "Channel.find(#{channel.id}) isn't a facebook channel!") if !channel.options[:adapter].match?(%r{\Afacebook}i)
  17. # check source object id
  18. if !ticket.preferences['channel_fb_object_id']
  19. log_error(article, "fb object id is missing in ticket.preferences['channel_fb_object_id'] for Ticket.find(#{ticket.id})")
  20. end
  21. # fill in_reply_to
  22. if article.in_reply_to.blank?
  23. article.in_reply_to = ticket.articles.first.message_id
  24. end
  25. begin
  26. facebook = Channel::Driver::Facebook.new
  27. post = facebook.deliver(
  28. channel.options,
  29. ticket.preferences[:channel_fb_object_id],
  30. {
  31. type: article.type.name,
  32. to: article.to,
  33. body: article.body,
  34. in_reply_to: article.in_reply_to,
  35. }
  36. )
  37. rescue => e
  38. log_error(article, e.message)
  39. return
  40. end
  41. if !post
  42. log_error(article, 'Got no post!')
  43. return
  44. end
  45. # fill article with post info
  46. article.from = post['from']['name']
  47. article.message_id = post['id']
  48. # set delivery status
  49. article.preferences['delivery_status_message'] = nil
  50. article.preferences['delivery_status'] = 'success'
  51. article.preferences['delivery_status_date'] = Time.zone.now
  52. article.save!
  53. Rails.logger.info "Send facebook to: '#{article.to}' (from #{article.from})"
  54. article
  55. end
  56. def log_error(local_record, message)
  57. local_record.preferences['delivery_status'] = 'fail'
  58. local_record.preferences['delivery_status_message'] = message.encode!('UTF-8', 'UTF-8', invalid: :replace, replace: '?')
  59. local_record.preferences['delivery_status_date'] = Time.zone.now
  60. local_record.save
  61. Rails.logger.error message
  62. if local_record.preferences['delivery_retry'] > 3
  63. Ticket::Article.create(
  64. ticket_id: local_record.ticket_id,
  65. content_type: 'text/plain',
  66. body: "Unable to send post: #{message}",
  67. internal: true,
  68. sender: Ticket::Article::Sender.find_by(name: 'System'),
  69. type: Ticket::Article::Type.find_by(name: 'note'),
  70. preferences: {
  71. delivery_article_id_related: local_record.id,
  72. delivery_message: true,
  73. },
  74. updated_by_id: 1,
  75. created_by_id: 1,
  76. )
  77. end
  78. raise message
  79. end
  80. end