summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/common.pro7
-rw-r--r--common/mastodonauthutils.h143
-rw-r--r--common/mastodonnotificationsdatabase.cpp87
-rw-r--r--common/mastodonnotificationsdatabase.h46
4 files changed, 146 insertions, 137 deletions
diff --git a/common/common.pro b/common/common.pro
index 89f0117..02c78ee 100644
--- a/common/common.pro
+++ b/common/common.pro
@@ -10,12 +10,11 @@ TARGET = mastodoncommon
TARGET = $$qtLibraryTarget($$TARGET)
HEADERS += \
- $$PWD/mastodonpostsdatabase.h \
- $$PWD/mastodonnotificationsdatabase.h
+ $$PWD/mastodonauthutils.h \
+ $$PWD/mastodonpostsdatabase.h
SOURCES += \
- $$PWD/mastodonpostsdatabase.cpp \
- $$PWD/mastodonnotificationsdatabase.cpp
+ $$PWD/mastodonpostsdatabase.cpp
TARGETPATH = $$[QT_INSTALL_LIBS]
target.path = $$TARGETPATH
diff --git a/common/mastodonauthutils.h b/common/mastodonauthutils.h
new file mode 100644
index 0000000..3f1fc85
--- /dev/null
+++ b/common/mastodonauthutils.h
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2013-2026 Jolla Ltd.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef MASTODONAUTHUTILS_H
+#define MASTODONAUTHUTILS_H
+
+#include <QtCore/QVariantMap>
+#include <QtCore/QUrl>
+
+#include <Accounts/Account>
+
+#include <SignOn/SessionData>
+
+namespace MastodonAuthUtils {
+
+inline QString normalizeApiHost(const QString &rawHost)
+{
+ QString host = rawHost.trimmed();
+ if (host.isEmpty()) {
+ host = QStringLiteral("https://mastodon.social");
+ }
+ if (!host.startsWith(QLatin1String("https://"))
+ && !host.startsWith(QLatin1String("http://"))) {
+ host.prepend(QStringLiteral("https://"));
+ }
+
+ QUrl url(host);
+ if (!url.isValid() || url.host().isEmpty()) {
+ return QStringLiteral("https://mastodon.social");
+ }
+
+ QString normalized = QString::fromLatin1(url.toEncoded(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment));
+ if (normalized.endsWith(QLatin1Char('/'))) {
+ normalized.chop(1);
+ }
+ return normalized;
+}
+
+inline QString signOnHost(Accounts::Account *account)
+{
+ QString configuredHost = account->value(QStringLiteral("auth/oauth2/web_server/Host")).toString().trimmed();
+ if (configuredHost.isEmpty()) {
+ configuredHost = normalizeApiHost(account->value(QStringLiteral("api/Host")).toString());
+ }
+
+ if (configuredHost.startsWith(QLatin1String("https://"))) {
+ configuredHost.remove(0, 8);
+ } else if (configuredHost.startsWith(QLatin1String("http://"))) {
+ configuredHost.remove(0, 7);
+ }
+
+ const int separator = configuredHost.indexOf(QLatin1Char('/'));
+ if (separator > -1) {
+ configuredHost.truncate(separator);
+ }
+ while (configuredHost.endsWith(QLatin1Char('/'))) {
+ configuredHost.chop(1);
+ }
+ if (configuredHost.isEmpty()) {
+ configuredHost = QStringLiteral("mastodon.social");
+ }
+
+ return configuredHost;
+}
+
+inline void addSignOnSessionParameters(Accounts::Account *account, QVariantMap *sessionData)
+{
+ sessionData->insert(QStringLiteral("Host"), signOnHost(account));
+
+ const QString authPath = account->value(QStringLiteral("auth/oauth2/web_server/AuthPath")).toString().trimmed();
+ if (!authPath.isEmpty()) {
+ sessionData->insert(QStringLiteral("AuthPath"), authPath);
+ }
+
+ const QString tokenPath = account->value(QStringLiteral("auth/oauth2/web_server/TokenPath")).toString().trimmed();
+ if (!tokenPath.isEmpty()) {
+ sessionData->insert(QStringLiteral("TokenPath"), tokenPath);
+ }
+
+ const QString responseType = account->value(QStringLiteral("auth/oauth2/web_server/ResponseType")).toString().trimmed();
+ if (!responseType.isEmpty()) {
+ sessionData->insert(QStringLiteral("ResponseType"), responseType);
+ }
+
+ const QString redirectUri = account->value(QStringLiteral("auth/oauth2/web_server/RedirectUri")).toString().trimmed();
+ if (!redirectUri.isEmpty()) {
+ sessionData->insert(QStringLiteral("RedirectUri"), redirectUri);
+ }
+
+ const QVariant scopeValue = account->value(QStringLiteral("auth/oauth2/web_server/Scope"));
+ if (scopeValue.isValid()) {
+ sessionData->insert(QStringLiteral("Scope"), scopeValue);
+ }
+
+ const QString clientId = account->value(QStringLiteral("auth/oauth2/web_server/ClientId")).toString().trimmed();
+ if (!clientId.isEmpty()) {
+ sessionData->insert(QStringLiteral("ClientId"), clientId);
+ }
+
+ const QString clientSecret = account->value(QStringLiteral("auth/oauth2/web_server/ClientSecret")).toString().trimmed();
+ if (!clientSecret.isEmpty()) {
+ sessionData->insert(QStringLiteral("ClientSecret"), clientSecret);
+ }
+
+ sessionData->insert(QStringLiteral("UiPolicy"), SignOn::NoUserInteractionPolicy);
+}
+
+inline QString accessToken(const QVariantMap &sessionResponseData)
+{
+ QString token = sessionResponseData.value(QLatin1String("AccessToken")).toString().trimmed();
+ if (token.isEmpty()) {
+ token = sessionResponseData.value(QLatin1String("access_token")).toString().trimmed();
+ }
+ return token;
+}
+
+inline QVariantMap responseDataToMap(const SignOn::SessionData &responseData)
+{
+ QVariantMap data;
+ foreach (const QString &key, responseData.propertyNames()) {
+ data.insert(key, responseData.getProperty(key));
+ }
+ return data;
+}
+
+} // namespace MastodonAuthUtils
+
+#endif // MASTODONAUTHUTILS_H
diff --git a/common/mastodonnotificationsdatabase.cpp b/common/mastodonnotificationsdatabase.cpp
deleted file mode 100644
index abb55b9..0000000
--- a/common/mastodonnotificationsdatabase.cpp
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright (C) 2013-2026 Jolla Ltd.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "mastodonnotificationsdatabase.h"
-
-static const char *DB_NAME = "mastodonNotifications.db";
-static const char *ACCOUNT_NAME_KEY = "account_name";
-static const char *URL_KEY = "url";
-static const char *BOOSTED_BY_KEY = "boosted_by";
-static const char *INSTANCE_URL_KEY = "instance_url";
-
-MastodonNotificationsDatabase::MastodonNotificationsDatabase()
- : AbstractSocialPostCacheDatabase(QStringLiteral("mastodon"), QLatin1String(DB_NAME))
-{
-}
-
-MastodonNotificationsDatabase::~MastodonNotificationsDatabase()
-{
-}
-
-void MastodonNotificationsDatabase::addMastodonNotification(
- const QString &identifier,
- const QString &name,
- const QString &accountName,
- const QString &body,
- const QDateTime &timestamp,
- const QString &icon,
- const QList<QPair<QString, SocialPostImage::ImageType> > &images,
- const QString &url,
- const QString &boostedBy,
- const QString &instanceUrl,
- int account)
-{
- QVariantMap extra;
- extra.insert(ACCOUNT_NAME_KEY, accountName);
- extra.insert(URL_KEY, url);
- extra.insert(BOOSTED_BY_KEY, boostedBy);
- extra.insert(INSTANCE_URL_KEY, instanceUrl);
- addPost(identifier, name, body, timestamp, icon, images, extra, account);
-}
-
-QString MastodonNotificationsDatabase::accountName(const SocialPost::ConstPtr &post)
-{
- if (post.isNull()) {
- return QString();
- }
- return post->extra().value(ACCOUNT_NAME_KEY).toString();
-}
-
-QString MastodonNotificationsDatabase::url(const SocialPost::ConstPtr &post)
-{
- if (post.isNull()) {
- return QString();
- }
- return post->extra().value(URL_KEY).toString();
-}
-
-QString MastodonNotificationsDatabase::boostedBy(const SocialPost::ConstPtr &post)
-{
- if (post.isNull()) {
- return QString();
- }
- return post->extra().value(BOOSTED_BY_KEY).toString();
-}
-
-QString MastodonNotificationsDatabase::instanceUrl(const SocialPost::ConstPtr &post)
-{
- if (post.isNull()) {
- return QString();
- }
- return post->extra().value(INSTANCE_URL_KEY).toString();
-}
diff --git a/common/mastodonnotificationsdatabase.h b/common/mastodonnotificationsdatabase.h
deleted file mode 100644
index cc69095..0000000
--- a/common/mastodonnotificationsdatabase.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (C) 2013-2026 Jolla Ltd.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef MASTODONNOTIFICATIONSDATABASE_H
-#define MASTODONNOTIFICATIONSDATABASE_H
-
-#include <socialcache/abstractsocialpostcachedatabase.h>
-
-class MastodonNotificationsDatabase: public AbstractSocialPostCacheDatabase
-{
- Q_OBJECT
-public:
- MastodonNotificationsDatabase();
- ~MastodonNotificationsDatabase();
-
- void addMastodonNotification(const QString &identifier, const QString &name,
- const QString &accountName, const QString &body,
- const QDateTime &timestamp,
- const QString &icon,
- const QList<QPair<QString, SocialPostImage::ImageType> > &images,
- const QString &url, const QString &boostedBy,
- const QString &instanceUrl,
- int account);
-
- static QString accountName(const SocialPost::ConstPtr &post);
- static QString url(const SocialPost::ConstPtr &post);
- static QString boostedBy(const SocialPost::ConstPtr &post);
- static QString instanceUrl(const SocialPost::ConstPtr &post);
-};
-
-#endif // MASTODONNOTIFICATIONSDATABASE_H