rocketchatserver.cpp 72.8 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
armin committed
31

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
armin committed
36

armin's avatar
armin committed
37
}
armin's avatar
armin committed
38

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

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

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

armin's avatar
as    
armin committed
57
58
59
60
61
62
63
64
65
66
67
            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 );
                        mMessageService->persistMessages( messageList );
                    }
armin's avatar
armin committed
68
                }
armin's avatar
ds    
armin committed
69
            }
armin's avatar
armin committed
70

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

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

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

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

armin's avatar
ad    
armin committed
83
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
    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
111
112
113
    setRestApi( new RestApi( this, protocol + mBaseUrl ) );
    mFileService = new FileService( this );
    mEmojiService = new EmojiService( this, mFileService, mStorage );
114

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

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

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

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

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

armin's avatar
ds    
armin committed
127
128
129
    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
130

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

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

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

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

armin's avatar
ds    
armin committed
144
145
146
147
148
149
150
151
152
153
    mMessageService = new MessageService( this, mStorage, this, mEmojiRepo );
    mChannelService = new RocketChatChannelService( this, this, mMessageService );
    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
154
155


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

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

        if ( !tokenDb.first.isEmpty() && tokenDb.second > currentTime.toTime_t() ) {
            loginWithToken( userDb, tokenDb.first );
        } else {
armin's avatar
ad    
armin committed
166
            wipeDbAndReconnect();
armin's avatar
sd    
armin committed
167
            emit loggedOut( mServerId );
armin's avatar
armin committed
168
        }
armin's avatar
ds    
armin committed
169
    } else if ( !lastServer.isEmpty() ) {
armin's avatar
ad    
armin committed
170
        wipeDbAndReconnect();
armin's avatar
ds    
armin committed
171
172
        emit loggedOut( mServerId );
    }
