mirror of
https://github.com/mt-mods/mail.git
synced 2025-04-30 08:21:44 -04:00
Remove checking all UUIDs for generating new one
The risk is ridiculous compared to the performance lost, even for several millions of messages.
This commit is contained in:
parent
2f59e9f0f5
commit
c052e5900e
2 changed files with 47 additions and 49 deletions
48
migrate.lua
48
migrate.lua
|
@ -80,34 +80,70 @@ local function migrate_v2_to_v3()
|
||||||
end)
|
end)
|
||||||
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 function repair_box(playername, box)
|
||||||
local e = mail.get_storage_entry(playername)
|
local e = mail.get_storage_entry(playername)
|
||||||
for _, m in ipairs(e[box]) do
|
for _, m in ipairs(e[box]) do
|
||||||
local uuid = m.id
|
local uuid = m.id
|
||||||
local exists = mail.is_uuid_existing(uuid)
|
local exists = is_uuid_existing(uuid)
|
||||||
if exists and not mail.are_message_sames(exists, m) then
|
if exists and not are_message_sames(exists, m) then
|
||||||
local new_uuid = mail.new_uuid() -- generates a new uuid to replace doublons
|
local new_uuid = mail.new_uuid() -- generates a new uuid to replace doublons
|
||||||
for _, k in ipairs(mail.storage:get_keys()) do
|
for _, k in ipairs(mail.storage:get_keys()) do
|
||||||
if string.sub(k,1,5) == "mail/" then
|
if string.sub(k,1,5) == "mail/" then
|
||||||
local p = string.sub(k, 6)
|
local p = string.sub(k, 6)
|
||||||
local er = mail.get_storage_entry(p)
|
local er = mail.get_storage_entry(p)
|
||||||
for _, r in ipairs(er.inbox) do
|
for _, r in ipairs(er.inbox) do
|
||||||
if r.id == uuid and not mail.are_message_sames(m, r) then
|
if r.id == uuid and not are_message_sames(m, r) then
|
||||||
r.id = new_uuid
|
r.id = new_uuid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for _, r in ipairs(er.outbox) do
|
for _, r in ipairs(er.outbox) do
|
||||||
if r.id == uuid and not mail.are_message_sames(m, r) then
|
if r.id == uuid and not are_message_sames(m, r) then
|
||||||
r.id = new_uuid
|
r.id = new_uuid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for _, r in ipairs(er.drafts) do
|
for _, r in ipairs(er.drafts) do
|
||||||
if r.id == uuid and not mail.are_message_sames(m, r) then
|
if r.id == uuid and not are_message_sames(m, r) then
|
||||||
r.id = new_uuid
|
r.id = new_uuid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for _, r in ipairs(er.trash) do
|
for _, r in ipairs(er.trash) do
|
||||||
if r.id == uuid and not mail.are_message_sames(m, r) then
|
if r.id == uuid and not are_message_sames(m, r) then
|
||||||
r.id = new_uuid
|
r.id = new_uuid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,49 +1,11 @@
|
||||||
-- source: https://gist.github.com/jrus/3197011
|
-- source: https://gist.github.com/jrus/3197011
|
||||||
local random = math.random
|
local random = math.random
|
||||||
|
|
||||||
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
|
|
||||||
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
|
|
||||||
|
|
||||||
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 = mail.search_box(p, b, uuid)
|
|
||||||
if result then return result end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
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()
|
function mail.new_uuid()
|
||||||
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
||||||
local candidate_uuid
|
return string.gsub(template, '[xy]',
|
||||||
repeat
|
|
||||||
candidate_uuid = string.gsub(template, '[xy]',
|
|
||||||
function (c)
|
function (c)
|
||||||
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
|
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
|
||||||
return string.format('%x', v)
|
return string.format('%x', v)
|
||||||
end)
|
end)
|
||||||
until not mail.is_uuid_existing(candidate_uuid)
|
|
||||||
return candidate_uuid
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue