mirror of
https://github.com/mt-mods/mail.git
synced 2025-07-13 01:41:57 -04:00
storage rewrite wip
This commit is contained in:
parent
e08238f50e
commit
894e5df4b1
7 changed files with 62 additions and 148 deletions
100
migrate.lua
100
migrate.lua
|
@ -1,58 +1,28 @@
|
|||
|
||||
local STORAGE_VERSION_KEY = "@@version"
|
||||
|
||||
function mail.migrate()
|
||||
local gen_file_v1 = io.open(minetest.get_worldpath().."/mail.db", "r")
|
||||
if gen_file_v1 then
|
||||
mail.migrate_v1_to_v2()
|
||||
end
|
||||
|
||||
local info_file_v3 = mail.read_json_file(mail.maildir .. "/mail.info.json")
|
||||
if not info_file_v3.dbversion then
|
||||
local version = mail.storage:get_int(STORAGE_VERSION_KEY)
|
||||
if version < 3 then
|
||||
mail.migrate_v2_to_v3()
|
||||
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
|
||||
end
|
||||
end
|
||||
|
||||
-- migrate from mail.db to player-file-based mailbox
|
||||
function mail.migrate_v1_to_v2()
|
||||
-- create directory, just in case
|
||||
minetest.mkdir(mail.maildir)
|
||||
minetest.mkdir(mail.contactsdir)
|
||||
|
||||
local file = io.open(minetest.get_worldpath().."/mail.db", "r")
|
||||
if file then
|
||||
print("[mail] migrating to new per-player storage")
|
||||
|
||||
local data = file:read("*a")
|
||||
local oldmails = minetest.deserialize(data)
|
||||
file:close()
|
||||
|
||||
for name, oldmessages in pairs(oldmails) do
|
||||
mail.setMessages(name, oldmessages)
|
||||
end
|
||||
|
||||
-- rename file
|
||||
print("[mail] migration done, renaming old mail.db")
|
||||
os.rename(minetest.get_worldpath().."/mail.db", minetest.get_worldpath().."/mail.db.old")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
-- migrate from v2 to v3 database
|
||||
function mail.migrate_v2_to_v3()
|
||||
minetest.mkdir(mail.maildir) -- if necessary (eg. first login)
|
||||
minetest.log("info", "[mail] Migration from v2 to v3 database")
|
||||
minetest.after(0,function()
|
||||
for playername, _ in minetest.get_auth_handler().iterate() do
|
||||
mail.migrate_contacts_v2_to_v3(playername)
|
||||
end
|
||||
end)
|
||||
mail.migrate_messages_v2_to_v3()
|
||||
mail.write_json_file(mail.maildir .. "/mail.info.json", { dbversion = 3.0 })
|
||||
end
|
||||
|
||||
function mail.migrate_messages_v2_to_v3()
|
||||
print("[mail] Migration from v2 to v3 database")
|
||||
local already_processed = {} -- store messages that are already process to avoid duplicates
|
||||
|
||||
minetest.after(0,function()
|
||||
-- check in every inbox to fetch messages
|
||||
for playername, _ in minetest.get_auth_handler().iterate() do
|
||||
local player_contacts = mail.read_json_file(mail.maildir .. "/contacts/" .. playername .. ".json")
|
||||
local entry = mail.get_storage_entry(playername)
|
||||
for _, c in pairs(player_contacts) do
|
||||
table.insert(entry.contacts, { name = c.name, note = c.note })
|
||||
end
|
||||
|
||||
local saneplayername = string.gsub(playername, "[.|/]", "")
|
||||
local player_inbox = mail.read_json_file(mail.maildir .. "/" .. saneplayername .. ".json")
|
||||
for _, msg in ipairs(player_inbox) do
|
||||
|
@ -79,46 +49,8 @@ function mail.migrate_messages_v2_to_v3()
|
|||
table.insert(already_processed, msg_id)
|
||||
end
|
||||
end
|
||||
|
||||
mail.set_storage_entry(playername, entry)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function mail.migrate_contacts(playername)
|
||||
local gen_file_v1 = io.open(minetest.get_worldpath().."/mail.db", "r")
|
||||
if gen_file_v1 then
|
||||
mail.migrate_contacts_v1_to_v2(playername)
|
||||
end
|
||||
|
||||
-- v2 to v3 directly in general function
|
||||
end
|
||||
|
||||
function mail.migrate_contacts_v1_to_v2(playername)
|
||||
local file = io.open(mail.getContactsFile(playername), 'r')
|
||||
if not file then
|
||||
-- file doesn't exist! This is a case for Migrate Man!
|
||||
local messages = mail.getMessages(playername)
|
||||
local contacts = {}
|
||||
|
||||
if messages and not contacts then
|
||||
for _, message in pairs(messages) do
|
||||
mail.ensure_new_format(message)
|
||||
if contacts[string.lower(message.from)] == nil then
|
||||
contacts[string.lower(message.from)] = {
|
||||
name = message.from,
|
||||
note = "",
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
file:close() -- uh, um, nope, let's leave those alone, shall we?
|
||||
end
|
||||
end
|
||||
|
||||
function mail.migrate_contacts_v2_to_v3(playername)
|
||||
local player_contacts = mail.read_json_file(mail.maildir .. "/contacts/" .. playername .. ".json")
|
||||
|
||||
for _, c in pairs(player_contacts) do
|
||||
mail.addContact(playername, { name = c.name, note = c.note })
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue