2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-16 21:46:45 +03:00
|
|
|
#pragma once
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
#include <atomic>
|
2019-12-14 19:08:36 +03:00
|
|
|
#include <optional>
|
|
|
|
#include <variant>
|
|
|
|
|
2019-12-05 17:31:53 +03:00
|
|
|
#include <mtx/common.hpp>
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <mtx/events.hpp>
|
|
|
|
#include <mtx/events/encrypted.hpp>
|
|
|
|
#include <mtx/events/member.hpp>
|
2022-10-13 18:19:54 +03:00
|
|
|
#include <mtx/events/policy_rules.hpp>
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <mtx/events/presence.hpp>
|
2020-12-18 05:04:18 +03:00
|
|
|
#include <mtx/secret_storage.hpp>
|
2018-06-09 16:03:14 +03:00
|
|
|
|
2017-10-28 15:46:39 +03:00
|
|
|
#include <QMap>
|
2019-07-22 04:58:11 +03:00
|
|
|
#include <QPoint>
|
2022-01-30 15:31:39 +03:00
|
|
|
#include <QSharedPointer>
|
2017-04-06 02:06:42 +03:00
|
|
|
#include <QTimer>
|
|
|
|
|
2022-08-05 22:44:40 +03:00
|
|
|
#include "ui/RoomSummary.h"
|
2017-10-28 20:46:34 +03:00
|
|
|
|
2017-10-28 15:46:39 +03:00
|
|
|
class TimelineViewManager;
|
2017-12-30 18:29:57 +03:00
|
|
|
class UserSettings;
|
2018-07-11 17:33:02 +03:00
|
|
|
class NotificationsManager;
|
2020-07-29 00:55:47 +03:00
|
|
|
class TimelineModel;
|
2020-10-27 19:45:28 +03:00
|
|
|
class CallManager;
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2020-10-27 19:45:28 +03:00
|
|
|
namespace mtx::requests {
|
|
|
|
struct CreateRoom;
|
|
|
|
}
|
|
|
|
namespace mtx::responses {
|
|
|
|
struct Notifications;
|
|
|
|
struct Sync;
|
|
|
|
struct Timeline;
|
|
|
|
struct Rooms;
|
2019-12-15 05:19:33 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 05:04:18 +03:00
|
|
|
using SecretsToDecrypt = std::map<std::string, mtx::secret_storage::AesHmacSha2EncryptedData>;
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class ChatPage final : public QObject
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
2017-04-06 02:06:42 +03:00
|
|
|
|
|
|
|
public:
|
2022-01-12 21:09:46 +03:00
|
|
|
ChatPage(QSharedPointer<UserSettings> userSettings, QObject *parent = nullptr);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Initialize all the components of the UI.
|
|
|
|
void bootstrap(QString userid, QString homeserver, QString token);
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
static ChatPage *instance() { return instance_; }
|
2018-03-11 18:56:40 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QSharedPointer<UserSettings> userSettings() { return userSettings_; }
|
|
|
|
CallManager *callManager() { return callManager_; }
|
|
|
|
TimelineViewManager *timelineManager() { return view_manager_; }
|
|
|
|
void deleteConfigs();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
void initiateLogout();
|
2018-07-04 00:05:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QString status() const;
|
|
|
|
void setStatus(const QString &status);
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::presence::PresenceState currentPresence() const;
|
2020-06-08 02:45:24 +03:00
|
|
|
|
2022-03-29 05:50:25 +03:00
|
|
|
void startChat(QString userid, std::optional<bool> encryptionEnabled);
|
2021-05-29 00:25:57 +03:00
|
|
|
|
2022-09-12 02:05:20 +03:00
|
|
|
//! Check if the given room is currently open.
|
|
|
|
bool isRoomActive(const QString &room_id);
|
|
|
|
|
2022-11-02 01:26:21 +03:00
|
|
|
const std::unique_ptr<mtx::pushrules::PushRuleEvaluator> &pushruleEvaluator() const
|
|
|
|
{
|
|
|
|
return pushrules;
|
|
|
|
}
|
|
|
|
|
2023-01-29 08:46:00 +03:00
|
|
|
void removeAllNotifications();
|
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
public slots:
|
2021-12-17 07:53:34 +03:00
|
|
|
bool handleMatrixUri(QString uri);
|
2021-09-18 01:22:33 +03:00
|
|
|
bool handleMatrixUri(const QUrl &uri);
|
|
|
|
|
2022-03-29 05:50:25 +03:00
|
|
|
void startChat(QString userid) { startChat(userid, std::nullopt); }
|
2022-03-31 00:38:38 +03:00
|
|
|
void leaveRoom(const QString &room_id, const QString &reason);
|
2021-09-18 01:22:33 +03:00
|
|
|
void createRoom(const mtx::requests::CreateRoom &req);
|
2022-03-31 00:38:38 +03:00
|
|
|
void joinRoom(const QString &room, const QString &reason = "");
|
2022-04-01 01:58:01 +03:00
|
|
|
void knockRoom(const QString &room, QString reason = "") { knockRoom(room, {}, reason, false); }
|
|
|
|
void knockRoom(const QString &room,
|
|
|
|
const std::vector<std::string> &via,
|
2022-08-05 22:44:40 +03:00
|
|
|
QString reason = "",
|
|
|
|
bool failedJoin = false,
|
|
|
|
bool promptForConfirmation = true);
|
2021-09-18 01:22:33 +03:00
|
|
|
void joinRoomVia(const std::string &room_id,
|
|
|
|
const std::vector<std::string> &via,
|
2022-03-31 00:38:38 +03:00
|
|
|
bool promptForConfirmation = true,
|
|
|
|
const QString &reason = "");
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-05-07 19:53:16 +03:00
|
|
|
void inviteUser(const QString &room, QString userid, QString reason);
|
|
|
|
void kickUser(const QString &room, QString userid, QString reason);
|
|
|
|
void banUser(const QString &room, QString userid, QString reason);
|
|
|
|
void unbanUser(const QString &room, QString userid, QString reason);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
void receivedSessionKey(const std::string &room_id, const std::string &session_id);
|
|
|
|
void decryptDownloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescription keyDesc,
|
|
|
|
const SecretsToDecrypt &secrets);
|
2022-11-04 19:42:09 +03:00
|
|
|
void sendNotificationReply(const QString &roomid, const QString &eventid, const QString &body);
|
2017-04-09 02:17:04 +03:00
|
|
|
signals:
|
2021-09-18 01:22:33 +03:00
|
|
|
void connectionLost();
|
|
|
|
void connectionRestored();
|
|
|
|
|
|
|
|
void contentLoaded();
|
|
|
|
void closing();
|
|
|
|
void changeWindowTitle(const int);
|
|
|
|
void unreadMessages(int count);
|
|
|
|
void showNotification(const QString &msg);
|
|
|
|
void showLoginPage(const QString &msg);
|
|
|
|
void showUserSettingsPage();
|
|
|
|
|
|
|
|
void ownProfileOk();
|
|
|
|
void setUserDisplayName(const QString &name);
|
|
|
|
void setUserAvatar(const QString &avatar);
|
|
|
|
void loggedOut();
|
|
|
|
|
|
|
|
void trySyncCb();
|
|
|
|
void tryDelayedSyncCb();
|
|
|
|
void tryInitialSyncCb();
|
|
|
|
void newSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token);
|
|
|
|
void leftRoom(const QString &room_id);
|
|
|
|
void newRoom(const QString &room_id);
|
|
|
|
void changeToRoom(const QString &room_id);
|
2022-05-14 02:42:21 +03:00
|
|
|
void startRemoveFallbackKeyTimer();
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2021-11-21 00:48:04 +03:00
|
|
|
void initializeViews(const mtx::responses::Sync &rooms);
|
2021-09-18 01:22:33 +03:00
|
|
|
void initializeEmptyViews();
|
2021-11-21 00:48:04 +03:00
|
|
|
void syncUI(const mtx::responses::Sync &sync);
|
2021-09-18 01:22:33 +03:00
|
|
|
void dropToLoginPageCb(const QString &msg);
|
|
|
|
|
|
|
|
void notifyMessage(const QString &roomid,
|
|
|
|
const QString &eventid,
|
|
|
|
const QString &roomname,
|
|
|
|
const QString &sender,
|
|
|
|
const QString &message,
|
|
|
|
const QImage &icon);
|
|
|
|
|
|
|
|
void retrievedPresence(const QString &statusMsg, mtx::presence::PresenceState state);
|
|
|
|
void themeChanged();
|
|
|
|
void decryptSidebarChanged();
|
|
|
|
|
|
|
|
//! Signals for device verificaiton
|
|
|
|
void receivedDeviceVerificationAccept(const mtx::events::msg::KeyVerificationAccept &message);
|
|
|
|
void receivedDeviceVerificationRequest(const mtx::events::msg::KeyVerificationRequest &message,
|
|
|
|
std::string sender);
|
|
|
|
void receivedRoomDeviceVerificationRequest(
|
|
|
|
const mtx::events::RoomEvent<mtx::events::msg::KeyVerificationRequest> &message,
|
|
|
|
TimelineModel *model);
|
|
|
|
void receivedDeviceVerificationCancel(const mtx::events::msg::KeyVerificationCancel &message);
|
|
|
|
void receivedDeviceVerificationKey(const mtx::events::msg::KeyVerificationKey &message);
|
|
|
|
void receivedDeviceVerificationMac(const mtx::events::msg::KeyVerificationMac &message);
|
|
|
|
void receivedDeviceVerificationStart(const mtx::events::msg::KeyVerificationStart &message,
|
|
|
|
std::string sender);
|
|
|
|
void receivedDeviceVerificationReady(const mtx::events::msg::KeyVerificationReady &message);
|
|
|
|
void receivedDeviceVerificationDone(const mtx::events::msg::KeyVerificationDone &message);
|
|
|
|
|
|
|
|
void downloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescription keyDesc,
|
|
|
|
const SecretsToDecrypt &secrets);
|
2020-12-18 05:04:18 +03:00
|
|
|
|
2022-08-05 22:44:40 +03:00
|
|
|
void showRoomJoinPrompt(RoomSummary *);
|
2022-04-01 01:58:01 +03:00
|
|
|
void internalKnock(const QString &room,
|
|
|
|
const std::vector<std::string> &via,
|
2022-08-05 22:44:40 +03:00
|
|
|
QString reason = "",
|
|
|
|
bool failedJoin = false,
|
|
|
|
bool promptForConfirmation = true);
|
2023-02-18 04:59:33 +03:00
|
|
|
void newOnlineKeyBackupAvailable();
|
2022-04-01 01:58:01 +03:00
|
|
|
|
2023-03-14 05:02:54 +03:00
|
|
|
void callFunctionOnGuiThread(std::function<void()>);
|
|
|
|
|
2017-04-11 17:45:47 +03:00
|
|
|
private slots:
|
2021-09-18 01:22:33 +03:00
|
|
|
void logout();
|
|
|
|
void removeRoom(const QString &room_id);
|
|
|
|
void changeRoom(const QString &room_id);
|
|
|
|
void dropToLoginPage(const QString &msg);
|
2018-06-09 16:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
void handleSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
|
|
|
private:
|
2021-09-18 01:22:33 +03:00
|
|
|
static ChatPage *instance_;
|
2018-01-03 19:05:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
void startInitialSync();
|
|
|
|
void tryInitialSync();
|
|
|
|
void trySync();
|
|
|
|
void verifyOneTimeKeyCountAfterStartup();
|
2022-05-14 02:42:21 +03:00
|
|
|
void ensureOneTimeKeyCount(const std::map<std::string, uint16_t> &counts,
|
|
|
|
const std::optional<std::vector<std::string>> &fallback_keys);
|
|
|
|
void removeOldFallbackKey();
|
2021-09-18 01:22:33 +03:00
|
|
|
void getProfileInfo();
|
|
|
|
void getBackupVersion();
|
2018-06-09 16:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
using UserID = QString;
|
|
|
|
using Membership = mtx::events::StateEvent<mtx::events::state::Member>;
|
|
|
|
using Memberships = std::map<std::string, Membership>;
|
2017-12-04 19:41:19 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
void loadStateFromCache();
|
|
|
|
void resetUI();
|
2017-05-07 17:15:38 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
template<class Collection>
|
|
|
|
Memberships getMemberships(const std::vector<Collection> &events) const;
|
2017-12-04 19:41:19 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
template<typename T>
|
|
|
|
void connectCallMessage();
|
2020-07-11 02:19:48 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
TimelineViewManager *view_manager_;
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QTimer connectivityTimer_;
|
|
|
|
std::atomic_bool isConnected_;
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Global user settings.
|
|
|
|
QSharedPointer<UserSettings> userSettings_;
|
2018-07-11 17:33:02 +03:00
|
|
|
|
2022-06-18 02:35:30 +03:00
|
|
|
NotificationsManager *notificationsManager;
|
2021-09-18 01:22:33 +03:00
|
|
|
CallManager *callManager_;
|
2022-10-13 18:19:54 +03:00
|
|
|
|
|
|
|
std::unique_ptr<mtx::pushrules::PushRuleEvaluator> pushrules;
|
2023-03-14 05:02:54 +03:00
|
|
|
|
|
|
|
QDateTime lastSpacesUpdate = QDateTime::currentDateTime();
|
2023-10-26 00:22:39 +03:00
|
|
|
|
|
|
|
// Stores when our windows lost focus. Invalid when our windows have focus.
|
|
|
|
QDateTime lastWindowActive;
|
2017-04-06 02:06:42 +03:00
|
|
|
};
|
2017-12-04 19:41:19 +03:00
|
|
|
|
|
|
|
template<class Collection>
|
|
|
|
std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>>
|
|
|
|
ChatPage::getMemberships(const std::vector<Collection> &collection) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>> memberships;
|
2017-12-04 19:41:19 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
using Member = mtx::events::StateEvent<mtx::events::state::Member>;
|
2017-12-04 19:41:19 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &event : collection) {
|
|
|
|
if (auto member = std::get_if<Member>(event)) {
|
|
|
|
memberships.emplace(member->state_key, *member);
|
2017-12-04 19:41:19 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2017-12-04 19:41:19 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return memberships;
|
2017-12-04 19:41:19 +03:00
|
|
|
}
|