2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-09-04 21:24:19 +00:00
|
|
|
module PushMessages
|
|
|
|
|
|
|
|
def self.enabled?
|
2020-10-22 13:57:01 +00:00
|
|
|
return true if Thread.current[:push_messages].instance_of?(Array)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-09-04 21:24:19 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.init
|
2016-09-06 05:51:12 +00:00
|
|
|
return true if enabled?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-09-04 21:24:19 +00:00
|
|
|
Thread.current[:push_messages] = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send(data)
|
|
|
|
if !PushMessages.enabled?
|
|
|
|
Sessions.broadcast(
|
|
|
|
data[:message],
|
|
|
|
data[:type],
|
|
|
|
data[:current_user_id],
|
|
|
|
)
|
|
|
|
return true
|
|
|
|
end
|
2017-01-04 14:40:19 +00:00
|
|
|
message = { type: 'broadcast', data: data }
|
|
|
|
Thread.current[:push_messages].push message
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_to(user_id, data)
|
|
|
|
if !PushMessages.enabled?
|
|
|
|
Sessions.send_to(user_id, data)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
message = { type: 'send_to', user_id: user_id, data: data }
|
|
|
|
Thread.current[:push_messages].push message
|
2016-09-04 21:24:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.finish
|
2016-09-06 05:51:12 +00:00
|
|
|
return false if !enabled?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
Thread.current[:push_messages].each do |message|
|
2017-01-04 14:40:19 +00:00
|
|
|
if message[:type] == 'send_to'
|
|
|
|
Sessions.send_to(message[:user_id], message[:data])
|
|
|
|
else
|
|
|
|
Sessions.broadcast(
|
|
|
|
message[:data][:message],
|
|
|
|
message[:data][:type],
|
|
|
|
message[:data][:current_user_id],
|
|
|
|
)
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-09-04 21:24:19 +00:00
|
|
|
Thread.current[:push_messages] = nil
|
2016-09-06 05:51:12 +00:00
|
|
|
true
|
2016-09-04 21:24:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|