forked from avral/golosnotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriptions.lua
160 lines (137 loc) · 4.09 KB
/
subscriptions.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
local MAX_PER_ACCOUNT = 50
function now()
return math.floor(clock.time() * 1000)
end
function migrate_subs()
if box.space.subs_migrated ~= nil then
print('Subs already migrated.')
return
end
print('Migrating subs...')
local subs = box.space.subs.index.by_subscriber_date:select({}, {iterator = 'GE'})
local start_time = 1700000000000
local num = start_time
local acc_name = nil
for i,sub in ipairs(subs) do
local acc = sub[2]
if acc_name == nil then
acc_name = acc
elseif acc ~= acc_name then
acc_name = acc
num = start_time
end
box.space.subs:update(sub[1], {{'=', 5, num}})
num = num + 1
end
box.schema.create_space('subs_migrated')
end
function subscribe_it(account, id, hashlink)
local res = {}
local subs = box.space.subs.index.by_subscriber_date:select({account}, {iterator = 'GE', limit = MAX_PER_ACCOUNT})
local total = 0
for i,sub in ipairs(subs) do
if sub[2] ~= account then
break
end
total = total + 1
end
if total == MAX_PER_ACCOUNT then
res.deleted = subs[1]
box.space.subs:delete(subs[1][1])
end
-- 1 - post replies. 1-500 are reserved for Golos, another can be used by 3rd party
local q = box.space.subs:auto_increment{account, 1, id, now(), 0, hashlink}
res.added = q[1]
return res
end
function put_event(account, entity_id, data)
-- 1 - comment. 1-500 are reserved for Golos, another can be used by 3rd party
local event = box.space.events:auto_increment{account, 1, entity_id, now(), data}
local subs = box.space.subs.index.by_entity_id_subscriber:select({entity_id, account})
if #subs > 0 then
box.space.subs:update(subs[1][1], {{'+', 6, 1}})
else
box.error('No such sub')
end
return event
end
local function fill_sub(sub, data)
sub.id = data[1]
sub.account = data[2]
sub.type = 'post_replies'
sub.entityId = data[4]
sub.createdMsec = data[5]
sub.eventCount = data[6]
sub.hashlink = data[7]
end
function get_subs(entity_id)
local res = {}
local subs = box.space.subs.index.by_entity_id_subscriber:select({entity_id})
for i,sub in ipairs(subs) do
local obj = {}
fill_sub(obj, sub)
res[i] = obj
end
return res
end
function list_subs(account, from, limit)
local subs = box.space.subs.index.by_subscriber_events:select({account}, {iterator = 'LE', limit = 100})
local arr = {}
if from == 0 then
from = 1
end
if limit == 0 then
limit = #subs
end
local total = 0
for i,sub in ipairs(subs) do
if sub[2] ~= account then
break
end
total = total + 1
if i >= from and #arr <= limit then
local obj = {}
fill_sub(obj, sub)
arr[#arr + 1] = obj
end
end
local res = {}
res.subs = arr
res.total = total
return res
end
function get_events(account, entity_id)
local events = box.space.events.index.by_entity_id_subscriber:select({entity_id, account})
local res = {}
for i,event in ipairs(events) do
local obj = {}
obj.account = account
obj.entityId = entity_id
obj.data = event[6]
res[i] = obj
end
return res
end
local function drop_events(account, entity_id)
local events = box.space.events.index.by_entity_id_subscriber:select({entity_id, account})
for i,event in ipairs(events) do
box.space.events:delete(event[1])
end
end
local function get_sub(account, entity_id)
local subs = box.space.subs.index.by_entity_id_subscriber:select({entity_id, account})
if #subs == 0 then
error('No such sub')
end
return subs[1]
end
function mark_read(account, entity_id)
local sub = get_sub(account, entity_id)
drop_events(account, entity_id)
box.space.subs:update(sub[1], {{'=', 6, 0}})
end
function unsubscribe_it(account, entity_id)
local sub = get_sub(account, entity_id)
drop_events(account, entity_id)
box.space.subs:delete(sub[1])
end