mention.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Mention < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include ChecksClientNotification
  5. include HasHistory
  6. include Mention::Assets
  7. after_create :update_mentionable
  8. after_destroy :update_mentionable
  9. belongs_to :user, class_name: 'User'
  10. belongs_to :mentionable, polymorphic: true
  11. association_attributes_ignored :created_by, :updated_by
  12. client_notification_events_ignored :update, :touch
  13. validates_with Validations::MentionValidator
  14. def notify_clients_data_attributes
  15. super.merge(
  16. 'mentionable_id' => mentionable_id,
  17. 'mentionable_type' => mentionable_type,
  18. )
  19. end
  20. def history_log_attributes
  21. {
  22. related_o_id: mentionable_id,
  23. related_history_object: mentionable_type,
  24. value_to: user.id,
  25. }
  26. end
  27. def history_destroy
  28. history_log('removed', created_by_id)
  29. end
  30. def self.duplicates(mentionable1, mentionable2)
  31. Mention.joins(', mentions as mentionsb').where('
  32. mentions.user_id = mentionsb.user_id
  33. AND mentions.mentionable_type = ?
  34. AND mentions.mentionable_id = ?
  35. AND mentionsb.mentionable_type = ?
  36. AND mentionsb.mentionable_id = ?
  37. ', mentionable1.class.to_s, mentionable1.id, mentionable2.class.to_s, mentionable2.id)
  38. end
  39. def update_mentionable
  40. # make sure mentionable is touched even if updated_by value stays the same
  41. mentionable.update(updated_by: updated_by, updated_at: Time.current)
  42. end
  43. # Check if user is subscribed to given object
  44. # @param target to check against
  45. # @param user
  46. # @return Boolean
  47. def self.subscribed?(object, user)
  48. object.mentions.exists? user: user
  49. end
  50. # Subscribe a user to changes of an object
  51. # @param target to subscribe to
  52. # @param user
  53. # @return Boolean
  54. def self.subscribe!(object, user)
  55. object.mentions.create!(user: user) if !subscribed?(object, user)
  56. true
  57. end
  58. # Unsubscribe a user from changes of an object
  59. # @param target to unsubscribe from
  60. # @param user
  61. # @return Boolean
  62. def self.unsubscribe!(object, user)
  63. object
  64. .mentions
  65. .find_by(user: user)
  66. &.destroy!
  67. true
  68. end
  69. # Unsubscribe all users from changes of an object
  70. # @param target to unsubscribe from
  71. # @return Boolean
  72. def self.unsubscribe_all!(object)
  73. object.mentions.destroy_all
  74. end
  75. # Check if given user is able to subscribe to a given object
  76. # @param object to subscribe to
  77. # @param mentioned user
  78. # @return Boolean
  79. def self.mentionable?(object, user)
  80. case object
  81. when Ticket
  82. TicketPolicy.new(user, object).agent_read_access?
  83. else
  84. false
  85. end
  86. end
  87. end