Skip to content

Commit

Permalink
feat: include more data when copying messages as JSON (#5600)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Sep 28, 2024
1 parent edf18a7 commit e149be3
Show file tree
Hide file tree
Showing 16 changed files with 504 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
- Dev: The timer for `StreamerMode` is now destroyed on the correct thread. (#5571)
- Dev: Cleanup some parts of the `magic_enum` adaptation for Qt. (#5587)
- Dev: Refactored `static`s in headers to only be present once in the final app. (#5588)
- Dev: The JSON output when copying a message (<kbd>SHIFT</kbd> + right-click) is now more extensive. (#5600)

## 2.5.1

Expand Down
37 changes: 37 additions & 0 deletions src/messages/Emote.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#include "messages/Emote.hpp"

#include "common/Literals.hpp"

#include <QJsonObject>

#include <unordered_map>

namespace chatterino {

using namespace literals;

bool operator==(const Emote &a, const Emote &b)
{
return std::tie(a.homePage, a.name, a.tooltip, a.images) ==
Expand All @@ -15,6 +21,37 @@ bool operator!=(const Emote &a, const Emote &b)
return !(a == b);
}

QJsonObject Emote::toJson() const
{
QJsonObject obj{
{"name"_L1, this->name.string},
{"images"_L1, this->images.toJson()},
{"tooltip"_L1, this->tooltip.string},
};
if (!this->homePage.string.isEmpty())
{
obj["homePage"_L1] = this->homePage.string;
}
if (this->zeroWidth)
{
obj["zeroWidth"_L1] = this->zeroWidth;
}
if (!this->id.string.isEmpty())
{
obj["id"_L1] = this->id.string;
}
if (!this->author.string.isEmpty())
{
obj["author"_L1] = this->author.string;
}
if (this->baseName)
{
obj["baseName"_L1] = this->baseName->string;
}

return obj;
}

EmotePtr cachedOrMakeEmotePtr(Emote &&emote, const EmoteMap &cache)
{
// reuse old shared_ptr if nothing changed
Expand Down
4 changes: 4 additions & 0 deletions src/messages/Emote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <optional>
#include <unordered_map>

class QJsonObject;

namespace chatterino {

struct Emote {
Expand All @@ -30,6 +32,8 @@ struct Emote {
{
return name.string;
}

QJsonObject toJson() const;
};

bool operator==(const Emote &a, const Emote &b);
Expand Down
20 changes: 20 additions & 0 deletions src/messages/ImageSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "messages/Image.hpp"
#include "singletons/Settings.hpp"

#include <QJsonObject>

namespace chatterino {

ImageSet::ImageSet()
Expand Down Expand Up @@ -135,4 +137,22 @@ bool ImageSet::operator!=(const ImageSet &other) const
return !this->operator==(other);
}

QJsonObject ImageSet::toJson() const
{
QJsonObject obj;
if (!this->imageX1_->isEmpty())
{
obj[u"1x"] = this->imageX1_->url().string;
}
if (!this->imageX2_->isEmpty())
{
obj[u"2x"] = this->imageX2_->url().string;
}
if (!this->imageX3_->isEmpty())
{
obj[u"3x"] = this->imageX3_->url().string;
}
return obj;
}

} // namespace chatterino
4 changes: 4 additions & 0 deletions src/messages/ImageSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <memory>

class QJsonObject;

namespace chatterino {

class Image;
Expand Down Expand Up @@ -34,6 +36,8 @@ class ImageSet
bool operator==(const ImageSet &other) const;
bool operator!=(const ImageSet &other) const;

QJsonObject toJson() const;

private:
ImagePtr imageX1_;
ImagePtr imageX2_;
Expand Down
78 changes: 78 additions & 0 deletions src/messages/Message.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
#include "messages/Message.hpp"

#include "Application.hpp"
#include "common/Literals.hpp"
#include "messages/MessageThread.hpp"
#include "providers/colors/ColorProvider.hpp"
#include "providers/twitch/TwitchBadge.hpp"
#include "singletons/Settings.hpp"
#include "util/DebugCount.hpp"
#include "util/QMagicEnum.hpp"
#include "widgets/helper/ScrollbarHighlight.hpp"

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>

namespace chatterino {

using namespace literals;

Message::Message()
: parseTime(QTime::currentTime())
{
Expand Down Expand Up @@ -80,4 +90,72 @@ ScrollbarHighlight Message::getScrollBarHighlight() const
return {};
}

QJsonObject Message::toJson() const
{
QJsonObject msg{
{"flags"_L1, qmagicenum::enumFlagsName(this->flags.value())},
{"id"_L1, this->id},
{"searchText"_L1, this->searchText},
{"messageText"_L1, this->messageText},
{"loginName"_L1, this->loginName},
{"displayName"_L1, this->displayName},
{"localizedName"_L1, this->localizedName},
{"timeoutUser"_L1, this->timeoutUser},
{"channelName"_L1, this->channelName},
{"usernameColor"_L1, this->usernameColor.name(QColor::HexArgb)},
{"count"_L1, static_cast<qint64>(this->count)},
{"serverReceivedTime"_L1,
this->serverReceivedTime.toString(Qt::ISODate)},
};

QJsonArray badges;
for (const auto &badge : this->badges)
{
badges.append(badge.key_);
}
msg["badges"_L1] = badges;

QJsonObject badgeInfos;
for (const auto &[key, value] : this->badgeInfos)
{
badgeInfos.insert(key, value);
}
msg["badgeInfos"_L1] = badgeInfos;

if (this->highlightColor)
{
msg["highlightColor"_L1] = this->highlightColor->name(QColor::HexArgb);
}

if (this->replyThread)
{
msg["replyThread"_L1] = this->replyThread->toJson();
}

if (this->replyParent)
{
msg["replyParent"_L1] = this->replyParent->id;
}

if (this->reward)
{
msg["reward"_L1] = this->reward->toJson();
}

// XXX: figure out if we can add this in tests
if (!getApp()->isTest())
{
msg["parseTime"_L1] = this->parseTime.toString(Qt::ISODate);
}

QJsonArray elements;
for (const auto &element : this->elements)
{
elements.append(element->toJson());
}
msg["elements"_L1] = elements;

return msg;
}

} // namespace chatterino
4 changes: 4 additions & 0 deletions src/messages/Message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <unordered_map>
#include <vector>

class QJsonObject;

namespace chatterino {
class MessageElement;
class MessageThread;
Expand Down Expand Up @@ -62,6 +64,8 @@ struct Message {
ScrollbarHighlight getScrollBarHighlight() const;

std::shared_ptr<ChannelPointReward> reward = nullptr;

QJsonObject toJson() const;
};

} // namespace chatterino
17 changes: 17 additions & 0 deletions src/messages/MessageColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ const QColor &MessageColor::getColor(Theme &themeManager) const
return _default;
}

QString MessageColor::toString() const
{
switch (this->type_)
{
case Type::Custom:
return this->customColor_.name(QColor::HexArgb);
case Type::Text:
return QStringLiteral("Text");
case Type::System:
return QStringLiteral("System");
case Type::Link:
return QStringLiteral("Link");
default:
return {};
}
}

} // namespace chatterino
3 changes: 3 additions & 0 deletions src/messages/MessageColor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <QColor>
#include <QString>

namespace chatterino {
class Theme;
Expand All @@ -13,6 +14,8 @@ struct MessageColor {

const QColor &getColor(Theme &themeManager) const;

QString toString() const;

private:
Type type_;
QColor customColor_;
Expand Down
Loading

0 comments on commit e149be3

Please sign in to comment.