taskbar.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Taskbar < ApplicationModel
  3. include ChecksClientNotification
  4. store :state
  5. store :params
  6. store :preferences
  7. belongs_to :user
  8. before_create :update_last_contact, :set_user, :update_preferences_infos
  9. before_update :update_last_contact, :set_user, :update_preferences_infos
  10. after_update :notify_clients
  11. after_destroy :update_preferences_infos, :notify_clients
  12. association_attributes_ignored :user
  13. client_notification_events_ignored :create, :update, :touch
  14. client_notification_send_to :user_id
  15. attr_accessor :local_update
  16. def state_changed?
  17. return false if state.blank?
  18. state.each_value do |value|
  19. if value.is_a? Hash
  20. value.each do |key1, value1|
  21. next if value1.blank?
  22. next if key1 == 'form_id'
  23. return true
  24. end
  25. else
  26. next if value.blank?
  27. return true
  28. end
  29. end
  30. false
  31. end
  32. def attributes_with_association_names
  33. add_attachments_to_attributes(super)
  34. end
  35. def attributes_with_association_ids
  36. add_attachments_to_attributes(super)
  37. end
  38. def as_json(options = {})
  39. add_attachments_to_attributes(super)
  40. end
  41. # form_id is saved directly in a new ticket, but inside of the article when updating an existing ticket
  42. def persisted_form_id
  43. state&.dig(:form_id) || state&.dig(:article, :form_id)
  44. end
  45. private
  46. def attachments
  47. return [] if persisted_form_id.blank?
  48. UploadCache.new(persisted_form_id).attachments
  49. end
  50. def add_attachments_to_attributes(attributes)
  51. attributes.tap do |result|
  52. result['attachments'] = attachments.map(&:attributes_for_display)
  53. end
  54. end
  55. def update_last_contact
  56. return true if local_update
  57. return true if changes.blank?
  58. if changes['notify']
  59. count = 0
  60. changes.each_key do |attribute|
  61. next if attribute == 'updated_at'
  62. next if attribute == 'created_at'
  63. count += 1
  64. end
  65. return true if count <= 1
  66. end
  67. self.last_contact = Time.zone.now
  68. end
  69. def set_user
  70. return true if local_update
  71. return true if !UserInfo.current_user_id
  72. self.user_id = UserInfo.current_user_id
  73. end
  74. def update_preferences_infos
  75. return true if key == 'Search'
  76. return true if local_update
  77. # find other same open tasks
  78. if !preferences
  79. self.preferences = {}
  80. end
  81. preferences[:tasks] = []
  82. Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
  83. if taskbar.id == id
  84. local_changed = state_changed?
  85. local_last_contact = last_contact
  86. else
  87. local_changed = taskbar.state_changed?
  88. local_last_contact = taskbar.last_contact
  89. end
  90. task = {
  91. id: taskbar.id,
  92. user_id: taskbar.user_id,
  93. last_contact: local_last_contact,
  94. changed: local_changed,
  95. }
  96. preferences[:tasks].push task
  97. end
  98. if !id
  99. changed = state_changed?
  100. task = {
  101. user_id: user_id,
  102. last_contact: last_contact,
  103. changed: changed,
  104. }
  105. preferences[:tasks].push task
  106. end
  107. # update other taskbars
  108. Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
  109. next if taskbar.id == id
  110. taskbar.with_lock do
  111. taskbar.preferences = preferences
  112. taskbar.local_update = true
  113. taskbar.save!
  114. end
  115. end
  116. return true if destroyed?
  117. # remember preferences for current taskbar
  118. self.preferences = preferences
  119. true
  120. end
  121. def notify_clients
  122. return true if !saved_change_to_attribute?('preferences')
  123. data = {
  124. event: 'taskbar:preferences',
  125. data: {
  126. id: id,
  127. key: key,
  128. preferences: preferences,
  129. },
  130. }
  131. PushMessages.send_to(
  132. user_id,
  133. data,
  134. )
  135. end
  136. end