rocketchatserver.h 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/********************************************************************************************
 *                                                                                          *
 * Copyright (C) 2017 Armin Felder, Dennis Beier                                            *
 * This file is part of RocketChatMobileEngine <https://git.fairkom.net/chat/fairchat>.     *
 *                                                                                          *
 * RocketChatMobileEngine is free software: you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by                     *
 * the Free Software Foundation, either version 3 of the License, or                        *
 * (at your option) any later version.                                                      *
 *                                                                                          *
 * RocketChatMobileEngine is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                           *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            *
 * GNU General Public License for more details.                                             *
 *                                                                                          *
 * You should have received a copy of the GNU General Public License                        *
 * along with RocketChatMobileEngine. If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                                          *
 ********************************************************************************************/



armin's avatar
armin committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef ROCKETCHATSERVER_H
#define ROCKETCHATSERVER_H

#include <QObject>
#include <QVariantList>
#include <algorithm>    // std::transform
#include <QVariant>
#include <QList>
#include <QString>
#include <QMimeType>
#include <QCryptographicHash>
#include <QJsonArray>
#include "utils.h"
#include "persistancelayer.h"
#include "api/meteorddp.h"
#include "api/restapi.h"
#include "repos/userrepository.h"
#include "repos/channelrepository.h"
armin's avatar
armin committed
41
42
#include "repos/loginmethodsrepository.h"
#include "repos/entities/loginmethod.h"
armin's avatar
armin committed
43
44
45
46
47
48
49
50
#include "ddpRequests/pingrequest.h"
#include "ddpRequests/ddploginrequest.h"
#include "ddpRequests/rocketchatsubscribechannelrequest.h"
#include "ddpRequests/ddpufscreaterequest.h"
#include "ddpRequests/rocketchatsubscribeusernotify.h"
#include "ddpRequests/rocketchatnotifynoticesrequest.h"
#include "ddpRequests/rocketchatsubscriptionchangedrequest.h"
#include "ddpRequests/rocketchatmarkchannelasreadrequest.h"
armin's avatar
armin committed
51
#include "ddpRequests/rocketchatcreatepublicgrouprequest.h"
armin's avatar
armin committed
52
53
54
55
56
57
58
#include "ddpRequests/ddpunsubscriptionrequest.h"
#include "ddpRequests/rocketchatcreateprivategrouprequest.h"
#include "ddpRequests/rocketchatadduserstochannel.h"
#include "ddpRequests/rocketchatsubscriberoomrequest.h"
#include "ddpRequests/rocketchatupdatepushtokenrequest.h"
#include "ddpRequests/ddplogoutrequest.h"
#include "ddpRequests/rocketchatsubscribeactiveusers.h"
armin's avatar
armin committed
59
60
61
#include "ddpRequests/rocketchatsubscribeuserdata.h"
#include "ddpRequests/rocketchatchangeuserpresencedefaultstatus.h"
#include "ddpRequests/rocketchatchangeuserpresencestatus.h"
armin's avatar
armin committed
62
#include "ddpRequests/rocketchatupdatejitsitimeout.h"
armin's avatar
armin committed
63
#include "ddpRequests/rocketchatsubscribeloginmethods.h"
armin's avatar
armin committed
64
#include "ddpRequests/ddpopenidloginrequest.h"
armin's avatar
armin committed
65
#include "ddpRequests/ddpsamlloginrequest.h"
66
67
#include "ddpRequests/rocketchatblockuserrequest.h"
#include "ddpRequests/rocketchatunblockuserrequest.h"
armin's avatar
armin committed
68
#include "ddpRequests/rocketchatsubscriberoomschanged.h"
armin's avatar
armin committed
69
#include "ddpRequests/rocketchatmessagesearchrequest.h"
70
71
#include "ddpRequests/rocketchatgetusernamesuggestion.h"
#include "ddpRequests/rocketchatsetusername.h"
armin's avatar
armin committed
72
73
#include "restRequests/restrequest.h"
#include "restRequests/restlogoutrequest.h"
74
#include "restRequests/getserverinforequest.h"
armin's avatar
armin committed
75
76
77
78
79
80
81
#include "fileuploader.h"
#include "notifications/notifications.h"
#include "services/rocketchatchannelservice.h"
#include "services/messageservice.h"
#include "services/requests/loadhistoryservicerequest.h"
#include "services/fileservice.h"
#include "repos/emojirepo.h"
armin's avatar
as    
armin committed
82
#include "repos/filesrepo.h"
armin's avatar
armin committed
83
84
85
86
87
88
#include "rocketchatserverconfig.h"
#include "repos/channelrepository.h"
#include "services/emojiservice.h"
#include "restRequests/getrequest.h"
#include "segfaulthandler.h"
#include "CustomModels/usermodel.h"
armin's avatar
armin committed
89
#include "CustomModels/loginmethodsmodel.h"
armin's avatar
armin committed
90
#include "restRequests/restmerequest.h"
91
#include "ddpRequests/rocketchatcreateaccount.h"
armin's avatar
armin committed
92
93
#include "ddpRequests/rocketchatgetroombyid.h"
#include "ddpRequests/rocketchatgetroombynameandtype.h"
armin's avatar
ias    
armin committed
94
95
#include "ddpRequests/rocketchatgetroomidbynameorid.h"
#include "ddpRequests/rocketchatspotlightrequest.h"
armin's avatar
armin committed
96
97
98
99
100
101
102
103
104
105

