rocketchatserver.cpp 73.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
/********************************************************************************************
 *                                                                                          *
 * 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
22
23
#include "rocketchatserver.h"

armin's avatar
armin committed
24
#include "CustomModels/models.h"
armin's avatar
armin committed
25

armin's avatar
armin committed
26
RocketChatServerData::RocketChatServerData( QString pId, QString pBaseUrl, bool pUnsecure ): mBaseUrl( std::move( pBaseUrl ) ), mServerId( std::move( pId ) ), userModel( Models::getUsersModel() ), loginMethodsModel( Models::getLoginMethodsModel() ), channelsModel( Models::getPublicGroupsModel() ),
armin's avatar
ias    
armin committed
27
    directModel( Models::getDirectChannelsModel() ), groupsModel( Models::getPublicGroupsModel() )
armin's avatar
armin committed
28
{
armin's avatar
armin committed
29
    qRegisterMetaType<User>( "User" );
armin's avatar
d    
armin committed
30
    qRegisterMetaType<QList<QSharedPointer<Emoji>>>( "QList<QSharedPointer<Emoji>>" );
armin's avatar
as    
armin committed
31
    connect( this, &RocketChatServerData::destroyed, this, &RocketChatServerData::persistData, Qt::UniqueConnection );
armin's avatar
armin committed
32
33
34
    mUnsecureConnection = pUnsecure;

    mApiUri = QStringLiteral( "/api/v1" );
armin's avatar
ds    
armin committed
35
    mEmojiRepo = new EmojiRepo( this );
armin's avatar
as    
armin committed
36
    mFilesRepo = new FilesRepo( this );
armin's avatar
armin committed
37

armin's avatar
armin committed
38
}
armin's avatar
armin committed
39

armin's avatar
armin committed
40
void RocketChatServerData::init()
armin's avatar
ds    
armin committed
41
42
43
44
45
{
    initDb();
}

void RocketChatServerData::initDb()
armin's avatar
armin committed
46
{
armin's avatar
armin committed
47
    mStorage = PersistanceLayer::instance();
armin's avatar
ds    
armin committed
48
49
50
    connect( mStorage, &PersistanceLayer::ready, this, &RocketChatServerData::initConnections, Qt::UniqueConnection );
    mStorage->init();
}
armin's avatar
armin committed
51

armin's avatar
ds    
armin committed
52
53
54
55
56
57
void RocketChatServerData::initConnections()
{
    historyLoaded = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) {
        if ( messages ) {
            mStorage->transaction();

armin's avatar
as    
armin committed
58
59
60
61
62
63
64
65
66
            QSet<QString> duplicateSet;

            for ( auto it = messages->begin(); it != messages->end(); it++ ) {
                if ( !duplicateSet.contains( it.key() ) ) {
                    duplicateSet.insert( it.key() );

                    if ( Q_LIKELY( mChannels->contains( it.key() ) && !mChannels->get( it.key() ).isNull() ) ) {
                        MessageList messageList = messages->values( it.key() );
                        mChannels->get( it.key() )->addMessages( messageList );
armin's avatar
as    
armin committed
67
                        // mMessageService->persistMessages( messageList );
armin's avatar
as    
armin committed
68
                    }
armin's avatar
armin committed
69
                }
armin's avatar
ds    
armin committed
70
            }
armin's avatar
armin committed
71

armin's avatar
ad    
armin committed
72
            mStorage->askForcommit();
armin's avatar
armin committed
73

armin's avatar
as    
armin committed
74
75
            delete messages;

armin's avatar
ds    
armin committed
76
77
78
79
80
81
82
        }
    };
    QString protocol = QStringLiteral( "https://" );

    if ( mUnsecureConnection ) {
        protocol = QStringLiteral( "http://" );
    }
armin's avatar
armin committed
83

armin's avatar
ad    
armin committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    if ( mRestApi != nullptr ) {
        mRestApi->deleteLater();
    }

    if ( mFileService != nullptr ) {
        delete mFileService;
    }

    if ( mEmojiService != nullptr ) {
        delete mEmojiService;
    }

    if ( mDdpApi != nullptr ) {
        mDdpApi->deleteLater();
    }

    if ( mChannels != nullptr ) {
        mChannels->deleteLater();
    }

    if ( mMessageService != nullptr ) {
        mMessageService->deleteLater();
    }

    if ( mChannelService != nullptr ) {
        mChannelService->deleteLater();
    }

armin's avatar
ds    
armin committed
112
113
    setRestApi( new RestApi( this, protocol + mBaseUrl ) );
    mFileService = new FileService( this );
armin's avatar
as    
armin committed
114
    mEmojiService = new EmojiService( this, mFileService );
115

armin's avatar
ds    
armin committed
116
    mDdpApi = new MeteorDDP( this, mBaseUrl, mUnsecureConnection );
armin's avatar
armin committed
117

armin's avatar
ds    
armin committed
118
119
    mRestApi->moveToThread( QThread::currentThread() );
    mDdpApi->moveToThread( QThread::currentThread() );
armin's avatar
armin committed
120

armin's avatar
ds    
armin committed
121
    mChannels = new ChannelRepository( this );
122

armin's avatar
ds    
armin committed
123
    RocketChatServerConfig::init();
124

armin's avatar
as    
armin committed
125
126
    auto result = QMetaObject::invokeMethod( mRestApi, "init" );
    Q_ASSERT( result );
armin's avatar
armin committed
127

armin's avatar
ds    
armin committed
128
129
130
    mDdpApi->registerMessageHandler( this );
    connect( mDdpApi, &MeteorDDP::ddpConnected, this, &RocketChatServerData::onDDPConnected, Qt::UniqueConnection );
    connect( mDdpApi, &MeteorDDP::ddpDisconnected, this, &RocketChatServerData::onDDPDisonnected, Qt::UniqueConnection );
armin's avatar
armin committed
131

armin's avatar
ds    
armin committed
132
133
134
    QPair<QString, uint> tokenDb = mStorage->getToken();
    QString token = tokenDb.first;
    QString userDb = mStorage->getUserName();
Dennis Beier's avatar
Dennis Beier committed
135

armin's avatar
ds    
armin committed
136
137
138
    if ( !token.isEmpty() && !userDb.isEmpty() ) {
        emit offlineMode();
    }
armin's avatar
armin committed
139

armin's avatar
ds    
armin committed
140
141
    setUserId( mStorage->getUserId() );
    mUsername = mStorage->getUserName();
armin's avatar
armin committed
142

armin's avatar
ds    
armin committed
143
    loadEmojis();
armin's avatar
armin committed
144

armin's avatar
avatar    
armin committed
145
    mMessageService = new MessageService( this, mStorage, this, mEmojiRepo, mFileService );
armin's avatar
armin committed
146
    mChannelService = new RocketChatChannelService( this, this, mMessageService, mFileService );
armin's avatar
ds    
armin committed
147
148
149
150
151
152
153
154
    mChannelService->setDdp( mDdpApi );
    mChannelService->setChannels( mChannels );
    connect( mChannelService, &RocketChatChannelService::channelsLoaded, this, &RocketChatServerData::onChannelsLoaded, Qt::UniqueConnection );
    connect( mChannelService, &RocketChatChannelService::usersLoaded, this, &RocketChatServerData::onUsersLoaded, Qt::UniqueConnection );
    connect( mChannelService, &RocketChatChannelService::directChannelReady, this, &RocketChatServerData::switchChannelByName, Qt::UniqueConnection );
    connect( channelsModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection );
    connect( directModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection );
    connect( groupsModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection );
armin's avatar
armin committed
155
156


armin's avatar
ds    
armin committed
157
    QString lastServer = mStorage->getSetting( QStringLiteral( "currentServer" ) );
armin's avatar
armin committed
158

armin's avatar
ds    
armin committed
159
160
161
162
163
    if ( lastServer == mBaseUrl ) {
        QPair<QString, uint> tokenDb = mStorage->getToken();
        QString userDb = mStorage->getUserName();
        QDateTime currentTime = QDateTime::currentDateTime();

armin's avatar
as    
armin committed
164
        //TODO: needs more testing
armin's avatar
ds    
armin committed
165
166
167
        if ( !tokenDb.first.isEmpty() && tokenDb.second > currentTime.toTime_t() ) {
            loginWithToken( userDb, tokenDb.first );
        } else {
armin's avatar
ad    
armin committed
168
            wipeDbAndReconnect();
armin's avatar
sd    
armin committed
169
            emit loggedOut( mServerId );
armin's avatar
armin committed
170
        }
armin's avatar
ds    
armin committed
171
    } else if ( !lastServer.isEmpty() ) {
armin's avatar
ad    
armin committed
172
        wipeDbAndReconnect();
armin's avatar
ds    
armin committed
173
174
        emit loggedOut( mServerId );
    }
armin's avatar
armin committed
175

armin's avatar
ds    
armin committed
176
177
    emit readyToCheckForPendingNotification();
    mChannelService->loadJoinedChannelsFromDb();
armin's avatar
armin committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
}

void RocketChatServerData::setRestApi( RestApi *pNewRestApi )
{

    connect( pNewRestApi, &RestApi::loggedIn, this, &RocketChatServerData::onLoggedIn, Qt::UniqueConnection );
    connect( pNewRestApi, &RestApi::loginError, this, &RocketChatServerData::onLoginError, Qt::UniqueConnection );
    this->mRestApi = pNewRestApi;
    QPair<QString, uint> token = mStorage->getToken();

    if ( token.first.length() && token.second > 0 ) {
        mResumeToken = token.first;
        mTokenExpire = token.second;
    }
}

void RocketChatServerData::loadEmojis()
{

armin's avatar
armin committed
197
198
199
200
201
    /* QFile file(":/emoji.json");
     if(!file.open(QFile::ReadOnly)){
         qDebug()<<file.errorString();
         qDebug()<<file.error();
     }
armin's avatar
armin committed
202

armin's avatar
armin committed
203
     QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
armin's avatar
armin committed
204

armin's avatar
armin committed
205
     QJsonObject obj = doc.object();
armin's avatar
armin committed
206

armin's avatar
armin committed
207
208
209
210
211
212
213
214
215
216
217
218
     for(QString key: obj.keys()){
         QJsonObject emojiObj = obj[key].toObject();
         QString id = ":"+key+":";
         QString file = "qrc:/res/emojis/"+emojiObj["unicode"].toString()+".png";
         QString html = "<img height='20' width='20' src='"+file+"' />";
         QString unicode = Utils::escapeUnicodeEmoji(emojiObj["unicode"].toString());
         QString category = emojiObj["category"].toString();
         QString sort_order = emojiObj["emoji_order"].toString();
         int sort_orderInt = sort_order.toInt();
         qDebug()<<sort_orderInt;
         mStorage->addCustomEmoji(id,file,html,category,unicode,sort_orderInt);
     }*/
