taskbar.rb 3.4 KB

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