Skip to content

Commit bc739ca

Browse files
author
Martin Rotter
committed
fix #420 and add some more fixes for XML -> json utility function, also make filtering dialog work with "rawContents" to enable basic testing
1 parent ac6092f commit bc739ca

File tree

6 files changed

+70
-26
lines changed

6 files changed

+70
-26
lines changed

resources/docs/Message-filters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Here is the reference of methods and properties of some types available in your
5050
| `String url` | URL of the message. |
5151
| `String author` | Author of the message. |
5252
| `String contents` | Contents of the message. |
53-
| `String rawContents` | This is RAW contents of the message as it was obtained from remote service/feed. You can expect raw `XML` or `JSON` element data here. Note that this attribute has some value only if `alreadyStoredInDb` returns `false`. In other words, this attribute is not persistently stored inside RSS Guard's DB. Also, this attribute might not be filled when testing the filter, it is only filled during live filter execution on real incoming messages. |
53+
| `String rawContents` | This is RAW contents of the message as it was obtained from remote service/feed. You can expect raw `XML` or `JSON` element data here. Note that this attribute has some value only if `alreadyStoredInDb` returns `false`. In other words, this attribute is not persistently stored inside RSS Guard's DB. Also, this attribute is artificially filled with ATOM-like data when testing the filter. |
5454
| `Number score` | Arbitrary number in range <0.0, 100.0>. You can use this number to sort messages in a custom fashion as this attribute also has its own column in messages list. |
5555
| `Date created` | Date/time of the message. |
5656
| `Boolean isRead` | Is message read? |

src/librssguard/core/filterutils.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ QString jsonProcessXmlElement(const QDomElement& elem) {
3333
}
3434

3535
QStringList elems;
36+
QString elem_text;
3637

3738
for (int i = 0; i < elem.childNodes().size(); i++) {
39+
if (elem.childNodes().at(i).isText()) {
40+
elem_text = jsonEscapeString(elem.childNodes().at(i).nodeValue());
41+
}
42+
3843
if (!elem.childNodes().at(i).isElement()) {
3944
continue;
4045
}
@@ -43,22 +48,26 @@ QString jsonProcessXmlElement(const QDomElement& elem) {
4348
jsonProcessXmlElement(elem.childNodes().at(i).toElement()));
4449
}
4550

