taskbar.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. def self.taskbar_entities
  33. ApplicationModel.descendants.select { |model| model.included_modules.include?(HasTaskbars) }.each_with_object([]) do |model, result|
  34. model.taskbar_entities&.each do |entity|
  35. result << entity
  36. end
  37. end | TASKBAR_STATIC_ENTITIES
  38. end
  39. def state_changed?
  40. return false if state.blank?
  41. state.each do |key, value|
  42. if value.is_a? Hash
  43. value.each do |key1, value1|
  44. next if value1.blank?
  45. next if key1 == 'form_id'
  46. return true
  47. end
  48. else
  49. next if value.blank?
  50. next if key == 'form_id'
  51. return true
  52. end
  53. end
  54. false
  55. end
  56. def attributes_with_association_names(empty_keys: false)
  57. add_attachments_to_attributes(super)
  58. end
  59. def attributes_with_association_ids
  60. add_attachments_to_attributes(super)
  61. end
  62. def as_json(options = {})
  63. add_attachments_to_attributes(super)
  64. end
  65. def preferences_task_info
  66. output = { user_id:, apps: { app.to_sym => { last_contact: last_contact, changed: state_changed? } } }
  67. output[:id] = id if persisted?
  68. output
  69. end
  70. def related_taskbars
  71. self.class.related_taskbars(self)
  72. end
  73. def touch_last_contact!
  74. # Don't inform the current user (only!) about live user and item updates.
  75. self.skip_live_user_trigger = true
  76. self.skip_item_trigger = true
  77. self.last_contact = Time.zone.now
  78. save!
  79. end
  80. def saved_change_to_dirty?
  81. return false if !saved_change_to_preferences?
  82. !!preferences[:dirty] != !!preferences_previously_was[:dirty]
  83. end
  84. private
  85. def update_last_contact
  86. return true if local_update
  87. return true if changes.blank?
  88. if changes['notify']
  89. count = 0
  90. changes.each_key do |attribute|
  91. next if attribute == 'updated_at'
  92. next if attribute == 'created_at'
  93. count += 1
  94. end
  95. return true if count <= 1
  96. end
  97. self.last_contact = Time.zone.now
  98. end
  99. def set_user
  100. return true if local_update
  101. return true if !UserInfo.current_user_id
  102. self.user_id = UserInfo.current_user_id
  103. end
  104. def update_preferences_infos
  105. return if key == 'Search'
  106. return if local_update
  107. preferences = self.preferences || {}
  108. preferences[:tasks] = collect_related_tasks
  109. update_related_taskbars(preferences)
  110. # remember preferences for current taskbar
  111. self.preferences = preferences if !destroyed?
  112. end
  113. def collect_related_tasks
  114. related_taskbars.map(&:preferences_task_info)
  115. .tap { |arr| arr.push(preferences_task_info) if !destroyed? }
  116. .each_with_object({}) { |elem, memo| reduce_related_tasks(elem, memo) }
  117. .values
  118. .sort_by { |elem| elem[:id] || Float::MAX } # sort by IDs to pass old tests
  119. end
  120. def reduce_related_tasks(elem, memo)
  121. key = elem[:user_id]
  122. if memo[key]
  123. memo[key].deep_merge! elem
  124. return
  125. end
  126. memo[key] = elem
  127. end
  128. def update_related_taskbars(preferences)
  129. related_taskbars.each do |taskbar|
  130. taskbar.with_lock do
  131. taskbar.preferences = preferences
  132. taskbar.local_update = true
  133. taskbar.skip_item_trigger = true
  134. taskbar.save!
  135. end
  136. end
  137. end
  138. def notify_clients
  139. return true if !saved_change_to_attribute?('preferences')
  140. data = {
  141. event: 'taskbar:preferences',
  142. data: {
  143. id: id,
  144. key: key,
  145. preferences: preferences,
  146. },
  147. }
  148. PushMessages.send_to(
  149. user_id,
  150. data,
  151. )
  152. end
  153. end