taskbar.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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
  13. return false if state.empty?
  14. state.each { |_key, value|
  15. if value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess
  16. value.each { |_key1, value1|
  17. next if value1 && value1.empty?
  18. return true
  19. }
  20. else
  21. next if value && value.empty?
  22. return true
  23. end
  24. }
  25. false
  26. end
  27. private
  28. def update_last_contact
  29. return true if local_update
  30. return true if changes.empty?
  31. if changes['notify']
  32. count = 0
  33. changes.each { |attribute, _value|
  34. next if attribute == 'updated_at'
  35. next if attribute == 'created_at'
  36. count += 1
  37. }
  38. return true if count <= 1
  39. end
  40. self.last_contact = Time.zone.now
  41. end
  42. def set_user
  43. return true if local_update
  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).each { |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. }
  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).each { |taskbar|
  80. next if taskbar.id == id
  81. taskbar.preferences = preferences
  82. taskbar.local_update = true
  83. taskbar.save!
  84. }
  85. return true if destroyed?
  86. # remember preferences for current taskbar
  87. self.preferences = preferences
  88. true
  89. end
  90. def notify_clients
  91. return true if !changes['preferences']
  92. data = {
  93. event: 'taskbar:preferences',
  94. data: {
  95. id: id,
  96. key: key,
  97. preferences: preferences,
  98. },
  99. }
  100. PushMessages.send_to(
  101. user_id,
  102. data,
  103. )
  104. end
  105. end