reaction.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Whatsapp::Webhook::Message::Reaction < Whatsapp::Webhook::Message
  3. attr_reader :related_article
  4. def type
  5. :reaction
  6. end
  7. def process
  8. @related_article = find_related_article
  9. raise Whatsapp::Webhook::Payload::ProcessableError, __('No related article found to process the reaction message on.') if @related_article.nil?
  10. @ticket = @related_article.ticket
  11. return if ticket_done?
  12. @user = create_or_update_user
  13. UserInfo.current_user_id = @user.id
  14. history_type = determine_history_type
  15. update_related_article
  16. update_ticket(ticket: @ticket)
  17. update_ticket_history_entry(history_type)
  18. notify_agents
  19. schedule_reminder_job
  20. end
  21. private
  22. def ticket_done?
  23. state_ids = Ticket::State.where(name: %w[closed merged removed]).pluck(:id)
  24. state_ids.include?(@ticket.state_id)
  25. end
  26. def emoji
  27. message.fetch(:emoji, nil)
  28. end
  29. def find_related_article
  30. Ticket::Article.where(message_id: message[:message_id])&.first
  31. end
  32. def update_related_article
  33. @related_article.update!(update_related_article_attributes)
  34. end
  35. def update_related_article_attributes
  36. preferences = @related_article.preferences
  37. preferences[:whatsapp] ||= {}
  38. preferences[:whatsapp][:reaction] = {
  39. emoji: emoji,
  40. author: user.fullname
  41. }
  42. { preferences: }
  43. end
  44. def determine_history_type
  45. return 'created' if @related_article.preferences[:whatsapp]&.fetch(:reaction, nil).nil?
  46. emoji.nil? ? 'removed' : 'updated'
  47. end
  48. def update_ticket_history_entry(history_type)
  49. History.add(
  50. history_type: history_type,
  51. history_object: 'Ticket::Article',
  52. history_attribute: 'reaction',
  53. o_id: @related_article.id,
  54. related_history_object: 'Ticket',
  55. related_o_id: @ticket.id,
  56. value_from: @related_article.created_by.fullname,
  57. value_to: emoji || '',
  58. created_by_id: @user.id,
  59. )
  60. end
  61. def notify_agents
  62. return if emoji.nil?
  63. TransactionJob.perform_now(
  64. object: 'Ticket::Article',
  65. type: 'update.reaction',
  66. object_id: @related_article.ticket.id,
  67. article_id: @related_article.id,
  68. user_id: @user.id,
  69. )
  70. end
  71. end