File indexing completed on 2024-04-14 15:03:47

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "managechannels.h"
0008 
0009 #include "channelgroupbasejob.h"
0010 #include "channels/channeljoinjob.h"
0011 #include "channels/channelopenjob.h"
0012 #include "connection.h"
0013 #include "groups/groupopenjob.h"
0014 #include "ruqola.h"
0015 #include "ruqola_debug.h"
0016 
0017 ManageChannels::ManageChannels(RocketChatAccount *account, QObject *parent)
0018     : QObject(parent)
0019     , mAccount(account)
0020 {
0021 }
0022 
0023 ManageChannels::~ManageChannels() = default;
0024 
0025 ManageChannels::SearchChannelFound ManageChannels::searchOpenChannels(const QString &roomId)
0026 {
0027     ManageChannels::SearchChannelFound result = ManageChannels::SearchChannelFound::NoFound;
0028     const auto roomModel = mAccount->roomModel();
0029     for (int roomIdx = 0, nRooms = roomModel->rowCount(); roomIdx < nRooms; ++roomIdx) {
0030         const auto roomModelIndex = roomModel->index(roomIdx, 0);
0031         const auto identifier = roomModelIndex.data(RoomModel::RoomId).toString();
0032         if (identifier == roomId) {
0033             if (roomModelIndex.data(RoomModel::RoomOpen).toBool()) {
0034                 result = ManageChannels::SearchChannelFound::ChannelOpened;
0035                 Q_EMIT selectRoomByRoomIdRequested(roomId);
0036             } else {
0037                 result = ManageChannels::SearchChannelFound::ChannelHidden;
0038             }
0039             break;
0040         }
0041     }
0042     return result;
0043 }
0044 
0045 RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo ManageChannels::generateGroupInfo(const QString &roomId, RocketChatAccount::ChannelTypeInfo typeInfo)
0046 {
0047     RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo info;
0048     switch (typeInfo) {
0049     case RocketChatAccount::ChannelTypeInfo::RoomId:
0050         info.channelGroupInfoType = RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfoType::Identifier;
0051         break;
0052     case RocketChatAccount::ChannelTypeInfo::RoomName:
0053         info.channelGroupInfoType = RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfoType::Name;
0054         break;
0055     }
0056     info.identifier = roomId;
0057     return info;
0058 }
0059 
0060 void ManageChannels::channelJoin(const RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo &channelInfo, const QString &joinCode)
0061 {
0062     auto job = new RocketChatRestApi::ChannelJoinJob(this);
0063     mAccount->restApi()->initializeRestApiJob(job);
0064     job->setJoinCode(joinCode);
0065     job->setChannelGroupInfo(channelInfo);
0066     connect(job, &RocketChatRestApi::ChannelJoinJob::setChannelJoinDone, this, &ManageChannels::setChannelJoinDone);
0067     connect(job, &RocketChatRestApi::ChannelJoinJob::missingChannelPassword, this, &ManageChannels::missingChannelPassword);
0068     connect(job, &RocketChatRestApi::ChannelJoinJob::openArchivedRoom, this, &ManageChannels::openArchivedRoom);
0069     if (!job->start()) {
0070         qCDebug(RUQOLA_LOG) << "Impossible to start setChannelJoin";
0071     }
0072 }
0073 
0074 void ManageChannels::openPrivateGroup(const QString &roomId, RocketChatAccount::ChannelTypeInfo typeInfo)
0075 {
0076     const ManageChannels::SearchChannelFound result = searchOpenChannels(roomId);
0077     if (result == ManageChannels::SearchChannelFound::ChannelOpened) {
0078         return;
0079     }
0080 
0081     const RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo info = generateGroupInfo(roomId, typeInfo);
0082     qCDebug(RUQOLA_LOG) << "opening group" << roomId;
0083     switch (result) {
0084     case ManageChannels::SearchChannelFound::ChannelOpened:
0085         // Nothing
0086         break;
0087     case ManageChannels::SearchChannelFound::NoFound: {
0088         // TODO verify
0089         channelJoin(info, QString());
0090         break;
0091     }
0092     case ManageChannels::SearchChannelFound::ChannelHidden: {
0093         auto job = new RocketChatRestApi::GroupOpenJob(this);
0094         job->setChannelGroupInfo(info);
0095         mAccount->restApi()->initializeRestApiJob(job);
0096         connect(job,
0097                 &RocketChatRestApi::GroupOpenJob::groupOpenDone,
0098                 this,
0099                 [this](const QJsonObject &, const RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo &channelInfo) {
0100                     Q_EMIT selectRoomByRoomIdRequested(channelInfo.identifier);
0101                 });
0102         if (!job->start()) {
0103             qCWarning(RUQOLA_LOG) << "Impossible to start GroupOpenJob job";
0104         }
0105         break;
0106     }
0107     }
0108 }
0109 
0110 void ManageChannels::openChannel(const QString &roomId, RocketChatAccount::ChannelTypeInfo typeInfo)
0111 {
0112     const ManageChannels::SearchChannelFound result = searchOpenChannels(roomId);
0113     if (result == ManageChannels::SearchChannelFound::ChannelOpened) {
0114         return;
0115     }
0116     const RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo info = generateGroupInfo(roomId, typeInfo);
0117     qCDebug(RUQOLA_LOG) << "opening channel" << roomId;
0118     switch (result) {
0119     case ManageChannels::SearchChannelFound::ChannelOpened:
0120         // Nothing
0121         break;
0122     case ManageChannels::SearchChannelFound::NoFound: {
0123         channelJoin(info, QString());
0124         break;
0125     }
0126     case ManageChannels::SearchChannelFound::ChannelHidden: {
0127         auto job = new RocketChatRestApi::ChannelOpenJob(this);
0128         job->setChannelGroupInfo(info);
0129         mAccount->restApi()->initializeRestApiJob(job);
0130         connect(job,
0131                 &RocketChatRestApi::ChannelOpenJob::channelOpenDone,
0132                 this,
0133                 [this](const QJsonObject &, const RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo &channelInfo) {
0134                     Q_EMIT selectRoomByRoomIdRequested(channelInfo.identifier);
0135                 });
0136         if (!job->start()) {
0137             qCWarning(RUQOLA_LOG) << "Impossible to start ChannelOpenJob job";
0138         }
0139         break;
0140     }
0141     }
0142 }
0143 
0144 void ManageChannels::setChannelJoinDone(const RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfo &channelInfo)
0145 {
0146     qCDebug(RUQOLA_LOG) << " setChannelJoinDone type:" << channelInfo.channelGroupInfoType << " channelInfo.identifier " << channelInfo.identifier;
0147     mAccount->ddp()->subscribeRoomMessage(channelInfo.identifier);
0148     // FIXME room is not added yet...
0149     switch (channelInfo.channelGroupInfoType) {
0150     case RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfoType::Unknown:
0151         qCWarning(RUQOLA_LOG) << "setChannelJoinDone : RocketChatRestApi::ChannelBaseJob::ChannelInfoType::Unknown";
0152         break;
0153     case RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfoType::Identifier:
0154         Q_EMIT selectRoomByRoomIdRequested(channelInfo.identifier);
0155         break;
0156     case RocketChatRestApi::ChannelGroupBaseJob::ChannelGroupInfoType::Name:
0157         Q_EMIT selectRoomByRoomNameRequested(channelInfo.identifier);
0158         break;
0159     }
0160 }
0161 
0162 #include "moc_managechannels.cpp"