aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/event.cpp3
-rw-r--r--lib/events/event.h41
-rw-r--r--lib/events/stickerevent.cpp26
-rw-r--r--lib/events/stickerevent.h38
4 files changed, 88 insertions, 20 deletions
diff --git a/lib/events/event.cpp b/lib/events/event.cpp
index 6014183e..97edb4e0 100644
--- a/lib/events/event.cpp
+++ b/lib/events/event.cpp
@@ -49,11 +49,14 @@ QString Event::matrixType() const { return fullJson()[TypeKeyL].toString(); }
QByteArray Event::originalJson() const { return QJsonDocument(_json).toJson(); }
+// On const below: this is to catch accidental attempts to change event JSON
+// NOLINTNEXTLINE(readability-const-return-type)
const QJsonObject Event::contentJson() const
{
return fullJson()[ContentKeyL].toObject();
}
+// NOLINTNEXTLINE(readability-const-return-type)
const QJsonObject Event::unsignedJson() const
{
return fullJson()[UnsignedKeyL].toObject();
diff --git a/lib/events/event.h b/lib/events/event.h
index e9d42333..c5752a7a 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -286,7 +286,7 @@ using Events = EventsArray<Event>;
// === is<>(), eventCast<>() and visit<>() ===
-template <typename EventT>
+template <class EventT>
inline bool is(const Event& e)
{
return e.type() == typeId<EventT>();
@@ -297,7 +297,7 @@ inline bool isUnknown(const Event& e)
return e.type() == unknownEventTypeId();
}
-template <typename EventT, typename BasePtrT>
+template <class EventT, typename BasePtrT>
inline auto eventCast(const BasePtrT& eptr)
-> decltype(static_cast<EventT*>(&*eptr))
{
@@ -307,7 +307,7 @@ inline auto eventCast(const BasePtrT& eptr)
}
// A single generic catch-all visitor
-template <typename BaseEventT, typename FnT>
+template <class BaseEventT, typename FnT>
inline auto visit(const BaseEventT& event, FnT&& visitor)
-> decltype(visitor(event))
{
@@ -315,18 +315,18 @@ inline auto visit(const BaseEventT& event, FnT&& visitor)
}
namespace _impl {
- template <typename T, typename FnT>
- constexpr auto needs_downcast()
- {
- return !std::is_convertible_v<T, fn_arg_t<FnT>>;
- }
+ // Using bool instead of auto below because auto apparently upsets MSVC
+ template <class BaseT, typename FnT>
+ inline 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>>>;
}
// A single type-specific void visitor
-template <typename BaseEventT, typename FnT>
-inline std::enable_if_t<_impl::needs_downcast<BaseEventT, FnT>()
+template <class BaseT, typename FnT>
+inline auto visit(const BaseT& event, FnT&& visitor)
+ -> std::enable_if_t<_impl::needs_downcast<BaseT, FnT>
&& std::is_void_v<fn_return_t<FnT>>>
-visit(const BaseEventT& event, FnT&& visitor)
{
using event_type = fn_arg_t<FnT>;
if (is<std::decay_t<event_type>>(event))
@@ -335,10 +335,10 @@ visit(const BaseEventT& event, FnT&& visitor)
// A single type-specific non-void visitor with an optional default value
// non-voidness is guarded by defaultValue type
-template <typename BaseEventT, typename FnT>
-inline std::enable_if_t<_impl::needs_downcast<BaseEventT, FnT>(), fn_return_t<FnT>>
-visit(const BaseEventT& event, FnT&& visitor,
- fn_return_t<FnT>&& defaultValue = {})
+template <class BaseT, typename FnT>
+inline auto visit(const BaseT& event, FnT&& visitor,
+ fn_return_t<FnT>&& defaultValue = {})
+ -> std::enable_if_t<_impl::needs_downcast<BaseT, FnT>, fn_return_t<FnT>>
{
using event_type = fn_arg_t<FnT>;
if (is<std::decay_t<event_type>>(event))
@@ -347,9 +347,10 @@ visit(const BaseEventT& event, FnT&& visitor,
}
// A chain of 2 or more visitors
-template <typename BaseEventT, typename FnT1, typename FnT2, typename... FnTs>
-inline fn_return_t<FnT1> visit(const BaseEventT& event, FnT1&& visitor1,
- FnT2&& visitor2, FnTs&&... visitors)
+template <class BaseT, typename FnT1, typename FnT2, typename... FnTs>
+inline std::common_type_t<fn_return_t<FnT1>, fn_return_t<FnT2>> visit(
+ const BaseT& event, FnT1&& visitor1, FnT2&& visitor2,
+ FnTs&&... visitors)
{
using event_type1 = fn_arg_t<FnT1>;
if (is<std::decay_t<event_type1>>(event))
@@ -362,8 +363,8 @@ inline fn_return_t<FnT1> visit(const BaseEventT& event, FnT1&& visitor1,
// over a range of event pointers
template <typename RangeT, typename... FnTs>
inline auto visitEach(RangeT&& events, FnTs&&... visitors)
- -> std::enable_if_t<std::is_convertible_v<
- std::decay_t<decltype(**events.begin())>, Event>>
+ -> std::enable_if_t<std::is_void_v<
+ decltype(visit(**begin(events), std::forward<FnTs>(visitors)...))>>
{
for (auto&& evtPtr: events)
visit(*evtPtr, std::forward<FnTs>(visitors)...);
diff --git a/lib/events/stickerevent.cpp b/lib/events/stickerevent.cpp
new file mode 100644
index 00000000..ea4dff3f
--- /dev/null
+++ b/lib/events/stickerevent.cpp
@@ -0,0 +1,26 @@
+// SDPX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#include "stickerevent.h"
+
+using namespace Quotient;
+
+StickerEvent::StickerEvent(const QJsonObject &obj)
+ : RoomEvent(typeId(), obj)
+ , m_imageContent(EventContent::ImageContent(obj["content"_ls].toObject()))
+{}
+
+QString StickerEvent::body() const
+{
+ return content<QString>("body"_ls);
+}
+
+const EventContent::ImageContent &StickerEvent::image() const
+{
+ return m_imageContent;
+}
+
+QUrl StickerEvent::url() const
+{
+ return m_imageContent.url;
+}
diff --git a/lib/events/stickerevent.h b/lib/events/stickerevent.h
new file mode 100644
index 00000000..93671086
--- /dev/null
+++ b/lib/events/stickerevent.h
@@ -0,0 +1,38 @@
+// SDPX-FileCopyrightText: 2020 Carl Schwan <carlschwan@kde.org>
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#pragma once
+
+#include "roomevent.h"
+#include "eventcontent.h"
+
+namespace Quotient {
+
+/// Sticker messages are specialised image messages that are displayed without
+/// controls (e.g. no "download" link, or light-box view on click, as would be
+/// displayed for for m.image events).
+class StickerEvent : public RoomEvent
+{
+public:
+ DEFINE_EVENT_TYPEID("m.sticker", StickerEvent)
+
+ explicit StickerEvent(const QJsonObject &obj);
+
+ /// \brief A textual representation or associated description of the
+ /// sticker image.
+ ///
+ /// This could be the alt text of the original image, or a message to
+ /// accompany and further describe the sticker.
+ QString body() const;
+
+ /// \brief Metadata about the image referred to in url including a
+ /// thumbnail representation.
+ const EventContent::ImageContent &image() const;
+
+ /// \brief The URL to the sticker image. This must be a valid mxc:// URI.
+ QUrl url() const;
+private:
+ EventContent::ImageContent m_imageContent;
+};
+REGISTER_EVENT_TYPE(StickerEvent)
+} // namespace Quotient