File indexing completed on 2024-05-12 05:02:13

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "videoconferencemessageinfomanager.h"
0008 #include "connection.h"
0009 #include "rocketchataccount.h"
0010 #include "ruqola_videoconference_core_debug.h"
0011 #include "updatevideoconferencemessagejob.h"
0012 #include "video-conference/videoconferenceinfojob.h"
0013 #include <QTimer>
0014 #include <chrono>
0015 using namespace std::chrono_literals;
0016 
0017 VideoConferenceMessageInfoManager::VideoConferenceMessageInfoManager(RocketChatAccount *account, QObject *parent)
0018     : QObject{parent}
0019     , mRocketChatAccount(account)
0020     , mTimer(new QTimer(this))
0021 {
0022     mTimer->setSingleShot(true);
0023     mTimer->setInterval(500ms);
0024     connect(mTimer, &QTimer::timeout, this, &VideoConferenceMessageInfoManager::slotUpdateMessage);
0025 }
0026 
0027 VideoConferenceMessageInfoManager::~VideoConferenceMessageInfoManager() = default;
0028 
0029 RocketChatAccount *VideoConferenceMessageInfoManager::rocketChatAccount() const
0030 {
0031     return mRocketChatAccount;
0032 }
0033 
0034 void VideoConferenceMessageInfoManager::addCallId(const QString &callId)
0035 {
0036     if (!mCallIdList.contains(callId)) {
0037         mCallIdList.append(callId);
0038     }
0039 
0040     if (!mCallIdList.isEmpty()) {
0041         mTimer->start();
0042     }
0043 }
0044 
0045 void VideoConferenceMessageInfoManager::slotUpdateMessage()
0046 {
0047     if (!mCallIdList.isEmpty()) {
0048         const QString callId = mCallIdList.takeFirst();
0049         updateVideoConferenceInfo(callId);
0050     }
0051 }
0052 
0053 void VideoConferenceMessageInfoManager::updateVideoConferenceInfo(const QString &callId)
0054 {
0055     auto conferenceInfoJob = new RocketChatRestApi::VideoConferenceInfoJob(this);
0056     conferenceInfoJob->setCallId(callId);
0057     mRocketChatAccount->restApi()->initializeRestApiJob(conferenceInfoJob);
0058     connect(conferenceInfoJob, &RocketChatRestApi::VideoConferenceInfoJob::videoConferenceInfoDone, this, [this](const QJsonObject &videoConfObj) {
0059         VideoConferenceInfo info;
0060         info.parse(videoConfObj);
0061         UpdateVideoConferenceMessageJob *job = new UpdateVideoConferenceMessageJob(this);
0062         job->setRocketChatAccount(mRocketChatAccount);
0063         job->setVideoConferenceInfo(info);
0064         job->start();
0065     });
0066     if (!conferenceInfoJob->start()) {
0067         qCWarning(RUQOLA_VIDEO_CONFERENCE_LOG) << "Impossible to start VideoConferenceInfoJob job";
0068     }
0069     if (!mCallIdList.isEmpty()) {
0070         mTimer->start();
0071     }
0072 }
0073 
0074 #include "moc_videoconferencemessageinfomanager.cpp"