Handle conversion of historical CLEARCHAT messages to NOTICE messages in Chatterino instead of relying on the Recent Messages API to handle it for us (#1804)
This has historically been done in the Recent Messages API, but this functionality is being moved to Chatterino instead * Remove `clearchatToNotice=true` query parameter to the Recent Messages API
This commit is contained in:
+1
-1
@@ -50,7 +50,7 @@ Env::Env()
|
||||
: recentMessagesApiUrl(
|
||||
readStringEnv("CHATTERINO2_RECENT_MESSAGES_URL",
|
||||
"https://recent-messages.robotty.de/api/v2/"
|
||||
"recent-messages/%1?clearchatToNotice=true"))
|
||||
"recent-messages/%1"))
|
||||
, linkResolverUrl(readStringEnv(
|
||||
"CHATTERINO2_LINK_RESOLVER_URL",
|
||||
"https://braize.pajlada.com/chatterino/link_resolver/%1"))
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "singletons/Toasts.hpp"
|
||||
#include "singletons/WindowManager.hpp"
|
||||
#include "util/FormatTime.hpp"
|
||||
#include "util/PostToThread.hpp"
|
||||
#include "widgets/Window.hpp"
|
||||
|
||||
@@ -36,6 +37,47 @@ namespace {
|
||||
constexpr int TITLE_REFRESH_PERIOD = 10;
|
||||
constexpr char MAGIC_MESSAGE_SUFFIX[] = u8" \U000E0000";
|
||||
|
||||
// convertClearchatToNotice takes a Communi::IrcMessage that is a CLEARCHAT command and converts it to a readable NOTICE message
|
||||
// This has historically been done in the Recent Messages API, but this functionality is being moved to Chatterino instead
|
||||
auto convertClearchatToNotice(Communi::IrcMessage *message)
|
||||
{
|
||||
auto channelName = message->parameter(0);
|
||||
QString noticeMessage{};
|
||||
if (message->tags().contains("target-user-id"))
|
||||
{
|
||||
auto target = message->parameter(1);
|
||||
|
||||
if (message->tags().contains("ban-duration"))
|
||||
{
|
||||
// User was timed out
|
||||
noticeMessage =
|
||||
QString("%1 has been timed out for %2.")
|
||||
.arg(target)
|
||||
.arg(formatTime(
|
||||
message->tag("ban-duration").toString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
// User was permanently banned
|
||||
noticeMessage =
|
||||
QString("%1 has been permanently banned.").arg(target);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Chat was cleared
|
||||
noticeMessage = "Chat has been cleared by a moderator.";
|
||||
}
|
||||
|
||||
// rebuild the raw irc message so we can convert it back to an ircmessage again!
|
||||
// this could probably be done in a smarter way
|
||||
auto s = QString(":tmi.twitch.tv NOTICE %1 :%2")
|
||||
.arg(channelName)
|
||||
.arg(noticeMessage);
|
||||
|
||||
return Communi::IrcMessage::fromData(s.toUtf8(), nullptr);
|
||||
}
|
||||
|
||||
// parseRecentMessages takes a json object and returns a vector of
|
||||
// Communi IrcMessages
|
||||
auto parseRecentMessages(const QJsonObject &jsonRoot, ChannelPtr channel)
|
||||
@@ -49,8 +91,15 @@ namespace {
|
||||
for (const auto jsonMessage : jsonMessages)
|
||||
{
|
||||
auto content = jsonMessage.toString().toUtf8();
|
||||
messages.emplace_back(
|
||||
Communi::IrcMessage::fromData(content, nullptr));
|
||||
|
||||
auto message = Communi::IrcMessage::fromData(content, nullptr);
|
||||
|
||||
if (message->command() == "CLEARCHAT")
|
||||
{
|
||||
message = convertClearchatToNotice(message);
|
||||
}
|
||||
|
||||
messages.emplace_back(std::move(message));
|
||||
}
|
||||
|
||||
return messages;
|
||||
|
||||
@@ -50,4 +50,16 @@ QString formatTime(int totalSeconds)
|
||||
return res;
|
||||
}
|
||||
|
||||
QString formatTime(QString totalSecondsString)
|
||||
{
|
||||
bool ok = true;
|
||||
int totalSeconds(totalSecondsString.toInt(&ok));
|
||||
if (ok)
|
||||
{
|
||||
return formatTime(totalSeconds);
|
||||
}
|
||||
|
||||
return "n/a";
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -6,5 +6,6 @@ namespace chatterino {
|
||||
|
||||
// format: 1h 23m 42s
|
||||
QString formatTime(int totalSeconds);
|
||||
QString formatTime(QString totalSecondsString);
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
Reference in New Issue
Block a user