2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-05-29 15:11:11 +00:00
|
|
|
class Taskbar < ApplicationModel
|
2020-09-08 15:06:23 +00:00
|
|
|
include ChecksClientNotification
|
2021-07-22 12:35:13 +00:00
|
|
|
include ::Taskbar::HasAttachments
|
2020-09-08 15:06:23 +00:00
|
|
|
|
2013-05-31 23:10:40 +00:00
|
|
|
store :state
|
|
|
|
store :params
|
2017-01-04 14:40:19 +00:00
|
|
|
store :preferences
|
2020-09-08 15:06:23 +00:00
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
2017-01-04 14:40:19 +00:00
|
|
|
before_create :update_last_contact, :set_user, :update_preferences_infos
|
|
|
|
before_update :update_last_contact, :set_user, :update_preferences_infos
|
|
|
|
|
|
|
|
after_update :notify_clients
|
|
|
|
after_destroy :update_preferences_infos, :notify_clients
|
|
|
|
|
2020-09-14 12:34:36 +00:00
|
|
|
association_attributes_ignored :user
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
client_notification_events_ignored :create, :update, :touch
|
|
|
|
|
|
|
|
client_notification_send_to :user_id
|
|
|
|
|
2017-01-04 14:40:19 +00:00
|
|
|
attr_accessor :local_update
|
|
|
|
|
|
|
|
def state_changed?
|
2017-09-08 08:28:34 +00:00
|
|
|
return false if state.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
state.each_value do |value|
|
2017-09-08 08:28:34 +00:00
|
|
|
if value.is_a? Hash
|
2018-05-03 14:09:33 +00:00
|
|
|
value.each do |key1, value1|
|
2017-09-08 08:28:34 +00:00
|
|
|
next if value1.blank?
|
2018-05-03 14:09:33 +00:00
|
|
|
next if key1 == 'form_id'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-04 14:40:19 +00:00
|
|
|
return true
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
else
|
2017-09-08 08:28:34 +00:00
|
|
|
next if value.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-04 14:40:19 +00:00
|
|
|
return true
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
false
|
|
|
|
end
|
2013-05-29 15:11:11 +00:00
|
|
|
|
2021-08-18 12:38:34 +00:00
|
|
|
def attributes_with_association_names(empty_keys: false)
|
2018-05-03 14:09:33 +00:00
|
|
|
add_attachments_to_attributes(super)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes_with_association_ids
|
|
|
|
add_attachments_to_attributes(super)
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(options = {})
|
|
|
|
add_attachments_to_attributes(super)
|
|
|
|
end
|
|
|
|
|
2013-05-31 23:10:40 +00:00
|
|
|
private
|
2015-05-01 12:31:46 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def update_last_contact
|
2017-01-04 14:40:19 +00:00
|
|
|
return true if local_update
|
2017-11-23 08:09:44 +00:00
|
|
|
return true if changes.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-14 06:53:26 +00:00
|
|
|
if changes['notify']
|
|
|
|
count = 0
|
2017-11-23 08:09:44 +00:00
|
|
|
changes.each_key do |attribute|
|
2017-01-14 06:53:26 +00:00
|
|
|
next if attribute == 'updated_at'
|
|
|
|
next if attribute == 'created_at'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-14 06:53:26 +00:00
|
|
|
count += 1
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-14 06:53:26 +00:00
|
|
|
return true if count <= 1
|
|
|
|
end
|
2015-05-08 10:20:33 +00:00
|
|
|
self.last_contact = Time.zone.now
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2013-08-22 20:56:54 +00:00
|
|
|
def set_user
|
2017-01-04 14:40:19 +00:00
|
|
|
return true if local_update
|
2017-09-10 11:52:07 +00:00
|
|
|
return true if !UserInfo.current_user_id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2013-08-22 20:56:54 +00:00
|
|
|
self.user_id = UserInfo.current_user_id
|
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
|
|
|
|
def update_preferences_infos
|
2020-06-17 15:41:45 +00:00
|
|
|
return true if key == 'Search'
|
2017-01-04 14:40:19 +00:00
|
|
|
return true if local_update
|
|
|
|
|
|
|
|
# find other same open tasks
|
2017-01-18 08:45:20 +00:00
|
|
|
if !preferences
|
2017-01-18 09:11:56 +00:00
|
|
|
self.preferences = {}
|
2017-01-18 08:45:20 +00:00
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
preferences[:tasks] = []
|
2017-10-01 12:25:52 +00:00
|
|
|
Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
|
2017-01-09 12:22:07 +00:00
|
|
|
if taskbar.id == id
|
|
|
|
local_changed = state_changed?
|
|
|
|
local_last_contact = last_contact
|
|
|
|
else
|
|
|
|
local_changed = taskbar.state_changed?
|
|
|
|
local_last_contact = taskbar.last_contact
|
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
task = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: taskbar.id,
|
|
|
|
user_id: taskbar.user_id,
|
2017-01-09 12:22:07 +00:00
|
|
|
last_contact: local_last_contact,
|
2018-12-19 17:31:51 +00:00
|
|
|
changed: local_changed,
|
2017-01-04 14:40:19 +00:00
|
|
|
}
|
|
|
|
preferences[:tasks].push task
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
if !id
|
|
|
|
changed = state_changed?
|
|
|
|
task = {
|
2018-12-19 17:31:51 +00:00
|
|
|
user_id: user_id,
|
2017-01-04 14:40:19 +00:00
|
|
|
last_contact: last_contact,
|
2018-12-19 17:31:51 +00:00
|
|
|
changed: changed,
|
2017-01-04 14:40:19 +00:00
|
|
|
}
|
|
|
|
preferences[:tasks].push task
|
|
|
|
end
|
|
|
|
|
|
|
|
# update other taskbars
|
2017-10-01 12:25:52 +00:00
|
|
|
Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
|
2017-01-04 14:40:19 +00:00
|
|
|
next if taskbar.id == id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-08-22 17:36:48 +00:00
|
|
|
taskbar.with_lock do
|
|
|
|
taskbar.preferences = preferences
|
|
|
|
taskbar.local_update = true
|
|
|
|
taskbar.save!
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
|
|
|
|
return true if destroyed?
|
|
|
|
|
|
|
|
# remember preferences for current taskbar
|
|
|
|
self.preferences = preferences
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_clients
|
2017-09-23 06:25:55 +00:00
|
|
|
return true if !saved_change_to_attribute?('preferences')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-04 14:40:19 +00:00
|
|
|
data = {
|
|
|
|
event: 'taskbar:preferences',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
|
|
|
id: id,
|
|
|
|
key: key,
|
2017-01-04 14:40:19 +00:00
|
|
|
preferences: preferences,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
PushMessages.send_to(
|
|
|
|
user_id,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-05-31 23:10:40 +00:00
|
|
|
end
|