File indexing completed on 2024-05-12 05:04:11

0001 // SPDX-FileCopyrightText: 2023 Rishi Kumar <rsi.dev17@gmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "iprulestoolmodel.h"
0005 
0006 #include "abstractaccount.h"
0007 #include "accountmanager.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 using namespace Qt::Literals::StringLiterals;
0012 
0013 IpRulesToolModel::IpRulesToolModel(QObject *parent)
0014     : QAbstractListModel(parent)
0015 {
0016     filltimeline();
0017 }
0018 
0019 QVariant IpRulesToolModel::data(const QModelIndex &index, int role) const
0020 {
0021     Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
0022 
0023     const auto &ipInfo = m_ipinfo[index.row()];
0024 
0025     switch (role) {
0026     case IdRole:
0027         return ipInfo.id();
0028     case IpRole:
0029         return ipInfo.ip();
0030     case SeverityRole:
0031         return ipInfo.severity();
0032     case CommentRole:
0033         return ipInfo.comment();
0034     case CreatedAtRole:
0035         return ipInfo.createdAt();
0036     case ExpiredAtRole:
0037         return ipInfo.expiresAt();
0038     default:
0039         return {};
0040     }
0041 }
0042 
0043 bool IpRulesToolModel::loading() const
0044 {
0045     return m_loading;
0046 }
0047 
0048 void IpRulesToolModel::setLoading(bool loading)
0049 {
0050     if (m_loading == loading) {
0051         return;
0052     }
0053     m_loading = loading;
0054     Q_EMIT loadingChanged();
0055 }
0056 
0057 int IpRulesToolModel::rowCount(const QModelIndex &parent) const
0058 {
0059     return parent.isValid() ? 0 : m_ipinfo.count();
0060 }
0061 
0062 QHash<int, QByteArray> IpRulesToolModel::roleNames() const
0063 {
0064     return {
0065         {IdRole, "id"},
0066         {IpRole, "ip"},
0067         {SeverityRole, "severity"},
0068         {CommentRole, "comment"},
0069         {CreatedAtRole, "createdAt"},
0070         {ExpiredAtRole, "expiredAt"},
0071     };
0072 }
0073 
0074 void IpRulesToolModel::newIpBlock(const QString &ip, const int expiresIn, const QString &comment, const QString &severity)
0075 {
0076     const QJsonObject obj{
0077         {QStringLiteral("ip"), ip},
0078         {QStringLiteral("severity"), severity},
0079         {QStringLiteral("comment"), comment},
0080         {QStringLiteral("expires_in"), expiresIn},
0081     };
0082 
0083     const auto doc = QJsonDocument(obj);
0084 
0085     const auto account = AccountManager::instance().selectedAccount();
0086 
0087     const QUrl url = account->apiUrl(QStringLiteral("/api/v1/admin/ip_blocks"));
0088 
0089     account->post(url, doc, true, this, [=](QNetworkReply *reply) {
0090         auto doc = QJsonDocument::fromJson(reply->readAll());
0091         auto jsonObj = doc.object();
0092         auto newIpInfo = IpInfo::fromSourceData(jsonObj);
0093 
0094         beginInsertRows({}, m_ipinfo.size(), m_ipinfo.size());
0095         m_ipinfo += newIpInfo;
0096         endInsertRows();
0097     });
0098 }
0099 
0100 void IpRulesToolModel::updateIpBlock(const int row, const QString &ip, const QString &severity, const QString &comment, const int expiresAt)
0101 {
0102     const QJsonObject obj{
0103         {QStringLiteral("ip"), ip},
0104         {QStringLiteral("severity"), severity},
0105         {QStringLiteral("comment"), comment},
0106         {QStringLiteral("expires_in"), expiresAt},
0107     };
0108 
0109     const auto doc = QJsonDocument(obj);
0110 
0111     const auto account = AccountManager::instance().selectedAccount();
0112     auto &ipInfo = m_ipinfo[row];
0113     const auto ipBlockId = ipInfo.id();
0114 
0115     account->put(account->apiUrl(QStringLiteral("/api/v1/admin/ip_blocks/%1").arg(ipBlockId)), doc, true, this, [=, &ipInfo](QNetworkReply *reply) {
0116         const auto doc = QJsonDocument::fromJson(reply->readAll());
0117         const auto jsonObj = doc.object();
0118 
0119         if (!jsonObj.value("error"_L1).isUndefined()) {
0120             account->errorOccured(i18n("Error occured when making a PUT request to update the domain block."));
0121         }
0122         ipInfo.setIp(ip);
0123         ipInfo.setSeverity(severity);
0124         ipInfo.setComment(comment);
0125         ipInfo.setExpiredAt(expiresAt);
0126         Q_EMIT dataChanged(index(row, 0), index(row, 0));
0127     });
0128 }
0129 
0130 void IpRulesToolModel::deleteIpBlock(const int row)
0131 {
0132     const auto account = AccountManager::instance().selectedAccount();
0133     const auto &ipInfo = m_ipinfo[row];
0134     const auto ipBlockId = ipInfo.id();
0135 
0136     account->deleteResource(account->apiUrl(QStringLiteral("/api/v1/admin/ip_blocks/%1").arg(ipBlockId)), true, this, [=](QNetworkReply *reply) {
0137         const auto doc = QJsonDocument::fromJson(reply->readAll()).object();
0138         beginRemoveRows({}, row, row);
0139         m_ipinfo.removeAt(row);
0140         endRemoveRows();
0141         Q_EMIT dataChanged(index(row, 0), index(row, 0));
0142     });
0143 }
0144 
0145 void IpRulesToolModel::filltimeline()
0146 {
0147     const auto account = AccountManager::instance().selectedAccount();
0148 
0149     if (m_loading) {
0150         return;
0151     }
0152     setLoading(true);
0153     QUrl url;
0154     if (m_next.isEmpty()) {
0155         url = account->apiUrl(QStringLiteral("/api/v1/admin/ip_blocks"));
0156     } else {
0157         url = m_next;
0158     }
0159 
0160     account->get(url, true, this, [this](QNetworkReply *reply) {
0161         const auto doc = QJsonDocument::fromJson(reply->readAll());
0162         const auto ipblocks = doc.array();
0163 
0164         if (!ipblocks.isEmpty()) {
0165             static QRegularExpression re(QStringLiteral("<(.*)>; rel=\"next\""));
0166             const auto next = reply->rawHeader(QByteArrayLiteral("Link"));
0167             const auto match = re.match(QString::fromUtf8(next));
0168             if (re.isValid()) {
0169                 m_next = QUrl::fromUserInput(match.captured(1));
0170             }
0171             QList<IpInfo> fetchedIpblocks;
0172 
0173             std::transform(
0174                 ipblocks.cbegin(),
0175                 ipblocks.cend(),
0176                 std::back_inserter(fetchedIpblocks),
0177                 [=](const QJsonValue &value) -> auto{ return IpInfo::fromSourceData(value.toObject()); });
0178             beginInsertRows({}, m_ipinfo.size(), m_ipinfo.size() + fetchedIpblocks.size() - 1);
0179             m_ipinfo += fetchedIpblocks;
0180             endInsertRows();
0181         }
0182         setLoading(false);
0183     });
0184 }
0185 
0186 #include "moc_iprulestoolmodel.cpp"