--
--   Copyright 2014 Ilkka Oksanen <iao@iki.fi>
--
--   Licensed under the Apache License, Version 2.0 (the "License");
--   you may not use this file except in compliance with the License.
--   You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing,
--   software distributed under the License is distributed on an "AS
--   IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
--   express or implied.  See the License for the specific language
--   governing permissions and limitations under the License.
--

local userId = ARGV[1]
local windowId = ARGV[2]
local command = ARGV[3]
local excludeSession = ARGV[4]

redis.call('LPUSH', 'windowmsgs:' .. userId .. ':' .. windowId, command)

local sessions = redis.call('ZRANGE', 'sessionlist:' .. userId, 0, -1)

for i = 1, #sessions do
    if sessions[i] ~= excludeSession then
        redis.call('LPUSH', 'outbox:' .. userId .. ':' .. sessions[i], command)
    end
end

--
--   Copyright 2014 Ilkka Oksanen <iao@iki.fi>
--
--   Licensed under the Apache License, Version 2.0 (the "License");
--   you may not use this file except in compliance with the License.
--   You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing,
--   software distributed under the License is distributed on an "AS
--   IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
--   express or implied.  See the License for the specific language
--   governing permissions and limitations under the License.
--

local MSG_BUFFER_SIZE = 200

local userId = ARGV[1]
local windowId = ARGV[2]
local command = ARGV[3]
local excludeSession = ARGV[4]

local msgsKey = 'windowmsgs:' .. userId .. ':' .. windowId

redis.call('LPUSH', msgsKey, command)
redis.call('LTRIM', msgsKey, 0, MSG_BUFFER_SIZE - 1)

local sessions = redis.call('ZRANGE', 'sessionlist:' .. userId, 0, -1)

for i = 1, #sessions do
    if sessions[i] ~= excludeSession then
        redis.call('LPUSH', 'outbox:' .. userId .. ':' .. sessions[i], command)
    end
end

Add limit to message buffer
Add limit to message buffer
