aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2019-10-29 22:04:40 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2019-11-01 08:43:09 +0900
commit60bb1cf942ad0815dcf42cbfe8acd1e076d848cf (patch)
tree3998826b6b78d9f7d3100570aa28aa86f8b040fd /lib/events
parent8a574f8727cc8b1c91acf0840e99c9382c289098 (diff)
downloadlibquotient-60bb1cf942ad0815dcf42cbfe8acd1e076d848cf.tar.gz
libquotient-60bb1cf942ad0815dcf42cbfe8acd1e076d848cf.zip
Derive Omittable<> from std::optional<>
That breaks API all over the place but: 1. The fixes are trivial. 2. More of std:: is used instead of home-baking the same stuff.
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/accountdataevents.h10
-rw-r--r--lib/events/roommessageevent.cpp23
-rw-r--r--lib/events/roommessageevent.h2
3 files changed, 18 insertions, 17 deletions
diff --git a/lib/events/accountdataevents.h b/lib/events/accountdataevents.h
index 31176766..a55016d9 100644
--- a/lib/events/accountdataevents.h
+++ b/lib/events/accountdataevents.h
@@ -1,5 +1,3 @@
-#include <utility>
-
/******************************************************************************
* Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net>
*
@@ -34,13 +32,13 @@ struct TagRecord {
order_type order;
- TagRecord(order_type order = none) : order(order) {}
+ TagRecord(order_type order = none) : order(std::move(order)) {}
bool operator<(const TagRecord& other) const
{
- // Per The Spec, rooms with no order should be after those with order
- return !order.omitted()
- && (other.order.omitted() || order.value() < other.order.value());
+ // Per The Spec, rooms with no order should be after those with order,
+ // against optional<>::operator<() convention.
+ return order && (!other.order || *order < *other.order);
}
};
diff --git a/lib/events/roommessageevent.cpp b/lib/events/roommessageevent.cpp
index 09562d65..078ae70a 100644
--- a/lib/events/roommessageevent.cpp
+++ b/lib/events/roommessageevent.cpp
@@ -95,6 +95,11 @@ MsgType jsonToMsgType(const QString& matrixType)
return MsgType::Unknown;
}
+inline bool isReplacement(const Omittable<RelatesTo>& rel)
+{
+ return rel && rel->type == RelatesTo::ReplacementTypeId();
+}
+
QJsonObject RoomMessageEvent::assembleContentJson(const QString& plainBody,
const QString& jsonMsgType,
TypedBase* content)
@@ -111,6 +116,7 @@ QJsonObject RoomMessageEvent::assembleContentJson(const QString& plainBody,
// After the above, we know for sure that the content is TextContent
// and that its RelatesTo structure is not omitted
auto* textContent = static_cast<const TextContent*>(content);
+ Q_ASSERT(textContent && textContent->relatesTo.has_value());
if (textContent->relatesTo->type == RelatesTo::ReplacementTypeId()) {
auto newContentJson = json.take("m.new_content"_ls).toObject();
newContentJson.insert(BodyKey, plainBody);
@@ -243,9 +249,7 @@ QString RoomMessageEvent::replacedEvent() const
return {};
const auto& rel = static_cast<const TextContent*>(content())->relatesTo;
- return !rel.omitted() && rel->type == RelatesTo::ReplacementTypeId()
- ? rel->eventId
- : QString();
+ return isReplacement(rel) ? rel->eventId : QString();
}
QString rawMsgTypeForMimeType(const QMimeType& mimeType)
@@ -269,10 +273,10 @@ QString RoomMessageEvent::rawMsgTypeForFile(const QFileInfo& fi)
return rawMsgTypeForMimeType(QMimeDatabase().mimeTypeForFile(fi));
}
-TextContent::TextContent(const QString& text, const QString& contentType,
+TextContent::TextContent(QString text, const QString& contentType,
Omittable<RelatesTo> relatesTo)
: mimeType(QMimeDatabase().mimeTypeForName(contentType))
- , body(text)
+ , body(std::move(text))
, relatesTo(std::move(relatesTo))
{
if (contentType == HtmlContentTypeId)
@@ -304,10 +308,9 @@ TextContent::TextContent(const QJsonObject& json)
static const auto PlainTextMimeType = db.mimeTypeForName("text/plain");
static const auto HtmlMimeType = db.mimeTypeForName("text/html");
- const auto actualJson =
- relatesTo.omitted() || relatesTo->type != RelatesTo::ReplacementTypeId()
- ? json
- : json.value("m.new_content"_ls).toObject();
+ const auto actualJson = isReplacement(relatesTo)
+ ? json.value("m.new_content"_ls).toObject()
+ : json;
// Special-casing the custom matrix.org's (actually, Riot's) way
// of sending HTML messages.
if (actualJson["format"_ls].toString() == HtmlContentTypeId) {
@@ -331,7 +334,7 @@ void TextContent::fillJson(QJsonObject* json) const
json->insert(FormatKey, HtmlContentTypeId);
json->insert(FormattedBodyKey, body);
}
- if (!relatesTo.omitted()) {
+ if (relatesTo) {
json->insert(QStringLiteral("m.relates_to"),
QJsonObject { { relatesTo->type, relatesTo->eventId } });
if (relatesTo->type == RelatesTo::ReplacementTypeId()) {
diff --git a/lib/events/roommessageevent.h b/lib/events/roommessageevent.h
index e95aabfc..ded5e572 100644
--- a/lib/events/roommessageevent.h
+++ b/lib/events/roommessageevent.h
@@ -114,7 +114,7 @@ namespace EventContent {
*/
class TextContent : public TypedBase {
public:
- TextContent(const QString& text, const QString& contentType,
+ TextContent(QString text, const QString& contentType,
Omittable<RelatesTo> relatesTo = none);
explicit TextContent(const QJsonObject& json);