PoC that scriptable msgs filtering could work. #31 #225.

This commit is contained in:
Martin Rotter 2020-06-18 15:29:50 +02:00
parent dfdf2c6f65
commit 0b2d752c3b
4 changed files with 103 additions and 2 deletions

View File

@ -2,7 +2,7 @@ CONFIG *= c++1z warn_on resources_big
CONFIG -= debug_and_release
DEFINES *= QT_USE_QSTRINGBUILDER QT_USE_FAST_CONCATENATION QT_USE_FAST_OPERATOR_PLUS UNICODE _UNICODE
VERSION = $$APP_VERSION
QT *= core gui widgets sql network xml
QT *= core gui widgets sql network xml qml
equals(USE_WEBENGINE, true) {
message($$MSG_PREFIX: Application will be compiled WITH QtWebEngine module.)

View File

@ -7,6 +7,7 @@
#include "services/abstract/feed.h"
#include <QDebug>
#include <QJSEngine>
#include <QMutexLocker>
#include <QRegularExpression>
#include <QString>
@ -99,6 +100,43 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
.remove(QRegularExpression(QSL("([\\n\\r])|(^\\s)")));
}
/*
///// Initial PoC for JS-based msgs filtering engine.
// Perform per-message filtering.
QJSEngine filter_engine;
// Create JavaScript communication wrapper for the message.
MessageObject msg_obj;
// Register the wrapper.
auto js_object = filter_engine.newQObject(&msg_obj);
filter_engine.globalObject().setProperty("msg", js_object);
for (int i = 0; i < msgs.size(); i++) {
// Attach live message object to wrapper.
msg_obj.setMessage(&msgs[i]);
// Call the filtering logic, given function must return integer value from
// FilteringAction enumeration.
// All Qt properties of MessageObject class are accessible.
// For example msg.title.includes("A") returns true if message's title includes "A" etc.
QJSValue filter_func = filter_engine.evaluate("(function() { "
"msg.duplicationAttributeCheck = 3;"
"return msg.isDuplicate ? 2 : 8; "
"})");
auto filter_output = filter_func.call().toInt();
FilteringAction decision = FilteringAction(filter_output);
// Do something according to decision.
//bool should_skip = PerformFilteringAction(&msgs[i]);
//if (should_skip) {
// msgs.removeAt(i--);
//}
}
*/
m_feedsUpdated++;
// Now make sure, that messages are actually stored to SQL in a locked state.

View File

@ -128,3 +128,26 @@ uint qHash(const Message& key, uint seed) {
uint qHash(const Message& key) {
return (uint(key.m_accountId) * 10000) + uint(key.m_id);
}
MessageObject::MessageObject(QObject* parent) : QObject(parent), m_message(nullptr) {}
void MessageObject::setMessage(const Message* message) {
m_message = message;
}
bool MessageObject::isDuplicate() const {
// TODO: Check database according to duplication attribute_check.
return m_duplicationAttributeCheck == 3;
}
QString MessageObject::title() const {
return m_message->m_title;
}
int MessageObject::duplicationAttributeCheck() const {
return m_duplicationAttributeCheck;
}
void MessageObject::setDuplicationAttributeCheck(int duplicationAttributeCheck) {
m_duplicationAttributeCheck = duplicationAttributeCheck;
}

View File

@ -46,7 +46,6 @@ class Message {
QString m_customHash;
bool m_isRead;
bool m_isImportant;
QList<Enclosure> m_enclosures;
// Is true if "created" date was obtained directly
@ -71,4 +70,45 @@ QDataStream& operator>>(QDataStream& in, Message& myObj);
uint qHash(const Message& key, uint seed);
uint qHash(const Message& key);
enum class FilteringAction {
Accept = 1,
Ignore = 2,
MarkRead = 4,
MarkUnread = 8,
MarkImportant = 16,
MarkUnimportant = 32
};
enum class DuplicationAttributeCheck {
SameTitle = 1,
SameUrl = 2,
SameAuthor = 4,
SameFeed = 8,
SameDateCreated = 16
};
class MessageObject : public QObject {
Q_OBJECT
Q_PROPERTY(QString title READ title)
Q_PROPERTY(bool isDuplicate READ isDuplicate)
Q_PROPERTY(int duplicationAttributeCheck READ duplicationAttributeCheck WRITE setDuplicationAttributeCheck)
public:
explicit MessageObject(QObject* parent = nullptr);
void setMessage(const Message* message);
int duplicationAttributeCheck() const;
void setDuplicationAttributeCheck(int duplicationAttributeCheck);
bool isDuplicate() const;
// Generic Message's properties bindings.
QString title() const;
private:
const Message* m_message;
int m_duplicationAttributeCheck;
};
#endif // MESSAGE_H