aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-01-18 19:25:52 +0100
committerAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-01-18 19:25:52 +0100
commit142fc5a21f541e2a7592119df075a543527195b9 (patch)
treef38cd3f38465a0cf8b38c198b202e624fe939ae1 /lib/events
parentecbff4c1a21ff4c0ab72141bc1a34ae189d33483 (diff)
parentbf82aeea369cacfc93a0e6d6d9feb01f1f2afdb2 (diff)
downloadlibquotient-142fc5a21f541e2a7592119df075a543527195b9.tar.gz
libquotient-142fc5a21f541e2a7592119df075a543527195b9.zip
Merge branch 'kitsune/cleanup2' into dev
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/accountdataevents.h29
-rw-r--r--lib/events/event.h6
-rw-r--r--lib/events/eventcontent.h4
-rw-r--r--lib/events/roommessageevent.cpp66
4 files changed, 52 insertions, 53 deletions
diff --git a/lib/events/accountdataevents.h b/lib/events/accountdataevents.h
index c0f2202d..12f1f00b 100644
--- a/lib/events/accountdataevents.h
+++ b/lib/events/accountdataevents.h
@@ -4,27 +4,24 @@
#pragma once
#include "event.h"
+#include "util.h"
namespace Quotient {
-constexpr const char* FavouriteTag = "m.favourite";
-constexpr const char* LowPriorityTag = "m.lowpriority";
-constexpr const char* ServerNoticeTag = "m.server_notice";
+constexpr auto FavouriteTag [[maybe_unused]] = "m.favourite"_ls;
+constexpr auto LowPriorityTag [[maybe_unused]] = "m.lowpriority"_ls;
+constexpr auto ServerNoticeTag [[maybe_unused]] = "m.server_notice"_ls;
struct TagRecord {
- using order_type = Omittable<float>;
-
- order_type order;
-
- TagRecord(order_type order = none) : order(order) {}
-
- bool operator<(const TagRecord& other) const
- {
- // Per The Spec, rooms with no order should be after those with order,
- // against std::optional<>::operator<() convention.
- return order && (!other.order || *order < *other.order);
- }
+ Omittable<float> order = none;
};
+inline bool operator<(TagRecord lhs, TagRecord rhs)
+{
+ // Per The Spec, rooms with no order should be after those with order,
+ // against std::optional<>::operator<() convention.
+ return lhs.order && (!rhs.order || *lhs.order < *rhs.order);
+}
+
template <>
struct JsonObjectConverter<TagRecord> {
static void fillFrom(const QJsonObject& jo, TagRecord& rec)
@@ -41,7 +38,7 @@ struct JsonObjectConverter<TagRecord> {
rec.order = none;
}
}
- static void dumpTo(QJsonObject& jo, const TagRecord& rec)
+ static void dumpTo(QJsonObject& jo, TagRecord rec)
{
addParam<IfNotEmpty>(jo, QStringLiteral("order"), rec.order);
}
diff --git a/lib/events/event.h b/lib/events/event.h
index f12e525e..f10f6a8d 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -264,9 +264,9 @@ using Events = EventsArray<Event>;
// provide matrixTypeId() and typeId().
#define DEFINE_EVENT_TYPEID(Id_, Type_) \
static constexpr event_type_t TypeId = Id_##_ls; \
- [[deprecated("Use _Type::TypeId directly instead")]] \
+ [[deprecated("Use " #Type_ "::TypeId directly instead")]] \
static constexpr event_mtype_t matrixTypeId() { return Id_; } \
- [[deprecated("Use _Type::TypeId directly instead")]] \
+ [[deprecated("Use " #Type_ "::TypeId directly instead")]] \
static event_type_t typeId() { return TypeId; } \
// End of macro
@@ -311,7 +311,7 @@ inline auto switchOnType(const BaseEventT& event, FnT&& fn)
namespace _impl {
// Using bool instead of auto below because auto apparently upsets MSVC
template <class BaseT, typename FnT>
- inline constexpr bool needs_downcast =
+ constexpr bool needs_downcast =
std::is_base_of_v<BaseT, std::decay_t<fn_arg_t<FnT>>>
&& !std::is_same_v<BaseT, std::decay_t<fn_arg_t<FnT>>>;
}
diff --git a/lib/events/eventcontent.h b/lib/events/eventcontent.h
index 87ea3672..de9a792b 100644
--- a/lib/events/eventcontent.h
+++ b/lib/events/eventcontent.h
@@ -149,10 +149,8 @@ namespace EventContent {
*/
class QUOTIENT_API Thumbnail : public ImageInfo {
public:
- Thumbnail() = default; // Allow empty thumbnails
- Thumbnail(const QJsonObject& infoJson, const Omittable<EncryptedFile> &file = none);
- Thumbnail(const ImageInfo& info) : ImageInfo(info) {}
using ImageInfo::ImageInfo;
+ Thumbnail(const QJsonObject& infoJson, const Omittable<EncryptedFile> &file = none);
/**
* Writes thumbnail information to "thumbnail_info" subobject
diff --git a/lib/events/roommessageevent.cpp b/lib/events/roommessageevent.cpp
index 2b7b4166..5ab0f845 100644
--- a/lib/events/roommessageevent.cpp
+++ b/lib/events/roommessageevent.cpp
@@ -19,15 +19,15 @@ using namespace EventContent;
using MsgType = RoomMessageEvent::MsgType;
-static const auto RelatesToKeyL = "m.relates_to"_ls;
-static const auto MsgTypeKeyL = "msgtype"_ls;
-static const auto FormattedBodyKeyL = "formatted_body"_ls;
+namespace { // Supporting internal definitions
-static const auto TextTypeKey = "m.text";
-static const auto EmoteTypeKey = "m.emote";
-static const auto NoticeTypeKey = "m.notice";
-
-static const auto HtmlContentTypeId = QStringLiteral("org.matrix.custom.html");
+constexpr auto RelatesToKey = "m.relates_to"_ls;
+constexpr auto MsgTypeKey = "msgtype"_ls;
+constexpr auto FormattedBodyKey = "formatted_body"_ls;
+constexpr auto TextTypeKey = "m.text"_ls;
+constexpr auto EmoteTypeKey = "m.emote"_ls;
+constexpr auto NoticeTypeKey = "m.notice"_ls;
+constexpr auto HtmlContentTypeId = "org.matrix.custom.html"_ls;
template <typename ContentT>
TypedBase* make(const QJsonObject& json)
@@ -38,13 +38,13 @@ TypedBase* make(const QJsonObject& json)
template <>
TypedBase* make<TextContent>(const QJsonObject& json)
{
- return json.contains(FormattedBodyKeyL) || json.contains(RelatesToKeyL)
+ return json.contains(FormattedBodyKey) || json.contains(RelatesToKey)
? new TextContent(json)
: nullptr;
}
struct MsgTypeDesc {
- QString matrixType;
+ QLatin1String matrixType;
MsgType enumType;
TypedBase* (*maker)(const QJsonObject&);
};
@@ -53,11 +53,11 @@ const std::vector<MsgTypeDesc> msgTypes = {
{ TextTypeKey, MsgType::Text, make<TextContent> },
{ EmoteTypeKey, MsgType::Emote, make<TextContent> },
{ NoticeTypeKey, MsgType::Notice, make<TextContent> },
- { QStringLiteral("m.image"), MsgType::Image, make<ImageContent> },
- { QStringLiteral("m.file"), MsgType::File, make<FileContent> },
- { QStringLiteral("m.location"), MsgType::Location, make<LocationContent> },
- { QStringLiteral("m.video"), MsgType::Video, make<VideoContent> },
- { QStringLiteral("m.audio"), MsgType::Audio, make<AudioContent> }
+ { "m.image"_ls, MsgType::Image, make<ImageContent> },
+ { "m.file"_ls, MsgType::File, make<FileContent> },
+ { "m.location"_ls, MsgType::Location, make<LocationContent> },
+ { "m.video"_ls, MsgType::Video, make<VideoContent> },
+ { "m.audio"_ls, MsgType::Audio, make<AudioContent> }
};
QString msgTypeToJson(MsgType enumType)
@@ -89,17 +89,19 @@ inline bool isReplacement(const Omittable<RelatesTo>& rel)
return rel && rel->type == RelatesTo::ReplacementTypeId();
}
+} // anonymous namespace
+
QJsonObject RoomMessageEvent::assembleContentJson(const QString& plainBody,
const QString& jsonMsgType,
TypedBase* content)
{
auto json = content ? content->toJson() : QJsonObject();
- if (json.contains(RelatesToKeyL)) {
+ if (json.contains(RelatesToKey)) {
if (jsonMsgType != TextTypeKey && jsonMsgType != NoticeTypeKey
&& jsonMsgType != EmoteTypeKey) {
- json.remove(RelatesToKeyL);
+ json.remove(RelatesToKey);
qCWarning(EVENTS)
- << RelatesToKeyL << "cannot be used in" << jsonMsgType
+ << RelatesToKey << "cannot be used in" << jsonMsgType
<< "messages; the relation has been stripped off";
} else {
// After the above, we know for sure that the content is TextContent
@@ -109,9 +111,9 @@ QJsonObject RoomMessageEvent::assembleContentJson(const QString& plainBody,
if (textContent->relatesTo->type == RelatesTo::ReplacementTypeId()) {
auto newContentJson = json.take("m.new_content"_ls).toObject();
newContentJson.insert(BodyKey, plainBody);
- newContentJson.insert(MsgTypeKeyL, jsonMsgType);
+ newContentJson.insert(MsgTypeKey, jsonMsgType);
json.insert(QStringLiteral("m.new_content"), newContentJson);
- json[MsgTypeKeyL] = jsonMsgType;
+ json[MsgTypeKey] = jsonMsgType;
json[BodyKeyL] = "* " + plainBody;
return json;
}
@@ -177,8 +179,8 @@ RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj)
if (isRedacted())
return;
const QJsonObject content = contentJson();
- if (content.contains(MsgTypeKeyL) && content.contains(BodyKeyL)) {
- auto msgtype = content[MsgTypeKeyL].toString();
+ if (content.contains(MsgTypeKey) && content.contains(BodyKeyL)) {
+ auto msgtype = content[MsgTypeKey].toString();
bool msgTypeFound = false;
for (const auto& mt : msgTypes)
if (mt.matrixType == msgtype) {
@@ -204,7 +206,7 @@ RoomMessageEvent::MsgType RoomMessageEvent::msgtype() const
QString RoomMessageEvent::rawMsgtype() const
{
- return contentPart<QString>(MsgTypeKeyL);
+ return contentPart<QString>(MsgTypeKey);
}
QString RoomMessageEvent::plainBody() const
@@ -295,7 +297,7 @@ Omittable<RelatesTo> fromJson(const QJsonValue& jv)
} // namespace Quotient
TextContent::TextContent(const QJsonObject& json)
- : relatesTo(fromJson<Omittable<RelatesTo>>(json[RelatesToKeyL]))
+ : relatesTo(fromJson<Omittable<RelatesTo>>(json[RelatesToKey]))
{
QMimeDatabase db;
static const auto PlainTextMimeType = db.mimeTypeForName("text/plain");
@@ -308,7 +310,7 @@ TextContent::TextContent(const QJsonObject& json)
// of sending HTML messages.
if (actualJson["format"_ls].toString() == HtmlContentTypeId) {
mimeType = HtmlMimeType;
- body = actualJson[FormattedBodyKeyL].toString();
+ body = actualJson[FormattedBodyKey].toString();
} else {
// Falling back to plain text, as there's no standard way to describe
// rich text in messages.
@@ -320,7 +322,6 @@ TextContent::TextContent(const QJsonObject& json)
void TextContent::fillJson(QJsonObject* json) const
{
static const auto FormatKey = QStringLiteral("format");
- static const auto FormattedBodyKey = QStringLiteral("formatted_body");
Q_ASSERT(json);
if (mimeType.inherits("text/html")) {
@@ -328,11 +329,14 @@ void TextContent::fillJson(QJsonObject* json) const
json->insert(FormattedBodyKey, body);
}
if (relatesTo) {
- json->insert(QStringLiteral("m.relates_to"),
- relatesTo->type == RelatesTo::ReplyTypeId() ?
- QJsonObject { { relatesTo->type, QJsonObject{ { EventIdKey, relatesTo->eventId } } } } :
- QJsonObject { { "rel_type", relatesTo->type }, { EventIdKey, relatesTo->eventId } }
- );
+ json->insert(
+ QStringLiteral("m.relates_to"),
+ relatesTo->type == RelatesTo::ReplyTypeId()
+ ? QJsonObject { { relatesTo->type,
+ QJsonObject {
+ { EventIdKey, relatesTo->eventId } } } }
+ : QJsonObject { { "rel_type", relatesTo->type },
+ { EventIdKey, relatesTo->eventId } });
if (relatesTo->type == RelatesTo::ReplacementTypeId()) {
QJsonObject newContentJson;
if (mimeType.inherits("text/html")) {