File indexing completed on 2024-12-01 13:09:12
0001 /* 0002 0003 * SPDX-FileCopyrightText: 2016 Riccardo Iaconelli <riccardo@kde.org> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.0-or-later 0006 * 0007 */ 0008 0009 #include "messagequeue.h" 0010 #include "ddpapi/ddpauthenticationmanager.h" 0011 #include "ddpapi/ddpclient.h" 0012 #include "rocketchataccount.h" 0013 0014 #include <QCborValue> 0015 #include <QDataStream> 0016 #include <QDir> 0017 #include <QJsonArray> 0018 #include <QPair> 0019 0020 MessageQueue::MessageQueue(RocketChatAccount *account, QObject *parent) 0021 : QObject(parent) 0022 , mRocketChatAccount(account) 0023 { 0024 } 0025 0026 MessageQueue::~MessageQueue() 0027 { 0028 } 0029 0030 void MessageQueue::loadCache() 0031 { 0032 connect(mRocketChatAccount->ddp()->authenticationManager(), &DDPAuthenticationManager::loginStatusChanged, this, &MessageQueue::onLoginStatusChanged); 0033 } 0034 0035 QPair<QString, QJsonDocument> MessageQueue::fromJson(const QJsonObject &object) 0036 { 0037 QPair<QString, QJsonDocument> pair; 0038 0039 pair.first = object[QLatin1String("method")].toString(); 0040 QJsonArray arr = object[QLatin1String("params")].toArray(); 0041 pair.second = QJsonDocument(arr); 0042 return pair; 0043 } 0044 0045 QByteArray MessageQueue::serialize(const QPair<QString, QJsonDocument> &pair) 0046 { 0047 QJsonObject o; 0048 0049 o[QLatin1String("method")] = QJsonValue(pair.first); 0050 0051 QJsonArray arr; 0052 if (pair.second.isArray()) { 0053 arr.append(pair.second.array()); 0054 } else if (pair.second.isObject()) { 0055 arr.append(pair.second.object()); 0056 } 0057 0058 o[QLatin1String("params")] = QJsonValue(arr); 0059 0060 return QCborValue::fromJsonValue(o).toCbor(); 0061 } 0062 0063 void MessageQueue::onLoginStatusChanged() 0064 { 0065 // retry sending messages 0066 // TODO: check login status? 0067 processQueue(); 0068 } 0069 0070 void MessageQueue::processQueue() 0071 { 0072 // can be optimized using single shot timer 0073 while (mRocketChatAccount->loginStatus() == DDPAuthenticationManager::LoggedIn && !mRocketChatAccount->ddp()->messageQueue().empty()) { 0074 const QPair<QString, QJsonDocument> pair = mRocketChatAccount->ddp()->messageQueue().head(); 0075 const QString method = pair.first; 0076 const QJsonDocument params = pair.second; 0077 mRocketChatAccount->ddp()->method(method, params); 0078 } 0079 } 0080 0081 #include "moc_messagequeue.cpp"