From 442b68558e46d654bb22219ff588f33290b004ff Mon Sep 17 00:00:00 2001 From: Black Hat Date: Fri, 26 Jul 2019 17:48:17 +0800 Subject: [PATCH] Add/remove reactions. --- .../Component/Timeline/MessageDelegate.qml | 2 ++ .../Component/Timeline/ReactionDelegate.qml | 6 +++- src/spectralroom.cpp | 36 +++++++++++++++++-- src/spectralroom.h | 1 + 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/imports/Spectral/Component/Timeline/MessageDelegate.qml b/imports/Spectral/Component/Timeline/MessageDelegate.qml index 6276f2c38..18c39dd8e 100644 --- a/imports/Spectral/Component/Timeline/MessageDelegate.qml +++ b/imports/Spectral/Component/Timeline/MessageDelegate.qml @@ -222,6 +222,8 @@ ColumnLayout { } ReactionDelegate { + Layout.fillWidth: true + Layout.topMargin: 0 Layout.bottomMargin: 8 Layout.leftMargin: 16 diff --git a/imports/Spectral/Component/Timeline/ReactionDelegate.qml b/imports/Spectral/Component/Timeline/ReactionDelegate.qml index e536003ba..74509b005 100644 --- a/imports/Spectral/Component/Timeline/ReactionDelegate.qml +++ b/imports/Spectral/Component/Timeline/ReactionDelegate.qml @@ -3,9 +3,11 @@ import QtQuick.Controls 2.12 import QtQuick.Layouts 1.12 import Spectral.Setting 0.1 -RowLayout { +Flow { visible: reaction || false + spacing: 8 + Repeater { model: reaction @@ -40,6 +42,8 @@ RowLayout { return text } + + onClicked: currentRoom.toggleReaction(eventId, modelData.reaction) } } diff --git a/src/spectralroom.cpp b/src/spectralroom.cpp index 6b6b9dfcd..389d3d7b6 100644 --- a/src/spectralroom.cpp +++ b/src/spectralroom.cpp @@ -10,9 +10,9 @@ #include "csapi/rooms.h" #include "csapi/typing.h" #include "events/accountdataevents.h" +#include "events/reactionevent.h" #include "events/roommessageevent.h" #include "events/typingevent.h" -#include "events/reactionevent.h" #include "jobs/downloadfilejob.h" #include @@ -306,7 +306,7 @@ QString SpectralRoom::markdownToHTML(const QString& markdown) { std::string html(tmp_buf); - free((char *)tmp_buf); + free((char*)tmp_buf); auto result = QString::fromStdString(html).trimmed(); @@ -420,3 +420,35 @@ void SpectralRoom::postHtmlMessage(const QString& text, Room::postHtmlMessage(text, html, type); } + +void SpectralRoom::toggleReaction(const QString& eventId, + const QString& reaction) { + const auto eventIt = findInTimeline(eventId); + if (eventIt == timelineEdge()) + return; + + const auto& evt = **eventIt; + + QString redactEventId = ""; + + const auto& annotations = relatedEvents(evt, EventRelation::Annotation()); + if (!annotations.isEmpty()) { + for (const auto& a : annotations) { + if (auto e = eventCast(a)) { + if (e->relation().key != reaction) + continue; + + if (e->senderId() == localUser()->id()) { + redactEventId = e->id(); + break; + } + } + } + } + + if (!redactEventId.isEmpty()) { + redactEvent(redactEventId); + } else { + postEvent(new ReactionEvent(EventRelation::annotate(eventId, reaction))); + } +} diff --git a/src/spectralroom.h b/src/spectralroom.h index e4885b8f5..9c84368fc 100644 --- a/src/spectralroom.h +++ b/src/spectralroom.h @@ -315,6 +315,7 @@ class SpectralRoom : public Room { void changeAvatar(QUrl localFile); void addLocalAlias(const QString& alias); void removeLocalAlias(const QString& alias); + void toggleReaction(const QString& eventId, const QString& reaction); }; #endif // SpectralRoom_H