-
Notifications
You must be signed in to change notification settings - Fork 23
/
ussender.lua
35 lines (31 loc) · 1.02 KB
/
ussender.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- Race conditions in this files are possible. It is ok by biz logic.
function ussender_add(user_id, sender_id)
local user_id = box.unpack("i", user_id)
local sender_id = box.unpack("l", sender_id)
local selected = { box.select_limit(0, 0, 0, 1, user_id) }
if #selected == 0 then
box.insert(0, user_id, sender_id)
else
local fun, param, state = selected[1]:pairs()
state, _ = fun(param, state) -- skip the first element of tuple
for state, v in fun, param, state do
local cur_id = box.unpack("l", v)
if cur_id == sender_id then
return
end
end
box.update(0, user_id, "!p", -1, sender_id)
end
end
function ussender_select(user_id)
local user_id = box.unpack("i", user_id)
local ret = {box.select_limit(0, 0, 0, 1, user_id)}
if #ret == 0 then
return {user_id}
end
return ret
end
function ussender_delete(user_id)
local user_id = box.unpack("i", user_id)
box.delete(0, user_id)
end