Skip to content

Commit

Permalink
better preprocessing of reaction to minimize code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Jun 24, 2024
1 parent bb8380e commit 10ea450
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/chatview_webkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,15 +697,18 @@ void ChatView::dispatchMessage(const MessageView &mv)
m["sender"] = mv.nick();
m["messageid"] = mv.reactionsId();
{
auto r = updateReactions(mv.nick(), mv.reactionsId(), mv.reactions());
auto vmr = QVariantMap();

QMapIterator<QString, QStringList> it(r);
while (it.hasNext()) {
it.next();
vmr[it.key()] = it.value();
auto rl = updateReactions(mv.nick(), mv.reactionsId(), mv.reactions());
auto vl = QVariantList();
for (auto &r : std::as_const(rl)) {
auto vmr = QVariantMap();
if (!r.base.isEmpty()) {
vmr[QLatin1String("base")] = r.base;
}
vmr[QLatin1String("text")] = r.code;
vmr[QLatin1String("nicks")] = r.nicks;
vl << vmr;
}
m["reactions"] = vmr;
m[QLatin1String("reactions")] = vl;
}
break;
case MessageView::FileTransferRequest:
Expand Down
13 changes: 11 additions & 2 deletions src/chatviewcommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool ChatViewCommon::compatibleColors(const QColor &c1, const QColor &c2)
return !((dC < 80. && dV > 100) || (dC < 110. && dV <= 100 && dV > 10) || (dC < 125. && dV <= 10));
}

const QMap<QString, QStringList> &
QList<ChatViewCommon::ReactionsItem>
ChatViewCommon::updateReactions(const QString &senderNickname, const QString &messageId, const QSet<QString> &reactions)
{
auto msgIt = _reactions.find(messageId);
Expand Down Expand Up @@ -157,5 +157,14 @@ ChatViewCommon::updateReactions(const QString &senderNickname, const QString &me
for (auto const &v : toRemove) {
msgIt.value().total[v].removeOne(senderNickname);
}
return msgIt.value().total;
auto &total = msgIt.value().total;

QList<ReactionsItem> ret;
for (auto it = total.begin(); it != total.end(); ++it) {
static const auto skinRemove = QRegularExpression("([\\x{1F3FB}-\\x{1F3FF}]|[\\x{1F9B0}-\\x{1F9B2}])");
QString orig = it.key();
auto sanitized = orig.remove(skinRemove);
ret << ReactionsItem { sanitized != orig ? sanitized : QString {}, orig, it.value() };
}
return ret;
}
10 changes: 8 additions & 2 deletions src/chatviewcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ class ChatViewCommon {
QHash<QString, QSet<QString>> perUser; // nickname => unicode reactions
};

struct ReactionsItem {
QString base;
QString code;
QStringList nicks;
};

void addUser(const QString &nickname);
void removeUser(const QString &nickname);
void renameUser(const QString &oldNickname, const QString &newNickname);

const QMap<QString, QStringList> &updateReactions(const QString &senderNickname, const QString &messageId,
const QSet<QString> &reactions);
QList<ReactionsItem> updateReactions(const QString &senderNickname, const QString &messageId,
const QSet<QString> &reactions);

protected:
QDateTime _lastMsgTime;
Expand Down

0 comments on commit 10ea450

Please sign in to comment.