View sent messages (new database, add maillists) (#26)

* Add tabheader & sent formspec

* Add show_sent function and show sent messages

* Remove comment on selected_idxs test (show_sent)

* Add variable to keep the previous tab instead of going back to the first one

* Remove index variable verification on mark read/unread buttons since they are necessarily clicked on inbox view

* Resize messages table to be aligned with close button at the bottom

* Show date time (#27)

* Show date in message reading

* Fix wrong registered dates

Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>

* Rework header layout to add better space for date

---------

Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>

* Add insertion of messages into global storage mail.messages.json

* Receive player messages from global storage

* Add automatic generation of status for a new message (unread)

* Mark read/unread/delete a message

* Fix messages loading

* Show every message received/sent via specific functions

* Use global contacts functions and reconfigure add/remove functions

* Create mail lists formspec based on contacts

* Add deleting contact

* Add ability to create mail lists

* Fix inability to edit contact

* Rework on editing/deletion of contacts/maillists

* Add at symbol as prefix on maillists view

* Add ability to choose default status (to/cc/bcc)

Signed-off-by: Athozus <athozus@gmail.com>

* Add ability to add multiples players and choose their default status (to/cc/bcc)

* Add ability to use maillist in messages and receive messages from them

* Fix repetition of code causing a crash

* Avoid multiples occurences of the same messages due to player both in maillist and receivers

* Fix selected indexes for inbox/sent

Now separated, fixed show_message() func selection of id from table dcl/read btn

* Fix many issues related to maillists

Notably : edit, delete, selection, creation, registration of players

* Set up database version v3 and its migration from v2

+ Check versions to choose v1->v2 or v2->v3

* Fix mtt.lua

Due to old function getMessages(), replaced by getPlayerInboxMessages()

* Add 10 seconds security to mtt.lua

* Fix migrate.lua non-declared variable

* Send msg table with string keys in mtt

* Better log messages

* Add message check

* Fix mtt crash

* Better syntax in storage.lua

* Fix bcc forgotten in mail.send()

* Fix mtt issue

* Better compatibility for messages storage

Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>

* Replace mail.split by builtin func

Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>

* Use builtin split func

Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>

* Use builtin split func in storage.lua

* re-add mtt if

* luacheck on PR

* add check for an ancient issue with missing `to` field

* Fix luacheck on storage.lua

* Fix luacheck warnings in migrate.lua

* Fix luacheck warnings in gui.lua

* Fix luacheck (too long lines) in storage.lua

* Unused loop values in migrate.lua

* Whitespace line in gui.lua

* Whitespace line (init.lua)

* Whitespace line (api.lua)

* Significantly improve maillist behaviour

Replace maillist by its players when sending a message
List of players separated by ,
Avoid doublons when editing more than 2 times a maillist

* Fix luacheck

* Fix table insertions at first index when no needed

* Use funcs

* Do not add maillist as a new contact when sending a mail

* Fix removing elements from tables

* Check maillists not added in contacts

* storage rewrite wip

* storage format docs

* refactor ui components

* show_compose cleanup

* remove unused channel.lua

* error -> err

* status refactoring

* contacts refactoring

* maillist refactoring

* docs

* tests

* fix some issues

* re-enable migrations

* contributors

* prefix mail entries in the mod storage

* internalize old mail-paths to migration module

* add v1 and v2 player db examples and migration test

* Ui improvements & fixes

Move events code (if fields.x then) to events.lua (instead of inbox.lua), fix tab selection when going backward

* Show most recent messages at first (outbox)

* unified-inv fix

---------

Signed-off-by: Athozus <athozus@gmail.com>
Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com>
Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
This commit is contained in:
Athozus 2023-03-29 17:25:01 +02:00 committed by GitHub
parent b0a5bc7e47
commit b3e0c158f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 1500 additions and 960 deletions

View file

@ -1,47 +1,195 @@
-- storage getter/setter
local STORAGE_PREFIX = "mail/"
function mail.getMailFile(playername)
local saneplayername = string.gsub(playername, "[.|/]", "")
return mail.maildir .. "/" .. saneplayername .. ".json"
-- create or populate empty fields on an entry
local function populate_entry(e)
e = e or {}
e.contacts = e.contacts or {}
e.inbox = e.inbox or {}
e.outbox = e.outbox or {}
e.lists = e.lists or {}
return e
end
function mail.getContactsFile(playername)
local saneplayername = string.gsub(playername, "[.|/]", "")
return mail.maildir .. "/contacts/" .. saneplayername .. ".json"
end
function mail.getMessages(playername)
local messages = mail.read_json_file(mail.getMailFile(playername))
if messages then
for _, msg in ipairs(messages) do
if not msg.time then
-- add missing time field if not available (happens with old data)
msg.time = 0
end
end
-- sort by received date descending
table.sort(messages, function(a,b) return a.time > b.time end)
-- show hud notification
mail.hud_update(playername, messages)
end
return messages
end
function mail.setMessages(playername, messages)
if mail.write_json_file(mail.getMailFile(playername), messages) then
mail.hud_update(playername, messages)
return true
function mail.get_storage_entry(playername)
local str = mail.storage:get_string(STORAGE_PREFIX .. playername)
if str == "" then
-- new entry
return populate_entry()
else
minetest.log("error","[mail] Save failed - messages may be lost! ("..playername..")")
return false
-- deserialize existing entry
local e = minetest.parse_json(str)
return populate_entry(e)
end
end
function mail.set_storage_entry(playername, entry)
mail.storage:set_string(STORAGE_PREFIX .. playername, minetest.write_json(entry))
end
function mail.getContacts(playername)
return mail.read_json_file(mail.getContactsFile(playername))
-- get a mail by id from the players in- or outbox
function mail.get_message(playername, msg_id)
local entry = mail.get_storage_entry(playername)
for _, msg in ipairs(entry.inbox) do
if msg.id == msg_id then
return msg
end
end
for _, msg in ipairs(entry.outbox) do
if msg.id == msg_id then
return msg
end
end
end
-- marks a mail read by its id
function mail.mark_read(playername, msg_id)
local entry = mail.get_storage_entry(playername)
for _, msg in ipairs(entry.inbox) do
if msg.id == msg_id then
msg.read = true
mail.set_storage_entry(playername, entry)
return
end
end
end
-- marks a mail unread by its id
function mail.mark_unread(playername, msg_id)
local entry = mail.get_storage_entry(playername)
for _, msg in ipairs(entry.inbox) do
if msg.id == msg_id then
msg.read = false
mail.set_storage_entry(playername, entry)
return
end
end
end
-- deletes a mail by its id
function mail.delete_mail(playername, msg_id)
local entry = mail.get_storage_entry(playername)
for i, msg in ipairs(entry.inbox) do
if msg.id == msg_id then
table.remove(entry.outbox, i)
mail.set_storage_entry(playername, entry)
return
end
end
for i, msg in ipairs(entry.outbox) do
if msg.id == msg_id then
table.remove(entry.outbox, i)
mail.set_storage_entry(playername, entry)
return
end
end
end
-- add or update a contact
function mail.update_contact(playername, contact)
local entry = mail.get_storage_entry(playername)
local existing_updated = false
for i, existing_contact in ipairs(entry.contacts) do
if existing_contact.name == contact.name then
-- update
entry.contacts[i] = contact
existing_updated = true
break
end
end
if not existing_updated then
-- insert
table.insert(entry.contacts, contact)
end
mail.set_storage_entry(playername, entry)
end
-- deletes a contact
function mail.delete_contact(playername, contactname)
local entry = mail.get_storage_entry(playername)
for i, existing_contact in ipairs(entry.contacts) do
if existing_contact.name == contactname then
-- delete
table.remove(entry.contacts, i)
mail.set_storage_entry(playername, entry)
return
end
end
end
-- get all contacts
function mail.get_contacts(playername)
local entry = mail.get_storage_entry(playername)
return entry.contacts
end
-- returns the maillists of a player
function mail.get_maillists(playername)
local entry = mail.get_storage_entry(playername)
return entry.lists
end
-- returns the maillists of a player
function mail.get_maillist_by_name(playername, listname)
local entry = mail.get_storage_entry(playername)
for _, list in ipairs(entry.lists) do
if list.name == listname then
return list
end
end
end
-- updates or creates a maillist
function mail.update_maillist(playername, list)
local entry = mail.get_storage_entry(playername)
local existing_updated = false
for i, existing_list in ipairs(entry.lists) do
if existing_list.name == list.name then
-- update
entry.lists[i] = list
existing_updated = true
break
end
end
if not existing_updated then
-- insert
table.insert(entry.lists, list)
end
mail.set_storage_entry(playername, entry)
end
function mail.delete_maillist(playername, listname)
local entry = mail.get_storage_entry(playername)
for i, list in ipairs(entry.lists) do
if list.name == listname then
-- delete
table.remove(entry.lists, i)
mail.set_storage_entry(playername, entry)
return
end
end
end
function mail.extractMaillists(receivers_string, maillists_owner)
local globalReceivers = mail.parse_player_list(receivers_string) -- receivers including maillists
local receivers = {} -- extracted receivers
-- extract players from mailing lists
for _, receiver in ipairs(globalReceivers) do
local receiverInfo = receiver:split("@") -- @maillist
if receiverInfo[1] and receiver == "@" .. receiverInfo[1] then
local maillist = mail.get_maillist_by_name(maillists_owner, receiverInfo[1])
if maillist then
for _, playername in ipairs(maillist.players) do
table.insert(receivers, playername)
end
end
else -- in case of player
table.insert(receivers, receiver)
end
end
return receivers
end
function mail.pairsByKeys(t, f)
@ -63,33 +211,3 @@ function mail.pairsByKeys(t, f)
return iter
end
function mail.setContacts(playername, contacts)
if mail.write_json_file(mail.getContactsFile(playername), contacts) then
return true
else
minetest.log("error","[mail] Save failed - contacts may be lost! ("..playername..")")
return false
end
end
function mail.read_json_file(path)
local file = io.open(path, "r")
local content = {}
if file then
local json = file:read("*a")
content = minetest.parse_json(json or "[]") or {}
file:close()
end
return content
end
function mail.write_json_file(path, content)
local file = io.open(path,"w")
local json = minetest.write_json(content)
if file and file:write(json) and file:close() then
return true
else
return false
end
end