armin's avatar
d    
armin committed
219
    auto emojiModel = Models::getEmojisModel();
armin's avatar
armin committed
220

armin's avatar
armin committed
221
    auto emojiList = mEmojiService->loadEmojisFromDb();
armin's avatar
as    
armin committed
222
    QHash<QString, QList<QSharedPointer<Emoji>>> emojisByCategory;
armin's avatar
armin committed
223

224
    for ( auto &emoji : emojiList ) {
armin's avatar
armin committed
225
226
        if ( mEmojiRepo != nullptr && !emoji.isNull() ) {
            mEmojiRepo->add( emoji->getIdentifier(), emoji );
armin's avatar
armin committed
227
        }
armin's avatar
d    
armin committed
228
229
230
231
232

        auto category = emoji->getCategory();

        if ( !emojisByCategory.contains( category ) ) {
            emojisByCategory.insert( category, QList<QSharedPointer<Emoji>>() );
armin's avatar
armin committed
233
            emojisByCategory[category].reserve( 500 );
armin's avatar
armin committed
234
235
236
237

            if ( category == "custom" ) {
                mCustomEmojisReady = true;
            }
armin's avatar
d    
armin committed
238
239
        }

armin's avatar
sa    
armin committed
240
        emojisByCategory[category].append( std::move( emoji ) );
armin's avatar
d    
armin committed
241
242
    }

