taskbar.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. def touch_last_contact!
  69. # Don't inform the current user (only!) about live user and item updates.
  70. self.skip_live_user_trigger = true
  71. self.skip_item_trigger = true
  72. self.last_contact = Time.zone.now
  73. save!
  74. end
  75. private
  76. def update_last_contact
  77. return true if local_update
  78. return true if changes.blank?
  79. if changes['notify']
  80. count = 0
  81. changes.each_key do |attribute|
  82. next if attribute == 'updated_at'
  83. next if attribute == 'created_at'
  84. count += 1
  85. end
  86. return true if count <= 1
  87. end
  88. self.last_contact = Time.zone.now
  89. end
  90. def set_user
  91. return true if local_update
  92. return true if !UserInfo.current_user_id
  93. self.user_id = UserInfo.current_user_id
  94. end
  95. def update_preferences_infos
  96. return if key == 'Search'
  97. return if local_update
  98. preferences = self.preferences || {}
  99. preferences[:tasks] = collect_related_tasks
  100. update_related_taskbars(preferences)
  101. # remember preferences for current taskbar
  102. self.preferences = preferences if !destroyed?
  103. end
  104. def collect_related_tasks
  105. related_taskbars.map(&:preferences_task_info)
  106. .tap { |arr| arr.push(preferences_task_info) if !destroyed? }
  107. .each_with_object({}) { |elem, memo| reduce_related_tasks(elem, memo) }
  108. .values
  109. .sort_by { |elem| elem[:id] || Float::MAX } # sort by IDs to pass old tests
  110. end
  111. def reduce_related_tasks(elem, memo)
  112. key = elem[:user_id]
  113. if memo[key]
  114. memo[key].deep_merge! elem
  115. return
  116. end
  117. memo[key] = elem
  118. end
  119. def update_related_taskbars(preferences)
  120. related_taskbars.each do |taskbar|
  121. taskbar.with_lock do
  122. taskbar.preferences = preferences
  123. taskbar.local_update = true
  124. taskbar.skip_item_trigger = true
  125. taskbar.save!
  126. end
  127. end
  128. end
  129. def notify_clients
  130. return true if !saved_change_to_attribute?('preferences')
  131. data = {
  132. event: 'taskbar:preferences',
  133. data: {
  134. id: id,
  135. key: key,
  136. preferences: preferences,
  137. },
  138. }
  139. PushMessages.send_to(
  140. user_id,
  141. data,
  142. )
  143. end
  144. end