Skip to content

Commit

Permalink
reactions in private chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Jun 30, 2024
1 parent c0cd337 commit 3d64d67
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 52 deletions.
108 changes: 61 additions & 47 deletions src/chatview_webkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ class ChatViewPrivate {
ret.append(QStringView { msg }.mid(post));
return ret;
}

void sendJsObject(const QVariantMap &map)
{
jsBuffer_.append(map);
checkJsBuffer();
}

void checkJsBuffer();

void sendReactionsToUI(const QString &nick, const QString &messageId, const QSet<QString> &reactions)
{
QVariantMap m;
// m["type"] = "message";
m["type"] = QLatin1String("reactions");
m["sender"] = nick;
m["messageid"] = messageId;
auto rl = q->updateReactions(nick, messageId, 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[QLatin1String("reactions")] = vl;
sendJsObject(m);
}
};

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -483,12 +513,12 @@ ChatView::ChatView(QWidget *parent) : QFrame(parent), d(new ChatViewPrivate(this
QVariantMap m;
m["type"] = "receivehooks";
m["hooks"] = PluginManager::instance()->messageViewJSFilters();
sendJsObject(m);
d->sendJsObject(m);
connect(PluginManager::instance(), &PluginManager::jsFiltersUpdated, this, [this]() {
QVariantMap m;
m["type"] = "receivehooks";
m["hooks"] = PluginManager::instance()->messageViewJSFilters();
sendJsObject(m);
d->sendJsObject(m);
});
#endif
}
Expand Down Expand Up @@ -538,7 +568,7 @@ void ChatView::markReceived(QString id)
m["type"] = "receipt";
m["id"] = id;
m["encrypted"] = d->isEncryptionEnabled_;
sendJsObject(m);
d->sendJsObject(m);
}

QSize ChatView::sizeHint() const { return minimumSizeHint(); }
Expand Down Expand Up @@ -594,7 +624,7 @@ void ChatView::changeEvent(QEvent *event)
|| event->type() == QEvent::FontChange) {
QVariantMap m;
m["type"] = "settings";
sendJsObject(m);
d->sendJsObject(m);
}
QFrame::changeEvent(event);
}
Expand All @@ -610,26 +640,11 @@ void ChatView::psiOptionChanged(const QString &option)
}
}

void ChatView::sendJsObject(const QVariantMap &map)
{
d->jsBuffer_.append(map);
checkJsBuffer();
}

void ChatView::checkJsBuffer()
{
if (d->sessionReady_) {
while (!d->jsBuffer_.isEmpty()) {
emit d->jsObject->newMessage(d->jsBuffer_.takeFirst());
}
}
}

void ChatView::sessionInited()
{
qDebug("Session is initialized");
d->sessionReady_ = true;
checkJsBuffer();
d->checkJsBuffer();
}

bool ChatView::handleCopyEvent(QObject *object, QEvent *event, ChatEdit *chatEdit)
Expand Down Expand Up @@ -664,8 +679,6 @@ void ChatView::dispatchMessage(const MessageView &mv)
types.insert(MessageView::Reactions, "reactions");
}
QVariantMap m;
m["time"] = mv.dateTime();
m["type"] = types.value(mv.type());
switch (mv.type()) {
case MessageView::Message:
m["message"] = d->prepareShares(ChatViewPrivate::closeIconTags(mv.formattedText()));
Expand Down Expand Up @@ -724,38 +737,26 @@ void ChatView::dispatchMessage(const MessageView &mv)
m["urls"] = vmUrls;
break;
}
case MessageView::Reactions:
m["sender"] = mv.nick();
m["messageid"] = mv.reactionsId();
{
auto n = d->isMuc_ ? mv.nick() : QString::fromLatin1(mv.isLocal() ? "l" : "r");
auto rl = updateReactions(n, 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[QLatin1String("reactions")] = vl;
}
break;
case MessageView::Reactions: {
auto n = d->isMuc_ ? mv.nick() : QString::fromLatin1(mv.isLocal() ? "l" : "r");
d->sendReactionsToUI(n, mv.reactionsId(), mv.reactions());
return;
}
case MessageView::FileTransferRequest:
case MessageView::FileTransferFinished:
break;
}