46-
if (attrs.isEmpty()) {
47-
if (elems.isEmpty()) {
48-
return QSL("\"%1\"").arg(jsonEscapeString(elem.text()));
49-
}
50-
else {
51-
return QSL("{%1}").arg(elems.join(QSL(",\n")));
52-
}
51+
QString str;
52+
53+
if (!elems.isEmpty() && !attrs.isEmpty()) {
54+
str = QSL("{%1, %2, %3}").arg(attrs.join(QSL(",\n")),
55+
elems.join(QSL(",\n")),
56+
QSL("\"__text\": \"%1\"").arg(elem_text));
57+
}
58+
else if (!elems.isEmpty()) {
59+
str = QSL("{%1, %2}").arg(elems.join(QSL(",\n")),
60+
QSL("\"__text\": \"%1\"").arg(elem_text));
5361
}
54-
else if (elems.isEmpty()) {
55-
return QSL("{%1, \"__text\": \"%2\"}").arg(attrs.join(QSL(",\n")),
56-
jsonEscapeString(elem.text()));
62+
else if (!attrs.isEmpty()) {
63+
str = QSL("{%1, %2}").arg(attrs.join(QSL(",\n")),
64+
QSL("\"__text\": \"%1\"").arg(elem_text));
5765
}
5866
else {
59-
return QSL("{%1, %2}").arg(attrs.join(QSL(",\n")),
60-
elems.join(QSL(",\n")));
67+
str = QSL("{%1}").arg(QSL("\"__text\": \"%1\"").arg(elem_text));
6168
}
69+
70+
return str;
6271
}
6372

6473
QString FilterUtils::fromXmlToJson(const QString& xml) const {

src/librssguard/core/message.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ Message Message::fromSqlRecord(const QSqlRecord& record, bool* result) {
120120
return message;
121121
}
122122

123+
QString Message::generateRawAtomContents(const Message& msg) {
124+
return QSL("<entry>"
125+
"<title>%1</title>"
126+
"<link href=\"%2\" rel=\"alternate\" type=\"text/html\" title=\"%1\"/>"
127+
"<published>%3</published>"
128+
"<author><name>%6</name></author>"
129+
"<updated>%3</updated>"
130+
"<id>%4</id>"
131+
"<summary type=\"html\">%5</summary>"
132+
"</entry>").arg(msg.m_title,
133+
msg.m_url,
134+
msg.m_created.toUTC().toString(QSL("yyyy-MM-ddThh:mm:ss")),
135+
msg.m_url,
136+
msg.m_contents.toHtmlEscaped(),
137+
msg.m_author);
138+
}
139+
123140
QDataStream& operator<<(QDataStream& out, const Message& my_obj) {
124141
out << my_obj.m_accountId
125142
<< my_obj.m_customHash

src/librssguard/core/message.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class RSSGUARD_DLLSPEC Message {
3939
// Creates Message from given record, which contains
4040
// row from query SELECT * FROM Messages WHERE ....;
4141
static Message fromSqlRecord(const QSqlRecord& record, bool* result = nullptr);
42+
static QString generateRawAtomContents(const Message& msg);
4243

4344
public:
4445
QString m_title;

src/librssguard/core/messagesforfiltersmodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ void MessagesForFiltersModel::testFilter(MessageFilter* filter, QJSEngine* engin
117117
for (int i = 0; i < m_messages.size(); i++) {
118118
Message* msg = messageForRow(i);
119119

120+
msg->m_rawContents = Message::generateRawAtomContents(*msg);
120121
msg_proxy->setMessage(msg);
121122

122123
try {

src/librssguard/gui/dialogs/formmessagefiltersmanager.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,13 @@ void FormMessageFiltersManager::testFilter() {
263263
" Author = '%3'\n"
264264
" Is read/important = '%4/%5'\n"
265265
" Created on = '%6'\n"
266-
" Contents = '%7'").arg(msg.m_title, msg.m_url, msg.m_author,
267-
msg.m_isRead ? tr("yes") : tr("no"),
268-
msg.m_isImportant ? tr("yes") : tr("no"),
269-
QString::number(msg.m_created.toMSecsSinceEpoch()),
270-
msg.m_contents);
266+
" Contents = '%7'\n"
267+
" RAW contents = '%8'").arg(msg.m_title, msg.m_url, msg.m_author,
268+
msg.m_isRead ? tr("yes") : tr("no"),
269+
msg.m_isImportant ? tr("yes") : tr("no"),
270+
QString::number(msg.m_created.toMSecsSinceEpoch()),
271+
msg.m_contents,
272+
msg.m_rawContents);
271273

272274
m_ui.m_txtErrors->insertPlainText(answer);
273275
}
@@ -315,22 +317,35 @@ void FormMessageFiltersManager::processCheckedFeeds() {
315317

316318
// Create backup of message.
317319
Message* msg = &msgs[i]; msg->m_assignedLabels = labels_in_message;
320+
321+
msg->m_rawContents = Message::generateRawAtomContents(*msg);
322+
318323
Message msg_backup(*msg);
319324

320325
msg_obj.setMessage(msg);
321326

322-
MessageObject::FilteringAction result = fltr->filterMessage(&filter_engine);
323327
bool remove_from_list = false;
324328

325-
if (result == MessageObject::FilteringAction::Purge) {
326-
remove_from_list = true;
329+
try {
330+
MessageObject::FilteringAction result = fltr->filterMessage(&filter_engine);
331+
332+
if (result == MessageObject::FilteringAction::Purge) {
333+
remove_from_list = true;
327334

328-
// Purge the message completely and remove leftovers.
329-
DatabaseQueries::purgeMessage(database, msg->m_id);
330-
DatabaseQueries::purgeLeftoverLabelAssignments(database, msg->m_accountId);
335+
// Purge the message completely and remove leftovers.
336+
DatabaseQueries::purgeMessage(database, msg->m_id);
337+
DatabaseQueries::purgeLeftoverLabelAssignments(database, msg->m_accountId);
338+
}
339+
else if (result == MessageObject::FilteringAction::Ignore) {
340+
remove_from_list = true;
341+
}
331342
}
332-
else if (result == MessageObject::FilteringAction::Ignore) {
333-
remove_from_list = true;
343+
catch (const FilteringException& ex) {
344+
qCriticalNN << LOGSEC_CORE
345+
<< "Error when running script when processing existing messages:"
346+
<< QUOTE_W_SPACE_DOT(ex.message());
347+
348+
continue;
334349
}
335350

336351
if (!msg_backup.m_isRead && msg->m_isRead) {
@@ -585,6 +600,7 @@ Message FormMessageFiltersManager::testingMessage() const {
585600
msg.m_isImportant = m_ui.m_cbSampleImportant->isChecked();
586601
msg.m_created = QDateTime::fromMSecsSinceEpoch(m_ui.m_txtSampleCreatedOn->text().toLongLong());
587602
msg.m_contents = m_ui.m_txtSampleContents->toPlainText();
603+
msg.m_rawContents = Message::generateRawAtomContents(msg);
588604

589605
return msg;
590606
}

0 commit comments

Comments
 (0)