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
@@ -234,6 +234,8 @@ void PluginController::initSol(sol::state_view &lua, Plugin *plugin)
c2["MessageElementFlag"] = lua::createEnumTable<MessageElementFlag>(lua);
c2["FontStyle"] = lua::createEnumTable<FontStyle>(lua);
c2["MessageContext"] = lua::createEnumTable<MessageContext>(lua);
c2["LinkType"] =
lua::createEnumTable<lua::api::message::ExposedLinkType>(lua);
sol::table io = g["io"];
io.set_function(
+44
View File
@@ -3,8 +3,10 @@
# include "Application.hpp"
# include "common/QLogging.hpp"
# include "controllers/plugins/api/Message.hpp"
# include "controllers/plugins/LuaAPI.hpp"
# include "controllers/plugins/PluginController.hpp"
# include "messages/Link.hpp"
# include <QObject>
# include <QStringBuilder>
@@ -187,6 +189,48 @@ int sol_lua_push(sol::types<chatterino::lua::ThisPluginState>, lua_State *L,
} // namespace chatterino::lua
namespace chatterino {
// Link
bool sol_lua_check(sol::types<chatterino::Link>, lua_State *L, int index,
chatterino::FunctionRef<sol::check_handler_type> handler,
sol::stack::record &tracking)
{
return sol::stack::check<sol::table>(L, index, handler, tracking);
}
chatterino::Link sol_lua_get(sol::types<chatterino::Link>, lua_State *L,
int index, sol::stack::record &tracking)
{
sol::table table = sol::stack::get<sol::table>(L, index, tracking);
auto ty =
table.get<sol::optional<lua::api::message::ExposedLinkType>>("type");
if (!ty)
{
throw std::runtime_error("Missing 'type' in Link");
}
auto value = table.get<sol::optional<QString>>("value");
if (!value)
{
throw std::runtime_error("Missing 'value' in Link");
}
return {static_cast<Link::Type>(*ty), *value};
}
int sol_lua_push(sol::types<chatterino::Link>, lua_State *L,
const chatterino::Link &value)
{
sol::table table = sol::table::create(L, 0, 2);
table.set("type",
static_cast<lua::api::message::ExposedLinkType>(value.type));
table.set("value", value.value);
return sol::stack::push(L, table);
}
} // namespace chatterino
// NOLINTEND(readability-named-parameter)
#endif
+7
View File
@@ -25,6 +25,7 @@ constexpr bool IsOptional<std::optional<T>> = true;
namespace chatterino {
class Plugin;
struct Link;
} // namespace chatterino
@@ -189,6 +190,12 @@ SOL_STACK_FUNCTIONS(chatterino::lua::ThisPluginState)
} // namespace chatterino::lua
namespace chatterino {
SOL_STACK_FUNCTIONS(chatterino::Link)
} // namespace chatterino
SOL_STACK_FUNCTIONS(QString)
SOL_STACK_FUNCTIONS(QStringList)
SOL_STACK_FUNCTIONS(QByteArray)
+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;
}
+18
View File
@@ -1,9 +1,12 @@
#pragma once
#ifdef CHATTERINO_HAVE_PLUGINS
# include "messages/Link.hpp"
# include "messages/Message.hpp"
# include <sol/forward.hpp>
# include <cstdint>
namespace chatterino::lua::api::message {
/* @lua-fragment
@@ -32,6 +35,7 @@ c2.Message = {}
---@class MessageElementInitBase
---@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.
---@alias MessageColor "text"|"link"|"system"|string A color for a text element - "text", "link", and "system" are special values that take the current theme into account
@@ -86,6 +90,20 @@ c2.Message = {}
function c2.Message.new(init) end
*/
/** @lua@alias c2.Link { type: c2.LinkType, value: string } A link on a message element. */
// We only want certain links to be usable
// Note: code dependant on this needs these values to be meaningful `Link::Type`s
/** @exposeenum c2.LinkType */
enum class ExposedLinkType : std::uint8_t {
Url = Link::Type::Url,
UserInfo = Link::Type::UserInfo,
UserAction = Link::Type::UserAction, // run a command/send message
JumpToChannel = Link::Type::JumpToChannel,
CopyToClipboard = Link::Type::CopyToClipboard,
JumpToMessage = Link::Type::JumpToMessage,
};
/**
* @includefile singletons/Fonts.hpp
* @includefile messages/MessageElement.hpp
-4
View File
@@ -9,13 +9,9 @@ public:
enum Type {
None,
Url,
CloseCurrentSplit,
UserInfo,
UserTimeout,
UserBan,
UserWhisper,
InsertText,
ShowMessage,
UserAction,
AutoModAllow,
AutoModDeny,
+14 -2
View File
@@ -1106,6 +1106,7 @@ void TimestampElement::addToContainer(MessageLayoutContainer &container,
{
if (ctx.flags.hasAny(this->getFlags()))
{
this->setTooltip(this->getTooltip());
if (getSettings()->timestampFormat != this->format_)
{
this->format_ = getSettings()->timestampFormat.getValue();
@@ -1122,8 +1123,19 @@ TextElement *TimestampElement::formatTime(const QTime &time)
QString format = locale.toString(time, getSettings()->timestampFormat);
return new TextElement(format, MessageElementFlag::Timestamp,
MessageColor::System, FontStyle::TimestampMedium);
auto *text =
new TextElement(format, MessageElementFlag::Timestamp,
MessageColor::System, FontStyle::TimestampMedium);
text->setLink(this->getLink());
text->setTooltip(this->getTooltip());
return text;
}
MessageElement *TimestampElement::setLink(const Link &link)
{
MessageElement::setLink(link);
this->element_->setLink(link);
return this;
}
QJsonObject TimestampElement::toJson() const
+1
View File
@@ -508,6 +508,7 @@ public:
const MessageLayoutContext &ctx) override;
TextElement *formatTime(const QTime &time);
MessageElement *setLink(const Link &link) override;
QJsonObject toJson() const override;