From 3b7aa53d1d305e0b71de28b6e555aa9659544786 Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 23 Aug 2026 14:51:12 +0200 Subject: [PATCH] plugins: include exhaustiveFlags in clone (#7195) Reviewed-by: Mm2PL Reviewed-by: Nerixyz Parent-pr: 7192 --- docs/lua-meta/globals.lua | 5 +++++ src/controllers/plugins/api/Message.cpp | 5 +++++ src/controllers/plugins/api/Message.hpp | 5 +++++ src/messages/MessageElement.cpp | 10 +++++++++- src/providers/twitch/TwitchIrc.cpp | 2 ++ tests/lua/message/clone.lua | 5 ++++- tests/lua/message/element.lua | 12 ++++++++---- 7 files changed, 38 insertions(+), 6 deletions(-) diff --git a/docs/lua-meta/globals.lua b/docs/lua-meta/globals.lua index 6d58f0e7e..96bdbf117 100644 --- a/docs/lua-meta/globals.lua +++ b/docs/lua-meta/globals.lua @@ -592,6 +592,10 @@ function c2.Menu:insert_separator(before) end c2.MessageElementBase = {} -- ^^^ this is kinda fake - this table doesn't exist in Lua, we only declare it to add methods +--- Returns the pretty-printed JSON representation of the element. +--- This is meant for debugging and is subject to change. +function c2.MessageElementBase:to_json() end + --- Add flags to this element --- ---@param flags c2.MessageElementFlag @@ -602,6 +606,7 @@ function c2.MessageElementBase:add_flags(flags) end ---@field tooltip? string Tooltip text ---@field trailing_space? boolean Whether to add a trailing space after the element (default: true) ---@field link? c2.Link An action when clicking on this element. Mention and Link elements don't support this. They manage the link themselves. +---@field exhaustive_flags? boolean Whether this message should only be laid out if all its flags exist in the message layout context. ---@class c2.TextElement : c2.MessageElementBase ---@field type "text" diff --git a/src/controllers/plugins/api/Message.cpp b/src/controllers/plugins/api/Message.cpp index 6c8603628..26ccc8516 100644 --- a/src/controllers/plugins/api/Message.cpp +++ b/src/controllers/plugins/api/Message.cpp @@ -11,6 +11,7 @@ # include "messages/Message.hpp" # include "messages/MessageElement.hpp" +# include # include namespace { @@ -590,6 +591,10 @@ void createUserType(sol::table &c2) "type", sol::property([](const ElementRef &el) { return el.cref().type(); }), + "to_json", + [](const ElementRef &el) { + return QJsonDocument(el.cref().toJson()).toJson(); + }, "flags", sol::property([](const ElementRef &el) { return el.cref().getFlags().value(); }), diff --git a/src/controllers/plugins/api/Message.hpp b/src/controllers/plugins/api/Message.hpp index cd74c3b96..adc723f06 100644 --- a/src/controllers/plugins/api/Message.hpp +++ b/src/controllers/plugins/api/Message.hpp @@ -24,6 +24,10 @@ namespace chatterino::lua::api::message { c2.MessageElementBase = {} -- ^^^ this is kinda fake - this table doesn't exist in Lua, we only declare it to add methods +--- Returns the pretty-printed JSON representation of the element. +--- This is meant for debugging and is subject to change. +function c2.MessageElementBase:to_json() end + --- Add flags to this element --- ---@param flags c2.MessageElementFlag @@ -34,6 +38,7 @@ function c2.MessageElementBase:add_flags(flags) end ---@field tooltip? string Tooltip text ---@field trailing_space? boolean Whether to add a trailing space after the element (default: true) ---@field link? c2.Link An action when clicking on this element. Mention and Link elements don't support this. They manage the link themselves. +---@field exhaustive_flags? boolean Whether this message should only be laid out if all its flags exist in the message layout context. ---@class c2.TextElement : c2.MessageElementBase ---@field type "text" diff --git a/src/messages/MessageElement.cpp b/src/messages/MessageElement.cpp index 4f2c53e71..b9b2aa1b6 100644 --- a/src/messages/MessageElement.cpp +++ b/src/messages/MessageElement.cpp @@ -115,6 +115,7 @@ void MessageElement::cloneFrom(const MessageElement &source) this->tooltip_ = source.tooltip_; this->flags_ = source.flags_; this->trailingSpace = source.trailingSpace; + this->exhaustiveFlags = source.exhaustiveFlags; } bool MessageElement::matchesFlags(MessageElementFlags contextFlags) const @@ -125,7 +126,7 @@ bool MessageElement::matchesFlags(MessageElementFlags contextFlags) const QJsonObject MessageElement::toJson() const { - return { + QJsonObject msg{ {"trailingSpace"_L1, this->trailingSpace}, { "link"_L1, @@ -137,6 +138,13 @@ QJsonObject MessageElement::toJson() const {"tooltip"_L1, this->tooltip_}, {"flags"_L1, qmagicenum::enumFlagsName(this->flags_.value())}, }; + + if (this->exhaustiveFlags) + { + msg["exhaustiveFlags"_L1] = this->exhaustiveFlags; + } + + return msg; } // IMAGE diff --git a/src/providers/twitch/TwitchIrc.cpp b/src/providers/twitch/TwitchIrc.cpp index b3c77d210..1c344dd99 100644 --- a/src/providers/twitch/TwitchIrc.cpp +++ b/src/providers/twitch/TwitchIrc.cpp @@ -12,6 +12,8 @@ #include "util/Helpers.hpp" #include "util/IrcHelpers.hpp" +#include + namespace { using namespace chatterino; diff --git a/tests/lua/message/clone.lua b/tests/lua/message/clone.lua index a3ae7269a..85b9c7492 100644 --- a/tests/lua/message/clone.lua +++ b/tests/lua/message/clone.lua @@ -22,7 +22,7 @@ local tests = { server_received_time = 123345678, highlight_color = "#00ff00", elements = { - { type = "text", text = "abcde" }, + { type = "text", text = "abcde", exhaustive_flags = true }, { type = "twitch-moderation" }, }, }) @@ -47,6 +47,9 @@ local tests = { assert(msg.server_received_time == clone.server_received_time) assert(msg.highlight_color == clone.highlight_color) assert(#msg:elements() == #clone:elements()) + for i, element in pairs(msg:elements()) do + assert(element:to_json() == clone:elements()[i]:to_json()) + end end, } diff --git a/tests/lua/message/element.lua b/tests/lua/message/element.lua index f77921608..cd2c3da33 100644 --- a/tests/lua/message/element.lua +++ b/tests/lua/message/element.lua @@ -20,13 +20,13 @@ local tests = { local new_msg = c2.Message.new(msg) local append_init_ok, append_init_err = pcall(function() new_msg:append_element({ - type = 'text', - text = 'foo' + type = "text", + text = "foo", }) end) assert(append_init_ok) assert(new_msg:elements()[1]) - assert(new_msg:elements()[1].type == 'text' and new_msg:elements()[1].words[1] == 'foo') + assert(new_msg:elements()[1].type == "text" and new_msg:elements()[1].words[1] == "foo") local append_element_ok, append_element_err = pcall(function() for index, element in ipairs(msg:elements()) do new_msg:append_element(element) @@ -34,7 +34,7 @@ local tests = { end) assert(append_element_ok) assert(new_msg:elements()[2]) - assert(new_msg:elements()[2].type == 'text' and new_msg:elements()[2].words[1] == 'abcde') + assert(new_msg:elements()[2].type == "text" and new_msg:elements()[2].words[1] == "abcde") end, exhaustive_flags = function() local msg = c2.Message.new({ @@ -59,6 +59,10 @@ local tests = { assert(msg:elements()[1].type == "text" and msg:elements()[1].exhaustive_flags == false) assert(msg:elements()[2].type == "text" and msg:elements()[2].exhaustive_flags == false) assert(msg:elements()[3].type == "text" and msg:elements()[3].exhaustive_flags == true) + local cloned = msg:clone() + assert(cloned:elements()[1].type == "text" and cloned:elements()[1].exhaustive_flags == false) + assert(cloned:elements()[2].type == "text" and cloned:elements()[2].exhaustive_flags == false) + assert(cloned:elements()[3].type == "text" and cloned:elements()[3].exhaustive_flags == true) end, }