File indexing completed on 2024-04-14 04:56:43

0001 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "listeditorbackend.h"
0005 #include <KLocalizedString>
0006 
0007 #include "abstractaccount.h"
0008 #include "accountmanager.h"
0009 
0010 using namespace Qt::StringLiterals;
0011 
0012 const QStringList supportedPolicies{QStringLiteral("none"), QStringLiteral("list"), QStringLiteral("followed")};
0013 
0014 ListEditorBackend::ListEditorBackend(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 QString ListEditorBackend::listId() const
0020 {
0021     return m_listId;
0022 }
0023 
0024 void ListEditorBackend::setListId(const QString &listId)
0025 {
0026     if (m_listId == listId) {
0027         return;
0028     }
0029 
0030     m_listId = listId;
0031     Q_EMIT listIdChanged();
0032 
0033     // load previous list data
0034     m_loading = true;
0035     Q_EMIT loadingChanged();
0036 
0037     auto account = AccountManager::instance().selectedAccount();
0038 
0039     account->get(account->apiUrl(QStringLiteral("/api/v1/lists/%1").arg(m_listId)), true, this, [=](QNetworkReply *reply) {
0040         const auto document = QJsonDocument::fromJson(reply->readAll());
0041 
0042         m_title = document["title"_L1].toString();
0043         Q_EMIT titleChanged();
0044 
0045         m_repliesPolicy = document["replies_policy"_L1].toString();
0046 
0047         m_exclusive = document["exclusive"_L1].toBool();
0048         Q_EMIT exclusiveChanged();
0049 
0050         m_loading = false;
0051         Q_EMIT loadingChanged();
0052     });
0053 }
0054 
0055 int ListEditorBackend::replyPolicyIndex() const
0056 {
0057     return supportedPolicies.indexOf(m_repliesPolicy);
0058 }
0059 
0060 void ListEditorBackend::setReplyPolicyIndex(int index)
0061 {
0062     m_repliesPolicy = supportedPolicies[index];
0063 }
0064 
0065 bool ListEditorBackend::loading() const
0066 {
0067     return m_loading;
0068 }
0069 
0070 void ListEditorBackend::submit()
0071 {
0072     m_loading = true;
0073     Q_EMIT loadingChanged();
0074 
0075     auto account = AccountManager::instance().selectedAccount();
0076 
0077     QUrlQuery formdata;
0078 
0079     formdata.addQueryItem(QStringLiteral("title"), m_title);
0080     formdata.addQueryItem(QStringLiteral("replies_policy"), m_repliesPolicy);
0081     formdata.addQueryItem(QStringLiteral("exclusive"), m_exclusive ? QStringLiteral("1") : QStringLiteral("0"));
0082 
0083     // If the listId is empty, then create a new list
0084     if (m_listId.isEmpty()) {
0085         account->post(account->apiUrl(QStringLiteral("/api/v1/lists")), formdata, true, this, [=](QNetworkReply *) {
0086             Q_EMIT done();
0087         });
0088     } else {
0089         account->put(account->apiUrl(QStringLiteral("/api/v1/lists/%1").arg(m_listId)), formdata, true, this, [=](QNetworkReply *) {
0090             Q_EMIT done();
0091         });
0092     }
0093 }
0094 
0095 void ListEditorBackend::deleteList()
0096 {
0097     Q_ASSERT(!m_listId.isEmpty());
0098 
0099     auto account = AccountManager::instance().selectedAccount();
0100 
0101     account->deleteResource(account->apiUrl(QStringLiteral("/api/v1/lists/%1").arg(m_listId)), true, this, [=](QNetworkReply *) {
0102         Q_EMIT done();
0103     });
0104 }
0105 
0106 QStringList ListEditorBackend::replyPolicies()
0107 {
0108     return {i18n("No one"), i18n("Members of the list"), i18n("Any followed users")};
0109 }
0110 
0111 #include "moc_listeditorbackend.cpp"