File indexing completed on 2024-05-12 16:28:04

0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 #include "identity.h"
0005 #include "abstractaccount.h"
0006 #include "relationship.h"
0007 
0008 #include <QJsonObject>
0009 
0010 QString Identity::displayName() const
0011 {
0012     return !m_displayName.isEmpty() ? m_displayName : m_username;
0013 }
0014 
0015 QString Identity::username() const
0016 {
0017     return m_username;
0018 }
0019 
0020 QString Identity::bio() const
0021 {
0022     return m_bio;
0023 }
0024 
0025 QString Identity::account() const
0026 {
0027     return m_account;
0028 }
0029 
0030 bool Identity::locked() const
0031 {
0032     return m_locked;
0033 }
0034 
0035 QString Identity::visibility() const
0036 {
0037     return m_visibility;
0038 }
0039 
0040 QUrl Identity::avatarUrl() const
0041 {
0042     return m_avatarUrl;
0043 }
0044 
0045 QUrl Identity::backgroundUrl() const
0046 {
0047     return m_backgroundUrl;
0048 }
0049 
0050 int Identity::followersCount() const
0051 {
0052     return m_followersCount;
0053 }
0054 
0055 int Identity::followingCount() const
0056 {
0057     return m_followingCount;
0058 }
0059 
0060 int Identity::statusesCount() const
0061 {
0062     return m_statusesCount;
0063 }
0064 
0065 int Identity::permission() const
0066 {
0067     return m_permission;
0068 }
0069 
0070 QJsonArray Identity::fields() const
0071 {
0072     return m_fields;
0073 }
0074 
0075 Relationship *Identity::relationship() const
0076 {
0077     return m_relationship;
0078 }
0079 
0080 void Identity::setRelationship(Relationship *r)
0081 {
0082     if (m_relationship == r) {
0083         return;
0084     }
0085 
0086     // delete old relationship object if we receive a new one
0087     delete m_relationship;
0088 
0089     m_relationship = r;
0090     Q_EMIT relationshipChanged();
0091 }
0092 
0093 void Identity::reparentIdentity(AbstractAccount *parent)
0094 {
0095     m_parent = parent;
0096 }
0097 
0098 void Identity::fromSourceData(const QJsonObject &doc)
0099 {
0100     m_id = doc["id"].toString();
0101     m_displayName = doc["display_name"].toString();
0102     m_username = doc["username"].toString();
0103     m_account = doc["acct"].toString();
0104     m_bio = doc["note"].toString();
0105     m_locked = doc["locked"].toBool();
0106     m_backgroundUrl = QUrl(doc["header"].toString());
0107     m_avatarUrl = QUrl(doc["avatar"].toString());
0108     m_followersCount = doc["followers_count"].toInt();
0109     m_followingCount = doc["following_count"].toInt();
0110     m_statusesCount = doc["statuses_count"].toInt();
0111     m_fields = doc["fields"].toArray();
0112     m_url = doc["url"].toString();
0113     m_permission = doc["role"]["permissions"].toString().toInt();
0114     // When the user data is ourselves, we get source.privacy
0115     // with the default post privacy setting for the user. all others
0116     // will get empty strings.
0117     QJsonObject source = doc["source"].toObject();
0118     m_visibility = source["privacy"].toString();
0119 
0120     m_displayNameHtml = m_displayName.replace(QLatin1Char('<'), QStringLiteral("&lt;")).replace(QLatin1Char('>'), QStringLiteral("&gt;"));
0121 
0122     const auto emojis = doc["emojis"].toArray();
0123 
0124     for (const auto &emoji : emojis) {
0125         const auto emojiObj = emoji.toObject();
0126         m_displayNameHtml = m_displayNameHtml.replace(QLatin1Char(':') + emojiObj["shortcode"].toString() + QLatin1Char(':'),
0127                                                       "<img height=\"16\" align=\"middle\" width=\"16\" src=\"" + emojiObj["static_url"].toString() + "\">");
0128         m_bio = m_bio.replace(QLatin1Char(':') + emojiObj["shortcode"].toString() + QLatin1Char(':'),
0129                               "<img height=\"16\" width=\"16\" align=\"middle\" src=\"" + emojiObj["static_url"].toString() + "\">");
0130     }
0131 
0132     Q_EMIT identityUpdated();
0133 }
0134 
0135 QString Identity::id() const
0136 {
0137     return m_id;
0138 }
0139 
0140 QString Identity::displayNameHtml() const
0141 {
0142     return !m_displayNameHtml.isEmpty() ? m_displayNameHtml : m_username;
0143 }
0144 
0145 QUrl Identity::url() const
0146 {
0147     return m_url;
0148 }