taskbar.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. TASKBAR_APPS = %w[desktop mobile].freeze
  9. TASKBAR_STATIC_ENTITIES = %w[
  10. Search
  11. ].freeze
  12. store :state
  13. store :params
  14. store :preferences
  15. belongs_to :user
  16. validates :app, inclusion: { in: TASKBAR_APPS }
  17. validates :key, uniqueness: { scope: %i[user_id app] }
  18. before_validation :set_user
  19. before_create :update_last_contact, :update_preferences_infos
  20. before_update :update_last_contact, :update_preferences_infos
  21. after_update :notify_clients
  22. after_destroy :update_preferences_infos, :notify_clients
  23. association_attributes_ignored :user
  24. client_notification_events_ignored :create, :update, :touch
  25. client_notification_send_to :user_id
  26. attr_accessor :local_update
  27. default_scope { order(:id) }
  28. scope :related_taskbars, lambda { |taskbar|
  29. where(key: taskbar.key)
  30. .where.not(id: taskbar.id)
  31. }
  32. scope :app, ->(app) { where(app:) }
  33. def self.taskbar_entities
  34. @taskbar_entities ||= begin
  35. ApplicationModel.descendants.select { |model| model.included_modules.include?(HasTaskbars) }.each_with_object([]) do |model, result|
  36. model.taskbar_entities&.each do |entity|
  37. result << entity
  38. end
  39. end | TASKBAR_STATIC_ENTITIES
  40. end
  41. end
  42. def self.taskbar_ignore_state_updates_entities
  43. @taskbar_ignore_state_updates_entities ||= begin
  44. ApplicationModel.descendants.select { |model| model.included_modules.include?(HasTaskbars) }.each_with_object([]) do |model, result|
  45. model.taskbar_ignore_state_updates_entities&.each do |entity|
  46. result << entity
  47. end
  48. end
  49. end
  50. end
  51. def state_changed?
  52. return false if state.blank?
  53. state.each do |key, value|
  54. if value.is_a? Hash
  55. value.each do |key1, value1|
  56. next if value1.blank?
  57. next if key1 == 'form_id'
  58. return true
  59. end
  60. else
  61. next if value.blank?
  62. next if key == 'form_id'
  63. return true
  64. end
  65. end
  66. false
  67. end
  68. def attributes_with_association_names(empty_keys: false)
  69. add_attachments_to_attributes(super)
  70. end
  71. def attributes_with_association_ids
  72. add_attachments_to_attributes(super)
  73. end
  74. def as_json(options = {})
  75. add_attachments_to_attributes(super)
  76. end
  77. def preferences_task_info
  78. output = { user_id:, apps: { app.to_sym => { last_contact: last_contact, changed: state_changed? } } }
  79. output[:id] = id if persisted?
  80. output
  81. end
  82. def related_taskbars
  83. self.class.related_taskbars(self)
  84. end
  85. def touch_last_contact!
  86. # Don't inform the current user (only!) about live user and item updates.
  87. self.skip_live_user_trigger = true
  88. self.skip_item_trigger = true
  89. self.last_contact = Time.zone.now
  90. save!
  91. end
  92. def saved_change_to_dirty?
  93. return false if !saved_change_to_preferences?
  94. !!preferences[:dirty] != !!preferences_previously_was[:dirty]
  95. end
  96. private
  97. def update_last_contact
  98. return if local_update
  99. return if changes.blank?
  100. return if changed_only_prio?
  101. if changes['notify']
  102. count = 0
  103. changes.each_key do |attribute|
  104. next if attribute == 'updated_at'
  105. next if attribute == 'created_at'
  106. count += 1
  107. end
  108. return true if count <= 1
  109. end
  110. self.last_contact = Time.zone.now
  111. end
  112. def set_user
  113. return if local_update
  114. return if !UserInfo.current_user_id
  115. self.user_id = UserInfo.current_user_id
  116. end
  117. def update_preferences_infos
  118. return if key == 'Search'
  119. return if local_update
  120. return if changed_only_prio?
  121. preferences = self.preferences || {}
  122. preferences[:tasks] = collect_related_tasks
  123. update_related_taskbars(preferences)
  124. # remember preferences for current taskbar
  125. self.preferences = preferences if !destroyed?
  126. end
  127. def collect_related_tasks
  128. related_taskbars.map(&:preferences_task_info)
  129. .tap { |arr| arr.push(preferences_task_info) if !destroyed? }
  130. .each_with_object({}) { |elem, memo| reduce_related_tasks(elem, memo) }
  131. .values
  132. .sort_by { |elem| elem[:id] || Float::MAX } # sort by IDs to pass old tests
  133. end
  134. def changed_only_prio?
  135. changed_attribute_names_to_save.to_set == Set.new(%w[updated_at prio])
  136. end
  137. def reduce_related_tasks(elem, memo)
  138. key = elem[:user_id]
  139. if memo[key]
  140. memo[key].deep_merge! elem
  141. return
  142. end
  143. memo[key] = elem
  144. end
  145. def update_related_taskbars(preferences)
  146. related_taskbars.each do |taskbar|
  147. taskbar.with_lock do
  148. taskbar.preferences = preferences
  149. taskbar.local_update = true
  150. taskbar.skip_item_trigger = true
  151. taskbar.save!
  152. end
  153. end
  154. end
  155. def notify_clients
  156. return if !saved_change_to_attribute?('preferences')
  157. data = {
  158. event: 'taskbar:preferences',
  159. data: {
  160. id: id,
  161. key: key,
  162. preferences: preferences,
  163. },
  164. }
  165. PushMessages.send_to(
  166. user_id,
  167. data,
  168. )
  169. end
  170. end