From b04762204eaa4368a5e681e25077cefb7b444732 Mon Sep 17 00:00:00 2001 From: Athozus Date: Mon, 1 Apr 2024 16:34:21 +0200 Subject: [PATCH] Merge duplicated functions between migrate.lua and util/uuid.lua --- migrate.lua | 46 ++++++---------------------------------------- util/uuid.lua | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/migrate.lua b/migrate.lua index 8753bb9..d35c9db 100644 --- a/migrate.lua +++ b/migrate.lua @@ -80,68 +80,34 @@ local function migrate_v2_to_v3() end) end -local function search_box(playername, box, uuid) - local e = mail.get_storage_entry(playername) - for _, m in ipairs(e[box]) do - if m.id == uuid then - return { time = m.time, from = m.from, to = m.to, cc = m.cc, bcc = m.bcc, subject = m.subject, body = m.body } end - end - return false -end - -local function is_uuid_existing(uuid) - for _, k in ipairs(mail.storage:get_keys()) do - if string.sub(k,1,5) == "mail/" then - local p = string.sub(k, 6) - local result - local boxes = {"inbox", "outbox", "drafts", "trash"} - for _, b in ipairs(boxes) do - result = search_box(p, b, uuid) - if result then return result end - end - end - end - return false -end - -local function are_message_sames(a, b) - return a.time == b.time - and a.from == b.from - and a.to == b.to - and a.cc == b.cc - and a.bcc == b.bcc - and a.subject == b.subject - and a.body == b.body -end - local function repair_box(playername, box) local e = mail.get_storage_entry(playername) for _, m in ipairs(e[box]) do local uuid = m.id - local exists = is_uuid_existing(uuid) - if exists and not are_message_sames(exists, m) then + local exists = mail.is_uuid_existing(uuid) + if exists and not mail.are_message_sames(exists, m) then local new_uuid = mail.new_uuid() -- generates a new uuid to replace doublons for _, k in ipairs(mail.storage:get_keys()) do if string.sub(k,1,5) == "mail/" then local p = string.sub(k, 6) local er = mail.get_storage_entry(p) for _, r in ipairs(er.inbox) do - if r.id == uuid and not are_message_sames(m, r) then + if r.id == uuid and not mail.are_message_sames(m, r) then r.id = new_uuid end end for _, r in ipairs(er.outbox) do - if r.id == uuid and not are_message_sames(m, r) then + if r.id == uuid and not mail.are_message_sames(m, r) then r.id = new_uuid end end for _, r in ipairs(er.drafts) do - if r.id == uuid and not are_message_sames(m, r) then + if r.id == uuid and not mail.are_message_sames(m, r) then r.id = new_uuid end end for _, r in ipairs(er.trash) do - if r.id == uuid and not are_message_sames(m, r) then + if r.id == uuid and not mail.are_message_sames(m, r) then r.id = new_uuid end end diff --git a/util/uuid.lua b/util/uuid.lua index 49c878e..17127bd 100644 --- a/util/uuid.lua +++ b/util/uuid.lua @@ -1,7 +1,7 @@ -- source: https://gist.github.com/jrus/3197011 local random = math.random -local function search_box(playername, box, uuid) +function mail.search_box(playername, box, uuid) local e = mail.get_storage_entry(playername) for _, m in ipairs(e[box]) do if m.id == uuid then @@ -10,14 +10,14 @@ local function search_box(playername, box, uuid) return false end -local function is_uuid_existing(uuid) +function mail.is_uuid_existing(uuid) for _, k in ipairs(mail.storage:get_keys()) do if string.sub(k,1,5) == "mail/" then local p = string.sub(k, 6) local result local boxes = {"inbox", "outbox", "drafts", "trash"} for _, b in ipairs(boxes) do - result = search_box(p, b, uuid) + result = mail.search_box(p, b, uuid) if result then return result end end end @@ -25,6 +25,16 @@ local function is_uuid_existing(uuid) return false end +function mail.are_message_sames(a, b) + return a.time == b.time + and a.from == b.from + and a.to == b.to + and a.cc == b.cc + and a.bcc == b.bcc + and a.subject == b.subject + and a.body == b.body +end + function mail.new_uuid() local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' local candidate_uuid @@ -34,6 +44,6 @@ function mail.new_uuid() local v = (c == 'x') and random(0, 0xf) or random(8, 0xb) return string.format('%x', v) end) - until not is_uuid_existing(candidate_uuid) + until not mail.is_uuid_existing(candidate_uuid) return candidate_uuid end