unify filtering engine initialisation, some other fixes

This commit is contained in:
Martin Rotter 2020-09-23 10:36:59 +02:00
parent 626a216b94
commit 9f2983d49c
5 changed files with 18 additions and 13 deletions

View File

@ -123,16 +123,15 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
// Perform per-message filtering.
QJSEngine filter_engine;
MessageFilter::initializeFilteringEngine(filter_engine);
// Create JavaScript communication wrapper for the message.
MessageObject msg_obj(&database, feed->customId(), feed->getParentServiceRoot()->accountId());
// Register the wrapper.
auto js_object = filter_engine.newQObject(&msg_obj);
filter_engine.installExtensions(QJSEngine::Extension::ConsoleExtension);
filter_engine.globalObject().setProperty("msg", js_object);
filter_engine.globalObject().setProperty("MSG_ACCEPT", int(FilteringAction::Accept));
filter_engine.globalObject().setProperty("MSG_IGNORE", int(FilteringAction::Ignore));
qDebugNN << LOGSEC_FEEDDOWNLOADER << "Setting up JS evaluation took " << tmr.nsecsElapsed() / 1000 << " microseconds.";

View File

@ -5,8 +5,6 @@
#include "core/message.h"
#include "exceptions/filteringexception.h"
#include <QJSEngine>
MessageFilter::MessageFilter(int id, QObject* parent) : QObject(parent), m_id(id) {}
FilteringAction MessageFilter::filterMessage(QJSEngine* engine) {
@ -81,6 +79,12 @@ void MessageFilter::setScript(const QString& script) {
m_script = script;
}
void MessageFilter::initializeFilteringEngine(QJSEngine& engine) {
engine.installExtensions(QJSEngine::Extension::ConsoleExtension);
engine.globalObject().setProperty("MSG_ACCEPT", int(FilteringAction::Accept));
engine.globalObject().setProperty("MSG_IGNORE", int(FilteringAction::Ignore));
}
void MessageFilter::setId(int id) {
m_id = id;
}

View File

@ -7,7 +7,7 @@
#include "core/message.h"
class QJSEngine;
#include <QJSEngine>
// Class which represents one message filter.
class MessageFilter : public QObject {
@ -27,6 +27,8 @@ class MessageFilter : public QObject {
QString script() const;
void setScript(const QString& script);
static void initializeFilteringEngine(QJSEngine& engine);
private:
int m_id;
QString m_name;

View File

@ -147,6 +147,9 @@ void FormMessageFiltersManager::loadFilter() {
void FormMessageFiltersManager::testFilter() {
// Perform per-message filtering.
QJSEngine filter_engine;
MessageFilter::initializeFilteringEngine(filter_engine);
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
// Create JavaScript communication wrapper for the message.
@ -155,10 +158,7 @@ void FormMessageFiltersManager::testFilter() {
// Register the wrapper.
auto js_object = filter_engine.newQObject(&msg_obj);
filter_engine.installExtensions(QJSEngine::Extension::ConsoleExtension);
filter_engine.globalObject().setProperty("msg", js_object);
filter_engine.globalObject().setProperty("MSG_ACCEPT", int(FilteringAction::Accept));
filter_engine.globalObject().setProperty("MSG_IGNORE", int(FilteringAction::Ignore));
Message msg = testingMessage();

View File

@ -97,9 +97,9 @@ QString GmailNetworkFactory::sendEmail(Mimesis::Message msg, Message* reply_to_m
if (result.first != QNetworkReply::NetworkError::NoError) {
if (!out.isEmpty()) {
QJsonDocument doc = QJsonDocument::fromJson(out);
auto msg = doc.object()["error"].toObject()["message"].toString();
auto json_message = doc.object()["error"].toObject()["message"].toString();
throw ApplicationException(msg);
throw ApplicationException(json_message);
}
else {
throw ApplicationException(QString::fromUtf8(out));
@ -464,9 +464,9 @@ QMap<QString, QString> GmailNetworkFactory::getMessageMetadata(const QString& ms
if (res.first == QNetworkReply::NetworkError::NoError) {
QJsonDocument doc = QJsonDocument::fromJson(output);
QMap<QString, QString> result;
auto headers = doc.object()["payload"].toObject()["headers"].toArray();
auto json_headers = doc.object()["payload"].toObject()["headers"].toArray();
for (const auto& header : headers) {
for (const auto& header : json_headers) {
QJsonObject obj_header = header.toObject();
result.insert(obj_header["name"].toString(), obj_header["value"].toString());