From a84e62c6bbfdf7491baeeb7c248117db53886962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= Date: Sat, 17 Jul 2021 18:22:25 +0200 Subject: [PATCH] Use server time in received IRC messages where available (#3026) --- CHANGELOG.md | 2 ++ src/providers/irc/IrcMessageBuilder.cpp | 5 ++--- src/providers/irc/IrcServer.cpp | 7 +++++-- src/util/IrcHelpers.hpp | 23 ++++++++++++++++++++--- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7678c74fc..76e842cc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Major: Newly uploaded Twitch emotes are once again present in emote picker and can be autocompleted with Tab as well. (#2992) - Minor: Added autocompletion in /whispers for Twitch emotes, Global Bttv/Ffz emotes and emojis. (#2999) +- Minor: Received Twitch messages now use the exact same timestamp (obtained from Twitch's server) for every Chatterino user instead of assuming message timestamp on client's side. (#3021) +- Minor: Received IRC messages use `time` message tag for timestamp if it's available. (#3021) - Bugfix: Fixed "smiley" emotes being unable to be "Tabbed" with autocompletion, introduced in v2.3.3. (#3010) - Dev: Ubuntu packages are now available (#2936) diff --git a/src/providers/irc/IrcMessageBuilder.cpp b/src/providers/irc/IrcMessageBuilder.cpp index 0c43bf9c7..fab99afb1 100644 --- a/src/providers/irc/IrcMessageBuilder.cpp +++ b/src/providers/irc/IrcMessageBuilder.cpp @@ -40,7 +40,8 @@ MessagePtr IrcMessageBuilder::build() // PUSH ELEMENTS this->appendChannelName(); - this->emplace(); + this->emplace( + calculateMessageTimestamp(this->ircMessage)); this->appendUsername(); @@ -70,8 +71,6 @@ void IrcMessageBuilder::addWords(const QStringList &words) void IrcMessageBuilder::appendUsername() { - auto app = getApp(); - QString username = this->userName; this->message().loginName = username; this->message().displayName = username; diff --git a/src/providers/irc/IrcServer.cpp b/src/providers/irc/IrcServer.cpp index 31d4ea641..70227d6ce 100644 --- a/src/providers/irc/IrcServer.cpp +++ b/src/providers/irc/IrcServer.cpp @@ -9,6 +9,7 @@ #include "providers/irc/IrcChannel2.hpp" #include "providers/irc/IrcMessageBuilder.hpp" #include "singletons/Settings.hpp" +#include "util/IrcHelpers.hpp" #include "util/QObjectRef.hpp" #include @@ -92,7 +93,8 @@ void IrcServer::initializeConnectionSignals(IrcConnection *connection, // XD PAJLADA MessageBuilder builder; - builder.emplace(); + builder.emplace( + calculateMessageTimestamp(message)); builder.emplace( message->nick(), MessageElementFlag::Username); builder.emplace( @@ -259,7 +261,8 @@ void IrcServer::readConnectionMessageReceived(Communi::IrcMessage *message) { MessageBuilder builder; - builder.emplace(); + builder.emplace( + calculateMessageTimestamp(message)); builder.emplace(message->toData(), MessageElementFlag::Text); builder->flags.set(MessageFlag::Debug); diff --git a/src/util/IrcHelpers.hpp b/src/util/IrcHelpers.hpp index 6b6096903..4183d4d86 100644 --- a/src/util/IrcHelpers.hpp +++ b/src/util/IrcHelpers.hpp @@ -65,7 +65,7 @@ inline QTime calculateMessageTimestamp(const Communi::IrcMessage *message) if (message->tags().contains("historical")) { bool customReceived = false; - qint64 ts = + auto ts = message->tags().value("rm-received-ts").toLongLong(&customReceived); if (!customReceived) { @@ -74,10 +74,27 @@ inline QTime calculateMessageTimestamp(const Communi::IrcMessage *message) return QDateTime::fromMSecsSinceEpoch(ts).time(); } - else + + // If present, handle tmi-sent-ts tag and use it as timestamp + if (message->tags().contains("tmi-sent-ts")) { - return QTime::currentTime(); + auto ts = message->tags().value("tmi-sent-ts").toLongLong(); + return QDateTime::fromMSecsSinceEpoch(ts).time(); } + + // Some IRC Servers might have server-time tag containing UTC date in ISO format, use it as timestamp + // See: https://ircv3.net/irc/#server-time + if (message->tags().contains("time")) + { + QString timedate = message->tags().value("time").toString(); + + auto date = QDateTime::fromString(timedate, Qt::ISODate); + date.setTimeSpec(Qt::TimeSpec::UTC); + return date.toLocalTime().time(); + } + + // Fallback to current time + return QTime::currentTime(); } } // namespace chatterino