taskbar.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. Store.list(object: 'UploadCache', o_id: persisted_form_id)
  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 local_update
  71. # find other same open tasks
  72. if !preferences
  73. self.preferences = {}
  74. end
  75. preferences[:tasks] = []
  76. Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
  77. if taskbar.id == id
  78. local_changed = state_changed?
  79. local_last_contact = last_contact
  80. else
  81. local_changed = taskbar.state_changed?
  82. local_last_contact = taskbar.last_contact
  83. end
  84. task = {
  85. id: taskbar.id,
  86. user_id: taskbar.user_id,
  87. last_contact: local_last_contact,
  88. changed: local_changed,
  89. }
  90. preferences[:tasks].push task
  91. end
  92. if !id
  93. changed = state_changed?
  94. task = {
  95. user_id: user_id,
  96. last_contact: last_contact,
  97. changed: changed,
  98. }
  99. preferences[:tasks].push task
  100. end
  101. # update other taskbars
  102. Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
  103. next if taskbar.id == id
  104. taskbar.with_lock do
  105. taskbar.preferences = preferences
  106. taskbar.local_update = true
  107. taskbar.save!
  108. end
  109. end
  110. return true if destroyed?
  111. # remember preferences for current taskbar
  112. self.preferences = preferences
  113. true
  114. end
  115. def notify_clients
  116. return true if !saved_change_to_attribute?('preferences')
  117. data = {
  118. event: 'taskbar:preferences',
  119. data: {
  120. id: id,
  121. key: key,
  122. preferences: preferences,
  123. },
  124. }
  125. PushMessages.send_to(
  126. user_id,
  127. data,
  128. )
  129. end
  130. end