armin's avatar
armin committed
173

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

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
195
196
197
198
199
    /* QFile file(":/emoji.json");
     if(!file.open(QFile::ReadOnly)){
         qDebug()<<file.errorString();
         qDebug()<<file.error();
     }
armin's avatar
armin committed
200

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

armin's avatar
armin committed
203
     QJsonObject obj = doc.object();
armin's avatar
armin committed
204

armin's avatar
armin committed
205
206
207
208
209
210
211
212
213
214
215
216
     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
217
    auto emojiModel = Models::getEmojisModel();
armin's avatar
armin committed
218

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

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

        auto category = emoji->getCategory();

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

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

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

armin's avatar
as    
armin committed
241
242
243
    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
244
245
246
    }
}

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

253
254
    QSharedPointer<RocketChatChannel> channel = nullptr;

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

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

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

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

armin's avatar
ias    
armin committed
282
void RocketChatServerData::switchChannelByName( const QString &pName, const QString &pType )
armin's avatar
armin committed
283
{
armin's avatar
armin committed
284
285
286
    if ( mLoggedIn ) {
        if ( mChannels->getChannelByName( pName ) ) {
            auto channel = mChannels->getChannelByName( pName );
287
            switchChannel( QStringLiteral( "default" ), channel->getRoomId(), pName, channel->getType() );
armin's avatar
armin committed
288
            mPendingSwitchRoomRequest.clear();
armin's avatar
ias    
armin committed
289
            mPendingSwitchRoomRequestType.clear();
armin's avatar
armin committed
290
        } else {
armin's avatar
ias    
armin committed
291
292
293
294
295
296
297
            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 );
                }
            };
298
            auto request = QSharedPointer<RocketChatGetRoomByNameAndType>::create( pName, pType );
armin's avatar
ias    
armin committed
299
300
301
            request->setSuccess( success );
            sendDdprequest( request );
            //searchForRoomIdByName( pName );
armin's avatar
armin committed
302
        }
armin's avatar
armin committed
303
    } else {
armin's avatar
armin committed
304
        mPendingSwitchRoomRequest = pName;
armin's avatar
ias    
armin committed
305
        mPendingSwitchRoomRequestType = pType;
armin's avatar
armin committed
306
307
308
    }
}

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

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

armin's avatar
armin committed
320

armin's avatar
armin committed
321
322
            QVariantList mutedList;

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

armin's avatar
armin committed
327
328
            QVariantMap details;

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

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

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

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

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

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

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

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

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

armin's avatar
armin committed
381
void RocketChatServerData::createVideoCall( const QString &pRid )
armin's avatar
armin committed
382
{
armin's avatar
armin committed
383
    if ( !pRid.isEmpty() ) {
armin's avatar
armin committed
384

385
        auto request = QSharedPointer<RocketChatUpdateJitsiTimeout>::create( pRid );
armin's avatar
sad    
armin committed
386
        sendDdprequest( request );
armin's avatar
armin committed
387
        joinJitsiCall( pRid );
armin's avatar
armin committed
388
389
390
    }
}

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

armin's avatar
ias    
armin committed
397
398
        if ( data.contains( QStringLiteral( "result" ) ) ) {
            QJsonObject result = data[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
399

400
401
            if ( result.contains( QStringLiteral( "users" ) ) ) {
                QJsonArray users = result[QStringLiteral( "users" )].toArray();
armin's avatar
armin committed
402

armin's avatar
as    
armin committed
403
                for ( auto user : users ) {
armin's avatar
armin committed
404
                    QJsonObject userObj = user.toObject();
armin's avatar
armin committed
405

406
407
                    if ( userObj.contains( QStringLiteral( "username" ) ) && userObj.contains( QStringLiteral( "_id" ) ) ) {
                        QString username = userObj[QStringLiteral( "username" )].toString();
armin's avatar
armin committed
408
409

                        if ( !username.compare( pName, Qt::CaseInsensitive ) ) {
410
411
                            QString id = userObj[QStringLiteral( "_id" )].toString();
                            switchChannel( QStringLiteral( "default" ), id, pName, "d" );
armin's avatar
armin committed
412
413
414
                        }
                    }
                }
415
416
            } else if ( result.contains( QStringLiteral( "rooms" ) ) ) {
                QJsonArray rooms = result[QStringLiteral( "rooms" )].toArray();
armin's avatar
armin committed
417

armin's avatar
as    
armin committed
418
                for ( const auto room : rooms ) {
armin's avatar
armin committed
419
                    QJsonObject roomObj = room.toObject();
armin's avatar
armin committed
420

421
422
                    if ( roomObj.contains( QStringLiteral( "name" ) ) && roomObj.contains( QStringLiteral( "_id" ) ) ) {
                        QString roomname = roomObj[QStringLiteral( "name" )].toString();
armin's avatar
armin committed
423
424

                        if ( !roomname.compare( pName, Qt::CaseInsensitive ) ) {
425
426
                            QString id = roomObj[QStringLiteral( "_id" )].toString();
                            switchChannel( QStringLiteral( "default" ), id, pName, "c" );
armin's avatar
armin committed
427
428
429
430
431
                        }
                    }
                }
            }
        }
armin's avatar
armin committed
432

armin's avatar
armin committed
433
        mPendingSwitchRoomRequest.clear();
armin's avatar
ias    
armin committed
434
        mPendingSwitchRoomRequestType.clear();
armin's avatar
armin committed
435
436

    };
armin's avatar
armin committed
437
    request->setSuccess( success );
armin's avatar
ias    
armin committed
438
    sendApiRequest( request );
armin's avatar
armin committed
439
440
}

441
442
void RocketChatServerData::blockUser( const QString &pChannelId )
{
armin's avatar
armin committed
443
444
    auto userId = pChannelId;
    userId.replace( mUserId, "" );
armin's avatar
armin committed
445
446
447
448

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

450
451
452
        auto blockRequest1 = QSharedPointer<RocketChatBlockUserRequest>::create( possibleRoom1, userId );
        auto blockRequest2 = QSharedPointer<RocketChatBlockUserRequest>::create( possibleRoom2, userId );

armin's avatar
sad    
armin committed
453
454
        sendDdprequest( blockRequest1 );
        sendDdprequest( blockRequest2 );
455
456


armin's avatar
armin committed
457
    } else {
458
        auto blockRequest = QSharedPointer<RocketChatBlockUserRequest>::create( pChannelId, userId );
armin's avatar
sad    
armin committed
459
        sendDdprequest( blockRequest );
armin's avatar
armin committed
460
    }
461
462
463
464
}

void RocketChatServerData::unBlockUser( const QString &pChannelId )
{
armin's avatar
armin committed
465
466
    auto userId = pChannelId;
    userId.replace( mUserId, "" );
armin's avatar
armin committed
467
468
469
470

    if ( userId == pChannelId ) {
        QString possibleRoom1 = userId + mUserId;
        QString possibleRoom2 = mUserId + userId;
471
472
        auto unBlockRequest1 = QSharedPointer<RocketChatUnblockUserRequest>::create( possibleRoom1, userId );
        auto unBlockRequest2 = QSharedPointer<RocketChatUnblockUserRequest>::create( possibleRoom2, userId );
armin's avatar
sad    
armin committed
473
474
        sendDdprequest( unBlockRequest1 );
        sendDdprequest( unBlockRequest2 );
armin's avatar
armin committed
475
    } else {
476
        auto unBlockRequest = QSharedPointer<RocketChatUnblockUserRequest>::create( pChannelId, userId );
armin's avatar
sad    
armin committed
477
        sendDdprequest( unBlockRequest );
armin's avatar
armin committed
478
    }
479
480
}

armin's avatar
armin committed
481
482
483
484
485
QString RocketChatServerData::getUserId() const
{
    return mUserId;
}

armin's avatar
armin committed
486
void RocketChatServerData::setUserId( const QString &userId )
armin's avatar
armin committed
487
{
armin's avatar
armin committed
488
489
490
    auto users = Models::getUsersModel();
    auto user = users->getUserById( userId );

armin's avatar
sd    
armin committed
491
    if ( user.isNull() && !userId.isEmpty() ) {
492
        user = QSharedPointer<RocketChatUser>::create( userId ) ; //QSharedPointer<RocketChatUser>( new RocketChatUser( userId ) );
armin's avatar
as    
armin committed
493
494
        auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, user ) );
        Q_ASSERT( result );
armin's avatar
armin committed
495
496
    }

armin's avatar
ds    
armin committed
497
498
    if ( !user.isNull() && !userId.isEmpty() ) {
        mUserId = userId;
armin's avatar
armin committed
499

armin's avatar
ds    
armin committed
500
501
502
503
        connect( user.data(), &RocketChatUser::statusChanged, this, [ = ]() {
            emit userStatusChanged( static_cast<int>( user->getStatus() ) );
        } );
    }
armin's avatar
armin committed
504
505
}

armin's avatar
armin committed
506
507
void RocketChatServerData::disconnectFromServer()
{
armin's avatar
armin committed
508
    mConnectionState = ConnectionState::OFFLINE;
armin's avatar
armin committed
509
510
511
    mDdpApi->disconnectFromServer();
}

armin's avatar
armin committed
512
513
514
515
void RocketChatServerData::loadHistories()
{
    QStringList channelIds;

armin's avatar
armin committed
516
517
    for ( const auto &currentChannel : mChannels->getElements() ) {
        if ( Q_LIKELY( !currentChannel.isNull() && currentChannel->getJoined() ) ) {
armin's avatar
armin committed
518
519
520
521
            //joinChannel( currentChannel->getRoomId() );
            channelIds.append( currentChannel->getRoomId() );
        }
    }
armin's avatar
armin committed
522

523
524
    auto request = QSharedPointer<LoadHistoryServiceRequest>::create( channelIds, 1 );

armin's avatar
armin committed
525
    request->setLimit( 1 );
armin's avatar
armin committed
526
    request->setSuccess( historyLoaded );
armin's avatar
armin committed
527
    request->setNoLastThreeDays( true );
armin's avatar
armin committed
528
529
530
    mMessageService->loadHistory( request );
}

armin's avatar
ias    
armin committed
531
void RocketChatServerData::loadHistoryTill( const QString &pChannelId, qint64 pTs )
armin's avatar
armin committed
532
533
534
{

    std::function<void ( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages )> success = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) {
armin's avatar
ias    
armin committed
535
        historyLoaded( messages );
armin's avatar
armin committed
536
    };
537
    auto request = QSharedPointer<LoadHistoryServiceRequest>::create( pChannelId );
armin's avatar
ias    
armin committed
538
539
540
541
542
543
    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
544

armin's avatar
ias    
armin committed
545
    mMessageService->loadHistory( request );
armin's avatar
armin committed
546
547
}

armin's avatar
armin committed
548
void RocketChatServerData::onUsersLoaded( const QString &pChannelId, const QVector<QSharedPointer<RocketChatUser> > &pUserList )
armin's avatar
armin committed
549
550
551
{
    if ( Q_LIKELY( mChannels->contains( pChannelId ) ) ) {
        auto channel = mChannels->get( pChannelId );
armin's avatar
armin committed
552
553
554
555

        if ( !channel.isNull() ) {
            for ( const auto &user : pUserList ) {
                if ( !user.isNull() ) {
armin's avatar
armin committed
556
                    channel->addUser( user );
armin's avatar
as    
armin committed
557
558
                    auto result = QMetaObject::invokeMethod( userModel, "insertUser", Q_ARG( QString, pChannelId ), Q_ARG( User, user ) );
                    Q_ASSERT( result );
armin's avatar
armin committed
559
560
561
562
563
                }
            }
        }
    }
}
armin's avatar
armin committed
564

armin's avatar
armin committed
565
566
567
void RocketChatServerData::onUnreadCountChanged()
{
    uint number = 0;
armin's avatar
armin committed
568
569
570
571
    qDebug() << "on Unread count changed";

    for ( const auto &channel : mChannels->getElements() ) {
        if ( !channel.isNull() ) {
armin's avatar
armin committed
572
            number += channel->getUnreadMessages();
armin's avatar
armin committed
573
574
        }
    }
armin's avatar
armin committed
575

576
    emit unreadCountChanged( QStringLiteral( "default" ), number );
armin's avatar
armin committed
577
578
}

armin's avatar
cleanup    
armin committed
579
bool RocketChatServerData::handlesMessage( const QJsonObject &message )
580
581
{
    qDebug() << "RocketChatServerData message handler";
armin's avatar
armin committed
582
    onDDPMessageReceived( message );
583
584
585
    return true;
}

armin's avatar
armin committed
586
587
588
589
590
EmojiRepo *RocketChatServerData::getEmojiRepo() const
{
    return mEmojiRepo;
}

armin's avatar
armin committed
591
void RocketChatServerData::setEmojiRepo( EmojiRepo *emojiRepo )
armin's avatar
armin committed
592
593
594
595
{
    mEmojiRepo = emojiRepo;
}

armin's avatar
armin committed
596
void RocketChatServerData::createAccount( const QString &username, const QString &email, const QString &password )
597
{
598
    auto request = QSharedPointer<RocketChatCreateAccount>::create( username, email, password );
armin's avatar
armin committed
599
    sendDdprequest( request );
600
601
}

armin's avatar
armin committed
602
603
604
605
void RocketChatServerData::offlineLogin()
{
    QString userId = mStorage->getUserId();
    QString userName = mStorage->getUserName();
armin's avatar
armin committed
606
607

    if ( !userId.isEmpty() && !userName.isEmpty() ) {
armin's avatar
isad    
armin committed
608
        setUserId( userId );
609
        emit loggedIn( QStringLiteral( "default" ) );
armin's avatar
armin committed
610
611
612
    }
}

armin's avatar
ad    
armin committed
613
614
615
616
617
618
619
void RocketChatServerData::wipeDbAndReconnect()
{
    disconnectFromServer();
    mStorage->wipeDb();
    resume();
}

armin's avatar
isad    
armin committed
620
621
622
623
624
625
626
627
628
629
QSharedPointer<RocketChatUser> RocketChatServerData::getOwnUser() const
{
    return mOwnUser;
}

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

Dennis Beier's avatar
Dennis Beier committed
630
631
QString RocketChatServerData::getBaseUrl() const
{
armin's avatar
armin committed
632
633
634
635
    QString protocol = "https://";

    if ( mDdpApi->getUnsecure() ) {
        protocol = "http://";
636
    }
armin's avatar
armin committed
637
638

    return protocol + mBaseUrl;
Dennis Beier's avatar
Dennis Beier committed
639
640
}

armin's avatar
armin committed
641
void RocketChatServerData::login( const QString &pUsername, const QString &pPassword )
armin's avatar
armin committed
642
{
armin's avatar
ad    
armin committed
643
    QString pass;
armin's avatar
armin committed
644

armin's avatar
ad    
armin committed
645
646
    QByteArray hashArray = QCryptographicHash::hash( pPassword.toUtf8(), QCryptographicHash::Sha256 );
    pass = hashArray.toHex();
armin's avatar
armin committed
647

armin's avatar
ad    
armin committed
648
    loginWithHash( pUsername, pass );
armin's avatar
armin committed
649
650
}

armin's avatar
armin committed
651
void RocketChatServerData::loginWithHash( const QString &pUsername, const QString &pPswHash )
armin's avatar
armin committed
652
653
654
{
    qDebug() << "login with hash";

armin's avatar
ad    
armin committed
655
656
657
658
659
660
661
662
663
664
665
666
    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();
        }
667

armin's avatar
ad    
armin committed
668
    };
669
670
    auto request = QSharedPointer<DDPLoginRequest>::create( pUsername, pPswHash );

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

674
675
        if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
            QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
676

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

                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
699
700
701
702
703
704
705
706
707
708
            }
        }
    };
    DdpCallback error = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
        Q_UNUSED( pResponse );
        onLoginError();
    };

    request->setSuccess( success );
    request->setError( error );
armin's avatar
armin committed
709
    sendDdprequest( request, true, true );
armin's avatar
armin committed
710
    emit loggingIn();
armin's avatar
armin committed
711
712
713
    this->mUsername = pUsername;
}

armin's avatar
armin committed
714
void RocketChatServerData::loginWithToken( const QString &pUsername, const QString &pToken, bool pResume )
armin's avatar
armin committed
715
716
717
718
719
720
721
{
    Q_UNUSED( pResume );
    qDebug() << "login with token";

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

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

725
            if ( data.contains( QStringLiteral( "username" ) ) ) {
726
                self->mUsername = data[QStringLiteral( "username" )].toString();
armin's avatar
armin committed
727
                self->mStorage->setUserName( self->mUsername );
728
729
                self->onResume();
                self->onDDPAuthenticated();
armin's avatar
armin committed
730
731
732
733
            }

        };

734
        auto request = QSharedPointer<DDPLoginRequest>::create( pToken );
armin's avatar
sd    
armin committed
735
        DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
armin's avatar
armin committed
736
            qDebug() << "authenticated";
armin's avatar
sd    
armin committed
737
            mConnectionState = ConnectionState::ONLINE;
armin's avatar
armin committed
738

739
740
            if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
                QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
741

742
                if ( result.contains( QStringLiteral( "token" ) ) && result.contains( QStringLiteral( "id" ) ) ) {
armin's avatar
sd    
armin committed
743
                    mResumeToken = result[QStringLiteral( "token" )].toString();
744
745
                    QJsonObject expireObject = result[QStringLiteral( "tokenExpires" )].toObject();
                    double expireDouble = expireObject[QStringLiteral( "$date" )].toDouble();
armin's avatar
sd    
armin committed
746
                    mTokenExpire = static_cast<uint>( expireDouble / 1000 );
armin's avatar
armin committed
747

748
                    QString userId = result[QStringLiteral( "id" )].toString();
armin's avatar
sd    
armin committed
749
750
751
752
753
754
755
                    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
756

armin's avatar
armin committed
757
                    RestApiRequest meRequest
armin's avatar
armin committed
758
                        = RestApiRequest( new restMeRequest( meCallBackSuccess ) );
armin's avatar
armin committed
759

armin's avatar
sd    
armin committed
760
                    mRestApi->sendRequest( meRequest );
armin's avatar
armin committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
                }
            }
        };
        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 );
784
#ifdef Q_OS_IOS
armin's avatar
armin committed
785
        mDdpApi->sendRequest( request );
786
#else
armin's avatar
armin committed
787
        sendDdprequest( request, true, true );
788
#endif
armin's avatar
armin committed
789
790
        emit loggingIn();

armin's avatar
armin committed
791
792
793
794
795
796
        this->mUsername = pUsername;
    } else {
        qDebug() << "empty token";
    }
}

armin's avatar
armin committed
797
798
void RocketChatServerData::loginWtihSamlToken( const QString &pToken )
{
armin's avatar
armin committed
799
    if ( !pToken.isEmpty() ) {
armin's avatar
armin committed
800
        qDebug() << pToken;
armin's avatar
armin committed
801
802
        
        RestRequestCallback meCallBackSuccess = [ = ]( QNetworkReply *, QJsonObject data, RestApi * ) {
armin's avatar
armin committed
803

804
805
            if ( data.contains( QStringLiteral( "username" ) ) ) {
                mUsername = data[QStringLiteral( "username" )].toString();
armin's avatar
armin committed
806
                mStorage->setUserName( mUsername );
armin's avatar
armin committed
807
808
809
810
811
812
                onResume();
                this->onDDPAuthenticated();
            }
            
        };
        
813
        auto request = QSharedPointer<ddpSamlLoginRequest>::create( pToken );
armin's avatar
ias    
armin committed
814
        DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
armin's avatar
armin committed
815
816
817
            qDebug() << "authenticated";
            mConnectionState = ConnectionState::ONLINE;
            
818
819
            if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
                QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
820
                
821
822
823
824
                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
825
                    mTokenExpire = static_cast<uint>( expireDouble / 1000 );
armin's avatar
armin committed
826
                    
827
                    QString userId = result[QStringLiteral( "id" )].toString();
armin's avatar
armin committed
828
                    setUserId( userId );
armin's avatar
armin committed
829
830
831
832
833
834
835
836
837
                    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
838
839
840
                        = RestApiRequest( new restMeRequest( meCallBackSuccess ) );

                    mRestApi->sendRequest( meRequest );
armin's avatar
armin committed
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
                    
                    
                    
                }
            }
        };
        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
856
        sendDdprequest( request, true, true );
armin's avatar
armin committed
857
858
        emit loggingIn();

armin's avatar
armin committed
859
860
861
862
863
    } else {
        qDebug() << "empty token";
    }
}

armin's avatar
armin committed
864
void RocketChatServerData::loginWithOpenIDToken( const QString &pToken, const QString &pSecret )
armin's avatar
armin committed
865
866
{
    if ( !pToken.isEmpty() && !pSecret.isEmpty() ) {
867
        auto self = this;
868
        RestRequestCallback meCallBackSuccess = [ self ]( QNetworkReply *, QJsonObject data, RestApi * ) {
armin's avatar
armin committed
869

870
            if ( data.contains( QStringLiteral( "username" ) ) ) {
871
872
                self->mUsername = data[QStringLiteral( "username" )].toString();
                self->mStorage->transaction();
armin's avatar
armin committed
873
874
875
                self->mStorage->setUserData( self->mUsername, "" );
                self->mStorage->setToken( self->mResumeToken, self->mTokenExpire );
                self->mStorage->setUserId( self->mUserId );
armin's avatar
ad    
armin committed
876
                self->mStorage->askForcommit();
877
878
                self->onResume();
                self->onDDPAuthenticated();
armin's avatar
armin committed
879
880
881
882
            }

        };

883
        auto request = QSharedPointer<DDPOpenIDLoginRequest>::create( pToken, pSecret );
armin's avatar
ias    
armin committed
884
        DdpCallback success = [ = ]( QJsonObject pResponse, MeteorDDP * ) {
armin's avatar
armin committed
885
            qDebug() << "authenticated";
886
            self->mConnectionState = ConnectionState::ONLINE;
armin's avatar
armin committed
887

888
889
            if ( pResponse.contains( QStringLiteral( "result" ) ) ) {
                QJsonObject result = pResponse[QStringLiteral( "result" )].toObject();
armin's avatar
armin committed
890

891
                if ( result.contains( QStringLiteral( "token" ) ) && result.contains( QStringLiteral( "id" ) ) ) {
armin's avatar
sd    
armin committed
892
893
                    mConnectionState = ConnectionState::ONLINE;
                    mResumeToken = result[QStringLiteral( "token" )].toString();
894
895
                    QJsonObject expireObject = result[QStringLiteral( "tokenExpires" )].toObject();
                    double expireDouble = expireObject[QStringLiteral( "$date" )].toDouble();
armin's avatar
sd    
armin committed
896
                    mTokenExpire = static_cast<uint>( expireDouble / 1000 );
armin's avatar
armin committed
897

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

armin's avatar
sd    
armin committed
900
901
902
903
904
                    setUserId( userId );
                    mRestApi->setToken( mResumeToken );
                    mRestApi->setUserId( userId );
                    mDdpApi->setToken( mResumeToken );
                    mDdpApi->unsetResponseBinding( request->getFrame() );
armin's avatar
armin committed
905

armin's avatar
sd    
armin committed
906
                    // emit onHashLoggedIn( self->mServerId ) ;
armin's avatar
armin committed
907
908

                    RestApiRequest meRequest
armin's avatar
armin committed
909
                        = RestApiRequest( new restMeRequest( meCallBackSuccess ) );
armin's avatar
armin committed
910

911
                    self->mRestApi->sendRequest( meRequest );
armin's avatar
armin committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925

                }
            }
        };
        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
926
        sendDdprequest( request, true, true );
armin's avatar
armin committed
927
928
        emit loggingIn();

armin's avatar
armin committed
929
930
931
932
933
934

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

armin's avatar
armin committed
935
void RocketChatServerData::loginWithMethod( const QString &method, const QString &payload )
armin's avatar
armin committed
936
{
armin's avatar
armin committed
937
    if ( mLoginMethodRepo.contains( method ) ) {
armin's avatar
armin committed
938
        LoginMethod loginMethod = mLoginMethodRepo[method];
armin's avatar
armin committed
939
940

        if ( loginMethod.getType() == LoginMethod::Type::OPENID ) {
armin's avatar
armin committed
941
942
            QString token;
            QString secret;
armin's avatar
armin committed
943
            QJsonDocument doc = QJsonDocument::fromJson( payload.toUtf8() );
armin's avatar
armin committed
944
            QJsonObject obj = doc.object();
armin's avatar
armin committed
945

946
947
948
            if ( obj.contains( QStringLiteral( "credentialToken" ) ) && obj.contains( QStringLiteral( "credentialSecret" ) ) ) {
                token = obj[QStringLiteral( "credentialToken" )].toString();
                secret = obj[QStringLiteral( "credentialSecret" )].toString();
armin's avatar
armin committed
949
950
                // if app went offline during webview
                disconnectFromServer();