Reactions are refreshed correctly in the timeline.

This commit is contained in:
Black Hat
2019-07-30 21:29:55 +08:00
parent a9a59fad41
commit f7da8eebad
4 changed files with 52 additions and 31 deletions

View File

@@ -12,31 +12,38 @@ Menu {
id: root id: root
Flow { Item {
width: parent.width width: parent.width
height: 32
spacing: 0 Row {
anchors.centerIn: parent
Repeater { spacing: 0
model: ["👍", "👎️", "😄", "🎉", "🚀", "👀"]
delegate: ItemDelegate { Repeater {
width: 32 model: ["👍", "👎️", "😄", "🎉", "🚀", "👀"]
height: 32
contentItem: Label { delegate: ItemDelegate {
horizontalAlignment: Text.AlignHCenter width: 32
verticalAlignment: Text.AlignVCenter height: 32
font.pixelSize: 16 contentItem: Label {
text: modelData horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pixelSize: 16
text: modelData
}
onClicked: currentRoom.toggleReaction(eventId, modelData)
} }
onClicked: currentRoom.toggleReaction(eventId, modelData)
} }
} }
} }
MenuSeparator {}
MenuItem { MenuItem {
text: "View Source" text: "View Source"

View File

@@ -136,6 +136,10 @@ void MessageEventModel::setRoom(SpectralRoom* room) {
refreshLastUserEvents(refreshEvent(newEvent->id()) - refreshLastUserEvents(refreshEvent(newEvent->id()) -
timelineBaseIndex()); timelineBaseIndex());
}); });
connect(m_currentRoom, &Room::updatedEvent, this,
[this](const QString& eventId) {
refreshEventRoles(eventId, {ReactionRole});
});
connect(m_currentRoom, &Room::fileTransferProgress, this, connect(m_currentRoom, &Room::fileTransferProgress, this,
&MessageEventModel::refreshEvent); &MessageEventModel::refreshEvent);
connect(m_currentRoom, &Room::fileTransferCompleted, this, connect(m_currentRoom, &Room::fileTransferCompleted, this,
@@ -178,15 +182,24 @@ void MessageEventModel::refreshEventRoles(int row, const QVector<int>& roles) {
emit dataChanged(idx, idx, roles); emit dataChanged(idx, idx, roles);
} }
int MessageEventModel::refreshEventRoles(const QString& eventId, int MessageEventModel::refreshEventRoles(const QString& id,
const QVector<int>& roles) { const QVector<int>& roles) {
const auto it = m_currentRoom->findInTimeline(eventId); // On 64-bit platforms, difference_type for std containers is long long
if (it == m_currentRoom->timelineEdge()) { // but Qt uses int throughout its interfaces; hence casting to int below.
qWarning() << "Trying to refresh inexistent event:" << eventId; int row = -1;
return -1; // First try pendingEvents because it is almost always very short.
const auto pendingIt = m_currentRoom->findPendingEvent(id);
if (pendingIt != m_currentRoom->pendingEvents().end())
row = int(pendingIt - m_currentRoom->pendingEvents().begin());
else {
const auto timelineIt = m_currentRoom->findInTimeline(id);
if (timelineIt == m_currentRoom->timelineEdge()) {
qWarning() << "Trying to refresh inexistent event:" << id;
return -1;
}
row = int(timelineIt - m_currentRoom->messageEvents().rbegin()) +
timelineBaseIndex();
} }
const auto row =
it - m_currentRoom->messageEvents().rbegin() + timelineBaseIndex();
refreshEventRoles(row, roles); refreshEventRoles(row, roles);
return row; return row;
} }
@@ -485,14 +498,20 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const {
m_currentRoom->relatedEvents(evt, EventRelation::Annotation()); m_currentRoom->relatedEvents(evt, EventRelation::Annotation());
if (annotations.isEmpty()) if (annotations.isEmpty())
return {}; return {};
QMap<QString, QList<SpectralUser*>> reactions; QMap<QString, QList<SpectralUser*>> reactions = {};
for (const auto& a : annotations) { for (const auto& a : annotations) {
if (a->isRedacted()) // Just in case?
continue;
if (auto e = eventCast<const ReactionEvent>(a)) if (auto e = eventCast<const ReactionEvent>(a))
reactions[e->relation().key].append( reactions[e->relation().key].append(
static_cast<SpectralUser*>(m_currentRoom->user(e->senderId()))); static_cast<SpectralUser*>(m_currentRoom->user(e->senderId())));
} }
QVariantList res; if (reactions.isEmpty()) {
return {};
}
QVariantList res = {};
QMap<QString, QList<SpectralUser*>>::const_iterator i = QMap<QString, QList<SpectralUser*>>::const_iterator i =
reactions.constBegin(); reactions.constBegin();
while (i != reactions.constEnd()) { while (i != reactions.constEnd()) {

View File

@@ -432,7 +432,7 @@ void SpectralRoom::toggleReaction(const QString& eventId,
const auto& evt = **eventIt; const auto& evt = **eventIt;
QStringList redactEventIds; // What if there are multiple reaction events? QStringList redactEventIds; // What if there are multiple reaction events?
const auto& annotations = relatedEvents(evt, EventRelation::Annotation()); const auto& annotations = relatedEvents(evt, EventRelation::Annotation());
if (!annotations.isEmpty()) { if (!annotations.isEmpty()) {
@@ -450,15 +450,10 @@ void SpectralRoom::toggleReaction(const QString& eventId,
} }
if (!redactEventIds.isEmpty()) { if (!redactEventIds.isEmpty()) {
qDebug() << "Remove reaction event" << redactEventIds << "of event"
<< eventId << ":" << reaction;
for (auto redactEventId : redactEventIds) { for (auto redactEventId : redactEventIds) {
redactEvent(redactEventId); redactEvent(redactEventId);
} }
} else { } else {
qDebug() << "Add reaction event" << eventId << ":" << reaction;
postEvent(new ReactionEvent(EventRelation::annotate(eventId, reaction))); postEvent(new ReactionEvent(EventRelation::annotate(eventId, reaction)));
} }
qDebug() << "End of SpectralRoom::toggleReaction()";
} }