armin's avatar
as    
armin committed
243
244
245
    for ( auto it = emojisByCategory.begin(); it != emojisByCategory.end(); it++ ) {
        auto result = QMetaObject::invokeMethod( emojiModel, "addEmojisByCategory", Q_ARG( QString, it.key() ), Q_ARG( QList<QSharedPointer<Emoji>>, std::move( it.value() ) ) );
        Q_ASSERT( result );
armin's avatar
armin committed
246
247
248
    }
}

armin's avatar
armin committed
249
void RocketChatServerData::switchChannel( const QString &pServer, const QString &pRid, const QString &pName, const QString &pType )
armin's avatar
armin committed
250
{
armin's avatar
ias    
armin committed
251
    Q_UNUSED( pServer )
armin's avatar
armin committed
252
    qDebug() << "switch channel to: " << pRid;
armin's avatar
armin committed
253
    bool switchPossible = false;
armin's avatar
armin committed
254

255
256
    QSharedPointer<RocketChatChannel> channel = nullptr;

armin's avatar
armin committed
257
    if ( pRid.length() ) {
armin's avatar
armin committed
258
        if ( mChannels != nullptr && !mChannels->contains( pRid ) ) {
armin's avatar
armin committed
259
            if ( pType == "d" || pType == "c" || pType == "p" ) {
armin's avatar
armin committed
260
                qDebug() << "create new channel object to:" << pRid;
261
                mChannelService->createChannelObject( pRid, pName, pType );
armin's avatar
armin committed
262
263

                if ( mChannels->contains( pRid ) && !mChannels->get( pRid ).isNull() ) {
264
                    channel = mChannels->get( pRid );
armin's avatar
armin committed
265
266
                    switchPossible = true;
                }
armin's avatar
armin committed
267
            }
armin's avatar
armin committed
268
        } else if ( mChannels != nullptr ) {
269
            channel = mChannels->get( pRid );
armin's avatar
armin committed
270
271
272
            switchPossible = true;
        }
    }
armin's avatar
armin committed
273
274
275
276
277
278

    if ( switchPossible ) {
        qDebug() << "current Channel " << mCurrentChannel;
        qDebug() << "room " << pRid;

        if ( mCurrentChannel != pRid ) {
279
            emit channelSwitchRequest( channel );
armin's avatar
armin committed
280
281
282
283
        }
    }
}

armin's avatar
ias    
armin committed
284
void RocketChatServerData::switchChannelByName( const QString &pName, const QString &pType )
armin's avatar
armin committed
285
{
armin's avatar
armin committed
286
287
288
    if ( mLoggedIn ) {
        if ( mChannels->getChannelByName( pName ) ) {
            auto channel = mChannels->getChannelByName( pName );
289
            switchChannel( QStringLiteral( "default" ), channel->getRoomId(), pName, channel->getType() );
armin's avatar
armin committed
290
            mPendingSwitchRoomRequest.clear();
armin's avatar
ias    
armin committed
291
            mPendingSwitchRoomRequestType.clear();
armin's avatar
armin committed
292
        } else {
armin's avatar
ias    
armin committed
293
294
295
296
297
298
299
            DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
                if ( pResponse.contains( "result" ) ) {
                    QJsonObject result = pResponse["result"].toObject();
                    QString id = result["_id"].toString();
                    switchChannel( QStringLiteral( "default" ), id, pName, pType );
                }
            };
300
            auto request = QSharedPointer<RocketChatGetRoomByNameAndType>::create( pName, pType );
armin's avatar
ias    
armin committed
301
302
303
            request->setSuccess( success );
            sendDdprequest( request );
            //searchForRoomIdByName( pName );
armin's avatar
armin committed
304
        }
armin's avatar
armin committed
305
    } else {
armin's avatar
armin committed
306
        mPendingSwitchRoomRequest = pName;
armin's avatar
ias    
armin committed
307
        mPendingSwitchRoomRequestType = pType;
armin's avatar
armin committed
308
309
310
    }
}

armin's avatar
armin committed
311
void RocketChatServerData::requestGetChannelDetails( const QString &pChannelId )
armin's avatar
armin committed
312
313
314
{
    if ( mChannels->contains( pChannelId ) ) {
        auto channel = mChannels->get( pChannelId );
armin's avatar
armin committed
315
316

        if ( !channel.isNull() ) {
armin's avatar
as    
armin committed
317
318
319
320
            auto archived = channel->getArchived();
            auto readonly = channel->getReadOnly();
            auto muted = channel->getMuted();
            auto selfMuted = channel->getSelfMuted();
armin's avatar
armin committed
321

armin's avatar
armin committed
322

armin's avatar
armin committed
323
324
            QVariantList mutedList;

armin's avatar
armin committed
325
326
            for ( const auto &entry : muted ) {
                mutedList.append( entry );
armin's avatar
armin committed
327
            }
armin's avatar
armin committed
328

armin's avatar
armin committed
329
330
            QVariantMap details;

331
332
333
334
            details[QStringLiteral( "archived" )] = archived;
            details[QStringLiteral( "ro" )] = readonly;
            details[QStringLiteral( "muted" )] = mutedList;
            details[QStringLiteral( "selfMuted" )] = selfMuted;
335
            details[QStringLiteral( "type" )] = channel->getType();
336
            details[QStringLiteral( "name" )] = channel->getName();
337
338
            details[QStringLiteral( "ownerId" )] = channel->getOwnerId();
            details[QStringLiteral( "ownerName" )] = channel->getOwnerName();
339
            details[QStringLiteral( "blocked" )] = channel->getBlocked();
armin's avatar
armin committed
340

armin's avatar
armin committed
341
            emit channelDetailsReady( details, pChannelId );
armin's avatar
armin committed
342
        }
armin's avatar
armin committed
343
344
345
346
347
    }
}

