Skip to content

Commit

Permalink
Forwarding reactions to network
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Jun 27, 2024
1 parent c43eff3 commit 847c253
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/chatdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void ChatDlg::init()
chatView()->setDialog(this);
bool isPrivate = account()->groupchats().contains(jid().bare());
chatView()->setSessionData(false, isPrivate, jid(), jid().full()); // FIXME fix nick updating
chatView()->setLocalNickname(account()->nick());
#ifdef WEBKIT
chatView()->setAccount(account());
#else
Expand Down
4 changes: 4 additions & 0 deletions src/chatview_te.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ void ChatView::setSessionData(bool isMuc, bool isMucPrivate, const XMPP::Jid &ji
name_ = name;
}

void ChatView::setLocalNickname(const QString &nickname) {
localNickname_ = nickname;
}

void ChatView::clear()
{
PsiTextView::clear();
Expand Down
3 changes: 3 additions & 0 deletions src/chatview_te.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ChatView : public PsiTextView, public ChatViewCommon {
void init();
void setDialog(QWidget *dialog);
void setSessionData(bool isMuc, bool isMucPrivate, const XMPP::Jid &jid, const QString name);
void setLocalNickname(const QString &nickname);

void insertText(const QString &text, QTextCursor &insertCursor);
void appendText(const QString &text);
Expand Down Expand Up @@ -100,6 +101,7 @@ private slots:
void showNM(const QString &);
void quote(const QString &text);
void nickInsertClick(const QString &nick);
void outgoingReactions(const QString &messageId, const QSet<QString> &reactions);

private:
bool isMuc_;
Expand All @@ -109,6 +111,7 @@ private slots:
int oldTrackBarPosition;
XMPP::Jid jid_;
QString name_;
QString localNickname_;
QPointer<QWidget> dialog_;
QAction *actQuote_;
};
Expand Down
26 changes: 21 additions & 5 deletions src/chatview_webkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class ChatViewThemeSessionBridge;

class ChatViewPrivate {
public:
ChatViewPrivate() = default;
ChatViewPrivate(ChatView *q) : q(q) { }

ChatView *q;

Theme theme;

Expand All @@ -90,6 +92,7 @@ class ChatViewPrivate {
AvatarFactory::UserHashes remoteIcons;
AvatarFactory::UserHashes localIcons;
ChatViewThemeProvider *themeProvider = nullptr;
QString localNickName;

static QString closeIconTags(const QString &richText)
{
Expand Down Expand Up @@ -300,8 +303,11 @@ class ChatViewJSObject : public ChatViewThemeSession {

Q_INVOKABLE void react(const QString &messageId, const QString &reaction)
{
//_view->updateReactions()
qDebug() << "react" << messageId << reaction;
if (messageId.isEmpty()) {
qWarning("empty message id on sending reaction. broken theme?");
return;
}
_view->outgoingReaction(messageId, reaction);
}

Q_INVOKABLE void deleteMessage(const QString &messageId) { qDebug() << "deleteMessage" << messageId; }
Expand Down Expand Up @@ -438,7 +444,7 @@ class ChatViewPage : public QWebPage {
//----------------------------------------------------------------------------
// ChatView
//----------------------------------------------------------------------------
ChatView::ChatView(QWidget *parent) : QFrame(parent), d(new ChatViewPrivate)
ChatView::ChatView(QWidget *parent) : QFrame(parent), d(new ChatViewPrivate(this))
{
d->jsObject = new ChatViewJSObject(this); /* It's a session bridge between html and c++ part */
d->webView = new WebView(this);
Expand Down Expand Up @@ -554,6 +560,8 @@ void ChatView::setAccount(PsiAccount *acc)
connect(d->themeProvider, SIGNAL(themeChanged()), SLOT(init()));
}

void ChatView::setLocalNickname(const QString &nickname) { d->localNickName = nickname; }

void ChatView::contextMenuEvent(QContextMenuEvent *e)
{
QUrl linkUrl;
Expand Down Expand Up @@ -715,7 +723,8 @@ void ChatView::dispatchMessage(const MessageView &mv)
m["sender"] = mv.nick();
m["messageid"] = mv.reactionsId();
{
auto rl = updateReactions(mv.nick(), mv.reactionsId(), mv.reactions());
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();
Expand Down Expand Up @@ -823,4 +832,11 @@ QWidget *ChatView::realTextWidget() { return d->webView; }

QObject *ChatView::jsBridge() { return d->jsObject; }

void ChatView::outgoingReaction(const QString &messageId, const QString &reaction)
{
auto n = d->isMuc_ ? d->localNickName : QString::fromLatin1("l");
auto reactions = onReactionSwitched(n, messageId, reaction);
emit outgoingReactions(messageId, reactions);
}

#include "chatview_webkit.moc"
5 changes: 5 additions & 0 deletions src/chatview_webkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ChatView : public QFrame, public ChatViewCommon {
void setDialog(QWidget *dialog);
void setSessionData(bool isMuc, bool isMucPrivate, const XMPP::Jid &jid, const QString name);
void setAccount(PsiAccount *acc);
void setLocalNickname(const QString &nickname);

void contextMenuEvent(QContextMenuEvent *event);
bool handleCopyEvent(QObject *object, QEvent *event, ChatEdit *chatEdit);
Expand Down Expand Up @@ -80,6 +81,9 @@ public slots:
void changeEvent(QEvent *event);
// void keyPressEvent(QKeyEvent *);

private:
void outgoingReaction(const QString &messageId, const QString &reaction);

protected slots:
void psiOptionChanged(const QString &);
// void autoCopy();
Expand All @@ -95,6 +99,7 @@ private slots:
void showNM(const QString &);
void nickInsertClick(const QString &nick);
void quote(const QString &text);
void outgoingReactions(const QString &messageId, const QSet<QString> &reactions);

private:
friend class ChatViewPrivate;
Expand Down
20 changes: 20 additions & 0 deletions src/chatviewcommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,23 @@ ChatViewCommon::updateReactions(const QString &senderNickname, const QString &me
}
return ret;
}

QSet<QString> ChatViewCommon::onReactionSwitched(const QString &senderNickname, const QString &messageId,
const QString &reaction)
{
auto msgIt = _reactions.find(messageId);
if (msgIt == _reactions.end()) {
return { { reaction } };
}
auto userIt = msgIt->perUser.find(senderNickname);
if (userIt == msgIt->perUser.end()) {
return { { reaction } };
}
auto ret = *userIt;
if (ret.contains(reaction)) {
ret.remove(reaction);
} else {
ret.insert(reaction);
}
return ret;
}
5 changes: 5 additions & 0 deletions src/chatviewcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ChatViewCommon {
QList<QColor> getPalette();

protected:
// a cache of reactions per message
struct Reactions {
QMap<QString, QStringList> total; // unicode reaction => nicknames
QHash<QString, QSet<QString>> perUser; // nickname => unicode reactions
Expand All @@ -54,9 +55,13 @@ class ChatViewCommon {
void removeUser(const QString &nickname);
void renameUser(const QString &oldNickname, const QString &newNickname);

// takes incoming reactions and returns reactions to be send to UI
QList<ReactionsItem> updateReactions(const QString &senderNickname, const QString &messageId,
const QSet<QString> &reactions);

// 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);

protected:
QDateTime _lastMsgTime;

Expand Down
20 changes: 20 additions & 0 deletions src/groupchatdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ public slots:
if (!nick.isEmpty()) {
prev_self = self;
self = nick;
dlg->ui_.log->setLocalNickname(self);
dlg->account()->groupChatChangeNick(dlg->jid().domain(), dlg->jid().node(), self,
dlg->account()->status());
}
Expand Down Expand Up @@ -404,6 +405,7 @@ join <channel>{,<channel>} [pass{,<pass>}
// FIXME nick can't be empty....
prev_self = self;
self = nick;
dlg->ui_.log->setLocalNickname(self);
dlg->account()->groupChatChangeNick(dlg->jid().domain(), dlg->jid().node(), self,
dlg->account()->status());
newstate = nullptr;
Expand Down Expand Up @@ -675,6 +677,19 @@ void GCMainDlg::onNickInsertClick(const QString &nick)
ui_.mle->chatEdit()->setFocus();
}

void GCMainDlg::outgoingReactions(const QString &messageId, const QSet<QString> &reactions)
{
Message m(jid());
m.setType(Message::Type::Groupchat);
m.setReactions({ messageId, reactions });
QString id = account()->client()->genUniqueId();
m.setId(id); // we need id early for message manipulations in chatview
m.setTimeStamp(QDateTime::currentDateTime());
// d->mle()->appendMessageHistory(m.body());

emit aSend(m);
}

void GCMainDlg::doContactContextMenu(const QString &nick)
{
auto itm = d->usersModel->findEntry(nick);
Expand Down Expand Up @@ -865,6 +880,7 @@ GCMainDlg::GCMainDlg(PsiAccount *pa, const Jid &j, TabManager *tabManager) : Tab
d->tabmode = PsiOptions::instance()->getOption("options.ui.tabs.use-tabs").toBool();

ui_.log->setSessionData(true, false, jid(), jid().full()); // FIXME change conference name
ui_.log->setLocalNickname(d->self);
#ifdef WEBKIT
ui_.log->setAccount(account());
#else
Expand All @@ -874,6 +890,7 @@ GCMainDlg::GCMainDlg(PsiAccount *pa, const Jid &j, TabManager *tabManager) : Tab
connect(ui_.log, SIGNAL(showNM(QString)), this, SLOT(doContactContextMenu(QString)));
connect(URLObject::getInstance(), SIGNAL(openURL(QString)), SLOT(openURL(QString)));
connect(ui_.log, SIGNAL(nickInsertClick(QString)), SLOT(onNickInsertClick(QString)));
connect(ui_.log, &ChatView::outgoingReactions, this, &GCMainDlg::outgoingReactions);

PsiToolTip::install(ui_.le_topic);

Expand Down Expand Up @@ -1438,6 +1455,7 @@ void GCMainDlg::mle_returnPressed()
if (!nick.isEmpty() && newJid.isValid()) {
d->prev_self = d->self;
d->self = newJid.resource();
ui_.log->setLocalNickname(d->self);
account()->groupChatChangeNick(jid().domain(), jid().node(), d->self, account()->status());
}
ui_.mle->chatEdit()->setText("");
Expand Down Expand Up @@ -1697,6 +1715,7 @@ void GCMainDlg::setJid(const Jid &j)
{
TabbableWidget::setJid(j);
d->self = d->prev_self = j.resource();
ui_.log->setLocalNickname(d->self);
}

void GCMainDlg::goConn()
Expand Down Expand Up @@ -1823,6 +1842,7 @@ void GCMainDlg::presence(const QString &nick, const Status &s)
if (s.errorCode() == 409) {
message = tr("Please choose a different nickname");
d->self = d->prev_self;
ui_.log->setLocalNickname(d->self);
} else {
message = tr("An error occurred (errorcode: %1)").arg(s.errorCode());
}
Expand Down
1 change: 1 addition & 0 deletions src/groupchatdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private slots:
void avatarUpdated(const Jid &jid);
void doContactContextMenu(const QString &nick);

void outgoingReactions(const QString &messageId, const QSet<QString> &reactions);
public:
class Private;
friend class Private;
Expand Down

0 comments on commit 847c253

Please sign in to comment.