File indexing completed on 2024-10-06 04:34:02
0001 /* 0002 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "reactions.h" 0008 #include <QJsonArray> 0009 #include <QJsonObject> 0010 0011 Reactions::Reactions() = default; 0012 0013 void Reactions::setReactions(const QVector<Reaction> &reactions) 0014 { 0015 mReactions = reactions; 0016 } 0017 0018 QVector<Reaction> Reactions::reactions() const 0019 { 0020 return mReactions; 0021 } 0022 0023 void Reactions::parseReactions(const QJsonObject &reacts, EmojiManager *emojiManager) 0024 { 0025 mReactions.clear(); 0026 const QStringList lst = reacts.keys(); 0027 QStringList users; 0028 for (const QString &str : lst) { 0029 users.clear(); 0030 const QJsonObject obj = reacts.value(str).toObject(); 0031 const QJsonValue usernames = obj.value(QLatin1String("usernames")); 0032 if (!usernames.isUndefined()) { 0033 const QJsonArray array = usernames.toArray(); 0034 for (int i = 0; i < array.count(); ++i) { 0035 users.append(array.at(i).toString()); 0036 } 0037 if (!users.isEmpty()) { 0038 Reaction r; 0039 r.setReactionName(str, emojiManager); 0040 r.setUserNames(users); 0041 mReactions.append(std::move(r)); 0042 } 0043 } 0044 } 0045 } 0046 0047 bool Reactions::operator==(const Reactions &other) const 0048 { 0049 return mReactions == other.reactions(); 0050 } 0051 0052 QDebug operator<<(QDebug d, const Reactions &t) 0053 { 0054 for (int i = 0; i < t.reactions().count(); i++) { 0055 d.space() << t.reactions().at(i) << "\n"; 0056 } 0057 return d; 0058 } 0059 0060 QJsonObject Reactions::serialize(const Reactions &reactions) 0061 { 0062 QJsonObject obj; 0063 for (int i = 0; i < reactions.reactions().count(); ++i) { 0064 QJsonObject react; 0065 react[QLatin1String("usernames")] = QJsonArray::fromStringList(reactions.reactions().at(i).userNames()); 0066 obj[reactions.reactions().at(i).reactionName()] = react; 0067 } 0068 return obj; 0069 } 0070 0071 Reactions Reactions::deserialize(const QJsonObject &o, EmojiManager *emojiManager) 0072 { 0073 QVector<Reaction> reacts; 0074 const QStringList lst = o.keys(); 0075 QStringList users; 0076 for (const QString &str : lst) { 0077 const QJsonObject obj = o.value(str).toObject(); 0078 const QJsonValue usernames = obj.value(QLatin1String("usernames")); 0079 if (!usernames.isUndefined()) { 0080 const QJsonArray array = usernames.toArray(); 0081 for (int i = 0; i < array.count(); ++i) { 0082 users.append(array.at(i).toString()); 0083 } 0084 if (!users.isEmpty()) { 0085 Reaction r; 0086 r.setReactionName(str, emojiManager); 0087 r.setUserNames(users); 0088 reacts.append(std::move(r)); 0089 } 0090 } 0091 users.clear(); 0092 } 0093 Reactions final; 0094 final.setReactions(reacts); 0095 return final; 0096 } 0097 0098 bool Reactions::isEmpty() const 0099 { 0100 return mReactions.isEmpty(); 0101 }