taskbar.rb 4.4 KB

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