nheko/src/ChatPage.h

245 lines
8.5 KiB
C
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-03-05 00:35:15 +01:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-04-06 01:06:42 +02:00
#pragma once
2017-04-06 01:06:42 +02:00
#include <atomic>
2019-12-14 17:08:36 +01:00
#include <optional>
#include <variant>
2019-12-05 15:31:53 +01:00
#include <mtx/common.hpp>
2020-10-27 17:45:28 +01:00
#include <mtx/events.hpp>
#include <mtx/events/encrypted.hpp>
#include <mtx/events/member.hpp>
2022-10-13 17:19:54 +02:00
#include <mtx/events/policy_rules.hpp>
2020-10-27 17:45:28 +01:00
#include <mtx/events/presence.hpp>
2020-12-18 03:04:18 +01:00
#include <mtx/secret_storage.hpp>
2017-10-28 14:46:39 +02:00
#include <QMap>
#include <QPoint>
2022-01-30 13:31:39 +01:00
#include <QSharedPointer>
2017-04-06 01:06:42 +02:00
#include <QTimer>
#include "CacheCryptoStructs.h"
#include "CacheStructs.h"
#include "ui/RoomSummary.h"
2017-10-28 14:46:39 +02:00
class TimelineViewManager;
class UserSettings;
class NotificationsManager;
2020-07-28 23:55:47 +02:00
class TimelineModel;
2020-10-27 17:45:28 +01:00
class CallManager;
2017-04-06 01:06:42 +02:00
constexpr int CONSENSUS_TIMEOUT = 1000;
constexpr int SHOW_CONTENT_TIMEOUT = 3000;
constexpr int TYPING_REFRESH_TIMEOUT = 10000;
2020-10-27 17:45:28 +01:00
namespace mtx::requests {
struct CreateRoom;
}
namespace mtx::responses {
struct Notifications;
struct Sync;
struct Timeline;
struct Rooms;
}
2020-12-18 03:04:18 +01:00
using SecretsToDecrypt = std::map<std::string, mtx::secret_storage::AesHmacSha2EncryptedData>;
2022-10-10 14:38:29 +02:00
class ChatPage final : public QObject
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
Q_OBJECT
2017-04-06 01:06:42 +02:00
public:
2022-01-12 19:09:46 +01:00
ChatPage(QSharedPointer<UserSettings> userSettings, QObject *parent = nullptr);
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
// Initialize all the components of the UI.
void bootstrap(QString userid, QString homeserver, QString token);
2018-01-03 17:05:49 +01:00
2021-09-18 00:22:33 +02:00
static ChatPage *instance() { return instance_; }
2021-09-18 00:22:33 +02:00
QSharedPointer<UserSettings> userSettings() { return userSettings_; }
CallManager *callManager() { return callManager_; }
TimelineViewManager *timelineManager() { return view_manager_; }
void deleteConfigs();
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
void initiateLogout();
2021-09-18 00:22:33 +02:00
QString status() const;
void setStatus(const QString &status);
2021-09-18 00:22:33 +02:00
mtx::presence::PresenceState currentPresence() const;
void startChat(QString userid, std::optional<bool> encryptionEnabled);
2021-05-28 23:25:57 +02:00
//! Check if the given room is currently open.
bool isRoomActive(const QString &room_id);
const std::unique_ptr<mtx::pushrules::PushRuleEvaluator> &pushruleEvaluator() const
{
return pushrules;
}
public slots:
bool handleMatrixUri(QString uri);
2021-09-18 00:22:33 +02:00
bool handleMatrixUri(const QUrl &uri);
void startChat(QString userid) { startChat(userid, std::nullopt); }
2022-03-30 23:38:38 +02:00
void leaveRoom(const QString &room_id, const QString &reason);
2021-09-18 00:22:33 +02:00
void createRoom(const mtx::requests::CreateRoom &req);
2022-03-30 23:38:38 +02:00
void joinRoom(const QString &room, const QString &reason = "");
void knockRoom(const QString &room, QString reason = "") { knockRoom(room, {}, reason, false); }
void knockRoom(const QString &room,
const std::vector<std::string> &via,
QString reason = "",
bool failedJoin = false,
bool promptForConfirmation = true);
2021-09-18 00:22:33 +02:00
void joinRoomVia(const std::string &room_id,
const std::vector<std::string> &via,
2022-03-30 23:38:38 +02:00
bool promptForConfirmation = true,
const QString &reason = "");
2021-09-18 00:22:33 +02:00
2022-05-07 18:53:16 +02: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 00:22:33 +02:00
void receivedSessionKey(const std::string &room_id, const std::string &session_id);
void decryptDownloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescription keyDesc,
const SecretsToDecrypt &secrets);
void sendNotificationReply(const QString &roomid, const QString &eventid, const QString &body);
signals:
2021-09-18 00:22:33 +02: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 01:42:21 +02:00
void startRemoveFallbackKeyTimer();
2021-09-18 00:22:33 +02:00
void initializeViews(const mtx::responses::Sync &rooms);
2021-09-18 00:22:33 +02:00
void initializeEmptyViews();
void syncUI(const mtx::responses::Sync &sync);
2021-09-18 00:22:33 +02: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 03:04:18 +01:00
void showRoomJoinPrompt(RoomSummary *);
void internalKnock(const QString &room,
const std::vector<std::string> &via,
QString reason = "",
bool failedJoin = false,
bool promptForConfirmation = true);
private slots:
2021-09-18 00:22:33 +02:00
void logout();
void removeRoom(const QString &room_id);
void changeRoom(const QString &room_id);
void dropToLoginPage(const QString &msg);
2021-09-18 00:22:33 +02:00
void handleSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token);
2017-04-06 01:06:42 +02:00
private:
2021-09-18 00:22:33 +02:00
static ChatPage *instance_;
2018-01-03 17:05:49 +01:00
2021-09-18 00:22:33 +02:00
void startInitialSync();
void tryInitialSync();
void trySync();
void verifyOneTimeKeyCountAfterStartup();
2022-05-14 01:42:21 +02: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 00:22:33 +02:00
void getProfileInfo();
void getBackupVersion();
2021-09-18 00:22:33 +02:00
using UserID = QString;
using Membership = mtx::events::StateEvent<mtx::events::state::Member>;
using Memberships = std::map<std::string, Membership>;
2021-09-18 00:22:33 +02:00
void loadStateFromCache();
void resetUI();
2021-09-18 00:22:33 +02:00
template<class Collection>
Memberships getMemberships(const std::vector<Collection> &events) const;
2021-09-18 00:22:33 +02:00
template<typename T>
void connectCallMessage();
2020-07-11 01:19:48 +02:00
2021-09-18 00:22:33 +02:00
TimelineViewManager *view_manager_;
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
QTimer connectivityTimer_;
std::atomic_bool isConnected_;
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
// Global user settings.
QSharedPointer<UserSettings> userSettings_;
2022-06-18 01:35:30 +02:00
NotificationsManager *notificationsManager;
2021-09-18 00:22:33 +02:00
CallManager *callManager_;
2022-10-13 17:19:54 +02:00
std::unique_ptr<mtx::pushrules::PushRuleEvaluator> pushrules;
2017-04-06 01:06:42 +02: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 00:22:33 +02:00
std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>> memberships;
2021-09-18 00:22:33 +02:00
using Member = mtx::events::StateEvent<mtx::events::state::Member>;
2021-09-18 00:22:33 +02:00
for (const auto &event : collection) {
if (auto member = std::get_if<Member>(event)) {
memberships.emplace(member->state_key, *member);
}
2021-09-18 00:22:33 +02:00
}
2021-09-18 00:22:33 +02:00
return memberships;
}