m["time"] = mv.dateTime();
m["type"] = types.value(mv.type());
QString replaceId = mv.replaceId();
if (replaceId.isEmpty() && (mv.type() == MessageView::Message || mv.type() == MessageView::Subject)
&& updateLastMsgTime(mv.dateTime())) {
QVariantMap m;
m["date"] = mv.dateTime();
m["type"] = "message";
m["mtype"] = "lastDate";
sendJsObject(m);
d->sendJsObject(m);
}

m["encrypted"] = d->isEncryptionEnabled_;
Expand All @@ -766,15 +767,15 @@ void ChatView::dispatchMessage(const MessageView &mv)
m["mtype"] = m["type"];
m["type"] = "message";
}
sendJsObject(m);
d->sendJsObject(m);
}

void ChatView::sendJsCode(const QString &js)
{
QVariantMap m;
m["type"] = "js";
m["js"] = js;
sendJsObject(m);
d->sendJsObject(m);
}

void ChatView::scrollUp() { emit d->jsObject->scrollRequested(-50); }
Expand Down Expand Up @@ -813,22 +814,22 @@ void ChatView::updateAvatar(const Jid &jid, UserType utype)
m["type"] = "avatar";
m["sender"] = jid.resource();
m["avatar"] = ChatViewJSObject::avatarUrl(d->account_->avatarFactory()->userHashes(jid).avatar);
sendJsObject(m);
d->sendJsObject(m);
}
}

void ChatView::clear()
{
QVariantMap m;
m["type"] = "clear";
sendJsObject(m);
d->sendJsObject(m);
}

void ChatView::doTrackBar()
{
QVariantMap m;
m["type"] = "trackbar";
sendJsObject(m);
d->sendJsObject(m);
}

WebView *ChatView::textWidget() { return d->webView; }
Expand All @@ -842,6 +843,19 @@ void ChatView::outgoingReaction(const QString &messageId, const QString &reactio
auto n = d->isMuc_ ? d->localNickName : QString::fromLatin1("l");
auto reactions = onReactionSwitched(n, messageId, reaction);
emit outgoingReactions(messageId, reactions);
if (!d->isMuc_) {
// with private message we are not going to get it back. so back immediately
d->sendReactionsToUI(n, messageId, reactions);
}
}

void ChatViewPrivate::checkJsBuffer()
{
if (sessionReady_) {
while (!jsBuffer_.isEmpty()) {
emit jsObject->newMessage(jsBuffer_.takeFirst());
}
}
}

#include "chatview_webkit.moc"
2 changes: 0 additions & 2 deletions src/chatview_webkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class ChatView : public QFrame, public ChatViewCommon {
void contextMenuEvent(QContextMenuEvent *event);
bool handleCopyEvent(QObject *object, QEvent *event, ChatEdit *chatEdit);

void sendJsObject(const QVariantMap &);
void dispatchMessage(const MessageView &m);
void sendJsCode(const QString &js);

Expand Down Expand Up @@ -92,7 +91,6 @@ public slots:
void init();

private slots:
void checkJsBuffer();
void sessionInited();

signals:
Expand Down
10 changes: 8 additions & 2 deletions src/chatviewcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class QWidget;
class ChatViewCommon {
public:
enum UserType { LocalParty, RemoteParty, Participant };
enum class Feature { Reactions = 0x1 };
Q_DECLARE_FLAGS(Features, Feature)

ChatViewCommon() : _nickNumber(0) { }
void setLooks(QWidget *);
Expand Down Expand Up @@ -62,12 +64,16 @@ class ChatViewCommon {
// to be called from UI stuff. return list of reactions to send over network
QSet<QString> onReactionSwitched(const QString &senderNickname, const QString &messageId, const QString &reaction);

Features features();

protected:
QDateTime _lastMsgTime;
Features _featuers;

private:
QList<QColor> &generatePalette();
bool compatibleColors(const QColor &, const QColor &);
QList<QColor> &generatePalette();
bool compatibleColors(const QColor &, const QColor &);

int _nickNumber;
QMap<QString, int> _nicks;
QHash<QString, Reactions> _reactions; // messageId -> reactions
Expand Down
2 changes: 1 addition & 1 deletion themes/chatview/psi/bubble/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
dateFormat : "HH:mm",
proxy : function() { //optional
if (shared.cdata.mtype == "reactions") {
if (shared.cdata.type == "reactions") {
renderReactions(shared.cdata);
return false;
}
Expand Down

0 comments on commit 3d64d67

Please sign in to comment.