class RocketChatChannelService;
class ChannelRepository;
class FileUploader;
class PersistanceLayer;
typedef QSharedPointer<RestRequest> RestApiRequest;
class MessageService;
class EmojiService;
class DdpLogoutRequest;
class FileService;
armin's avatar
armin committed
106
107
class EmojiRepo;
class ChannelModel;
armin's avatar
armin committed
108
109
110
111
112
113
114

enum class ConnectionState {
    OFFLINE,
    CONNECTED,
    ONLINE
};

115
class RocketChatServerData : public MessageListener
armin's avatar
armin committed
116
117
118
119
{

        Q_OBJECT
    public:
armin's avatar
armin committed
120
        RocketChatServerData( QString pId, QString pBaseUrl,
armin's avatar
armin committed
121
                              bool pUnsecure = false );
armin's avatar
armin committed
122

Armin Felder's avatar
Armin Felder committed
123
        ~RocketChatServerData();
armin's avatar
armin committed
124
125
        void setServerId( const QString &pValue );

armin's avatar
as    
armin committed
126
        void persistData();
armin's avatar
armin committed
127
    public slots:
armin's avatar
armin committed
128
129

        void init();
armin's avatar
ds    
armin committed
130
131
        void initDb();
        void initConnections();
armin's avatar
armin committed
132

armin's avatar
armin committed
133
        void createPublicGroup( const QString &pChannelName, const QStringList &pUsers, bool pReadonly );
armin's avatar
armin committed
134
        void createPrivateGroup( const QString &pChannelName, const QStringList &pUsers, bool pReadonly );
armin's avatar
armin committed
135
        void addUsersToChannel( const QString &pChannelId, const QStringList &pUsers );
armin's avatar
armin committed
136
        void sendMessage( const QString &pMessage, const QString &pChannelId );
armin's avatar
armin committed
137

armin's avatar
armin committed
138
139
        void uploadFile( const QString &pChannelId, const QString &pFile );
        void cancelUpload( const QString &pFileId );
armin's avatar
armin committed
140

armin's avatar
armin committed
141
142
143
144
145
146
        void login( const QString &pUsername, const QString &pPassword );
        void loginWithHash( const QString &pUsername, const QString &pHash );
        void loginWithToken( const QString &pUsername, const QString &pToken, bool pResume = false );
        void loginWithOpenIDToken( const QString &pToken, const QString &pSecret );
        void loginWithMethod( const QString &method, const QString &payload );
        void loginWtihSamlToken( const QString &pToken );
armin's avatar
armin committed
147

armin's avatar
armin committed
148
        void joinChannel( const QString &pChannelId, bool pForce = false );
armin's avatar
ias    
armin committed
149
        void joinChannelByNameAndType( const QString &pChannelName, const QString &pType );
armin's avatar
armin committed
150

armin's avatar
armin committed
151
        void loadRecentHistory( const QString &pChannelId );
armin's avatar
armin committed
152

armin's avatar
ias    
armin committed
153
        void loadHistoryTill( const QString &pChannelId, qint64 pTs );
armin's avatar
armin committed
154
155


armin's avatar
armin committed
156
157
        void getFileRessource( const QString &pUrl );
        void getFileRessource( const QString &pUrl, const QString &pType );
armin's avatar
armin committed
158

armin's avatar
armin committed
159
        void getLoginMethods();
armin's avatar
armin committed
160

161
162
        void getServerInfo();

armin's avatar
armin committed
163
164
165
166
167
168
        void logout( void );

        void setConnectionState( const ConnectionState &pValue );

        void resume( void );

armin's avatar
armin committed
169
        void joinJitsiCall( const QString &pRoomId );
armin's avatar
armin committed
170

armin's avatar
armin committed
171
        void setUnreadMessages( const QString &pChannelId, int count );
armin's avatar
armin committed
172

armin's avatar
armin committed
173
        void openPrivateChannelWith( const QString &pUsername );
armin's avatar
armin committed
174

armin's avatar
armin committed
175
        void setCurrentChannel( const QString &value );
armin's avatar
armin committed
176

armin's avatar
armin committed
177
        void markChannelAsRead( const QString &pChannelId );
armin's avatar
armin committed
178

armin's avatar
armin committed
179
        void sendPushToken( const QString &pToken );
armin's avatar
armin committed
180

armin's avatar
armin committed
181
        void switchChannel( const QString &pServer, const QString &pRid, const QString &pName, const QString &pType );
armin's avatar
armin committed
182

armin's avatar
ias    
armin committed
183
        void switchChannelByName( const QString &pRid, const QString &pType );
armin's avatar
armin committed
184

armin's avatar
armin committed
185
        void requestGetChannelDetails( const QString &pChannelId );
armin's avatar
armin committed
186

armin's avatar
armin committed
187
188
        void requestIsLoggedIn();

armin's avatar
armin committed
189
190
        void setUserPresenceDefaultStatus( int pStatus );

armin's avatar
armin committed
191
        void setUserPresenceStatus( int pStatus );
armin's avatar
armin committed
192

armin's avatar
armin committed
193
        void onStateChanged( const Qt::ApplicationState &pState );
armin's avatar
armin committed
194

armin's avatar
armin committed
195
        void createVideoCall( const QString &pRid );
armin's avatar
armin committed
196

armin's avatar
armin committed
197
198
        void disconnectFromServer();

armin's avatar
armin committed
199
        void searchForRoomIdByName( const QString &pName );
armin's avatar
armin committed
200

201
202
203
204
        void blockUser( const QString &pChannelId );

        void unBlockUser( const QString &pChannelId );

armin's avatar
armin committed
205
206
        void leaveChannel( const QString &pId );

armin's avatar
armin committed
207
208
        void hideChannel( const QString &pId );

209
210
        void reportAbusiveContent( const QString &pMessageId, const QString &pAuthor );

armin's avatar
ias    
armin committed
211
        void searchMessage( const QString &pTerm, const QString &pRoom );
armin's avatar
armin committed
212

armin's avatar
ias    
armin committed
213
        void searchRoom( const QString &pTerm, const QString &pType );
armin's avatar
armin committed
214

armin's avatar
armin committed
215
216
217
218
219
        void requestNewVideoPath();

        void setSetting( const QString &pKey, const QString &pValue );

        void getSetting( const QString &pKey );
armin's avatar
armin committed
220
        void deleteMessage( QString rid, QString id );
221
        void reconnect();
armin's avatar
armin committed
222

armin's avatar
armin committed
223
    public:
armin's avatar
armin committed
224
225
226
227
228
        QString getServerId() const;
        bool isConnected() const;
        bool isWebsocketValid( void ) const;
        void getCustomEmojis();
        RestApi *getRestApi() const;
armin's avatar
armin committed
229
        ChannelRepository *getChannels() const;
armin's avatar
as    
armin committed
230
        FilesRepo *getFiles() const;
armin's avatar
ad    
armin committed
231
        RocketChatChannelService *mChannelService = nullptr;
armin's avatar
armin committed
232
        void handleChannelMessage( const QJsonObject &pMessage );
armin's avatar
armin committed
233
234
        uint diffToLastDDPPing();
        void sendUnsentMessages( void );
armin's avatar
armin committed
235
236
        void sendApiRequest( const RestApiRequest &pRequest, bool pRetry = true );
        void sendDdprequest( const QSharedPointer<DDPRequest> &pRequest, bool pRetry = true, bool pSendWhenConnected = false );
armin's avatar
armin committed
237
238
239
240
        bool initialised = 0;
        ConnectionState getConnectionState() const;
        QString getCurrentChannel() const;
        QString getUsername() const;
armin's avatar
armin committed
241
        void setUsername( const QString &pUsername );
armin's avatar
armin committed
242
        QString getUserId() const;
armin's avatar
armin committed
243
        void setUserId( const QString &userId );
armin's avatar
armin committed
244
        EmojiRepo *getEmojiRepo() const;
armin's avatar
armin committed
245
246
        void setEmojiRepo( EmojiRepo *emojiRepo );
        void createAccount( const QString &username, const QString &email, const QString &password );
armin's avatar
armin committed
247
        void offlineLogin();
armin's avatar
ad    
armin committed
248
        void wipeDbAndReconnect();
armin's avatar
armin committed
249
    protected:
armin's avatar
armin committed
250

armin's avatar
armin committed
251
252
253
254
255
256
257
        RocketChatServerData();
        ConnectionState mConnectionState = ConnectionState::OFFLINE;
        QString mUsername = "";
        QString mUserId = "";
        QString mBaseUrl = "";
        QString mApiUri = "";
        QString mServerId = "";
armin's avatar
armin committed
258
259
        uint mTokenExpire = 0;
        QString mResumeToken;
armin's avatar
armin committed
260
        bool mUnsecureConnection = false;
armin's avatar
armin committed
261
        QSharedPointer<RocketChatUser> mOwnUser;
armin's avatar
armin committed
262
263
264

        MeteorDDP *mDdpApi = nullptr;
        RestApi *mRestApi = nullptr;
armin's avatar
armin committed
265
        UserRepository mUsers;
armin's avatar
armin committed
266
        LoginMethodsRepository mLoginMethodRepo;
armin's avatar
armin committed
267
268
        ChannelRepository *mChannels = nullptr;
        EmojiRepo *mEmojiRepo = nullptr;
armin's avatar
as    
armin committed
269
        FilesRepo *mFilesRepo = nullptr;
armin's avatar
armin committed
270
271
        RocketChatServerConfig config;
        bool mConnected = false;
armin's avatar
armin committed
272
        bool mLoggedIn = false;
armin's avatar
armin committed
273
        bool mResumeOperation = false;
armin's avatar
armin committed
274
275
276
        MessageService *mMessageService = nullptr;
        PersistanceLayer *mStorage = nullptr;
        EmojiService *mEmojiService = nullptr;
277
        FileService *mFileService = nullptr;
armin's avatar
armin committed
278
        QLinkedList<QSharedPointer<DDPRequest>> mUnsendDdpRequests;
armin's avatar
armin committed
279
        QLinkedList<QSharedPointer<DDPRequest>> mUnsendDdpRequestsOnConnected;
armin's avatar
armin committed
280
281
282
283
        QLinkedList<RestApiRequest> mUnsendRestRequests;
        QMap<QString, FileUploader *> mFileUploads;
        QString mCurrentChannel;
        QMutex mUnsentMutex;
armin's avatar
ad    
armin committed
284
285
286
287
288
        UserModel *userModel = nullptr;
        LoginMethodsModel *loginMethodsModel = nullptr;
        ChannelModel *channelsModel = nullptr;
        ChannelModel *directModel = nullptr;
        ChannelModel *groupsModel = nullptr;
armin's avatar
armin committed
289
        std::function<void ( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages )> historyLoaded;
armin's avatar
armin committed
290
        QString mPendingSwitchRoomRequest;
armin's avatar
ias    
armin committed
291
292
        QString mPendingSwitchRoomRequestType;

armin's avatar
armin committed
293
        bool mCustomEmojisReady = false;
armin's avatar
armin committed
294

armin's avatar
armin committed
295
        void getServerSettings( void );
armin's avatar
armin committed
296
        void onDDPConnected();
armin's avatar
armin committed
297
298
        void onDDPDisonnected();

armin's avatar
armin committed
299
        void onDDPAuthenticated();
armin's avatar
armin committed
300
        void onDDPMessageReceived( const QJsonObject &pMessage );
armin's avatar
armin committed
301
        void onChannelsLoaded( const QList<QSharedPointer<RocketChatChannel> > &pChannels );
armin's avatar
armin committed
302
        void onLoggedIn();
armin's avatar
armin committed
303
304
305
306
307
        bool isStreamRoomMessage( const QJsonObject &pMessage ) const;
        bool isUserJoinMessage( const QJsonObject &pMessage ) const;
        void handleStreamRoomMessage( const QJsonObject &pMessage );
        void handleUserJoinMessage( const QJsonObject &pMessage );
        void onMessageReceived( const QString &pServerId, const QString &pChannelId, const QString &pMessage );
armin's avatar
armin committed
308
309
310
        void onLoginError();
        void onResume();
        void setRestApi( RestApi * );
armin's avatar
armin committed
311

armin's avatar
armin committed
312
313
        void loadEmojis();
        void loadHistories( void );
armin's avatar
armin committed
314
        void onUsersLoaded( const QString &pChannelId, const QVector<QSharedPointer<RocketChatUser>> &pUserList );
armin's avatar
armin committed
315

armin's avatar
armin committed
316
        void onUnreadCountChanged();
armin's avatar
armin committed
317
318

    signals:
armin's avatar
armin committed
319
320
321
        void ddpConnected( const QString &pServerId );
        void loggedIn( const QString &pServer );
        void onHashLoggedIn( const QString &pServer );
armin's avatar
armin committed
322
        void loginError( void );
armin's avatar
armin committed
323
324
325
326
327
328
        void loggedOut( const QString &pServerId );
        void fileUploadFinished( const QString &pData );
        void fileuploadStarted( const QString &pData );
        void fileUploadProgressChanged( const QString &pData );
        void error( const QString &pError );
        void fileRessourceProcessed( const QString &pUrl, const QString &pPath, bool pShowInline );
armin's avatar
armin committed
329
        void customEmojisReceived( QVariantList pEmojis );
330
        void channelSwitchRequest( QSharedPointer<RocketChatChannel> pChannel );
armin's avatar
armin committed
331
        void openUrl( const QString &url );
armin's avatar
armin committed
332
333
        void readyToCheckForPendingNotification();
        void registerForPush();
armin's avatar
armin committed
334
335
336
337
        void allChannelsReady( const QVariantList &channels );
        void channelDetailsReady( QVariantMap details, const QString &channelId );
        void unreadCountChanged( const QString &pServerId, uint pUnread );
        void newLoginMethod( const QMap<QString, QVariant> &entry );
338
        void resetLoginMethods();
armin's avatar
armin committed
339
        void offlineMode( void );
armin's avatar
armin committed
340
        void loggingIn();
armin's avatar
armin committed
341
        void userStatusChanged( int status );
armin's avatar
armin committed
342
343
        void videoPath( const QString &path );
        void setting( const QString &pKey, const QString &pValue );
armin's avatar
armin committed
344
        void offline( void );
armin's avatar
armin committed
345

346
        // MessageListener interface
347
    public:
armin's avatar
as    
armin committed
348
        bool handlesMessage( const QJsonObject &object ) override;
Dennis Beier's avatar
Dennis Beier committed
349
        QString getBaseUrl() const;
armin's avatar
isad    
armin committed
350
351
        QSharedPointer<RocketChatUser> getOwnUser() const;
        void setOwnUser( const QSharedPointer<RocketChatUser> &ownUser );
armin's avatar
armin committed
352
353
354
};

#endif // ROCKETCHATSERVER_H