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

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-FileCopyrightText: 2023 Rishi Kumar <rsi.dev17@gmail.com>
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "reportinfo.h"
0006 
0007 #include "abstractaccount.h"
0008 #include "accountmanager.h"
0009 
0010 using namespace Qt::StringLiterals;
0011 
0012 ReportInfo::ReportInfo(QObject *parent)
0013     : QObject(parent)
0014     , m_reportStatusList(this, &m_reportStatus)
0015 {
0016 }
0017 
0018 QString ReportInfo::reportId() const
0019 {
0020     return m_reportId;
0021 }
0022 
0023 bool ReportInfo::actionTaken() const
0024 {
0025     return m_actionTaken;
0026 }
0027 
0028 void ReportInfo::setActionTaken(bool actionTaken)
0029 {
0030     if (m_actionTaken == actionTaken) {
0031         return;
0032     }
0033     m_actionTaken = actionTaken;
0034     Q_EMIT actionTakenUpdated();
0035 }
0036 
0037 bool ReportInfo::assignedModerator() const
0038 {
0039     return m_assignedModerator;
0040 }
0041 
0042 void ReportInfo::setAssignedModerator(bool assignedModerator)
0043 {
0044     if (m_assignedModerator == assignedModerator) {
0045         return;
0046     }
0047     m_assignedModerator = assignedModerator;
0048     Q_EMIT assignedModeratorUpdated();
0049 }
0050 
0051 QDateTime ReportInfo::actionTakenAt() const
0052 {
0053     return m_actionTakenAt;
0054 }
0055 
0056 QString ReportInfo::category() const
0057 {
0058     return m_category;
0059 }
0060 
0061 QString ReportInfo::comment() const
0062 {
0063     return m_comment;
0064 }
0065 
0066 bool ReportInfo::forwarded() const
0067 {
0068     return m_forwarded;
0069 }
0070 
0071 QDateTime ReportInfo::createdAt() const
0072 {
0073     return m_createdAt;
0074 }
0075 
0076 QDateTime ReportInfo::updatedAt() const
0077 {
0078     return m_updatedAt;
0079 }
0080 
0081 int ReportInfo::statusCount() const
0082 {
0083     return m_reportStatus.length();
0084 }
0085 
0086 int ReportInfo::mediaAttachmentCount() const
0087 {
0088     int count = 0;
0089     for (auto &post : m_reportStatus) {
0090         if (!post->attachments().empty()) {
0091             count += post->attachments().length();
0092         }
0093     }
0094     return count;
0095 }
0096 
0097 AdminAccountInfo *ReportInfo::filedAccount() const
0098 {
0099     return m_filedAccount;
0100 }
0101 
0102 AdminAccountInfo *ReportInfo::targetAccount() const
0103 {
0104     return m_targetAccount;
0105 }
0106 
0107 AdminAccountInfo *ReportInfo::assignedAccount() const
0108 {
0109     return m_assignedAccount;
0110 }
0111 
0112 void ReportInfo::setAssignedAccount(AdminAccountInfo *newAssignedAccount)
0113 {
0114     if (m_assignedAccount == newAssignedAccount) {
0115         return;
0116     }
0117     m_assignedAccount = newAssignedAccount;
0118     Q_EMIT assignedAccountUpdated();
0119 }
0120 
0121 AdminAccountInfo *ReportInfo::actionTakenByAccount() const
0122 {
0123     return m_actionTakenByAccount;
0124 }
0125 
0126 QList<Post *> ReportInfo::reportStatus() const
0127 {
0128     return m_reportStatus;
0129 }
0130 
0131 QQmlListProperty<Post> ReportInfo::reportStatusList() const
0132 {
0133     return m_reportStatusList;
0134 }
0135 
0136 QJsonArray ReportInfo::rules() const
0137 {
0138     return m_rules;
0139 }
0140 
0141 void ReportInfo::reparentReportInfo(AbstractAccount *parent)
0142 {
0143     m_parent = parent;
0144 }
0145 
0146 void ReportInfo::fromSourceData(const QJsonObject &doc)
0147 {
0148     m_reportId = doc["id"_L1].toString();
0149 
0150     m_actionTaken = doc["action_taken"_L1].toBool();
0151     m_actionTakenAt = QDateTime::fromString(doc["action_taken_at"_L1].toString(), Qt::ISODate).toLocalTime();
0152     m_category = doc["category"_L1].toString();
0153     m_comment = doc["comment"_L1].toString();
0154     m_forwarded = doc["forwarded"_L1].toBool();
0155     m_createdAt = QDateTime::fromString(doc["created_at"_L1].toString(), Qt::ISODate).toLocalTime();
0156     m_updatedAt = QDateTime::fromString(doc["updated_at"_L1].toString(), Qt::ISODate).toLocalTime();
0157 
0158     auto account = AccountManager::instance().selectedAccount();
0159     const auto filedAccountdoc = doc["account"_L1];
0160     const auto targetAccountdoc = doc["target_account"_L1];
0161     const auto assignedAccountdoc = doc["assigned_account"_L1];
0162     const auto actionTakenByAccountdoc = doc["action_taken_by_account"_L1];
0163     m_filedAccount = account->adminIdentityLookupWithVanillaPointer(filedAccountdoc["id"_L1].toString(), filedAccountdoc.toObject());
0164     m_targetAccount = account->adminIdentityLookupWithVanillaPointer(targetAccountdoc["id"_L1].toString(), targetAccountdoc.toObject());
0165     m_assignedAccount = account->adminIdentityLookupWithVanillaPointer(assignedAccountdoc["id"_L1].toString(), assignedAccountdoc.toObject());
0166     m_actionTakenByAccount = account->adminIdentityLookupWithVanillaPointer(actionTakenByAccountdoc["id"_L1].toString(), actionTakenByAccountdoc.toObject());
0167 
0168     // remove this
0169     m_assignedModerator = !m_assignedAccount->userLevelIdentity()->account().isEmpty();
0170     // creating status array with the Post class
0171     const auto reportStatuses = doc[QStringLiteral("statuses")].toArray();
0172     std::transform(
0173         reportStatuses.cbegin(),
0174         reportStatuses.cend(),
0175         std::back_inserter(m_reportStatus),
0176         [ this, account ](const QJsonValue &value) -> auto{ return new Post(account, value.toObject(), this); });
0177 
0178     m_rules = doc["rules"_L1].toArray();
0179 
0180     Q_EMIT reportInfoUpdated();
0181 }
0182 
0183 #include "moc_reportinfo.cpp"