taskbar.rb 3.7 KB

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