taskbar.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 do |_key, value|
  14. if value.is_a? Hash
  15. value.each do |_key1, value1|
  16. next if value1.blank?
  17. return true
  18. end
  19. else
  20. next if value.blank?
  21. return true
  22. end
  23. end
  24. false
  25. end
  26. private
  27. def update_last_contact
  28. return true if local_update
  29. return true if changes.empty?
  30. if changes['notify']
  31. count = 0
  32. changes.each do |attribute, _value|
  33. next if attribute == 'updated_at'
  34. next if attribute == 'created_at'
  35. count += 1
  36. end
  37. return true if count <= 1
  38. end
  39. self.last_contact = Time.zone.now
  40. end
  41. def set_user
  42. return true if local_update
  43. return true if !UserInfo.current_user_id
  44. self.user_id = UserInfo.current_user_id
  45. end
  46. def update_preferences_infos
  47. return true if local_update
  48. # find other same open tasks
  49. if !preferences
  50. self.preferences = {}
  51. end
  52. preferences[:tasks] = []
  53. Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
  54. if taskbar.id == id
  55. local_changed = state_changed?
  56. local_last_contact = last_contact
  57. else
  58. local_changed = taskbar.state_changed?
  59. local_last_contact = taskbar.last_contact
  60. end
  61. task = {
  62. id: taskbar.id,
  63. user_id: taskbar.user_id,
  64. last_contact: local_last_contact,
  65. changed: local_changed,
  66. }
  67. preferences[:tasks].push task
  68. end
  69. if !id
  70. changed = state_changed?
  71. task = {
  72. user_id: user_id,
  73. last_contact: last_contact,
  74. changed: changed,
  75. }
  76. preferences[:tasks].push task
  77. end
  78. # update other taskbars
  79. Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
  80. next if taskbar.id == id
  81. taskbar.with_lock do
  82. taskbar.preferences = preferences
  83. taskbar.local_update = true
  84. taskbar.save!
  85. end
  86. end
  87. return true if destroyed?
  88. # remember preferences for current taskbar
  89. self.preferences = preferences
  90. true
  91. end
  92. def notify_clients
  93. return true if !saved_change_to_attribute?('preferences')
  94. data = {
  95. event: 'taskbar:preferences',
  96. data: {
  97. id: id,
  98. key: key,
  99. preferences: preferences,
  100. },
  101. }
  102. PushMessages.send_to(
  103. user_id,
  104. data,
  105. )
  106. end
  107. end