taskbar.rb 3.5 KB

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