summary refs log tree commit diff
path: root/include
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-11-30 13:19:34 +0200
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-11-30 13:19:34 +0200
commit5663c58dd92150422b8f881cf0720859173091b8 (patch)
treef9bab72660d2b62c25e8cdd4f4474dfee310e267 /include
parentSimplify timeline item creation (diff)
downloadnheko-5663c58dd92150422b8f881cf0720859173091b8.tar.xz
Use templates for the TimelineItem generation
Diffstat (limited to 'include')
-rw-r--r--include/TimelineView.h88
1 files changed, 79 insertions, 9 deletions
diff --git a/include/TimelineView.h b/include/TimelineView.h
index 5453ea5c..898a304e 100644
--- a/include/TimelineView.h
+++ b/include/TimelineView.h
@@ -18,6 +18,7 @@
 #pragma once
 
 #include <QApplication>
+#include <QDebug>
 #include <QLayout>
 #include <QList>
 #include <QQueue>
@@ -90,15 +91,6 @@ public:
                      const QString &room_id,
                      QWidget *parent = 0);
 
-        // For events with custom display widgets.
-        template<class Event, class Widget>
-        TimelineItem *createTimelineItem(const Event &event, bool withSender);
-
-        // For events without custom display widgets.
-        // TODO: All events should have custom widgets.
-        template<class Event>
-        TimelineItem *createTimelineItem(const Event &event, bool withSender);
-
         // Add new events at the end of the timeline.
         int addEvents(const Timeline &timeline);
         void addUserMessage(matrix::events::MessageEventType ty, const QString &msg);
@@ -141,6 +133,22 @@ private:
         void readLastEvent() const;
         QString getLastEventId() const;
 
+        template<class Event, class Widget>
+        TimelineItem *processMessageEvent(const QJsonObject &event, TimelineDirection direction);
+
+        // TODO: Remove this eventually.
+        template<class Event>
+        TimelineItem *processMessageEvent(const QJsonObject &event, TimelineDirection direction);
+
+        // For events with custom display widgets.
+        template<class Event, class Widget>
+        TimelineItem *createTimelineItem(const Event &event, bool withSender);
+
+        // For events without custom display widgets.
+        // TODO: All events should have custom widgets.
+        template<class Event>
+        TimelineItem *createTimelineItem(const Event &event, bool withSender);
+
         // Used to determine whether or not we should prefix a message with the
         // sender's name.
         bool isSenderRendered(const QString &user_id, TimelineDirection direction);
@@ -238,3 +246,65 @@ TimelineView::createTimelineItem(const Event &event, bool withSender)
 
         return item;
 }
+
+template<class Event>
+TimelineItem *
+TimelineView::processMessageEvent(const QJsonObject &data, TimelineDirection direction)
+{
+        Event event;
+
+        try {
+                event.deserialize(data);
+        } catch (const DeserializationException &e) {
+                qWarning() << e.what() << data;
+                return nullptr;
+        }
+
+        if (isDuplicate(event.eventId()))
+                return nullptr;
+
+        eventIds_[event.eventId()] = true;
+
+        QString txnid = event.unsignedData().transactionId();
+        if (!txnid.isEmpty() && isPendingMessage(txnid, event.sender(), local_user_)) {
+                removePendingMessage(txnid);
+                return nullptr;
+        }
+
+        auto with_sender = isSenderRendered(event.sender(), direction);
+
+        updateLastSender(event.sender(), direction);
+
+        return createTimelineItem<Event>(event, with_sender);
+}
+
+template<class Event, class Widget>
+TimelineItem *
+TimelineView::processMessageEvent(const QJsonObject &data, TimelineDirection direction)
+{
+        Event event;
+
+        try {
+                event.deserialize(data);
+        } catch (const DeserializationException &e) {
+                qWarning() << e.what() << data;
+                return nullptr;
+        }
+
+        if (isDuplicate(event.eventId()))
+                return nullptr;
+
+        eventIds_[event.eventId()] = true;
+
+        QString txnid = event.unsignedData().transactionId();
+        if (!txnid.isEmpty() && isPendingMessage(txnid, event.sender(), local_user_)) {
+                removePendingMessage(txnid);
+                return nullptr;
+        }
+
+        auto with_sender = isSenderRendered(event.sender(), direction);
+
+        updateLastSender(event.sender(), direction);
+
+        return createTimelineItem<Event, Widget>(event, with_sender);
+}