feat(plugins): minimal account API (#6554)

Co-authored-by: Mm2PL <mm2pl+gh@kotmisia.pl>
Reviewed-by: Mm2PL <mm2pl+gh@kotmisia.pl>
This commit is contained in:
nerix
2025-12-05 15:56:51 +01:00
committed by GitHub
parent c4ed716c33
commit c67953d14f
10 changed files with 266 additions and 0 deletions
+1
View File
@@ -82,6 +82,7 @@ sol::table toTable(lua_State *L, const CompletionEvent &ev);
/**
* @includefile common/Channel.hpp
* @includefile controllers/plugins/api/Accounts.hpp
* @includefile controllers/plugins/api/ChannelRef.hpp
* @includefile controllers/plugins/api/HTTPResponse.hpp
* @includefile controllers/plugins/api/HTTPRequest.hpp
@@ -7,6 +7,7 @@
# include "common/QLogging.hpp"
# include "controllers/commands/CommandContext.hpp"
# include "controllers/commands/CommandController.hpp"
# include "controllers/plugins/api/Accounts.hpp"
# include "controllers/plugins/api/ChannelRef.hpp"
# include "controllers/plugins/api/HTTPRequest.hpp"
# include "controllers/plugins/api/HTTPResponse.hpp"
@@ -232,6 +233,7 @@ void PluginController::initSol(sol::state_view &lua, Plugin *plugin)
lua::api::HTTPRequest::createUserType(c2);
lua::api::WebSocket::createUserType(c2, plugin);
lua::api::message::createUserType(c2);
lua::api::createAccounts(c2);
c2["ChannelType"] = lua::createEnumTable<Channel::Type>(lua);
c2["HTTPMethod"] = lua::createEnumTable<NetworkRequestType>(lua);
c2["EventType"] = lua::createEnumTable<lua::api::EventType>(lua);
+97
View File
@@ -0,0 +1,97 @@
#include "controllers/plugins/api/Accounts.hpp"
#include "Application.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/plugins/SolTypes.hpp" // IWYU pragma: keep
#include "providers/twitch/TwitchAccount.hpp"
#include "util/WeakPtrHelpers.hpp"
#include <sol/sol.hpp>
#include <optional>
namespace {
using namespace chatterino;
class WeakTwitchAccount
{
public:
WeakTwitchAccount(std::weak_ptr<TwitchAccount> account)
: weak(std::move(account))
{
}
QString login() const
{
return this->shared()->getUserName();
}
QString id() const
{
return this->shared()->getUserId();
}
std::optional<QString> color() const
{
auto c = this->shared()->color();
if (c.isValid())
{
return c.name(QColor::HexArgb);
}
return std::nullopt;
}
bool isAnon() const
{
return this->shared()->isAnon();
}
bool isValid() const
{
return !this->weak.expired();
}
bool operator==(const WeakTwitchAccount &other) const
{
return weakOwnerEquals(this->weak, other.weak);
}
private:
std::shared_ptr<TwitchAccount> shared() const
{
auto shared = this->weak.lock();
if (!shared)
{
throw std::runtime_error("Expired account");
}
return shared;
}
std::weak_ptr<TwitchAccount> weak;
};
} // namespace
namespace chatterino::lua::api {
void createAccounts(sol::table &c2)
{
c2.new_usertype<WeakTwitchAccount>(
// clang-format off
"TwitchAccount", sol::no_constructor,
sol::meta_function::to_string, &WeakTwitchAccount::login,
"is_valid", &WeakTwitchAccount::isValid,
"login", &WeakTwitchAccount::login,
"id", &WeakTwitchAccount::id,
"color", &WeakTwitchAccount::color,
"is_anon", &WeakTwitchAccount::isAnon
// clang-format on
);
c2.set_function("current_account", [] {
return WeakTwitchAccount(getApp()->getAccounts()->twitch.getCurrent());
});
}
} // namespace chatterino::lua::api
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#ifdef CHATTERINO_HAVE_PLUGINS
# include <sol/forward.hpp>
namespace chatterino::lua::api {
/* @lua-fragment
---@class c2.TwitchAccount
c2.TwitchAccount = {}
--- Returns true if the account this object points to is valid.
--- If the object expired, returns false
---
---@return boolean success
function c2.TwitchAccount:is_valid() end
---@return string user_login The (login) name of the account
function c2.TwitchAccount:login() end
---@return string user_id The Twitch user ID of the account
function c2.TwitchAccount:id() end
---@return string? color Color in chat of this account. `nil` if not yet known
function c2.TwitchAccount:color() end
---@return boolean is_anon `true` if this account is an anonymous account (no associated Twitch user)
function c2.TwitchAccount:is_anon() end
---@return string str
function c2.TwitchAccount:__tostring() end
---Gets the currently logged in Twitch account. This account might be an anonymous one (see `is_anon`).
---@return c2.TwitchAccount account
function c2.current_account() end
*/
void createAccounts(sol::table &c2);
} // namespace chatterino::lua::api
#endif