void RocketChatServerData::requestIsLoggedIn()
{
armin's avatar
armin committed
348
    if ( mLoggedIn ) {
349
        emit loggedIn( QStringLiteral( "default" ) );
armin's avatar
armin committed
350
351
352
    }
}

armin's avatar
armin committed
353
354
355
356
void RocketChatServerData::setUserPresenceDefaultStatus( int pStatus )
{
    if ( pStatus >= 0 && pStatus < 5 && isWebsocketValid() && diffToLastDDPPing() < 29 ) {

357
        auto request = QSharedPointer<RocketChatChangeUserPresenceDefaultStatus>::create( static_cast<RocketChatUser::status>( pStatus ) );
armin's avatar
armin committed
358
359
360
361
        sendDdprequest( request, true );
    }
}

armin's avatar
armin committed
362
void RocketChatServerData::setUserPresenceStatus( int pStatus )
armin's avatar
armin committed
363
{
armin's avatar
armin committed
364
    if ( pStatus >= 0 && pStatus < 5 && isWebsocketValid() && diffToLastDDPPing() < 29 ) {
armin's avatar
armin committed
365

366
        auto request = QSharedPointer<RocketChatChangeUserPresenceStatus>::create( static_cast<RocketChatUser::status>( pStatus ) );
armin's avatar
armin committed
367
        sendDdprequest( request, true );
armin's avatar
armin committed
368
369
    }
}
armin's avatar
armin committed
370

armin's avatar
armin committed
371
void RocketChatServerData::onStateChanged( const Qt::ApplicationState &pState )
armin's avatar
armin committed
372
{
armin's avatar
armin committed
373
374
    Q_UNUSED( pState )

armin's avatar
armin committed
375
376
377
    if ( !isWebsocketValid() || diffToLastDDPPing() > 29 ) {
        qDebug() << "call resume";
        resume();
armin's avatar
armin committed
378
    } else {
armin's avatar
armin committed
379
        setUserPresenceStatus( static_cast<int>( RocketChatUser::status::ONLINE ) );
armin's avatar
armin committed
380
    }
armin's avatar
as    
armin committed
381
382
383
384

    if ( pState == Qt::ApplicationState::ApplicationSuspended || pState == Qt::ApplicationState::ApplicationInactive ) {
        persistData();
    }
armin's avatar
armin committed
385
386
}

armin's avatar
armin committed
387
void RocketChatServerData::createVideoCall( const QString &pRid )
armin's avatar
armin committed
388
{
armin's avatar
armin committed
389
    if ( !pRid.isEmpty() ) {
armin's avatar
armin committed
390

391
        auto request = QSharedPointer<RocketChatUpdateJitsiTimeout>::create( pRid );
armin's avatar
sad    
armin committed
392
        sendDdprequest( request );
armin's avatar
armin committed
393
        joinJitsiCall( pRid );
armin's avatar
armin committed
394
395
396
    }
}

