File indexing completed on 2024-12-08 04:33:20

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "moderationreportinfos.h"
0008 #include "ruqola_debug.h"
0009 
0010 #include <QJsonArray>
0011 
0012 ModerationReportInfos::ModerationReportInfos() = default;
0013 
0014 QDebug operator<<(QDebug d, const ModerationReportInfos &t)
0015 {
0016     d.space() << "total" << t.total();
0017     d.space() << "offset" << t.offset();
0018     d.space() << "ModerationReportInfosCount" << t.moderationReportInfosCount() << "\n";
0019     for (int i = 0, total = t.moderationReportInfosList().count(); i < total; ++i) {
0020         d << t.moderationReportInfosList().at(i) << "\n";
0021     }
0022     return d;
0023 }
0024 
0025 int ModerationReportInfos::offset() const
0026 {
0027     return mOffset;
0028 }
0029 
0030 void ModerationReportInfos::setOffset(int newOffset)
0031 {
0032     mOffset = newOffset;
0033 }
0034 
0035 int ModerationReportInfos::total() const
0036 {
0037     return mTotal;
0038 }
0039 
0040 void ModerationReportInfos::setTotal(int newTotal)
0041 {
0042     mTotal = newTotal;
0043 }
0044 
0045 int ModerationReportInfos::moderationReportInfosCount() const
0046 {
0047     return mModerationReportInfosCount;
0048 }
0049 
0050 void ModerationReportInfos::setModerationReportInfosCount(int newModerationReportInfosCount)
0051 {
0052     mModerationReportInfosCount = newModerationReportInfosCount;
0053 }
0054 
0055 const QVector<ModerationReportInfo> &ModerationReportInfos::moderationReportInfosList() const
0056 {
0057     return mModerationReportInfosList;
0058 }
0059 
0060 void ModerationReportInfos::setModerationReportInfosList(const QVector<ModerationReportInfo> &newModerationReportInfosList)
0061 {
0062     mModerationReportInfosList = newModerationReportInfosList;
0063 }
0064 
0065 bool ModerationReportInfos::isEmpty() const
0066 {
0067     return mModerationReportInfosList.isEmpty();
0068 }
0069 
0070 void ModerationReportInfos::clear()
0071 {
0072     mModerationReportInfosList.clear();
0073 }
0074 
0075 int ModerationReportInfos::count() const
0076 {
0077     return mModerationReportInfosList.count();
0078 }
0079 
0080 ModerationReportInfo ModerationReportInfos::at(int index) const
0081 {
0082     if (index < 0 || index > mModerationReportInfosList.count()) {
0083         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0084         return {};
0085     }
0086     return mModerationReportInfosList.at(index);
0087 }
0088 
0089 void ModerationReportInfos::parseModerationReportInfos(const QJsonObject &moderationReportInfosObj)
0090 {
0091     mModerationReportInfosList.clear();
0092     qDebug() << " moderationReportInfosObj " << moderationReportInfosObj;
0093     mModerationReportInfosCount = moderationReportInfosObj[QLatin1String("count")].toInt();
0094     mOffset = moderationReportInfosObj[QLatin1String("offset")].toInt();
0095     mTotal = moderationReportInfosObj[QLatin1String("total")].toInt();
0096     mModerationReportInfosList.reserve(mModerationReportInfosCount);
0097     parseModerationReportInfosObj(moderationReportInfosObj);
0098 }
0099 
0100 void ModerationReportInfos::parseModerationReportInfosObj(const QJsonObject &moderationReportInfosObj)
0101 {
0102     const QJsonArray moderationsArray = moderationReportInfosObj[QLatin1String("reports")].toArray();
0103     for (const QJsonValue &current : moderationsArray) {
0104         if (current.type() == QJsonValue::Object) {
0105             const QJsonObject moderationObject = current.toObject();
0106             ModerationReportInfo m;
0107             m.parseModerationReportInfo(moderationObject);
0108             mModerationReportInfosList.append(std::move(m));
0109         } else {
0110             qCWarning(RUQOLA_LOG) << "Problem when parsing moderations" << current;
0111         }
0112     }
0113 }
0114 
0115 void ModerationReportInfos::parseMoreModerationReportInfos(const QJsonObject &moderationReportInfosObj)
0116 {
0117     const int moderationReportInfosCount = moderationReportInfosObj[QLatin1String("count")].toInt();
0118     mOffset = moderationReportInfosObj[QLatin1String("offset")].toInt();
0119     mTotal = moderationReportInfosObj[QLatin1String("total")].toInt();
0120     parseModerationReportInfosObj(moderationReportInfosObj);
0121     mModerationReportInfosCount += moderationReportInfosCount;
0122 }
0123 
0124 ModerationReportInfo ModerationReportInfos::takeAt(int index)
0125 {
0126     return mModerationReportInfosList.takeAt(index);
0127 }