2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 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>
|
|
|
|
#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>
|
|
|
|
|
2020-10-02 02:14:42 +03:00
|
|
|
#include "CacheCryptoStructs.h"
|
2019-12-15 01:39:02 +03:00
|
|
|
#include "CacheStructs.h"
|
2018-07-11 17:33:02 +03:00
|
|
|
#include "notifications/Manager.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
|
|
|
|
2017-10-31 21:11:49 +03:00
|
|
|
constexpr int CONSENSUS_TIMEOUT = 1000;
|
|
|
|
constexpr int SHOW_CONTENT_TIMEOUT = 3000;
|
|
|
|
constexpr int TYPING_REFRESH_TIMEOUT = 10000;
|
2017-10-08 21:35:37 +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-01-12 21:09:46 +03:00
|
|
|
class ChatPage : 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
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// TODO(Nico): Get rid of this!
|
|
|
|
QString currentRoom() const;
|
2021-05-29 00:25:57 +03:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
void startChat(QString userid);
|
|
|
|
void leaveRoom(const QString &room_id);
|
|
|
|
void createRoom(const mtx::requests::CreateRoom &req);
|
|
|
|
void joinRoom(const QString &room);
|
|
|
|
void joinRoomVia(const std::string &room_id,
|
|
|
|
const std::vector<std::string> &via,
|
|
|
|
bool promptForConfirmation = true);
|
|
|
|
|
|
|
|
void inviteUser(QString userid, QString reason);
|
|
|
|
void kickUser(QString userid, QString reason);
|
|
|
|
void banUser(QString userid, QString reason);
|
|
|
|
void unbanUser(QString userid, QString reason);
|
|
|
|
|
|
|
|
void receivedSessionKey(const std::string &room_id, const std::string &session_id);
|
|
|
|
void decryptDownloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescription keyDesc,
|
|
|
|
const SecretsToDecrypt &secrets);
|
2017-04-09 02:17:04 +03:00
|
|
|
signals:
|
2021-09-18 01:22:33 +03:00
|
|
|
void connectionLost();
|
|
|
|
void connectionRestored();
|
|
|
|
|
|
|
|
void notificationsRetrieved(const mtx::responses::Notifications &);
|
|
|
|
void highlightedNotifsRetrieved(const mtx::responses::Notifications &, const QPoint widgetPos);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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();
|
|
|
|
void initializeMentions(const QMap<QString, mtx::responses::Notifications> ¬ifs);
|
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
|
|
|
|
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();
|
|
|
|
void ensureOneTimeKeyCount(const std::map<std::string, uint16_t> &counts);
|
|
|
|
void getProfileInfo();
|
|
|
|
void getBackupVersion();
|
2018-06-09 16:03:14 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
//! Check if the given room is currently open.
|
|
|
|
bool isRoomActive(const QString &room_id);
|
2018-05-07 12:00:49 +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
|
|
|
//! Send desktop notification for the received messages.
|
|
|
|
void sendNotifications(const mtx::responses::Notifications &);
|
2018-02-08 20:07:58 +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
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
NotificationsManager notificationsManager;
|
|
|
|
CallManager *callManager_;
|
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
|
|
|
}
|