From a44a4f36af67c016e207b1cbd63817c6751ed89d Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 2 Sep 2017 16:47:59 +0300 Subject: [PATCH] Add read support for m.emote events (#41) --- include/TimelineItem.h | 4 ++++ include/TimelineView.h | 3 +++ src/TimelineItem.cc | 39 +++++++++++++++++++++++++++++++++++++-- src/TimelineView.cc | 29 +++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/include/TimelineItem.h b/include/TimelineItem.h index df43c26..59c79d5 100644 --- a/include/TimelineItem.h +++ b/include/TimelineItem.h @@ -25,6 +25,7 @@ #include "Sync.h" #include "Avatar.h" +#include "Emote.h" #include "Image.h" #include "MessageEvent.h" #include "Notice.h" @@ -44,6 +45,9 @@ public: TimelineItem(const events::MessageEvent &e, bool with_sender, QWidget *parent = 0); + TimelineItem(const events::MessageEvent &e, + bool with_sender, + QWidget *parent = 0); // For local messages. TimelineItem(const QString &userid, QString body, QWidget *parent = 0); diff --git a/include/TimelineView.h b/include/TimelineView.h index c6eef26..7583e4c 100644 --- a/include/TimelineView.h +++ b/include/TimelineView.h @@ -28,6 +28,7 @@ #include "TimelineItem.h" #include "Image.h" +#include "Emote.h" #include "Notice.h" #include "RoomInfoListItem.h" #include "Text.h" @@ -77,6 +78,8 @@ public: bool with_sender); TimelineItem *createTimelineItem(const events::MessageEvent &e, bool with_sender); + TimelineItem *createTimelineItem(const events::MessageEvent &e, + bool with_sender); // Add new events at the end of the timeline. int addEvents(const Timeline &timeline); diff --git a/src/TimelineItem.cc b/src/TimelineItem.cc index cfa5a98..62ebc51 100644 --- a/src/TimelineItem.cc +++ b/src/TimelineItem.cc @@ -191,6 +191,43 @@ TimelineItem::TimelineItem(const events::MessageEvent &event, mainLayout_->addWidget(body_); } +/* + * Used to display remote emote messages. + */ +TimelineItem::TimelineItem(const events::MessageEvent &event, + bool with_sender, + QWidget *parent) + : QWidget(parent) +{ + init(); + + auto body = event.content().body().trimmed().toHtmlEscaped(); + auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp()); + auto displayName = TimelineViewManager::displayName(event.sender()); + auto emoteMsg = QString("* %1 %2").arg(displayName).arg(body); + + descriptionMsg_ = { "", + event.sender(), + emoteMsg, + descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.timestamp())) }; + + generateTimestamp(timestamp); + emoteMsg.replace(URL_REGEX, URL_HTML); + + if (with_sender) { + generateBody(displayName, emoteMsg); + setupAvatarLayout(displayName); + mainLayout_->addLayout(headerLayout_); + + AvatarProvider::resolve(event.sender(), this); + } else { + generateBody(emoteMsg); + setupSimpleLayout(); + } + + mainLayout_->addWidget(body_); +} + /* * Used to display remote text messages. */ @@ -216,8 +253,6 @@ TimelineItem::TimelineItem(const events::MessageEvent &event, body.replace(URL_REGEX, URL_HTML); if (with_sender) { - auto displayName = TimelineViewManager::displayName(event.sender()); - generateBody(displayName, body); setupAvatarLayout(displayName); diff --git a/src/TimelineView.cc b/src/TimelineView.cc index 114e5fd..4dd6360 100644 --- a/src/TimelineView.cc +++ b/src/TimelineView.cc @@ -274,6 +274,28 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire updateLastSender(img.sender(), direction); return createTimelineItem(img, with_sender); + } else if (msg_type == events::MessageEventType::Emote) { + events::MessageEvent emote; + + try { + emote.deserialize(event); + } catch (const DeserializationException &e) { + qWarning() << e.what() << event; + return nullptr; + } + + if (isDuplicate(emote.eventId())) + return nullptr; + + eventIds_[emote.eventId()] = true; + + // TODO Check if it's a message waiting for validation + + auto with_sender = isSenderRendered(emote.sender(), direction); + + updateLastSender(emote.sender(), direction); + + return createTimelineItem(emote, with_sender); } else if (msg_type == events::MessageEventType::Unknown) { qWarning() << "Unknown message type" << event; return nullptr; @@ -403,6 +425,13 @@ TimelineView::createTimelineItem(const events::MessageEvent &event, return item; } +TimelineItem * +TimelineView::createTimelineItem(const events::MessageEvent &event, bool with_sender) +{ + TimelineItem *item = new TimelineItem(event, with_sender, scroll_widget_); + return item; +} + void TimelineView::addTimelineItem(TimelineItem *item, TimelineDirection direction) {