From 103c4ae441a3294ddbba2b71a2ca2e6a41fe87c9 Mon Sep 17 00:00:00 2001 From: Athozus Date: Fri, 29 Dec 2023 22:51:42 +0100 Subject: [PATCH] Add mute list setting Add sharing between mute_list and beerchat Add check for mute list --- storage.lua | 7 +++++-- ui/inbox.lua | 3 +++ ui/message.lua | 15 ++++++++++++++- util/colors.lua | 1 + util/settings.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/storage.lua b/storage.lua index 5bb0c56..0348f35 100644 --- a/storage.lua +++ b/storage.lua @@ -441,8 +441,11 @@ function mail.get_setting(playername, key) or {entry.settings[key]})[1] if mail.settings[key].sync then -- in case this setting is shared with another mod - value = mail.settings[key].sync(playername, key) -- get new value - mail.set_setting(playername, key, value, false) -- update the setting in mail storage and don't transfer it again + local sync_value = mail.settings[key].sync(playername) -- get new value + if sync_value then + value = sync_value + mail.set_setting(playername, key, value, true) -- update the setting in mail storage and don't transfer it again + end end return value diff --git a/ui/inbox.lua b/ui/inbox.lua index 220d70d..4d49837 100644 --- a/ui/inbox.lua +++ b/ui/inbox.lua @@ -103,6 +103,9 @@ function mail.show_inbox(name, sortfieldindex, sortdirection, filter) if message.spam then table.insert(displayed_color, "warning") end + if table.indexof(mail.get_setting(name, "mute_list"), message.from) >= 1 then + table.insert(displayed_color, "muted") + end formspec[#formspec + 1] = "," .. mail.get_color(displayed_color) formspec[#formspec + 1] = "," formspec[#formspec + 1] = minetest.formspec_escape(message.from) diff --git a/ui/message.lua b/ui/message.lua index 55c0acf..f8c46c7 100644 --- a/ui/message.lua +++ b/ui/message.lua @@ -37,10 +37,13 @@ function mail.show_message(name, id) button[7.25,1.0;2.75,1;reply;]] .. S("Reply") .. [[] button[7.25,1.8;2.75,1;replyall;]] .. S("Reply all") .. [[] button[7.25,2.6;2.75,1;forward;]] .. S("Forward") .. [[] + button[7.25,3.6;2.75,1;markspam;]] .. S("Mark Spam") .. [[] button[7.25,4.4;2.75,1;unmarkspam;]] .. S("Unmark Spam") .. [[] - box[7.25,5.4;2.5,4.0;]] .. mail.get_color("disabled") .. [[] + button[7.25,5.4;2.75,1;togglemute;]] .. S("(Un)mute sender") .. [[] + + box[7.25,6.4;2.5,3.0;]] .. mail.get_color("disabled") .. [[] button[7.25,9.5;2.75,1;delete;]] .. S("Delete") .. [[] @@ -145,6 +148,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) elseif fields.unmarkspam then mail.unmark_spam(name, message.id) + elseif fields.togglemute then + local mutes = table.copy(mail.get_setting(name, "mute_list")) + local mute_indexof = table.indexof(mutes, message.from) + if mute_indexof == -1 then -- mute + table.insert(mutes, message.from) + else -- unmute + table.remove(mutes, mute_indexof) + end + mail.set_setting(name, "mute_list", mutes) + elseif fields.delete then if mail.get_setting(name, "trash_move_enable") and mail.selected_idxs.boxtab[name] ~= 4 then mail.trash_mail(name, message.id) diff --git a/util/colors.lua b/util/colors.lua index cd88ad7..adc9aa4 100644 --- a/util/colors.lua +++ b/util/colors.lua @@ -7,6 +7,7 @@ local generic_colors = { new = "#00F529", warning = "#FF8800", disabled = "#332222", + muted = "#CCCCCC", } local function get_base_color(c) diff --git a/util/settings.lua b/util/settings.lua index b0f345b..92da8f5 100644 --- a/util/settings.lua +++ b/util/settings.lua @@ -45,11 +45,16 @@ mail.settings = { type = "string", default = "%Y-%m-%d %X", group = "other", index = 3, label = S("Date format"), dataset = {"%Y-%m-%d %X", "%d/%m/%y %X", "%A %d %B %Y %X"}, format = os.date }, + mute_list = { + type = "list", default = {}, group = "spam", index = 1, + label = S("Mute list") + }, } mail.settings_groups = { { name = "notifications", label = S("Notifications")}, { name = "message_list", label = S("Message list")}, + { name = "spam", label = S("Spam")}, { name = "other", label = S("Other")} } @@ -59,3 +64,44 @@ for s, d in pairs(mail.settings) do mail.selected_idxs["index_" .. s] = {} end end + +function mail.settings.mute_list.check(name, value) + local valid_players = {} + for _, p in ipairs(value) do + if p ~= name and minetest.player_exists(p) then + table.insert(valid_players, p) + end + end + return valid_players +end + +function mail.settings.mute_list.sync(name) + if minetest.get_modpath("beerchat") then + local players = {} + for other_player, _ in minetest.get_auth_handler().iterate() do + if beerchat.has_player_muted_player(name, other_player) then + table.insert(players, other_player) + end + end + return players + end + return nil +end + +function mail.settings.mute_list.transfer(name, value) + if minetest.get_modpath("beerchat") then + for other_player, _ in minetest.get_auth_handler().iterate() do -- unmute all + if not beerchat.execute_callbacks("before_mute", name, other_player) then + return false + end + minetest.get_player_by_name(name):get_meta():set_string( + "beerchat:muted:" .. other_player, "") + end + for _, other_player in ipairs(value) do -- then mute only players in table + minetest.get_player_by_name(name):get_meta():set_string( + "beerchat:muted:" .. other_player, "true") + end + return true + end + return nil +end