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

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include "poll.h"
0005 
0006 #include "utils/customemoji.h"
0007 
0008 using namespace Qt::Literals::StringLiterals;
0009 
0010 Poll::Poll() = default;
0011 
0012 Poll::Poll(const QJsonObject &json)
0013 {
0014     m_id = json[QStringLiteral("id")].toString();
0015     m_expiresAt = QDateTime::fromString(json[QStringLiteral("expires_at")].toString(), Qt::ISODate);
0016     m_expired = json[QStringLiteral("expired")].toBool();
0017     m_multiple = json[QStringLiteral("multiple")].toBool();
0018     m_votesCount = json[QStringLiteral("votes_count")].toInt(-1);
0019     m_votersCount = json[QStringLiteral("voters_count")].toInt(-1);
0020     m_voted = json[QStringLiteral("voted")].toBool();
0021     const auto ownVotes = json[QStringLiteral("own_votes")].toArray();
0022     std::transform(
0023         ownVotes.cbegin(),
0024         ownVotes.cend(),
0025         std::back_inserter(m_ownVotes),
0026         [](const QJsonValue &value) -> auto{ return value.toInt(); });
0027 
0028     const auto emojis = CustomEmoji::parseCustomEmojis(json[QStringLiteral("emojis")].toArray());
0029 
0030     const auto options = json[QStringLiteral("options")].toArray();
0031     std::transform(options.cbegin(), options.cend(), std::back_inserter(m_options), [emojis](const QJsonValue &value) -> QVariantMap {
0032         const auto option = value.toObject();
0033         QString title = CustomEmoji::replaceCustomEmojis(emojis, option[QStringLiteral("title")].toString());
0034 
0035         return {
0036             {"title"_L1, title},
0037             {"votesCount"_L1, option[QStringLiteral("votes_count")].toInt(-1)},
0038         };
0039     });
0040 }
0041 
0042 QString Poll::id() const
0043 {
0044     return m_id;
0045 }
0046 
0047 QDateTime Poll::expiresAt() const
0048 {
0049     return m_expiresAt;
0050 }
0051 
0052 bool Poll::expired() const
0053 {
0054     return m_expired;
0055 }
0056 
0057 bool Poll::multiple() const
0058 {
0059     return m_multiple;
0060 }
0061 
0062 int Poll::votesCount() const
0063 {
0064     return m_votesCount;
0065 }
0066 
0067 int Poll::votersCount() const
0068 {
0069     return m_votersCount;
0070 }
0071 
0072 bool Poll::voted() const
0073 {
0074     return m_voted;
0075 }
0076 
0077 QList<int> Poll::ownVotes() const
0078 {
0079     return m_ownVotes;
0080 }
0081 
0082 QList<QVariantMap> Poll::options() const
0083 {
0084     return m_options;
0085 }
0086 
0087 #include "moc_poll.cpp"