| 1 | // For license of this file, see |
| 2 | // <project-root-folder>/LICENSE.md. |
| 3 | |
| 4 | #include "network-web/apiserver.h" |
| 5 | |
| 6 | #include "3rd-party/boolinq/boolinq.h" |
| 7 | #include "core/feedsmodel.h" |
| 8 | #include "database/databasefactory.h" |
| 9 | #include "database/databasequeries.h" |
| 10 | #include "definitions/definitions.h" |
| 11 | #include "gui/dialogs/formmain.h" |
| 12 | #include "gui/feedmessageviewer.h" |
| 13 | #include "gui/messagesview.h" |
| 14 | #include "miscellaneous/application.h" |
| 15 | |
| 16 | #include <QJsonArray> |
| 17 | #include <QMetaEnum> |
| 18 | |
| 19 | ApiServer::ApiServer(QObject* parent) : HttpServer(parent) {} |
| 20 | |
| 21 | void ApiServer::answerClient(QTcpSocket* socket, const HttpRequest& request) { |
| 22 | QByteArray incoming_data = socket->readAll(); |
| 23 | QByteArray reply_message; |
| 24 | |
| 25 | if (request.m_method == HttpRequest::Method::Options) { |
| 26 | reply_message = processCorsPreflight(); |
| 27 | } |
| 28 | else if (request.m_url.path().contains("rssguard")) { |
| 29 | reply_message = processHtmlPage(); |
| 30 | } |
| 31 | else { |
| 32 | QJsonParseError json_err; |
| 33 | QByteArray json_data; |
| 34 | QJsonDocument incoming_doc = QJsonDocument::fromJson(incoming_data, &json_err); |
| 35 | |
| 36 | if (json_err.error != QJsonParseError::ParseError::NoError) { |
| 37 | json_data = |
| 38 | ApiResponse(ApiResponse::Result::Error, ApiRequest::Method::Unknown, QJsonValue(json_err.errorString())) |
| 39 | .toJson() |
| 40 | .toJson(); |
| 41 | } |
| 42 | else { |
| 43 | ApiRequest req(incoming_doc); |
| 44 | |
| 45 | try { |
| 46 | ApiResponse resp(processRequest(req)); |
| 47 | |
| 48 | json_data = resp.toJson().toJson(); |
| 49 | } |
| 50 | catch (const ApplicationException& ex) { |
| 51 | ApiResponse err_resp(ApiResponse::Result::Error, req.m_method, ex.message()); |
| 52 | |
| 53 | json_data = err_resp.toJson().toJson(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | reply_message = generateHttpAnswer(200, |
| 58 | {{QSL("Access-Control-Allow-" |
| 59 | "Origin"), |
| 60 | QSL("*")}, |
| 61 | {QSL("Access-Control-Allow-" |
| 62 | "Headers"), |
| 63 | QSL("*")}, |
| 64 | {QSL("Content-Type"), |
| 65 | QSL("application/json; " |
| 66 | "charset=\"utf-8\"")}}, |
| 67 | json_data); |
| 68 | |
| 69 | #if !defined(NDEBUG) |
| 70 | IOFactory::writeFile("a.out", json_data); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | socket->write(reply_message); |
| 75 | socket->disconnectFromHost(); |
| 76 | } |
| 77 | |
| 78 | QByteArray ApiServer::processCorsPreflight() const { |
| 79 | return generateHttpAnswer(204, |
| 80 | {{QSL("Access-Control-Allow-" |
| 81 | "Origin"), |
| 82 | QSL("*")}, |
| 83 | {QSL("Access-Control-Allow-" |
| 84 | "Headers"), |
| 85 | QSL("*")}, |
| 86 | {QSL("Access-Control-Allow-" |
| 87 | "Methods"), |
| 88 | QSL("POST, GET, OPTIONS, " |
| 89 | "DELETE")}}); |
| 90 | } |
| 91 | |
| 92 | QByteArray ApiServer::processHtmlPage() const { |
| 93 | QByteArray page; |
| 94 | QString runtime_page_path = QCoreApplication::applicationDirPath() + QDir::separator() + WEB_UI_FILE; |
| 95 | |
| 96 | if (QFile::exists(runtime_page_path)) { |
| 97 | page = IOFactory::readFile(runtime_page_path); |
| 98 | } |
| 99 | else { |
| 100 | page = IOFactory::readFile(WEB_UI_FOLDER + QL1C('/') + WEB_UI_FILE); |
| 101 | } |
| 102 | |
| 103 | QByteArray data = generateHttpAnswer(200, |
| 104 | {{QSL("Access-Control-Allow-" |
| 105 | "Origin"), |
| 106 | QSL("*")}, |
| 107 | {QSL("Access-Control-Allow-" |
| 108 | "Headers"), |
| 109 | QSL("*")}, |
| 110 | {QSL("Access-Control-Allow-" |
| 111 | "Methods"), |
| 112 | QSL("POST, GET, OPTIONS, " |
| 113 | "DELETE")}, |
| 114 | {QSL("Content-Type"), |
| 115 | QSL("text/html; " |
| 116 | "charset=\"utf-8\"")}}, |
| 117 | page); |
| 118 | |
| 119 | return data; |
| 120 | } |
| 121 | |
| 122 | ApiResponse ApiServer::processRequest(const ApiRequest& req) const { |
| 123 | switch (req.m_method) { |
| 124 | case ApiRequest::Method::AppVersion: |
| 125 | return processAppVersion(); |
| 126 | |
| 127 | case ApiRequest::Method::ArticlesFromFeed: |
| 128 | return processArticlesFromFeed(req.m_parameters); |
| 129 | |
| 130 | case ApiRequest::Method::MarkArticles: |
| 131 | return processMarkArticles(req.m_parameters); |
| 132 | |
| 133 | case ApiRequest::Method::Unknown: |
| 134 | default: |
| 135 | return processUnknown(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | ApiResponse ApiServer::processAppVersion() const { |
| 140 | return ApiResponse(ApiResponse::Result::Success, ApiRequest::Method::AppVersion, QSL(APP_VERSION)); |
| 141 | } |
| 142 | |
| 143 | ApiResponse ApiServer::processMarkArticles(const QJsonValue& req) const { |
| 144 | QJsonObject data = req.toObject(); |
| 145 | |
| 146 | bool mark_read = data.value(QSL("mark_read")).toBool(); |
| 147 | bool mark_unread = data.value(QSL("mark_unread")).toBool(); |
| 148 | bool mark_starred = data.value(QSL("mark_starred")).toBool(); |
| 149 | bool mark_unstarred = data.value(QSL("mark_unstarred")).toBool(); |
| 150 | |
| 151 | QMap<int, QStringList> articles_per_accounts; |
| 152 | |
| 153 | for (const QJsonValue& article_val : data.value(QSL("articles")).toArray()) { |
| 154 | QJsonObject article_obj = article_val.toObject(); |
| 155 | |
| 156 | articles_per_accounts[article_obj.value(QSL("account")).toInt()] |
| 157 | .append(article_obj.value(QSL("article_custom_id")).toString()); |
| 158 | } |
| 159 | |
| 160 | QMapIterator<int, QStringList> articles_per_accounts_iter(articles_per_accounts); |
| 161 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 162 | |
| 163 | RootItem::ReadStatus target_read = mark_read ? RootItem::ReadStatus::Read : RootItem::ReadStatus::Unread; |
| 164 | |
| 165 | if (!mark_read && !mark_unread) { |
| 166 | target_read = RootItem::ReadStatus::Unknown; |
| 167 | } |
| 168 | |
| 169 | RootItem::Importance target_important = |
| 170 | mark_starred ? RootItem::Importance::Important : RootItem::Importance::NotImportant; |
| 171 | |
| 172 | if (!mark_starred && !mark_unstarred) { |
| 173 | target_important = RootItem::Importance::Unknown; |
| 174 | } |
| 175 | |
| 176 | QList<ServiceRoot*> accts = qApp->feedReader()->feedsModel()->serviceRoots(); |
| 177 | auto linq_accts = boolinq::from(accts); |
| 178 | |
| 179 | while (articles_per_accounts_iter.hasNext()) { |
| 180 | auto nxt = articles_per_accounts_iter.next(); |
| 181 | int account_id = nxt.key(); |
| 182 | QStringList custom_ids = nxt.value(); |
| 183 | ServiceRoot* acc = linq_accts.firstOrDefault([=](ServiceRoot* acc) { |
| 184 | return acc->accountId() == account_id; |
| 185 | }); |
| 186 | |
| 187 | if (acc == nullptr) { |
| 188 | throw ApplicationException(tr("account with ID %1 not found").arg(account_id)); |
| 189 | } |
| 190 | |
| 191 | DatabaseQueries::markMessagesReadUnreadImportant(database, account_id, custom_ids, target_read, target_important); |
| 192 | } |
| 193 | |
| 194 | // All updates are done, recalculate. |
| 195 | qApp->feedReader()->feedsModel()->reloadCountsOfWholeModel(); |
| 196 | qApp->mainForm()->tabWidget()->feedMessageViewer()->messagesView()->reloadSelections(); |
| 197 | |
| 198 | ApiResponse resp(ApiResponse::Result::Success, ApiRequest::Method::MarkArticles); |
| 199 | return resp; |
| 200 | } |
| 201 | |
| 202 | ApiResponse ApiServer::processArticlesFromFeed(const QJsonValue& req) const { |
| 203 | QJsonObject data = req.toObject(); |
| 204 | |
| 205 | QString feed_id = data.value(QSL("feed")).toString(); |
| 206 | qint64 start_after_article_date = qint64(data.value(QSL("start_after_article_date")).toDouble()); |
| 207 | int account_id = data.value(QSL("account")).toInt(); |
| 208 | bool newest_first = data.value(QSL("newest_first")).toBool(); |
| 209 | bool unread_only = data.value(QSL("unread_only")).toBool(); |
| 210 | bool starred_only = data.value(QSL("starred_only")).toBool(); |
| 211 | int row_offset = data.value(QSL("row_offset")).toInt(); |
| 212 | int row_limit = data.value(QSL("row_limit")).toInt(100000); |
| 213 | |
| 214 | // NOTE: Fixup arguments. |
| 215 | if (feed_id == QSL("0")) { |
| 216 | feed_id = QString(); |
| 217 | } |
| 218 | |
| 219 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 220 | QList<Message> msgs = DatabaseQueries::getArticlesSlice(database, |
| 221 | feed_id, |
| 222 | account_id, |
| 223 | newest_first, |
| 224 | unread_only, |
| 225 | starred_only, |
| 226 | start_after_article_date, |
| 227 | row_offset, |
| 228 | row_limit); |
| 229 | QJsonArray msgs_json_array; |
| 230 | |
| 231 | for (const Message& msg : msgs) { |
| 232 | msgs_json_array.append(msg.toJson()); |
| 233 | } |
| 234 | |
| 235 | ApiResponse resp(ApiResponse::Result::Success, ApiRequest::Method::ArticlesFromFeed, msgs_json_array); |
| 236 | |
| 237 | return resp; |
| 238 | } |
| 239 | |
| 240 | ApiResponse ApiServer::processUnknown() const { |
| 241 | return ApiResponse(ApiResponse::Result::Error, |
| 242 | ApiRequest::Method::Unknown, |
| 243 | QSL("unknown " |
| 244 | "method")); |
| 245 | } |
| 246 | |
| 247 | ApiResponse::ApiResponse(Result result, ApiRequest::Method method, const QJsonValue& response) |
| 248 | : m_result(result), m_method(method), m_response(response) {} |
| 249 | |
| 250 | QJsonDocument ApiResponse::toJson() const { |
| 251 | QJsonObject obj; |
| 252 | |
| 253 | static QMetaEnum enumer_method = QMetaEnum::fromType<ApiRequest::Method>(); |
| 254 | static QMetaEnum enumer_result = QMetaEnum::fromType<ApiResponse::Result>(); |
| 255 | |
| 256 | obj.insert(QSL("method"), enumer_method.valueToKey(int(m_method))); |
| 257 | obj.insert(QSL("result"), enumer_result.valueToKey(int(m_result))); |
| 258 | |
| 259 | if (!m_response.isNull() && !m_response.isUndefined()) { |
| 260 | obj.insert(QSL("data"), m_response); |
| 261 | } |
| 262 | |
| 263 | return QJsonDocument(obj); |
| 264 | } |
| 265 | |
| 266 | ApiRequest::ApiRequest(const QJsonDocument& data) |
| 267 | : m_method(), m_parameters(data.object().value(QSL("dat" |
| 268 | "a"))) { |
| 269 | static QMetaEnum enumer = QMetaEnum::fromType<ApiRequest::Method>(); |
| 270 | |
| 271 | QByteArray method_name = data.object().value(QSL("method")).toString().toLocal8Bit(); |
| 272 | |
| 273 | m_method = Method(enumer.keysToValue(method_name.constData())); |
| 274 | } |