File indexing completed on 2024-05-12 16:28:06
0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu> 0002 // SPDX-FileCopyrightText: 2023 Rishi Kumar <rsi.dev17@gmail.com> 0003 // SPDX-License-Identifier: GPL-3.0-or-later 0004 0005 #include "adminaccountinfo.h" 0006 #include "account/abstractaccount.h" 0007 #include "account/accountmanager.h" 0008 #include <KLocalizedString> 0009 #include <QDateTime> 0010 #include <QJsonObject> 0011 0012 QString AdminAccountInfo::role() const 0013 { 0014 return m_role; 0015 } 0016 0017 QString AdminAccountInfo::loginStatus() const 0018 { 0019 if (m_suspended) { 0020 return i18nc("login status", "Suspended"); 0021 } else if (m_silenced) { 0022 return i18nc("login status", "Silenced"); 0023 } else if (m_sensitized) { 0024 return i18nc("login status", "Sensitized"); 0025 } else if (m_disabled) { 0026 return i18nc("login status", "Frozen"); 0027 } else if (!m_emailStatus && m_ip != "") { 0028 return i18nc("login status", "Email Not confirmed"); 0029 } else if (!m_approved && m_ip != "") { 0030 return i18nc("login status", "Not Approved"); 0031 } else { 0032 return i18nc("login status", "No Limits Imposed"); 0033 } 0034 } 0035 0036 QString AdminAccountInfo::inviteRequest() const 0037 { 0038 return m_inviteRequest; 0039 } 0040 0041 bool AdminAccountInfo::emailStatus() const 0042 { 0043 return m_emailStatus; 0044 } 0045 0046 QJsonArray AdminAccountInfo::ips() const 0047 { 0048 return m_ips; 0049 } 0050 0051 Identity *AdminAccountInfo::invitedByIdentity() const 0052 { 0053 return m_invitedByIdentity.get(); 0054 } 0055 0056 Identity *AdminAccountInfo::userLevelIdentity() const 0057 { 0058 return m_userLevelIdentity.get(); 0059 } 0060 0061 bool AdminAccountInfo::suspended() const 0062 { 0063 return m_suspended; 0064 } 0065 0066 void AdminAccountInfo::setSuspended(bool suspended) 0067 { 0068 if (m_suspended == suspended) { 0069 return; 0070 } 0071 m_suspended = suspended; 0072 Q_EMIT adminAccountInfoUpdated(); 0073 } 0074 0075 bool AdminAccountInfo::silenced() const 0076 { 0077 return m_silenced; 0078 } 0079 0080 void AdminAccountInfo::setSilence(bool silenced) 0081 { 0082 if (m_silenced == silenced) { 0083 return; 0084 } 0085 m_silenced = silenced; 0086 Q_EMIT adminAccountInfoUpdated(); 0087 } 0088 0089 bool AdminAccountInfo::sensitized() const 0090 { 0091 return m_sensitized; 0092 } 0093 0094 void AdminAccountInfo::setSensitized(bool sensitized) 0095 { 0096 if (m_sensitized == sensitized) { 0097 return; 0098 } 0099 m_sensitized = sensitized; 0100 Q_EMIT adminAccountInfoUpdated(); 0101 } 0102 0103 bool AdminAccountInfo::disabled() const 0104 { 0105 return m_disabled; 0106 } 0107 0108 void AdminAccountInfo::setDisabled(bool disabled) 0109 { 0110 if (m_disabled == disabled) { 0111 return; 0112 } 0113 m_disabled = disabled; 0114 Q_EMIT adminAccountInfoUpdated(); 0115 } 0116 0117 bool AdminAccountInfo::approved() const 0118 { 0119 return m_approved; 0120 } 0121 0122 void AdminAccountInfo::setApproved(bool approved) 0123 { 0124 if (m_approved == approved) { 0125 return; 0126 } 0127 m_approved = approved; 0128 Q_EMIT adminAccountInfoUpdated(); 0129 } 0130 0131 bool AdminAccountInfo::isLocal() const 0132 { 0133 // hack to determine if an account is local 0134 return m_ip != ""; 0135 } 0136 0137 int AdminAccountInfo::position() const 0138 { 0139 return m_position; 0140 } 0141 0142 void AdminAccountInfo::reparentAdminAccountInfo(AbstractAccount *parent) 0143 { 0144 m_parent = parent; 0145 } 0146 0147 void AdminAccountInfo::fromSourceData(const QJsonObject &jdoc) 0148 { 0149 auto account = AccountManager::instance().selectedAccount(); 0150 const auto doc = jdoc["account"]; 0151 m_userLevelIdentity = account->identityLookup(doc["id"].toString(), doc.toObject()); 0152 0153 m_role = jdoc["role"]["name"].toString(); 0154 m_ip = jdoc["ip"].toString(); 0155 m_ips = jdoc["ips"].toArray(); 0156 m_email = jdoc["email"].toString(); 0157 m_inviteRequest = jdoc["invite_request"].toString(); 0158 m_emailStatus = jdoc["confirmed"].toBool(); 0159 m_suspended = jdoc["suspended"].toBool(); 0160 m_silenced = jdoc["silenced"].toBool(); 0161 m_sensitized = jdoc["sensitized"].toBool(); 0162 m_disabled = jdoc["disabled"].toBool(); 0163 m_approved = jdoc["approved"].toBool(); 0164 m_locale = jdoc["locale"].toString(); 0165 m_position = jdoc["role"]["position"].toInt(); 0166 m_joined = QDateTime::fromString(jdoc["created_at"].toString(), Qt::ISODate).toLocalTime(); 0167 0168 // logic for last used activity 0169 const QJsonArray ipsArray = jdoc["ips"].toArray(); 0170 calculateRecentActivity(ipsArray); 0171 0172 if (jdoc["email"].toString().length() > 0) { 0173 m_emailProvider = jdoc["email"].toString().split('@').at(1); 0174 } 0175 0176 auto invited_by_account_id = jdoc["invited_by_account_id"].toString(); 0177 0178 if (invited_by_account_id != "") { 0179 QUrl url = account->apiUrl(QString("/api/v1/accounts/%1").arg(invited_by_account_id)); 0180 account->get(url, true, this, [this, account, invited_by_account_id](QNetworkReply *reply) { 0181 const auto doc = QJsonDocument::fromJson(reply->readAll()).object(); 0182 m_invitedByIdentity = account->identityLookup(invited_by_account_id, doc); 0183 }); 0184 } 0185 0186 Q_EMIT adminAccountInfoUpdated(); 0187 } 0188 0189 QString AdminAccountInfo::ip() const 0190 { 0191 return m_ip; 0192 } 0193 0194 QString AdminAccountInfo::email() const 0195 { 0196 return m_email; 0197 } 0198 0199 QString AdminAccountInfo::emailProvider() const 0200 { 0201 return m_emailProvider; 0202 } 0203 0204 QString AdminAccountInfo::locale() const 0205 { 0206 return m_locale; 0207 } 0208 0209 QDateTime AdminAccountInfo::joined() const 0210 { 0211 return m_joined; 0212 } 0213 0214 QDateTime AdminAccountInfo::lastActive() const 0215 { 0216 return m_lastActive; 0217 } 0218 0219 void AdminAccountInfo::calculateRecentActivity(const QJsonArray &ipsArray) 0220 { 0221 QDateTime latestDateTime; 0222 for (const auto &ipValue : ipsArray) { 0223 const auto object = ipValue.toObject(); 0224 const QDateTime usedAtTime = QDateTime::fromString(object[QStringLiteral("used_at")].toString(), Qt::ISODate); 0225 if (usedAtTime > latestDateTime) 0226 latestDateTime = usedAtTime; 0227 } 0228 m_lastActive = latestDateTime.toLocalTime(); 0229 }