armin's avatar
armin committed
397
//TODO: rename, as method is not really for searching only
armin's avatar
armin committed
398
void RocketChatServerData::searchForRoomIdByName( const QString &pName )
armin's avatar
armin committed
399
{
400
    auto request = QSharedPointer<RestSpotlightRequest>::create( pName );
armin's avatar
ias    
armin committed
401
    std::function<void ( QNetworkReply *, QJsonObject data, RestApi * )> success = [ = ]( QNetworkReply *, QJsonObject data, RestApi * ) {
armin's avatar
armin committed
402

armin's avatar
ias    
armin committed
403
404
        if ( data.contains( QStringLiteral( "result" ) ) ) {
            QJsonObject result = data[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
405

406
407
            if ( result.contains( QStringLiteral( "users" ) ) ) {
                QJsonArray users = result[QStringLiteral( "users" )].toArray();
armin's avatar
armin committed
408

armin's avatar
as    
armin committed
409
                for ( auto user : users ) {
armin's avatar
armin committed
410
                    QJsonObject userObj = user.toObject();
armin's avatar
armin committed
411

412
413
                    if ( userObj.contains( QStringLiteral( "username" ) ) && userObj.contains( QStringLiteral( "_id" ) ) ) {
                        QString username = userObj[QStringLiteral( "username" )].toString();
armin's avatar
armin committed
414
415

                        if ( !username.compare( pName, Qt::CaseInsensitive ) ) {
416
417
                            QString id = userObj[QStringLiteral( "_id" )].toString();
                            switchChannel( QStringLiteral( "default" ), id, pName, "d" );
armin's avatar
armin committed
418
419
420
                        }
                    }
                }
421
422
            } else if ( result.contains( QStringLiteral( "rooms" ) ) ) {
                QJsonArray rooms = result[QStringLiteral( "rooms" )].toArray();
armin's avatar
armin committed
423

armin's avatar
as    
armin committed
424
                for ( const auto room : rooms ) {
armin's avatar
armin committed
425
                    QJsonObject roomObj = room.toObject();
armin's avatar
armin committed
426

427
428
                    if ( roomObj.contains( QStringLiteral( "name" ) ) && roomObj.contains( QStringLiteral( "_id" ) ) ) {
                        QString roomname = roomObj[QStringLiteral( "name" )].toString();
armin's avatar
armin committed
429
430

                        if ( !roomname.compare( pName, Qt::CaseInsensitive ) ) {
431
432
                            QString id = roomObj[QStringLiteral( "_id" )].toString();
                            switchChannel( QStringLiteral( "default" ), id, pName, "c" );
armin's avatar
armin committed
433
434
435
436
437
                        }
                    }
                }
            }
        }
armin's avatar
armin committed
438

armin's avatar
armin committed
439
        mPendingSwitchRoomRequest.clear();
armin's avatar
ias    
armin committed
440
        mPendingSwitchRoomRequestType.clear();
armin's avatar
armin committed
441
442

    };
armin's avatar
armin committed
443
    request->setSuccess( success );
armin's avatar
ias    
armin committed
444
    sendApiRequest( request );
armin's avatar
armin committed
445
446
}

447
448
void RocketChatServerData::blockUser( const QString &pChannelId )
{
armin's avatar
armin committed
449
450
    auto userId = pChannelId;
    userId.replace( mUserId, "" );
armin's avatar
armin committed
451
452
453
454

    if ( userId == pChannelId ) {
        QString possibleRoom1 = userId + mUserId;
        QString possibleRoom2 = mUserId + userId;
455

456
457
458
        auto blockRequest1 = QSharedPointer<RocketChatBlockUserRequest>::create( possibleRoom1, userId );
        auto blockRequest2 = QSharedPointer<RocketChatBlockUserRequest>::create( possibleRoom2, userId );

armin's avatar
sad    
armin committed
459
460
        sendDdprequest( blockRequest1 );
        sendDdprequest( blockRequest2 );
461
462


armin's avatar
armin committed
463
    } else {
464
        auto blockRequest = QSharedPointer<RocketChatBlockUserRequest>::create( pChannelId, userId );
armin's avatar
sad    
armin committed
465
        sendDdprequest( blockRequest );
armin's avatar
armin committed
466
    }
467
468
469
470
}

void RocketChatServerData::unBlockUser( const QString &pChannelId )
{
armin's avatar
armin committed
471
472
    auto userId = pChannelId;
    userId.replace( mUserId, "" );
armin's avatar
armin committed
473
474
475
476

    if ( userId == pChannelId ) {
        QString possibleRoom1 = userId + mUserId;
        QString possibleRoom2 = mUserId + userId;
477
478
        auto unBlockRequest1 = QSharedPointer<RocketChatUnblockUserRequest>::create( possibleRoom1, userId );
        auto unBlockRequest2 = QSharedPointer<RocketChatUnblockUserRequest>::create( possibleRoom2, userId );
armin's avatar
sad    
armin committed
479
480
        sendDdprequest( unBlockRequest1 );
        sendDdprequest( unBlockRequest2 );
armin's avatar
armin committed
481
    } else {
482
        auto unBlockRequest = QSharedPointer<RocketChatUnblockUserRequest>::create( pChannelId, userId );
armin's avatar
sad    
armin committed
483
        sendDdprequest( unBlockRequest );
armin's avatar
armin committed
484
    }
485
486
}

armin's avatar
armin committed
487
488
489
490
491
QString RocketChatServerData::getUserId() const
{
    return mUserId;
}

armin's avatar
armin committed
492
void RocketChatServerData::setUserId( const QString &userId )
armin's avatar
armin committed
493
{
armin's avatar
armin committed
494
495
496
    auto users = Models::getUsersModel();
    auto user = users->getUserById( userId );

armin's avatar
sd    
armin committed
497
    if ( user.isNull() && !userId.isEmpty() ) {
498
        user = QSharedPointer<RocketChatUser>::create( userId ) ; //QSharedPointer<RocketChatUser>( new RocketChatUser( userId ) );
armin's avatar
as    
armin committed
499
500
        auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, user ) );
        Q_ASSERT( result );
armin's avatar
armin committed
501
502
    }

armin's avatar
ds    
armin committed
503
504
    if ( !user.isNull() && !userId.isEmpty() ) {
        mUserId = userId;
armin's avatar
armin committed
505

armin's avatar
ds    
armin committed
506
507
508
509
        connect( user.data(), &RocketChatUser::statusChanged, this, [ = ]() {
            emit userStatusChanged( static_cast<int>( user->getStatus() ) );
        } );
    }
armin's avatar
armin committed
510
511
}

armin's avatar
armin committed
512
513
void RocketChatServerData::disconnectFromServer()
{
armin's avatar
armin committed
514
    mConnectionState = ConnectionState::OFFLINE;
armin's avatar
armin committed
515
516
517
    mDdpApi->disconnectFromServer();
}

armin's avatar
armin committed
518
519
520
521
void RocketChatServerData::loadHistories()
{
    QStringList channelIds;

armin's avatar
armin committed
522
523
    for ( const auto &currentChannel : mChannels->getElements() ) {
        if ( Q_LIKELY( !currentChannel.isNull() && currentChannel->getJoined() ) ) {
armin's avatar
armin committed
524
525
526
527
            //joinChannel( currentChannel->getRoomId() );
            channelIds.append( currentChannel->getRoomId() );
        }
    }
armin's avatar
armin committed
528

529
530
    auto request = QSharedPointer<LoadHistoryServiceRequest>::create( channelIds, 1 );

armin's avatar
armin committed
531
    request->setLimit( 1 );
armin's avatar
armin committed
532
    request->setSuccess( historyLoaded );
armin's avatar
armin committed
533
    request->setNoLastThreeDays( true );
armin's avatar
armin committed
534
535
536
    mMessageService->loadHistory( request );
}

armin's avatar
ias    
armin committed
537
void RocketChatServerData::loadHistoryTill( const QString &pChannelId, qint64 pTs )
armin's avatar
armin committed
538
539
540
{

    std::function<void ( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages )> success = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) {
armin's avatar
ias    
armin committed
541
        historyLoaded( messages );
armin's avatar
armin committed
542
    };
543
    auto request = QSharedPointer<LoadHistoryServiceRequest>::create( pChannelId );
armin's avatar
ias    
armin committed
544
545
546
547
548
549
    request->setEnd( pTs );
    request->setLimit( 0 );
    request->setSuccess( success );
    request->setNoLastThreeDays( true );
    request->setStart( 0 );
    request->setSource( LoadHistoryServiceRequest::Source::SERVER );
armin's avatar
armin committed
550

armin's avatar
ias    
armin committed
551
    mMessageService->loadHistory( request );
armin's avatar
armin committed
552
553
}

