From 991ded863aeb95d0eee505275e3760c3a8dda918 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 16 Apr 2021 09:07:26 +0200 Subject: [PATCH] fetch feedly username in official support too, fetch inoreader for account --- .../feedly/gui/feedlyaccountdetails.cpp | 25 +++++++++++--- .../feedly/gui/feedlyaccountdetails.h | 2 ++ .../gmail/gui/gmailaccountdetails.cpp | 8 ++--- .../services/greader/definitions.h | 1 + .../services/greader/greadernetwork.cpp | 33 +++++++++++++++++++ .../services/greader/greadernetwork.h | 5 ++- .../greader/gui/greaderaccountdetails.cpp | 3 ++ .../services/inoreader/definitions.h | 1 + .../gui/formeditinoreaderaccount.cpp | 6 ++++ .../inoreader/gui/formeditinoreaderaccount.h | 3 ++ .../inoreader/gui/inoreaderaccountdetails.cpp | 24 +++++++++++--- .../inoreader/gui/inoreaderaccountdetails.h | 5 ++- .../inoreader/inoreadernetworkfactory.cpp | 30 +++++++++++++++++ .../inoreader/inoreadernetworkfactory.h | 1 + 14 files changed, 132 insertions(+), 15 deletions(-) diff --git a/src/librssguard/services/feedly/gui/feedlyaccountdetails.cpp b/src/librssguard/services/feedly/gui/feedlyaccountdetails.cpp index 84f7aec65..61a4d67b4 100755 --- a/src/librssguard/services/feedly/gui/feedlyaccountdetails.cpp +++ b/src/librssguard/services/feedly/gui/feedlyaccountdetails.cpp @@ -17,7 +17,7 @@ #include -FeedlyAccountDetails::FeedlyAccountDetails(QWidget* parent) : QWidget(parent) { +FeedlyAccountDetails::FeedlyAccountDetails(QWidget* parent) : QWidget(parent), m_lastProxy({}) { #if defined(FEEDLY_OFFICIAL_SUPPORT) m_oauth = nullptr; #endif @@ -94,14 +94,30 @@ void FeedlyAccountDetails::onAuthError(const QString& error, const QString& deta } void FeedlyAccountDetails::onAuthGranted() { - m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok, - tr("Tested successfully. You may be prompted to login once more."), - tr("Your access was approved.")); + FeedlyNetwork factory; + + factory.setOauth(m_oauth); + + try { + auto prof = factory.profile(m_lastProxy); + + m_ui.m_txtUsername->lineEdit()->setText(prof["email"].toString()); + m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok, + tr("Tested successfully. You may be prompted to login once more."), + tr("Your access was approved.")); + } + catch (const ApplicationException& ex) { + qCriticalNN << LOGSEC_FEEDLY + << "Failed to obtain profile with error:" + << QUOTE_W_SPACE_DOT(ex.message()); + } } #endif void FeedlyAccountDetails::performTest(const QNetworkProxy& custom_proxy) { + m_lastProxy = custom_proxy; + #if defined(FEEDLY_OFFICIAL_SUPPORT) m_oauth->logout(false); @@ -113,7 +129,6 @@ void FeedlyAccountDetails::performTest(const QNetworkProxy& custom_proxy) { FeedlyNetwork factory; - factory.setUsername(m_ui.m_txtUsername->lineEdit()->text()); factory.setDeveloperAccessToken(m_ui.m_txtDeveloperAccessToken->lineEdit()->text()); try { diff --git a/src/librssguard/services/feedly/gui/feedlyaccountdetails.h b/src/librssguard/services/feedly/gui/feedlyaccountdetails.h index 26a19e604..5d3d366f2 100755 --- a/src/librssguard/services/feedly/gui/feedlyaccountdetails.h +++ b/src/librssguard/services/feedly/gui/feedlyaccountdetails.h @@ -45,6 +45,8 @@ class FeedlyAccountDetails : public QWidget { #if defined(FEEDLY_OFFICIAL_SUPPORT) OAuth2Service* m_oauth; #endif + + QNetworkProxy m_lastProxy; }; #endif // FEEDLYACCOUNTDETAILS_H diff --git a/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp b/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp index 87a3da4af..024d3cecf 100644 --- a/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp +++ b/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp @@ -90,11 +90,11 @@ void GmailAccountDetails::onAuthGranted() { tr("Tested successfully. You may be prompted to login once more."), tr("Your access was approved.")); - GmailNetworkFactory fac; - - fac.setOauth(m_oauth); - try { + GmailNetworkFactory fac; + + fac.setOauth(m_oauth); + auto resp = fac.getProfile(m_lastProxy); m_ui.m_txtUsername->lineEdit()->setText(resp["emailAddress"].toString()); diff --git a/src/librssguard/services/greader/definitions.h b/src/librssguard/services/greader/definitions.h index 289cae29c..70025d8bf 100755 --- a/src/librssguard/services/greader/definitions.h +++ b/src/librssguard/services/greader/definitions.h @@ -29,6 +29,7 @@ #define GREADER_API_STREAM_CONTENTS "reader/api/0/stream/contents/%1?output=json&n=%2" #define GREADER_API_EDIT_TAG "reader/api/0/edit-tag" #define GREADER_API_TOKEN "reader/api/0/token" +#define GREADER_API_USER_INFO "reader/api/0/user-info?output=json" // Misc. #define GREADER_API_EDIT_TAG_BATCH 200 diff --git a/src/librssguard/services/greader/greadernetwork.cpp b/src/librssguard/services/greader/greadernetwork.cpp index db966d998..c65672d1d 100755 --- a/src/librssguard/services/greader/greadernetwork.cpp +++ b/src/librssguard/services/greader/greadernetwork.cpp @@ -3,6 +3,8 @@ #include "services/greader/greadernetwork.h" #include "3rd-party/boolinq/boolinq.h" +#include "exceptions/applicationexception.h" +#include "exceptions/networkexception.h" #include "miscellaneous/application.h" #include "network-web/networkfactory.h" #include "network-web/webfactory.h" @@ -90,6 +92,34 @@ QNetworkReply::NetworkError GreaderNetwork::editLabels(const QString& state, return QNetworkReply::NetworkError::NoError; } +QVariantHash GreaderNetwork::userInfo(const QNetworkProxy& proxy) { + QString full_url = generateFullUrl(Operations::UserInfo); + int timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt(); + QNetworkReply::NetworkError network_err; + + if (!ensureLogin(proxy, &network_err)) { + throw NetworkException(network_err); + } + + QByteArray output; + auto res = NetworkFactory::performNetworkOperation(full_url, + timeout, + {}, + output, + QNetworkAccessManager::Operation::GetOperation, + { authHeader() }, + false, + {}, + {}, + proxy); + + if (res.first != QNetworkReply::NetworkError::NoError) { + throw NetworkException(res.first); + } + + return QJsonDocument::fromJson(output).object().toVariantHash(); +} + QNetworkReply::NetworkError GreaderNetwork::markMessagesRead(RootItem::ReadStatus status, const QStringList& msg_custom_ids, const QNetworkProxy& proxy) { @@ -617,6 +647,9 @@ QString GreaderNetwork::generateFullUrl(GreaderNetwork::Operations operation) co case Operations::StreamContents: return sanitizedBaseUrl() + GREADER_API_STREAM_CONTENTS; + case Operations::UserInfo: + return sanitizedBaseUrl() + GREADER_API_USER_INFO; + case Operations::EditTag: return sanitizedBaseUrl() + GREADER_API_EDIT_TAG; diff --git a/src/librssguard/services/greader/greadernetwork.h b/src/librssguard/services/greader/greadernetwork.h index 0b09ea4df..a50681821 100755 --- a/src/librssguard/services/greader/greadernetwork.h +++ b/src/librssguard/services/greader/greadernetwork.h @@ -19,7 +19,8 @@ class GreaderNetwork : public QObject { SubscriptionList, StreamContents, EditTag, - Token + Token, + UserInfo }; explicit GreaderNetwork(QObject* parent = nullptr); @@ -35,6 +36,8 @@ class GreaderNetwork : public QObject { QNetworkReply::NetworkError editLabels(const QString& state, bool assign, const QStringList& msg_custom_ids, const QNetworkProxy& proxy); + QVariantHash userInfo(const QNetworkProxy& proxy); + // Stream contents for a feed/label/etc. QList streamContents(ServiceRoot* root, const QString& stream_id, Feed::Status& error, const QNetworkProxy& proxy); diff --git a/src/librssguard/services/greader/gui/greaderaccountdetails.cpp b/src/librssguard/services/greader/gui/greaderaccountdetails.cpp index a12ad7037..53a15aa38 100755 --- a/src/librssguard/services/greader/gui/greaderaccountdetails.cpp +++ b/src/librssguard/services/greader/gui/greaderaccountdetails.cpp @@ -3,11 +3,14 @@ #include "services/greader/gui/greaderaccountdetails.h" #include "definitions/definitions.h" +#include "exceptions/applicationexception.h" #include "gui/guiutilities.h" #include "miscellaneous/systemfactory.h" #include "services/greader/definitions.h" #include "services/greader/greadernetwork.h" +#include + GreaderAccountDetails::GreaderAccountDetails(QWidget* parent) : QWidget(parent) { m_ui.setupUi(this); diff --git a/src/librssguard/services/inoreader/definitions.h b/src/librssguard/services/inoreader/definitions.h index 83d81214a..8b5d1dfb9 100644 --- a/src/librssguard/services/inoreader/definitions.h +++ b/src/librssguard/services/inoreader/definitions.h @@ -29,5 +29,6 @@ #define INOREADER_API_LIST_LABELS "https://www.inoreader.com/reader/api/0/tag/list?types=1" #define INOREADER_API_LIST_FEEDS "https://www.inoreader.com/reader/api/0/subscription/list" #define INOREADER_API_EDIT_TAG "https://www.inoreader.com/reader/api/0/edit-tag" +#define INOREADER_API_USER_INFO "https://www.inoreader.com/reader/api/0/user-info" #endif // INOREADER_DEFINITIONS_H diff --git a/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.cpp b/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.cpp index 81179166b..c8f1bb10a 100644 --- a/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.cpp +++ b/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.cpp @@ -19,6 +19,8 @@ FormEditInoreaderAccount::FormEditInoreaderAccount(QWidget* parent) insertCustomTab(m_details, tr("Server setup"), 0); activateTab(0); + connect(m_details->m_ui.m_btnTestSetup, &QPushButton::clicked, this, &FormEditInoreaderAccount::testSetup); + m_details->m_ui.m_txtUsername->setFocus(); } @@ -58,3 +60,7 @@ void FormEditInoreaderAccount::loadAccountData() { m_details->m_ui.m_spinLimitMessages->setValue(account()->network()->batchSize()); m_details->m_ui.m_cbDownloadOnlyUnreadMessages->setChecked(account()->network()->downloadOnlyUnreadMessages()); } + +void FormEditInoreaderAccount::testSetup() { + m_details->testSetup(m_proxyDetails->proxy()); +} diff --git a/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.h b/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.h index c885bd912..e99b0272d 100644 --- a/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.h +++ b/src/librssguard/services/inoreader/gui/formeditinoreaderaccount.h @@ -20,6 +20,9 @@ class FormEditInoreaderAccount : public FormAccountDetails { protected: virtual void loadAccountData(); + private slots: + void testSetup(); + private: InoreaderAccountDetails* m_details; }; diff --git a/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.cpp b/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.cpp index f78ab73ca..13e19aeb4 100755 --- a/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.cpp +++ b/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.cpp @@ -2,6 +2,7 @@ #include "services/inoreader/gui/inoreaderaccountdetails.h" +#include "exceptions/applicationexception.h" #include "gui/guiutilities.h" #include "miscellaneous/application.h" #include "network-web/oauth2service.h" @@ -10,7 +11,7 @@ #include "services/inoreader/inoreadernetworkfactory.h" InoreaderAccountDetails::InoreaderAccountDetails(QWidget* parent) - : QWidget(parent), m_oauth(nullptr) { + : QWidget(parent), m_oauth(nullptr), m_lastProxy({}) { m_ui.setupUi(this); GuiUtilities::setLabelAsNotice(*m_ui.m_lblInfo, true); @@ -43,7 +44,6 @@ InoreaderAccountDetails::InoreaderAccountDetails(QWidget* parent) connect(m_ui.m_txtAppKey->lineEdit(), &BaseLineEdit::textChanged, this, &InoreaderAccountDetails::checkOAuthValue); connect(m_ui.m_txtRedirectUrl->lineEdit(), &BaseLineEdit::textChanged, this, &InoreaderAccountDetails::checkOAuthValue); connect(m_ui.m_txtUsername->lineEdit(), &BaseLineEdit::textChanged, this, &InoreaderAccountDetails::checkUsername); - connect(m_ui.m_btnTestSetup, &QPushButton::clicked, this, &InoreaderAccountDetails::testSetup); connect(m_ui.m_btnRegisterApi, &QPushButton::clicked, this, &InoreaderAccountDetails::registerApi); emit m_ui.m_txtUsername->lineEdit()->textChanged(m_ui.m_txtUsername->lineEdit()->text()); @@ -54,12 +54,13 @@ InoreaderAccountDetails::InoreaderAccountDetails(QWidget* parent) hookNetwork(); } -void InoreaderAccountDetails::testSetup() { +void InoreaderAccountDetails::testSetup(const QNetworkProxy& custom_proxy) { + m_lastProxy = custom_proxy; + m_oauth->logout(); m_oauth->setClientId(m_ui.m_txtAppId->lineEdit()->text()); m_oauth->setClientSecret(m_ui.m_txtAppKey->lineEdit()->text()); m_oauth->setRedirectUrl(m_ui.m_txtRedirectUrl->lineEdit()->text()); - m_oauth->login(); } @@ -90,6 +91,21 @@ void InoreaderAccountDetails::onAuthGranted() { m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok, tr("Tested successfully. You may be prompted to login once more."), tr("Your access was approved.")); + + try { + InoreaderNetworkFactory fac; + + fac.setOauth(m_oauth); + + auto resp = fac.userInfo(m_lastProxy); + + m_ui.m_txtUsername->lineEdit()->setText(resp["userEmail"].toString()); + } + catch (const ApplicationException& ex) { + qCriticalNN << LOGSEC_INOREADER + << "Failed to obtain profile with error:" + << QUOTE_W_SPACE_DOT(ex.message()); + } } void InoreaderAccountDetails::hookNetwork() { diff --git a/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.h b/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.h index 389297f8f..b75d68c3b 100755 --- a/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.h +++ b/src/librssguard/services/inoreader/gui/inoreaderaccountdetails.h @@ -7,6 +7,8 @@ #include "ui_inoreaderaccountdetails.h" +#include + class OAuth2Service; class InoreaderAccountDetails : public QWidget { @@ -19,7 +21,7 @@ class InoreaderAccountDetails : public QWidget { private slots: void registerApi(); - void testSetup();; + void testSetup(const QNetworkProxy& custom_proxy);; void checkOAuthValue(const QString& value); void checkUsername(const QString& username); void onAuthFailed(); @@ -38,6 +40,7 @@ class InoreaderAccountDetails : public QWidget { // If editing existing account, then the pointer points // directly to existing OAuth from the account. OAuth2Service* m_oauth; + QNetworkProxy m_lastProxy; }; #endif // INOREADERACCOUNTDETAILS_H diff --git a/src/librssguard/services/inoreader/inoreadernetworkfactory.cpp b/src/librssguard/services/inoreader/inoreadernetworkfactory.cpp index 94fb51f48..1418ad6c8 100755 --- a/src/librssguard/services/inoreader/inoreadernetworkfactory.cpp +++ b/src/librssguard/services/inoreader/inoreadernetworkfactory.cpp @@ -5,6 +5,8 @@ #include "3rd-party/boolinq/boolinq.h" #include "database/databasequeries.h" #include "definitions/definitions.h" +#include "exceptions/applicationexception.h" +#include "exceptions/networkexception.h" #include "gui/dialogs/formmain.h" #include "gui/tabwidget.h" #include "miscellaneous/application.h" @@ -131,6 +133,34 @@ RootItem* InoreaderNetworkFactory::feedsCategories(bool obtain_icons) { return decodeFeedCategoriesData(output_labels, output_feeds, obtain_icons); } +QVariantHash InoreaderNetworkFactory::userInfo(const QNetworkProxy& custom_proxy) { + QString bearer = m_oauth2->bearer().toLocal8Bit(); + + if (bearer.isEmpty()) { + throw ApplicationException(tr("not logged in")); + } + + int timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt(); + QByteArray output; + auto res = NetworkFactory::performNetworkOperation(INOREADER_API_USER_INFO, + timeout, + {}, + output, + QNetworkAccessManager::Operation::GetOperation, + { { QString(HTTP_HEADERS_AUTHORIZATION).toLocal8Bit(), + bearer.toLocal8Bit() } }, + false, + {}, + {}, + custom_proxy); + + if (res.first != QNetworkReply::NetworkError::NoError) { + throw NetworkException(res.first); + } + + return QJsonDocument::fromJson(output).object().toVariantHash(); +} + QList InoreaderNetworkFactory::getLabels() { QList lbls; QString bearer = m_oauth2->bearer().toLocal8Bit(); diff --git a/src/librssguard/services/inoreader/inoreadernetworkfactory.h b/src/librssguard/services/inoreader/inoreadernetworkfactory.h index 580c66350..90e12dd3e 100755 --- a/src/librssguard/services/inoreader/inoreadernetworkfactory.h +++ b/src/librssguard/services/inoreader/inoreadernetworkfactory.h @@ -39,6 +39,7 @@ class InoreaderNetworkFactory : public QObject { // Returned items do not have primary IDs assigned. RootItem* feedsCategories(bool obtain_icons); + QVariantHash userInfo(const QNetworkProxy& custom_proxy); QList getLabels(); QList messages(ServiceRoot* root, const QString& stream_id, Feed::Status& error); QNetworkReply::NetworkError editLabels(const QString& state, bool assign, const QStringList& msg_custom_ids);