Fix reaction count

This commit is contained in:
Nicolas Werner 2020-05-06 11:15:45 +02:00
parent a1951056da
commit 8348a6c35d
2 changed files with 12 additions and 15 deletions

View File

@ -35,16 +35,19 @@ ReactionsModel::data(const QModelIndex &index, int role) const
return static_cast<int>(reactions[i].reactions.size()); return static_cast<int>(reactions[i].reactions.size());
case Users: { case Users: {
QString users; QString users;
for (size_t r = 0; r < reactions[i].reactions.size(); r++) { bool first = true;
if (r != 0) for (const auto &[event_id, reaction] : reactions[i].reactions) {
if (!first)
users += ", "; users += ", ";
users += QString::fromStdString(reactions[i].reactions[r].sender); else
first = false;
users += QString::fromStdString(reaction.sender);
} }
return users; return users;
} }
case SelfReacted: case SelfReacted:
for (const auto &reaction : reactions[i].reactions) for (const auto &reaction : reactions[i].reactions)
if (reaction.sender == http::client()->user_id().to_string()) if (reaction.second.sender == http::client()->user_id().to_string())
return true; return true;
return false; return false;
default: default:
@ -58,7 +61,7 @@ ReactionsModel::addReaction(const mtx::events::RoomEvent<mtx::events::msg::React
int idx = 0; int idx = 0;
for (auto &storedReactions : reactions) { for (auto &storedReactions : reactions) {
if (storedReactions.key == reaction.content.relates_to.key) { if (storedReactions.key == reaction.content.relates_to.key) {
storedReactions.reactions.push_back(reaction); storedReactions.reactions[reaction.event_id] = reaction;
emit dataChanged(index(idx, 0), index(idx, 0)); emit dataChanged(index(idx, 0), index(idx, 0));
return; return;
} }
@ -66,7 +69,8 @@ ReactionsModel::addReaction(const mtx::events::RoomEvent<mtx::events::msg::React
} }
beginInsertRows(QModelIndex(), idx, idx); beginInsertRows(QModelIndex(), idx, idx);
reactions.push_back(KeyReaction{reaction.content.relates_to.key, {reaction}}); reactions.push_back(
KeyReaction{reaction.content.relates_to.key, {{reaction.event_id, reaction}}});
endInsertRows(); endInsertRows();
} }
@ -76,14 +80,7 @@ ReactionsModel::removeReaction(const mtx::events::RoomEvent<mtx::events::msg::Re
int idx = 0; int idx = 0;
for (auto &storedReactions : reactions) { for (auto &storedReactions : reactions) {
if (storedReactions.key == reaction.content.relates_to.key) { if (storedReactions.key == reaction.content.relates_to.key) {
for (auto it = begin(storedReactions.reactions); storedReactions.reactions.erase(reaction.event_id);
it != end(storedReactions.reactions);
++it) {
if (it->event_id == reaction.event_id) {
storedReactions.reactions.erase(it);
break;
}
}
if (storedReactions.reactions.size() == 0) { if (storedReactions.reactions.size() == 0) {
beginRemoveRows(QModelIndex(), idx, idx); beginRemoveRows(QModelIndex(), idx, idx);

View File

@ -33,7 +33,7 @@ private:
struct KeyReaction struct KeyReaction
{ {
std::string key; std::string key;
std::vector<mtx::events::RoomEvent<mtx::events::msg::Reaction>> reactions; std::map<std::string, mtx::events::RoomEvent<mtx::events::msg::Reaction>> reactions;
}; };
std::vector<KeyReaction> reactions; std::vector<KeyReaction> reactions;
}; };