armin's avatar
armin committed
554
void RocketChatServerData::onUsersLoaded( const QString &pChannelId, const QVector<QSharedPointer<RocketChatUser> > &pUserList )
armin's avatar
armin committed
555
556
557
{
    if ( Q_LIKELY( mChannels->contains( pChannelId ) ) ) {
        auto channel = mChannels->get( pChannelId );
armin's avatar
armin committed
558
559
560
561

        if ( !channel.isNull() ) {
            for ( const auto &user : pUserList ) {
                if ( !user.isNull() ) {
armin's avatar
armin committed
562
                    channel->addUser( user );
armin's avatar
as    
armin committed
563
564
                    auto result = QMetaObject::invokeMethod( userModel, "insertUser", Q_ARG( QString, pChannelId ), Q_ARG( User, user ) );
                    Q_ASSERT( result );
armin's avatar
armin committed
565
566
567
568
569
                }
            }
        }
    }
}
armin's avatar
armin committed
570

armin's avatar
armin committed
571
572
573
void RocketChatServerData::onUnreadCountChanged()
{
    uint number = 0;
armin's avatar
armin committed
574
575
576
577
    qDebug() << "on Unread count changed";

    for ( const auto &channel : mChannels->getElements() ) {
        if ( !channel.isNull() ) {
armin's avatar
armin committed
578
            number += channel->getUnreadMessages();
armin's avatar
armin committed
579
580
        }
    }
armin's avatar
armin committed
581

582
    emit unreadCountChanged( QStringLiteral( "default" ), number );
armin's avatar
armin committed
583
584
}

armin's avatar
cleanup    
armin committed
585
bool RocketChatServerData::handlesMessage( const QJsonObject &message )
586
587
{
    qDebug() << "RocketChatServerData message handler";
armin's avatar
armin committed
588
    onDDPMessageReceived( message );
589
590
591
    return true;
}

armin's avatar
armin committed
592
593
594
595
596
EmojiRepo *RocketChatServerData::getEmojiRepo() const
{
    return mEmojiRepo;
}

armin's avatar
armin committed
597
void RocketChatServerData::setEmojiRepo( EmojiRepo *emojiRepo )
armin's avatar
armin committed
598
599
600
601
{
    mEmojiRepo = emojiRepo;
}

armin's avatar
armin committed
602
void RocketChatServerData::createAccount( const QString &username, const QString &email, const QString &password )
603
{
604
    auto request = QSharedPointer<RocketChatCreateAccount>::create( username, email, password );
armin's avatar
armin committed
605
    sendDdprequest( request );
606
607
}

armin's avatar
armin committed
608
609
610
611
void RocketChatServerData::offlineLogin()
{
    QString userId = mStorage->getUserId();
    QString userName = mStorage->getUserName();
armin's avatar
armin committed
612
613

    if ( !userId.isEmpty() && !userName.isEmpty() ) {
armin's avatar
isad    
armin committed
614
        setUserId( userId );
615
        emit loggedIn( QStringLiteral( "default" ) );
armin's avatar
armin committed
616
617
618
    }
}

armin's avatar
ad    
armin committed
619
620
621
622
623
624
625
void RocketChatServerData::wipeDbAndReconnect()
{
    disconnectFromServer();
    mStorage->wipeDb();
    resume();
}

armin's avatar
isad    
armin committed
626
627
628
629
630
631
632
633
634
635
QSharedPointer<RocketChatUser> RocketChatServerData::getOwnUser() const
{
    return mOwnUser;
}

void RocketChatServerData::setOwnUser( const QSharedPointer<RocketChatUser> &ownUser )
{
    mOwnUser = ownUser;
}

Dennis Beier's avatar
Dennis Beier committed
636
637
QString RocketChatServerData::getBaseUrl() const
{
armin's avatar
armin committed
638
639
640
641
    QString protocol = "https://";

    if ( mDdpApi->getUnsecure() ) {
        protocol = "http://";
642
    }
armin's avatar
armin committed
643
644

    return protocol + mBaseUrl;
Dennis Beier's avatar
Dennis Beier committed
645
646
}

armin's avatar
armin committed
647
void RocketChatServerData::login( const QString &pUsername, const QString &pPassword )
armin's avatar
armin committed
648
{
armin's avatar
ad    
armin committed
649
    QString pass;
armin's avatar
armin committed
650

armin's avatar
ad    
armin committed
651
652
    QByteArray hashArray = QCryptographicHash::hash( pPassword.toUtf8(), QCryptographicHash::Sha256 );
    pass = hashArray.toHex();
armin's avatar
armin committed
653

armin's avatar
ad    
armin committed
654
    loginWithHash( pUsername, pass );
armin's avatar
armin committed
655
656
}

armin's avatar
armin committed
657
void RocketChatServerData::loginWithHash( const QString &pUsername, const QString &pPswHash )
armin's avatar
armin committed
658
659
660
{
    qDebug() << "login with hash";

armin's avatar
ad    
armin committed
661
662
663
664
665
666
667
668
669
670
671
672
    RestRequestCallback meCallBackSuccess = [ = ]( QNetworkReply *, QJsonObject data, RestApi * ) {

        if ( data.contains( QStringLiteral( "username" ) ) ) {
            mUsername = data[QStringLiteral( "username" )].toString();
            mStorage->transaction();
            mStorage->setUserData( mUsername, "" );
            mStorage->setToken( mResumeToken, mTokenExpire );
            mStorage->setUserId( mUserId );
            mStorage->askForcommit();
            onResume();
            onDDPAuthenticated();
        }
673

armin's avatar
ad    
armin committed
674
    };
675
676
    auto request = QSharedPointer<DDPLoginRequest>::create( pUsername, pPswHash );

armin's avatar
ad    
armin committed
677
    DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
armin's avatar
armin committed
678
679
        qDebug() << "authenticated";

680
681
        if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
            QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
682

683
            if ( result.contains( QStringLiteral( "token" ) ) && result.contains( QStringLiteral( "id" ) ) ) {
armin's avatar
ad    
armin committed
684
685
                mConnectionState = ConnectionState::ONLINE;
                mResumeToken = result[QStringLiteral( "token" )].toString();
armin's avatar
as    
armin committed
686
                QJsonObject expireObject = result[QStringLiteral( "tokenExpires" )].toObject();
687
                double expireDouble = expireObject[QStringLiteral( "$date" )].toDouble();
armin's avatar
ad    
armin committed
688
                mTokenExpire = static_cast<uint>( expireDouble / 1000 );
689
                QString userId = result[QStringLiteral( "id" )].toString();
armin's avatar
ad    
armin committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704

                setUserId( userId );
                mStorage->transaction();
                mStorage->setUserData( pUsername, pPswHash );

                mRestApi->setToken( mResumeToken );
                mRestApi->setUserId( userId );
                mStorage->askForcommit();
                mDdpApi->setToken( mResumeToken );
                mDdpApi->unsetResponseBinding( request->getFrame() );

                RestApiRequest meRequest
                    = RestApiRequest( new restMeRequest( meCallBackSuccess ) );

                mRestApi->sendRequest( meRequest );
armin's avatar
armin committed
705
706
707
708
709
710
711
712
713
714
            }
        }
    };
    DdpCallback error = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
        Q_UNUSED( pResponse );
        onLoginError();
    };

    request->setSuccess( success );
    request->setError( error );
