feat: include more data when copying messages as JSON (#5600)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "messages/MessageElement.hpp"
|
||||
|
||||
#include "Application.hpp"
|
||||
#include "common/Literals.hpp"
|
||||
#include "controllers/moderationactions/ModerationAction.hpp"
|
||||
#include "debug/Benchmark.hpp"
|
||||
#include "messages/Emote.hpp"
|
||||
@@ -14,8 +15,14 @@
|
||||
#include "util/DebugCount.hpp"
|
||||
#include "util/Variant.hpp"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
using namespace literals;
|
||||
|
||||
namespace {
|
||||
|
||||
// Computes the bounding box for the given vector of images
|
||||
@@ -88,6 +95,22 @@ void MessageElement::addFlags(MessageElementFlags flags)
|
||||
this->flags_.set(flags);
|
||||
}
|
||||
|
||||
QJsonObject MessageElement::toJson() const
|
||||
{
|
||||
return {
|
||||
{"trailingSpace"_L1, this->trailingSpace},
|
||||
{
|
||||
"link"_L1,
|
||||
{{
|
||||
{"type"_L1, qmagicenum::enumNameString(this->link_.type)},
|
||||
{"value"_L1, this->link_.value},
|
||||
}},
|
||||
},
|
||||
{"tooltip"_L1, this->tooltip_},
|
||||
{"flags"_L1, qmagicenum::enumFlagsName(this->flags_.value())},
|
||||
};
|
||||
}
|
||||
|
||||
// IMAGE
|
||||
ImageElement::ImageElement(ImagePtr image, MessageElementFlags flags)
|
||||
: MessageElement(flags)
|
||||
@@ -108,6 +131,15 @@ void ImageElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject ImageElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"ImageElement"_s;
|
||||
base["url"_L1] = this->image_->url().string;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
CircularImageElement::CircularImageElement(ImagePtr image, int padding,
|
||||
QColor background,
|
||||
MessageElementFlags flags)
|
||||
@@ -131,6 +163,17 @@ void CircularImageElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject CircularImageElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"CircularImageElement"_s;
|
||||
base["url"_L1] = this->image_->url().string;
|
||||
base["padding"_L1] = this->padding_;
|
||||
base["background"_L1] = this->background_.name(QColor::HexArgb);
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// EMOTE
|
||||
EmoteElement::EmoteElement(const EmotePtr &emote, MessageElementFlags flags,
|
||||
const MessageColor &textElementColor)
|
||||
@@ -187,6 +230,19 @@ MessageLayoutElement *EmoteElement::makeImageLayoutElement(
|
||||
return new ImageLayoutElement(*this, image, size);
|
||||
}
|
||||
|
||||
QJsonObject EmoteElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"EmoteElement"_s;
|
||||
base["emote"_L1] = this->emote_->toJson();
|
||||
if (this->textElement_)
|
||||
{
|
||||
base["text"_L1] = this->textElement_->toJson();
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
LayeredEmoteElement::LayeredEmoteElement(
|
||||
std::vector<LayeredEmoteElement::Emote> &&emotes, MessageElementFlags flags,
|
||||
const MessageColor &textElementColor)
|
||||
@@ -350,6 +406,38 @@ std::vector<LayeredEmoteElement::Emote> LayeredEmoteElement::getUniqueEmotes()
|
||||
return unique;
|
||||
}
|
||||
|
||||
QJsonObject LayeredEmoteElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"LayeredEmoteElement"_s;
|
||||
|
||||
QJsonArray emotes;
|
||||
for (const auto &emote : this->emotes_)
|
||||
{
|
||||
emotes.append({{
|
||||
{"flags"_L1, qmagicenum::enumFlagsName(emote.flags.value())},
|
||||
{"emote"_L1, emote.ptr->toJson()},
|
||||
}});
|
||||
}
|
||||
base["emotes"_L1] = emotes;
|
||||
|
||||
QJsonArray tooltips;
|
||||
for (const auto &tooltip : this->emoteTooltips_)
|
||||
{
|
||||
emotes.append(tooltip);
|
||||
}
|
||||
base["tooltips"_L1] = tooltips;
|
||||
|
||||
if (this->textElement_)
|
||||
{
|
||||
base["text"_L1] = this->textElement_->toJson();
|
||||
}
|
||||
|
||||
base["textElementColor"_L1] = this->textElementColor_.toString();
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// BADGE
|
||||
BadgeElement::BadgeElement(const EmotePtr &emote, MessageElementFlags flags)
|
||||
: MessageElement(flags)
|
||||
@@ -390,6 +478,15 @@ MessageLayoutElement *BadgeElement::makeImageLayoutElement(
|
||||
return element;
|
||||
}
|
||||
|
||||
QJsonObject BadgeElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"BadgeElement"_s;
|
||||
base["emote"_L1] = this->emote_->toJson();
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// MOD BADGE
|
||||
ModBadgeElement::ModBadgeElement(const EmotePtr &data,
|
||||
MessageElementFlags flags_)
|
||||
@@ -408,6 +505,14 @@ MessageLayoutElement *ModBadgeElement::makeImageLayoutElement(
|
||||
return element;
|
||||
}
|
||||
|
||||
QJsonObject ModBadgeElement::toJson() const
|
||||
{
|
||||
auto base = BadgeElement::toJson();
|
||||
base["type"_L1] = u"ModBadgeElement"_s;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// VIP BADGE
|
||||
VipBadgeElement::VipBadgeElement(const EmotePtr &data,
|
||||
MessageElementFlags flags_)
|
||||
@@ -423,6 +528,14 @@ MessageLayoutElement *VipBadgeElement::makeImageLayoutElement(
|
||||
return element;
|
||||
}
|
||||
|
||||
QJsonObject VipBadgeElement::toJson() const
|
||||
{
|
||||
auto base = BadgeElement::toJson();
|
||||
base["type"_L1] = u"VipBadgeElement"_s;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// FFZ Badge
|
||||
FfzBadgeElement::FfzBadgeElement(const EmotePtr &data,
|
||||
MessageElementFlags flags_, QColor color_)
|
||||
@@ -440,6 +553,15 @@ MessageLayoutElement *FfzBadgeElement::makeImageLayoutElement(
|
||||
return element;
|
||||
}
|
||||
|
||||
QJsonObject FfzBadgeElement::toJson() const
|
||||
{
|
||||
auto base = BadgeElement::toJson();
|
||||
base["type"_L1] = u"FfzBadgeElement"_s;
|
||||
base["color"_L1] = this->color.name(QColor::HexArgb);
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// TEXT
|
||||
TextElement::TextElement(const QString &text, MessageElementFlags flags,
|
||||
const MessageColor &color, FontStyle style)
|
||||
@@ -549,6 +671,17 @@ void TextElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject TextElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"TextElement"_s;
|
||||
base["words"_L1] = QJsonArray::fromStringList(this->words_);
|
||||
base["color"_L1] = this->color_.toString();
|
||||
base["style"_L1] = qmagicenum::enumNameString(this->style_);
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
SingleLineTextElement::SingleLineTextElement(const QString &text,
|
||||
MessageElementFlags flags,
|
||||
const MessageColor &color,
|
||||
@@ -677,6 +810,22 @@ void SingleLineTextElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject SingleLineTextElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"SingleLineTextElement"_s;
|
||||
QJsonArray words;
|
||||
for (const auto &word : this->words_)
|
||||
{
|
||||
words.append(word.text);
|
||||
}
|
||||
base["words"_L1] = words;
|
||||
base["color"_L1] = this->color_.toString();
|
||||
base["style"_L1] = qmagicenum::enumNameString(this->style_);
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
LinkElement::LinkElement(const Parsed &parsed, const QString &fullUrl,
|
||||
MessageElementFlags flags, const MessageColor &color,
|
||||
FontStyle style)
|
||||
@@ -701,6 +850,17 @@ Link LinkElement::getLink() const
|
||||
return {Link::Url, this->linkInfo_.url()};
|
||||
}
|
||||
|
||||
QJsonObject LinkElement::toJson() const
|
||||
{
|
||||
auto base = TextElement::toJson();
|
||||
base["type"_L1] = u"LinkElement"_s;
|
||||
base["link"_L1] = this->linkInfo_.originalUrl();
|
||||
base["lowercase"_L1] = QJsonArray::fromStringList(this->lowercase_);
|
||||
base["original"_L1] = QJsonArray::fromStringList(this->original_);
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
MentionElement::MentionElement(const QString &displayName, QString loginName_,
|
||||
MessageColor fallbackColor_,
|
||||
MessageColor userColor_)
|
||||
@@ -756,7 +916,24 @@ Link MentionElement::getLink() const
|
||||
return {Link::UserInfo, this->userLoginName};
|
||||
}
|
||||
|
||||
QJsonObject MentionElement::toJson() const
|
||||
{
|
||||
auto base = TextElement::toJson();
|
||||
base["type"_L1] = u"MentionElement"_s;
|
||||
base["fallbackColor"_L1] = this->fallbackColor.toString();
|
||||
base["userColor"_L1] = this->userColor.toString();
|
||||
base["userLoginName"_L1] = this->userLoginName;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// TIMESTAMP
|
||||
TimestampElement::TimestampElement()
|
||||
: TimestampElement(getApp()->isTest() ? QTime::fromMSecsSinceStartOfDay(0)
|
||||
: QTime::currentTime())
|
||||
{
|
||||
}
|
||||
|
||||
TimestampElement::TimestampElement(QTime time)
|
||||
: MessageElement(MessageElementFlag::Timestamp)
|
||||
, time_(time)
|
||||
@@ -790,6 +967,17 @@ TextElement *TimestampElement::formatTime(const QTime &time)
|
||||
MessageColor::System, FontStyle::ChatMedium);
|
||||
}
|
||||
|
||||
QJsonObject TimestampElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"TimestampElement"_s;
|
||||
base["time"_L1] = this->time_.toString(Qt::ISODate);
|
||||
base["element"_L1] = this->element_->toJson();
|
||||
base["format"_L1] = this->format_;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
// TWITCH MODERATION
|
||||
TwitchModerationElement::TwitchModerationElement()
|
||||
: MessageElement(MessageElementFlag::ModeratorTools)
|
||||
@@ -824,6 +1012,14 @@ void TwitchModerationElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject TwitchModerationElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"TwitchModerationElement"_s;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
LinebreakElement::LinebreakElement(MessageElementFlags flags)
|
||||
: MessageElement(flags)
|
||||
{
|
||||
@@ -838,6 +1034,14 @@ void LinebreakElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject LinebreakElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"LinebreakElement"_s;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
ScalingImageElement::ScalingImageElement(ImageSet images,
|
||||
MessageElementFlags flags)
|
||||
: MessageElement(flags)
|
||||
@@ -864,6 +1068,15 @@ void ScalingImageElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject ScalingImageElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"ScalingImageElement"_s;
|
||||
base["image"_L1] = this->images_.getImage1()->url().string;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
ReplyCurveElement::ReplyCurveElement()
|
||||
: MessageElement(MessageElementFlag::RepliedMessage)
|
||||
{
|
||||
@@ -886,4 +1099,12 @@ void ReplyCurveElement::addToContainer(MessageLayoutContainer &container,
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject ReplyCurveElement::toJson() const
|
||||
{
|
||||
auto base = MessageElement::toJson();
|
||||
base["type"_L1] = u"ReplyCurveElement"_s;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
Reference in New Issue
Block a user