diff --git a/neochat.notifyrc b/neochat.notifyrc index 6d21fd334..f199f7aff 100644 --- a/neochat.notifyrc +++ b/neochat.notifyrc @@ -122,3 +122,8 @@ Comment[uk]=Надійшло нове повідомлення Comment[x-test]=xxThere is a new messagexx Comment[zh_CN]=有新消息 Action=Popup + +[Event/invite] +Name=New Invite +Comment=There is a new invite to a room +Action=Popup diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 862dc0b51..88674a89a 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -58,6 +58,21 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS } }); connect(this, &Room::displaynameChanged, this, &NeoChatRoom::displayNameChanged); + + connectSingleShot(this, &Room::baseStateLoaded, this, [this]() { + if (this->joinState() != JoinState::Invite) { + return; + } + const QString senderId = getCurrentState(localUser()->id())->senderId(); + QImage avatar_image; + if (!user(senderId)->avatarUrl(this).isEmpty()) { + avatar_image = user(senderId)->avatar(128, this); + } else { + qWarning() << "using this room's avatar"; + avatar_image = avatar(128); + } + NotificationsManager::instance().postInviteNotification(this, htmlSafeDisplayName(), htmlSafeMemberName(senderId), avatar_image); + }); } void NeoChatRoom::uploadFile(const QUrl &url, const QString &body) diff --git a/src/notificationsmanager.cpp b/src/notificationsmanager.cpp index 9317a57d8..41e0b69ce 100644 --- a/src/notificationsmanager.cpp +++ b/src/notificationsmanager.cpp @@ -71,3 +71,30 @@ void NotificationsManager::postNotification(NeoChatRoom *room, m_notifications.insert(room->id(), notification); } + +void NotificationsManager::postInviteNotification(NeoChatRoom *room, const QString &title, const QString &sender, const QImage &icon) +{ + if (!NeoChatConfig::self()->showNotifications()) { + return; + } + QPixmap img; + img.convertFromImage(icon); + KNotification *notification = new KNotification("invite"); + notification->setText(i18n("%1 invited you to a room", sender)); + notification->setTitle(title); + notification->setPixmap(img); + notification->setDefaultAction(i18n("Open this invite in NeoChat")); + connect(notification, &KNotification::defaultActivated, this, [=]() { + RoomManager::instance().enterRoom(room); + Q_EMIT Controller::instance().showWindow(); + }); + notification->setActions({i18n("Accept Invite"), i18n("Reject Invite")}); + connect(notification, &KNotification::action1Activated, this, [room]() { + room->acceptInvitation(); + }); + connect(notification, &KNotification::action2Activated, this, [room]() { + RoomManager::instance().leaveRoom(room); + }); + notification->sendEvent(); + m_notifications.insert(room->id(), notification); +} diff --git a/src/notificationsmanager.h b/src/notificationsmanager.h index 42cbf1aa0..fde1d72b6 100644 --- a/src/notificationsmanager.h +++ b/src/notificationsmanager.h @@ -21,6 +21,7 @@ public: Q_INVOKABLE void postNotification(NeoChatRoom *room, const QString &roomName, const QString &sender, const QString &text, const QImage &icon, const QString &replyEventId); + void postInviteNotification(NeoChatRoom *room, const QString &title, const QString &sender, const QImage &icon); private: NotificationsManager(QObject *parent = nullptr);