Skip to content

Commit

Permalink
refactor backup handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAFile committed Jul 24, 2018
1 parent 15e949e commit 205de3a
Showing 1 changed file with 72 additions and 63 deletions.
135 changes: 72 additions & 63 deletions lua/plugins/backup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,76 +85,85 @@ local imported_text = [['Done.
]]

function plugin.onTextMessage(msg, blocks)
if msg.from.admin then
if blocks[1] == 'snap' then
local key = 'chat:'..msg.chat.id..':lastsnap'
local last_user = db:get(key)
if last_user then
local ttl = db:ttl(key)
local time_remaining = get_time_remaining(ttl)
local text = i18n([[<i>I'm sorry, this command has been used for the last time less then 3 hours ago by</i> %s (ask them for the file).
if not msg.from.admin then
return
end
if blocks[1] == 'snap' then
local key = 'chat:'..msg.chat.id..':lastsnap'
local last_user = db:get(key)
if last_user then
-- A snapshot has been done recently
local ttl = db:ttl(key)
local time_remaining = get_time_remaining(ttl)
local text = i18n([[<i>I'm sorry, this command has been used for the last time less then 3 hours ago by</i> %s (ask them for the file).
Wait [<code>%s</code>] to use it again
]]):format(last_user, time_remaining)
api.sendReply(msg, text, 'html')
else
local name = u.getname_final(msg.from)
db:setex(key, 10800, name) --3 hours
local file_path = gen_backup(msg.chat.id)
api.sendReply(msg, i18n('*Sent in private*'), true)
api.sendDocument(msg.from.id, file_path, nil, ('#snap\n%s'):format(msg.chat.title))
end
api.sendReply(msg, text, 'html')
else
-- no snapshot has been done recently
local name = u.getname_final(msg.from)
db:setex(key, 10800, name) --3 hours
local file_path = gen_backup(msg.chat.id)
api.sendReply(msg, i18n('*Sent in private*'), true)
api.sendDocument(msg.from.id, file_path, nil, ('#snap\n%s'):format(msg.chat.title))
end
end
if blocks[1] == 'import' then
local text
if not msg.reply then
text = i18n('Invalid input. Please reply to the backup file (/snap command to get it)')
api.sendMessage(msg.chat.id, text)
return
end
if not msg.reply.document then
text = i18n('Invalid input. Please reply to a document')
api.sendMessage(msg.chat.id, text)
return
end
if msg.reply.document.file_name ~= 'snap'..msg.chat.id..'.gbb' then
text = i18n('This is not a valid backup file.\nReason: invalid name (%s)')
:format(tostring(msg.reply_to_message.document.file_name))
api.sendMessage(msg.chat.id, text)
return
end
if blocks[1] == 'import' then
local text
if msg.reply then
if msg.reply.document then
if msg.reply.document.file_name == 'snap'..msg.chat.id..'.gbb' then
local res = api.getFile(msg.reply.document.file_id)
local download_link = u.telegram_file_link(res)
local file_path, code = u.download_to_file(download_link, '/tmp/'..msg.chat.id..'.json')
if not file_path then
text = i18n('Download of the file failed with code %s'):format(tostring(code))
else
local data = load_data(file_path)
for chat_id, group_data in pairs(data) do
chat_id = tonumber(chat_id)
if tonumber(chat_id) ~= msg.chat.id then
text = i18n('Chat IDs don\'t match (%s and %s)'):format(tostring(chat_id), tostring(msg.chat.id))
else
--restoring sets
if group_data.sets and next(group_data.sets) then
for set, content in pairs(group_data.sets) do
db:sadd(('chat:%d:%s'):format(chat_id, set), unpack(content))
end
end
local res = api.getFile(msg.reply.document.file_id)
local download_link = u.telegram_file_link(res)
local file_path, code = u.download_to_file(download_link, '/tmp/'..msg.chat.id..'.json')

--restoring hashes
if group_data.hashes and next(group_data.hashes) then
for hash, content in pairs(group_data.hashes) do
if next(content) then
--[[for key, val in pairs(content) do
print('\tkey:', key)
db:hset(('chat:%d:%s'):format(chat_id, hash), key, val)
end]]
db:hmset(('chat:%d:%s'):format(chat_id, hash), content)
end
end
end
text = i18n(imported_text)
end
end
end
else
text = i18n('This is not a valid backup file.\nReason: invalid name (%s)')
:format(tostring(msg.reply_to_message.document.file_name))
if not file_path then
text = i18n('Download of the file failed with code %s'):format(tostring(code))
api.sendMessage(msg.chat.id, text)
return
end

local data = load_data(file_path)
for chat_id, group_data in pairs(data) do
chat_id = tonumber(chat_id)
if tonumber(chat_id) ~= msg.chat.id then
text = i18n('Chat IDs don\'t match (%s and %s)'):format(tostring(chat_id), tostring(msg.chat.id))
api.sendMessage(msg.chat.id, text)
return
end
--restoring sets
if group_data.sets and next(group_data.sets) then
for set, content in pairs(group_data.sets) do
db:sadd(('chat:%d:%s'):format(chat_id, set), unpack(content))
end
end

--restoring hashes
if group_data.hashes and next(group_data.hashes) then
for hash, content in pairs(group_data.hashes) do
if next(content) then
--[[for key, val in pairs(content) do
print('\tkey:', key)
db:hset(('chat:%d:%s'):format(chat_id, hash), key, val)
end]]
db:hmset(('chat:%d:%s'):format(chat_id, hash), content)
end
else
text = i18n('Invalid input. Please reply to a document')
end
else
text = i18n('Invalid input. Please reply to the backup file (/snap command to get it)')
end
api.sendMessage(msg.chat.id, text)
api.sendMessage(msg.chat.id, i18n(imported_text))
end
end
end
Expand Down

0 comments on commit 205de3a

Please sign in to comment.