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

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 "emailblocktoolmodel.h"
0005 
0006 #include "abstractaccount.h"
0007 #include "accountmanager.h"
0008 
0009 EmailBlockToolModel::EmailBlockToolModel(QObject *parent)
0010     : QAbstractListModel(parent)
0011 {
0012     filltimeline();
0013 }
0014 
0015 QVariant EmailBlockToolModel::data(const QModelIndex &index, int role) const
0016 {
0017     Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
0018 
0019     const auto &emailInfo = m_emailinfo[index.row()];
0020 
0021     switch (role) {
0022     case IdRole:
0023         return emailInfo.id();
0024     case DomainRole:
0025         return emailInfo.domain();
0026     case CreatedAtRole:
0027         return emailInfo.createdAt();
0028     case IpSignUpCount:
0029         return emailInfo.ipSignupCount();
0030     case AccountSignUpCount:
0031         return emailInfo.accountSignupCount();
0032     default:
0033         return {};
0034     }
0035 }
0036 
0037 bool EmailBlockToolModel::loading() const
0038 {
0039     return m_loading;
0040 }
0041 
0042 void EmailBlockToolModel::setLoading(bool loading)
0043 {
0044     if (m_loading == loading) {
0045         return;
0046     }
0047     m_loading = loading;
0048     Q_EMIT loadingChanged();
0049 }
0050 
0051 int EmailBlockToolModel::rowCount(const QModelIndex &parent) const
0052 {
0053     return parent.isValid() ? 0 : m_emailinfo.count();
0054 }
0055 
0056 QHash<int, QByteArray> EmailBlockToolModel::roleNames() const
0057 {
0058     return {
0059         {IdRole, "id"},
0060         {DomainRole, "domain"},
0061         {CreatedAtRole, "createdAt"},
0062         {AccountSignUpCount, "accountCount"},
0063         {IpSignUpCount, "ipCount"},
0064     };
0065 }
0066 
0067 void EmailBlockToolModel::newEmailBlock(const QString &domain)
0068 {
0069     const QJsonObject obj{
0070         {QStringLiteral("domain"), domain},
0071     };
0072 
0073     const auto doc = QJsonDocument(obj);
0074 
0075     const auto account = AccountManager::instance().selectedAccount();
0076 
0077     const QUrl url = account->apiUrl(QStringLiteral("/api/v1/admin/email_domain_blocks"));
0078 
0079     account->post(url, doc, true, this, [=](QNetworkReply *reply) {
0080         const auto doc = QJsonDocument::fromJson(reply->readAll());
0081         const auto jsonObj = doc.object();
0082         const auto newEmailInfo = EmailInfo::fromSourceData(jsonObj);
0083 
0084         beginInsertRows({}, m_emailinfo.size(), m_emailinfo.size());
0085         m_emailinfo += newEmailInfo;
0086         endInsertRows();
0087     });
0088 }
0089 
0090 void EmailBlockToolModel::deleteEmailBlock(const int row)
0091 {
0092     const auto account = AccountManager::instance().selectedAccount();
0093     const auto &emailinfo = m_emailinfo[row];
0094     const auto emailBlockId = emailinfo.id();
0095 
0096     account->deleteResource(account->apiUrl(QStringLiteral("/api/v1/admin/email_domain_blocks/%1").arg(emailBlockId)), true, this, [=](QNetworkReply *reply) {
0097         Q_UNUSED(reply);
0098         beginRemoveRows({}, row, row);
0099         m_emailinfo.removeAt(row);
0100         endRemoveRows();
0101         Q_EMIT dataChanged(index(row, 0), index(row, 0));
0102     });
0103 }
0104 
0105 void EmailBlockToolModel::filltimeline()
0106 {
0107     const auto account = AccountManager::instance().selectedAccount();
0108 
0109     if (m_loading) {
0110         return;
0111     }
0112     setLoading(true);
0113     QUrl url;
0114     if (m_next.isEmpty()) {
0115         url = account->apiUrl(QStringLiteral("/api/v1/admin/email_domain_blocks"));
0116     } else {
0117         url = m_next;
0118     }
0119 
0120     account->get(url, true, this, [this](QNetworkReply *reply) {
0121         const auto doc = QJsonDocument::fromJson(reply->readAll());
0122         const auto emailblocks = doc.array();
0123 
0124         if (!emailblocks.isEmpty()) {
0125             static QRegularExpression re(QStringLiteral("<(.*)>; rel=\"next\""));
0126             const auto next = reply->rawHeader(QByteArrayLiteral("Link"));
0127             const auto match = re.match(QString::fromUtf8(next));
0128             if (re.isValid()) {
0129                 m_next = QUrl::fromUserInput(match.captured(1));
0130             }
0131             QList<EmailInfo> fetchedEmailblocks;
0132 
0133             std::transform(
0134                 emailblocks.cbegin(),
0135                 emailblocks.cend(),
0136                 std::back_inserter(fetchedEmailblocks),
0137                 [=](const QJsonValue &value) -> auto{ return EmailInfo::fromSourceData(value.toObject()); });
0138             beginInsertRows({}, m_emailinfo.size(), m_emailinfo.size() + fetchedEmailblocks.size() - 1);
0139             m_emailinfo += fetchedEmailblocks;
0140             endInsertRows();
0141         }
0142         setLoading(false);
0143     });
0144 }
0145 
0146 #include "moc_emailblocktoolmodel.cpp"