Merge duplicated functions between migrate.lua and util/uuid.lua

This commit is contained in:
Athozus 2024-04-01 16:34:21 +02:00
parent 5515ef9883
commit b04762204e
No known key found for this signature in database
GPG key ID: B50895022E8484BF
2 changed files with 20 additions and 44 deletions

View file

@ -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

View file

@ -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