communicate_facebook_job.rb 3.0 KB

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