taskbar.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Taskbar < ApplicationModel
  3. include ChecksClientNotification
  4. include ::Taskbar::HasAttachments
  5. include Taskbar::Assets
  6. include Taskbar::TriggersSubscriptions
  7. include Taskbar::List
  8. include Taskbar::Validator
  9. TASKBAR_APPS = %w[desktop mobile].freeze
  10. TASKBAR_ENTITIES = %w[
  11. OrganizationProfile
  12. Search
  13. TicketCreate
  14. TicketZoom
  15. UserProfile
  16. ].freeze
  17. store :state
  18. store :params
  19. store :preferences
  20. belongs_to :user
  21. validates :app, inclusion: { in: TASKBAR_APPS }
  22. before_create :update_last_contact, :set_user, :update_preferences_infos
  23. before_update :update_last_contact, :set_user, :update_preferences_infos
  24. after_update :notify_clients
  25. after_destroy :update_preferences_infos, :notify_clients
  26. association_attributes_ignored :user
  27. client_notification_events_ignored :create, :update, :touch
  28. client_notification_send_to :user_id
  29. attr_accessor :local_update
  30. default_scope { order(:id) }
  31. scope :related_taskbars, lambda { |taskbar|
  32. where(key: taskbar.key)
  33. .where.not(id: taskbar.id)
  34. }
  35. def state_changed?
  36. return false if state.blank?
  37. state.each_value do |value|
  38. if value.is_a? Hash
  39. value.each do |key1, value1|
  40. next if value1.blank?
  41. next if key1 == 'form_id'
  42. return true
  43. end
  44. else
  45. next if value.blank?
  46. return true
  47. end
  48. end
  49. false
  50. end
  51. def attributes_with_association_names(empty_keys: false)
  52. add_attachments_to_attributes(super)
  53. end
  54. def attributes_with_association_ids
  55. add_attachments_to_attributes(super)
  56. end
  57. def as_json(options = {})
  58. add_attachments_to_attributes(super)
  59. end
  60. def preferences_task_info
  61. output = { user_id:, apps: { app.to_sym => { last_contact: last_contact, changed: state_changed? } } }
  62. output[:id] = id if persisted?
  63. output
  64. end
  65. def related_taskbars
  66. self.class.related_taskbars(self)
  67. end
  68. private
  69. def update_last_contact
  70. return true if local_update
  71. return true if changes.blank?
  72. if changes['notify']
  73. count = 0
  74. changes.each_key do |attribute|
  75. next if attribute == 'updated_at'
  76. next if attribute == 'created_at'
  77. count += 1
  78. end
  79. return true if count <= 1
  80. end
  81. self.last_contact = Time.zone.now
  82. end
  83. def set_user
  84. return true if local_update
  85. return true if !UserInfo.current_user_id
  86. self.user_id = UserInfo.current_user_id
  87. end
  88. def update_preferences_infos
  89. return if key == 'Search'
  90. return if local_update
  91. preferences = self.preferences || {}
  92. preferences[:tasks] = collect_related_tasks
  93. update_related_taskbars(preferences)
  94. # remember preferences for current taskbar
  95. self.preferences = preferences if !destroyed?
  96. end
  97. def collect_related_tasks
  98. related_taskbars.map(&:preferences_task_info)
  99. .tap { |arr| arr.push(preferences_task_info) if !destroyed? }
  100. .each_with_object({}) { |elem, memo| reduce_related_tasks(elem, memo) }
  101. .values
  102. .sort_by { |elem| elem[:id] || Float::MAX } # sort by IDs to pass old tests
  103. end
  104. def reduce_related_tasks(elem, memo)
  105. key = elem[:user_id]
  106. if memo[key]
  107. memo[key].deep_merge! elem
  108. return
  109. end
  110. memo[key] = elem
  111. end
  112. def update_related_taskbars(preferences)
  113. related_taskbars.each do |taskbar|
  114. taskbar.with_lock do
  115. taskbar.preferences = preferences
  116. taskbar.local_update = true
  117. taskbar.skip_trigger = true
  118. taskbar.save!
  119. end
  120. end
  121. end
  122. def notify_clients
  123. return true if !saved_change_to_attribute?('preferences')
  124. data = {
  125. event: 'taskbar:preferences',
  126. data: {
  127. id: id,
  128. key: key,
  129. preferences: preferences,
  130. },
  131. }
  132. PushMessages.send_to(
  133. user_id,
  134. data,
  135. )
  136. end
  137. end