Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show reactions #655

Merged
merged 10 commits into from
Apr 9, 2020
19 changes: 19 additions & 0 deletions client/chatroomwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <QtCore/QLocale>

#include <events/roommessageevent.h>
#include <events/reactionevent.h>
#include <csapi/message_pagination.h>
#include <user.h>
#include <connection.h>
Expand Down Expand Up @@ -737,6 +738,24 @@ void ChatRoomWidget::showMenu(int index, const QString& hoveredLink,
menu.exec(QCursor::pos());
}

void ChatRoomWidget::reactionButtonClicked(const QString& eventId, const QString& key)
{
using namespace Quotient;
const auto& annotations =
m_currentRoom->relatedEvents(eventId, EventRelation::Annotation());

for (const auto& a : annotations) {
auto* e = eventCast<const ReactionEvent>(a);
if (e != nullptr && e->relation().key == key
&& a->senderId() == m_currentRoom->localUser()->id()) {
m_currentRoom->redactEvent(a->id());
return;
}
}

m_currentRoom->postReaction(eventId, key);
}

void ChatRoomWidget::setGlobalSelectionBuffer(QString text)
{
if (QApplication::clipboard()->supportsSelection())
Expand Down
1 change: 1 addition & 0 deletions client/chatroomwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ChatRoomWidget: public QWidget
void saveFileAs(QString eventId);
void quote(const QString& htmlText);
void showMenu(int index, const QString& hoveredLink, bool showingDetails);
void reactionButtonClicked(const QString& eventId, const QString& key);
void fileDrop(const QString& url);
void textDrop(const QString& text);
void setGlobalSelectionBuffer(QString text);
Expand Down
47 changes: 44 additions & 3 deletions client/models/messageeventmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <events/roomcreateevent.h>
#include <events/roomtombstoneevent.h>
#include <events/roomcanonicalaliasevent.h>
#include <events/reactionevent.h>

QHash<int, QByteArray> MessageEventModel::roleNames() const
{
Expand All @@ -55,6 +56,7 @@ QHash<int, QByteArray> MessageEventModel::roleNames() const
roles[UserHueRole] = "userHue";
roles[EventResolvedTypeRole] = "eventResolvedType";
roles[RefRole] = "refId";
roles[ReactionsRole] = "reactions";
return roles;
}

Expand Down Expand Up @@ -166,6 +168,8 @@ void MessageEventModel::changeRoom(QuaternionRoom* room)
refreshLastUserEvents(
refreshEvent(newEvent->id()) - timelineBaseIndex());
});
connect(m_currentRoom, &Room::updatedEvent,
this, &MessageEventModel::refreshEvent);
connect(m_currentRoom, &Room::fileTransferProgress,
this, &MessageEventModel::refreshEvent);
connect(m_currentRoom, &Room::fileTransferCompleted,
Expand Down Expand Up @@ -656,7 +660,7 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const
return !Settings().get<bool>("UI/suppress_local_echo")
? pendingIt->deliveryStatus() : EventStatus::Hidden;

if (is<RedactionEvent>(evt))
if (is<RedactionEvent>(evt) || is<ReactionEvent>(evt))
return EventStatus::Hidden;

auto* memberEvent = timelineIt->viewAs<RoomMemberEvent>();
Expand Down Expand Up @@ -705,8 +709,45 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const
}

if( role == AnnotationRole )
if (isPending)
return pendingIt->annotation();
return isPending ? pendingIt->annotation() : QString();

if( role == ReactionsRole )
{
const auto& annotations =
m_currentRoom->relatedEvents(evt, EventRelation::Annotation());
if (annotations.isEmpty())
return {};
QJsonArray reactions;
for (const auto& a : annotations) {
if (auto e = eventCast<const ReactionEvent>(a)) {
QJsonObject obj;

for (const auto& reaction : reactions) {
if (reaction.toObject()["key"] == e->relation().key) {
obj = reaction.toObject();
break;
}
}

QJsonArray authors = obj["authors"].toArray()
+ m_currentRoom->roomMembername(e->senderId());

QJsonObject reaction {
{"authors", authors},
{"count", obj["count"].toInt() + 1},
{"key", e->relation().key},
};

auto it = std::find(reactions.begin(), reactions.end(), obj);
if (it != reactions.end())
reactions.replace(it - reactions.begin(), reaction);
else
reactions.append(reaction);
}
}

return reactions;
}

if( role == TimeRole || role == SectionRole)
{
Expand Down
1 change: 1 addition & 0 deletions client/models/messageeventmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MessageEventModel: public QAbstractListModel
AnnotationRole,
UserHueRole,
RefRole,
ReactionsRole,
EventResolvedTypeRole,
};

Expand Down
17 changes: 17 additions & 0 deletions client/qml/TimelineItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ Item {
}

Loader {
id: imageLoader
active: eventType == "image"

anchors.top: textField.bottom
Expand Down Expand Up @@ -411,6 +412,7 @@ Item {
}
}
Loader {
id: fileLoader
active: eventType == "file"

anchors.top: textField.bottom
Expand All @@ -420,6 +422,21 @@ Item {

sourceComponent: FileContent { }
}
Flow {
anchors.top: imageLoader.active ? imageLoader.bottom : fileLoader.bottom
anchors.left: textField.left
anchors.right: parent.right

Repeater {
model: reactions
Button {
text: modelData.key + ": " + modelData.count
onClicked: controller.reactionButtonClicked(eventId, modelData.key)
tooltip: qsTr("%1 reacted with %2", "", modelData.authors.length)
.arg(modelData.authors.join(", ")).arg(modelData.key)
}
}
}
Loader {
id: buttonAreaLoader
active: failed || // resendButton
Expand Down