armin's avatar
armin committed
715
    sendDdprequest( request, true, true );
armin's avatar
armin committed
716
    emit loggingIn();
armin's avatar
armin committed
717
718
719
    this->mUsername = pUsername;
}

armin's avatar
armin committed
720
void RocketChatServerData::loginWithToken( const QString &pUsername, const QString &pToken, bool pResume )
armin's avatar
armin committed
721
722
723
724
725
726
727
{
    Q_UNUSED( pResume );
    qDebug() << "login with token";

    qDebug() << "token from db: " << pToken;

    if ( !pToken.isEmpty() ) {
728
        auto self = this;
729
        RestRequestCallback meCallBackSuccess = [ self ]( QNetworkReply *, QJsonObject data, RestApi * ) {
armin's avatar
armin committed
730

731
            if ( data.contains( QStringLiteral( "username" ) ) ) {
732
                self->mUsername = data[QStringLiteral( "username" )].toString();
armin's avatar
armin committed
733
                self->mStorage->setUserName( self->mUsername );
734
735
                self->onResume();
                self->onDDPAuthenticated();
armin's avatar
armin committed
736
737
738
739
            }

        };

740
        auto request = QSharedPointer<DDPLoginRequest>::create( pToken );
armin's avatar
sd    
armin committed
741
        DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
armin's avatar
armin committed
742
            qDebug() << "authenticated";
armin's avatar
sd    
armin committed
743
            mConnectionState = ConnectionState::ONLINE;
armin's avatar
armin committed
744

745
746
            if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
                QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
747

748
                if ( result.contains( QStringLiteral( "token" ) ) && result.contains( QStringLiteral( "id" ) ) ) {
armin's avatar
sd    
armin committed
749
                    mResumeToken = result[QStringLiteral( "token" )].toString();
750
751
                    QJsonObject expireObject = result[QStringLiteral( "tokenExpires" )].toObject();
                    double expireDouble = expireObject[QStringLiteral( "$date" )].toDouble();
armin's avatar
sd    
armin committed
752
                    mTokenExpire = static_cast<uint>( expireDouble / 1000 );
armin's avatar
armin committed
753

754
                    QString userId = result[QStringLiteral( "id" )].toString();
armin's avatar
sd    
armin committed
755
756
757
758
759
760
761
                    setUserId( userId );
                    mStorage->setToken( self->mResumeToken, self->mTokenExpire );
                    mStorage->setUserId( userId );
                    mRestApi->setToken( self->mResumeToken );
                    mRestApi->setUserId( userId );
                    mDdpApi->setToken( self-> mResumeToken );
                    mDdpApi->unsetResponseBinding( request->getFrame() );
armin's avatar
armin committed
762

armin's avatar
armin committed
763
                    RestApiRequest meRequest
armin's avatar
armin committed
764
                        = RestApiRequest( new restMeRequest( meCallBackSuccess ) );
armin's avatar
armin committed
765

armin's avatar
sd    
armin committed
766
                    mRestApi->sendRequest( meRequest );
armin's avatar
armin committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
                }
            }
        };
        DdpCallback error = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
            Q_UNUSED( pResponse );
            qDebug() << "login error";
            //onLoginError();
            //try again with hash

            QString userDb = mStorage->getUserName();
            QString passDb = mStorage->getPassword();

            if ( !userDb.isEmpty() && !passDb.isEmpty() ) {

                loginWithHash( userDb, passDb );
            } else {
                qWarning() << "token rejected and no user+hash information present";
                onLoginError();
            }
        };

        request->setSuccess( success );
        request->setError( error );
790
#ifdef Q_OS_IOS
armin's avatar
armin committed
791
        mDdpApi->sendRequest( request );
792
#else
armin's avatar
armin committed
793
        sendDdprequest( request, true, true );
794
#endif
armin's avatar
armin committed
795
796
        emit loggingIn();

armin's avatar
armin committed
797
798
799
800
801
802
        this->mUsername = pUsername;
    } else {
        qDebug() << "empty token";
    }
}

