From 0333ba7fe744b4ae3791e632a945a688ff10e2d4 Mon Sep 17 00:00:00 2001 From: Athozus Date: Wed, 8 Mar 2023 19:03:27 +0100 Subject: [PATCH] Set up database version v3 and its migration from v2 + Check versions to choose v1->v2 or v2->v3 --- init.lua | 5 +++- migrate.lua | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index c607b1b..5df67cc 100644 --- a/init.lua +++ b/init.lua @@ -2,6 +2,9 @@ mail = { -- api version apiversion = 1.1, + -- database version + dbversion = 3.0, + -- mail directory maildir = minetest.get_worldpath().."/mails", contactsdir = minetest.get_worldpath().."/mails/contacts" @@ -23,4 +26,4 @@ mail.migrate() if minetest.get_modpath("mtt") then dofile(MP .. "/mtt.lua") -end \ No newline at end of file +end diff --git a/migrate.lua b/migrate.lua index 1cefbd7..0a6dbfe 100644 --- a/migrate.lua +++ b/migrate.lua @@ -1,7 +1,18 @@ +function mail.migrate() + local gen_file_v1 = io.open(minetest.get_worldpath().."/mail.db", "r") + if file 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 + mail.migrate_v2_to_v3() + end +end + -- migrate from mail.db to player-file-based mailbox - -function mail.migrate() +function mail.migrate_v1_to_v2() -- create directory, just in case minetest.mkdir(mail.maildir) minetest.mkdir(mail.contactsdir) @@ -25,8 +36,63 @@ function mail.migrate() end +-- migrate from v2 to v3 database +function mail.migrate_v2_to_v3() + minetest.log("info", "[mail] Migration from v2 to v3 database") + minetest.after(0,function() + for playername, value in minetest.get_auth_handler().iterate() do + mail.migrate_contacts_v2_to_v3(playername) + end + end) + mail.migrate_messages_v2_to_v3() + local info_file = mail.read_json_file(mail.maildir .. "/mail.info.json") + mail.write_json_file(mail.maildir .. "/mail.info.json", { dbversion = 3.0 }) +end + +function mail.migrate_messages_v2_to_v3() + 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, value in minetest.get_auth_handler().iterate() do + local saneplayername = string.gsub(playername, "[.|/]", "") + local player_inbox = mail.read_json_file(mail.maildir .. "/" .. saneplayername .. ".json") + for _, msg in ipairs(player_inbox) do + local msg_id = tostring(msg.time) .. msg.sender -- id like "123456789.0singleplayer" -- it presumes that a same sender cannot send two mails within a second + local new_msg = true -- check if that mail was already processed with another player + for _, cur_id in ipairs(already_processed) do + if cur_id == msg_id then + new_msg = false + break + end + end + if new_msg then + local msg_table = { + sender = msg.sender, + to = msg.to, + cc = msg.cc, + subject = msg.subject, + body = msg.body, + time = msg.time, + } + mail.addMessage(msg_table) + table.insert(already_processed, msg_id) + end + end + end + end) +end function mail.migrate_contacts(playername) + local gen_file_v1 = io.open(minetest.get_worldpath().."/mail.db", "r") + if file then + mail.migrate_contacts_v1_to_v2() + 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! @@ -48,3 +114,11 @@ function mail.migrate_contacts(playername) 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