Add support for plugin links (#6386)

Co-authored-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
Mm2PL
2025-10-02 20:56:51 +02:00
committed by GitHub
parent 3f05edc071
commit 33f6383122
14 changed files with 190 additions and 16 deletions
+55 -6
View File
@@ -1,12 +1,11 @@
#include "controllers/plugins/api/Message.hpp"
#include "Application.hpp"
#include "messages/MessageElement.hpp"
#ifdef CHATTERINO_HAVE_PLUGINS
# include "controllers/plugins/api/Message.hpp"
# include "Application.hpp"
# include "controllers/plugins/LuaUtilities.hpp"
# include "controllers/plugins/SolTypes.hpp"
# include "messages/Message.hpp"
# include "messages/MessageElement.hpp"
# include <sol/sol.hpp>
@@ -128,6 +127,7 @@ std::unique_ptr<MessageElement> elementFromTable(const sol::table &tbl)
{
auto type = requiredGet<QString>(tbl, "type");
std::unique_ptr<MessageElement> el;
bool linksAllowed = true;
if (type == u"text")
{
el = textElementFromTable(tbl);
@@ -139,6 +139,7 @@ std::unique_ptr<MessageElement> elementFromTable(const sol::table &tbl)
else if (type == u"mention")
{
el = mentionElementFromTable(tbl);
linksAllowed = false;
}
else if (type == u"timestamp")
{
@@ -155,6 +156,7 @@ std::unique_ptr<MessageElement> elementFromTable(const sol::table &tbl)
else if (type == u"reply-curve")
{
el = replyCurveElementFromTable();
linksAllowed = false;
}
else
{
@@ -163,7 +165,54 @@ std::unique_ptr<MessageElement> elementFromTable(const sol::table &tbl)
assert(el);
el->setTrailingSpace(tbl.get_or("trailing_space", true));
el->setTooltip(tbl.get_or("tooltip", QString{}));
auto link = tbl.get<sol::optional<Link>>("link");
if (link)
{
if (!linksAllowed)
{
throw std::runtime_error("'link' not supported on type='" +
type.toStdString() + '\'');
}
el->setLink(*link);
QString tooltip;
switch (link->type)
{
case Link::Url:
tooltip = QString("<b>URL:</b> %1").arg(link->value);
break;
case Link::UserAction:
tooltip = QString("<b>Command:</b> %1").arg(link->value);
break;
case Link::CopyToClipboard:
tooltip = "<b>Copy to clipboard</b>";
break;
// these links should be safe to click as they don't have any immediate action associated with them
case Link::JumpToChannel:
case Link::JumpToMessage:
case Link::UserInfo:
case Link::UserWhisper:
case Link::ReplyToMessage:
break;
// these types are not exposed to plugins
case Link::None:
case Link::AutoModAllow:
case Link::AutoModDeny:
case Link::InsertText:
case Link::OpenAccountsPage:
case Link::Reconnect:
throw std::runtime_error(
"Invalid link type. How'd this happen?");
}
el->setTooltip(tooltip);
}
else
{
el->setTooltip(tbl.get_or("tooltip", QString{}));
}
return el;
}