armin's avatar
armin committed
803
804
void RocketChatServerData::loginWtihSamlToken( const QString &pToken )
{
armin's avatar
armin committed
805
    if ( !pToken.isEmpty() ) {
armin's avatar
armin committed
806
        qDebug() << pToken;
armin's avatar
armin committed
807
808
        
        RestRequestCallback meCallBackSuccess = [ = ]( QNetworkReply *, QJsonObject data, RestApi * ) {
armin's avatar
armin committed
809

810
811
            if ( data.contains( QStringLiteral( "username" ) ) ) {
                mUsername = data[QStringLiteral( "username" )].toString();
armin's avatar
armin committed
812
                mStorage->setUserName( mUsername );
armin's avatar
armin committed
813
814
815
816
817
818
                onResume();
                this->onDDPAuthenticated();
            }
            
        };
        
819
        auto request = QSharedPointer<ddpSamlLoginRequest>::create( pToken );
armin's avatar
ias    
armin committed
820
        DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
armin's avatar
armin committed
821
822
823
            qDebug() << "authenticated";
            mConnectionState = ConnectionState::ONLINE;
            
824
825
            if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
                QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
826
                
827
828
829
830
                if ( result.contains( QStringLiteral( "token" ) ) && result.contains( QStringLiteral( "id" ) ) ) {
                    this->mResumeToken = result[QStringLiteral( "token" )].toString();
                    QJsonObject expireObject = result[QStringLiteral( "tokenExpires" )].toObject();
                    double expireDouble = expireObject[QStringLiteral( "$date" )].toDouble();
armin's avatar
armin committed
831
                    mTokenExpire = static_cast<uint>( expireDouble / 1000 );
armin's avatar
armin committed
832
                    
833
                    QString userId = result[QStringLiteral( "id" )].toString();
armin's avatar
armin committed
834
                    setUserId( userId );
armin's avatar
armin committed
835
836
837
838
839
840
841
842
843
                    mStorage->setToken( mResumeToken, mTokenExpire );
                    mStorage->setUserId( userId );
                    mRestApi->setToken( mResumeToken );
                    mRestApi->setUserId( userId );
                    mDdpApi->setToken( mResumeToken );
                    mDdpApi->unsetResponseBinding( request->getFrame() );
                    
                    
                    RestApiRequest meRequest
armin's avatar
armin committed
844
845
846
                        = RestApiRequest( new restMeRequest( meCallBackSuccess ) );

                    mRestApi->sendRequest( meRequest );
armin's avatar
armin committed
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
                    
                    
                    
                }
            }
        };
        DdpCallback error = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
            Q_UNUSED( pResponse );
            qDebug() << "login error";
            qWarning() << "SAML2 token rejected";
            onLoginError();
        };
        
        request->setSuccess( success );
        request->setError( error );
armin's avatar
armin committed
862
        sendDdprequest( request, true, true );
armin's avatar
armin committed
863
864
        emit loggingIn();

armin's avatar
armin committed
865
866
867
868
869
    } else {
        qDebug() << "empty token";
    }
}

armin's avatar
armin committed
870
void RocketChatServerData::loginWithOpenIDToken( const QString &pToken, const QString &pSecret )
armin's avatar
armin committed
871
872
{
    if ( !pToken.isEmpty() && !pSecret.isEmpty() ) {
873
        auto self = this;
874
        RestRequestCallback meCallBackSuccess = [ self ]( QNetworkReply *, QJsonObject data, RestApi * ) {
armin's avatar
armin committed
875

876
            if ( data.contains( QStringLiteral( "username" ) ) ) {
877
878
                self->mUsername = data[QStringLiteral( "username" )].toString();
                self->mStorage->transaction();
armin's avatar
armin committed
879
880
881
                self->mStorage->setUserData( self->mUsername, "" );
                self->mStorage->setToken( self->mResumeToken, self->mTokenExpire );
                self->mStorage->setUserId( self->mUserId );
armin's avatar
ad    
armin committed
882
                self->mStorage->askForcommit();
883
884
                self->onResume();
                self->onDDPAuthenticated();
armin's avatar
armin committed
885
886
887
888
            }

        };

889
        auto request = QSharedPointer<DDPOpenIDLoginRequest>::create( pToken, pSecret );
armin's avatar
ias    
armin committed
890
        DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
armin's avatar
armin committed
891
            qDebug() << "authenticated";
892
            self->mConnectionState = ConnectionState::ONLINE;
armin's avatar
armin committed
893

894
895
            if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
                QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
896

897
                if ( result.contains( QStringLiteral( "token" ) ) && result.contains( QStringLiteral( "id" ) ) ) {
armin's avatar
sd    
armin committed
898
899
                    mConnectionState = ConnectionState::ONLINE;
                    mResumeToken = result[QStringLiteral( "token" )].toString();
900
901
                    QJsonObject expireObject = result[QStringLiteral( "tokenExpires" )].toObject();
                    double expireDouble = expireObject[QStringLiteral( "$date" )].toDouble();
armin's avatar
sd    
armin committed
902
                    mTokenExpire = static_cast<uint>( expireDouble / 1000 );
armin's avatar
armin committed
903

904
                    QString userId = result[QStringLiteral( "id" )].toString();
armin's avatar
armin committed
905

armin's avatar
sd    
armin committed
906
907
908
909
910
                    setUserId( userId );
                    mRestApi->setToken( mResumeToken );
                    mRestApi->setUserId( userId );
                    mDdpApi->setToken( mResumeToken );
                    mDdpApi->unsetResponseBinding( request->getFrame() );
armin's avatar
armin committed
911

armin's avatar
sd    
armin committed
912
                    // emit onHashLoggedIn( self->mServerId ) ;
armin's avatar
armin committed
913
914

                    RestApiRequest meRequest
armin's avatar
armin committed
915
                        = RestApiRequest( new restMeRequest( meCallBackSuccess ) );
armin's avatar
armin committed
916

917
                    self->mRestApi->sendRequest( meRequest );
armin's avatar
armin committed
918
919
920
921
922
923
924
925
926
927
928
929
930
931

                }
            }
        };
        DdpCallback error = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
            Q_UNUSED( pResponse );
            qDebug() << "login error";

            qWarning() << "token rejected and no user+hash information present";
            onLoginError();
        };

        request->setSuccess( success );
        request->setError( error );
armin's avatar
armin committed
932
        sendDdprequest( request, true, true );
armin's avatar
armin committed
933
934
        emit loggingIn();

armin's avatar
armin committed
935
936
937
938
939
940

    } else {
        qDebug() << "empty token";
    }
}

armin's avatar
armin committed
941
void RocketChatServerData::loginWithMethod( const QString &method, const QString &payload )
armin's avatar
armin committed
942
{
armin's avatar
armin committed
943
    if ( mLoginMethodRepo.contains( method ) ) {
armin's avatar
armin committed
944