| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "core/message.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "miscellaneous/textfactory.h" |
| 7 | #include "network-web/webfactory.h" |
| 8 | #include "services/abstract/feed.h" |
| 9 | #include "services/abstract/label.h" |
| 10 | |
| 11 | #include <QDebug> |
| 12 | #include <QFlags> |
| 13 | #include <QJsonArray> |
| 14 | #include <QJsonObject> |
| 15 | #include <QRegularExpression> |
| 16 | #include <QUrl> |
| 17 | #include <QVariant> |
| 18 | #include <QVector> |
| 19 | |
| 20 | Enclosure::Enclosure(QString url, QString mime) : m_url(std::move(url)), m_mimeType(std::move(mime)) {} |
| 21 | |
| 22 | QList<Enclosure> Enclosures::decodeEnclosuresFromString(const QString& enclosures_data) { |
| 23 | QJsonParseError enc_err; |
| 24 | QJsonDocument enc_doc = QJsonDocument::fromJson(enclosures_data.toUtf8(), &enc_err); |
| 25 | QList<Enclosure> enclosures; |
| 26 | |
| 27 | if (enc_err.error != QJsonParseError::ParseError::NoError) { |
| 28 | // Provide backwards compatibility. |
| 29 | auto enc = enclosures_data.split(ENCLOSURES_OUTER_SEPARATOR, |
| 30 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 31 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 32 | #else |
| 33 | QString::SplitBehavior::SkipEmptyParts); |
| 34 | #endif |
| 35 | |
| 36 | enclosures.reserve(enc.size()); |
| 37 | |
| 38 | for (const QString& single_enclosure : std::as_const(enc)) { |
| 39 | Enclosure enclosure; |
| 40 | |
| 41 | if (single_enclosure.contains(ECNLOSURES_INNER_SEPARATOR)) { |
| 42 | QStringList mime_url = single_enclosure.split(ECNLOSURES_INNER_SEPARATOR); |
| 43 | |
| 44 | enclosure.m_mimeType = QString::fromUtf8(QByteArray::fromBase64(mime_url.at(0).toLocal8Bit())); |
| 45 | enclosure.m_url = QString::fromUtf8(QByteArray::fromBase64(mime_url.at(1).toLocal8Bit())); |
| 46 | } |
| 47 | else { |
| 48 | enclosure.m_url = QString::fromUtf8(QByteArray::fromBase64(single_enclosure.toLocal8Bit())); |
| 49 | } |
| 50 | |
| 51 | enclosures.append(enclosure); |
| 52 | } |
| 53 | } |
| 54 | else { |
| 55 | QJsonArray enc_arr = enc_doc.array(); |
| 56 | |
| 57 | for (const QJsonValue& enc_val : enc_arr) { |
| 58 | const QJsonObject& enc_obj = enc_val.toObject(); |
| 59 | |
| 60 | Enclosure enclosure; |
| 61 | |
| 62 | enclosure.m_mimeType = enc_obj.value(QSL("mime")).toString(); |
| 63 | enclosure.m_url = enc_obj.value(QSL("url")).toString(); |
| 64 | |
| 65 | enclosures.append(enclosure); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return enclosures; |
| 70 | } |
| 71 | |
| 72 | QJsonArray Enclosures::encodeEnclosuresToJson(const QList<Enclosure>& enclosures) { |
| 73 | QJsonArray enc_arr; |
| 74 | |
| 75 | for (const Enclosure& enc : enclosures) { |
| 76 | QJsonObject enc_obj; |
| 77 | |
| 78 | enc_obj.insert(QSL("mime"), enc.m_mimeType); |
| 79 | enc_obj.insert(QSL("url"), enc.m_url); |
| 80 | |
| 81 | enc_arr.append(enc_obj); |
| 82 | } |
| 83 | |
| 84 | return enc_arr; |
| 85 | } |
| 86 | |
| 87 | QString Enclosures::encodeEnclosuresToString(const QList<Enclosure>& enclosures) { |
| 88 | return QJsonDocument(encodeEnclosuresToJson(enclosures)).toJson(QJsonDocument::JsonFormat::Compact); |
| 89 | |
| 90 | /* |
| 91 | QStringList enclosures_str; |
| 92 | |
| 93 | for (const Enclosure& enclosure : enclosures) { |
| 94 | if (enclosure.m_mimeType.isEmpty()) { |
| 95 | enclosures_str.append(enclosure.m_url.toUtf8().toBase64()); |
| 96 | } |
| 97 | else { |
| 98 | enclosures_str.append(QString(enclosure.m_mimeType.toUtf8().toBase64()) + ECNLOSURES_INNER_SEPARATOR + |
| 99 | enclosure.m_url.toUtf8().toBase64()); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return enclosures_str.join(QString(ENCLOSURES_OUTER_SEPARATOR)); |
| 104 | */ |
| 105 | } |
| 106 | |
| 107 | Message::Message() { |
| 108 | m_title = m_url = m_author = m_contents = m_rawContents = m_feedId = m_feedTitle = m_customId = m_customHash = |
| 109 | QL1S(""); |
| 110 | m_enclosures = QList<Enclosure>(); |
| 111 | m_categories = QList<MessageCategory>(); |
| 112 | m_accountId = m_id = 0; |
| 113 | m_score = 0.0; |
| 114 | m_isRead = m_isImportant = m_isDeleted = false; |
| 115 | m_rtlBehavior = RtlBehavior::NoRtl; |
| 116 | m_assignedLabels = QList<Label*>(); |
| 117 | m_assignedLabelsByFilter = QList<Label*>(); |
| 118 | m_deassignedLabelsByFilter = QList<Label*>(); |
| 119 | } |
| 120 | |
| 121 | void Message::sanitize(const Feed* feed, bool fix_future_datetimes) { |
| 122 | static QRegularExpression reg_spaces(QString::fromUtf8(QByteArray("[\xE2\x80\xAF]"))); |
| 123 | static QRegularExpression reg_whites(QSL("[\\s]{2,}")); |
| 124 | static QRegularExpression reg_news(QSL("([\\n\\r])|(^\\s)")); |
| 125 | |
| 126 | // Sanitize title. |
| 127 | m_title = qApp->web()->stripTags(WebFactory::unescapeHtml(m_title)); |
| 128 | |
| 129 | m_title = m_title |
| 130 | |
| 131 | // Remove non-breaking spaces. |
| 132 | .replace(reg_spaces, QSL(" ")) |
| 133 | |
| 134 | // Shrink consecutive whitespaces. |
| 135 | .replace(reg_whites, QSL(" ")) |
| 136 | |
| 137 | // Remove all newlines and leading white space. |
| 138 | .remove(reg_news) |
| 139 | |
| 140 | // Remove non-breaking zero-width spaces. |
| 141 | .remove(QChar(65279)); |
| 142 | |
| 143 | // Sanitize author. |
| 144 | m_author = qApp->web()->stripTags(WebFactory::unescapeHtml(m_author)); |
| 145 | |
| 146 | // NOTE: We do not need to de-escape HTML here because it is the job of all plugins to provide correct |
| 147 | // HTML of their articles. |
| 148 | // m_contents = WebFactory::unescapeHtml(m_contents); |
| 149 | |
| 150 | // Sanitize URL. |
| 151 | m_url = m_url.trimmed(); |
| 152 | |
| 153 | // Check if messages contain relative URLs and if they do, then replace them. |
| 154 | if (m_url.startsWith(QL1S("//"))) { |
| 155 | m_url = QSL(URI_SCHEME_HTTPS) + m_url.mid(2); |
| 156 | } |
| 157 | else if (QUrl(m_url).isRelative()) { |
| 158 | QUrl base(feed->source()); |
| 159 | |
| 160 | if (base.isValid()) { |
| 161 | base = QUrl(base.scheme() + QSL("://") + base.host()); |
| 162 | |
| 163 | m_url = base.resolved(m_url).toString(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Fix datetimes in future. |
| 168 | if ((fix_future_datetimes && m_createdFromFeed && m_created.toUTC() > QDateTime::currentDateTimeUtc()) || |
| 169 | (m_createdFromFeed && (!m_created.isValid() || m_created.toSecsSinceEpoch() < 0))) { |
| 170 | qWarningNN << LOGSEC_CORE << "Fixing date of article" << QUOTE_W_SPACE(m_title) << "from invalid date/time" |
| 171 | << QUOTE_W_SPACE_DOT(m_created); |
| 172 | m_createdFromFeed = false; |
| 173 | m_created = QDateTime::currentDateTimeUtc(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | QJsonObject Message::toJson() const { |
| 178 | QJsonObject obj; |
| 179 | |
| 180 | obj.insert(QSL("contents"), m_contents); |
| 181 | obj.insert(QSL("is_read"), m_isRead); |
| 182 | obj.insert(QSL("is_important"), m_isImportant); |
| 183 | obj.insert(QSL("title"), m_title); |
| 184 | obj.insert(QSL("date_created"), m_created.toMSecsSinceEpoch()); |
| 185 | obj.insert(QSL("author"), m_author); |
| 186 | obj.insert(QSL("url"), m_url); |
| 187 | obj.insert(QSL("id"), m_id); |
| 188 | obj.insert(QSL("custom_id"), m_customId); |
| 189 | obj.insert(QSL("account_id"), m_accountId); |
| 190 | obj.insert(QSL("custom_hash"), m_customHash); |
| 191 | obj.insert(QSL("feed_custom_id"), m_feedId); |
| 192 | obj.insert(QSL("feed_title"), m_feedTitle); |
| 193 | obj.insert(QSL("is_rtl"), int(m_rtlBehavior)); |
| 194 | obj.insert(QSL("enclosures"), Enclosures::encodeEnclosuresToJson(m_enclosures)); |
| 195 | |
| 196 | return obj; |
| 197 | } |
| 198 | |
| 199 | Message Message::fromSqlRecord(const QSqlRecord& record, bool* result) { |
| 200 | if (record.count() != MSG_DB_LABELS_IDS + 1) { |
| 201 | if (result != nullptr) { |
| 202 | *result = false; |
| 203 | } |
| 204 | |
| 205 | return Message(); |
| 206 | } |
| 207 | |
| 208 | Message message; |
| 209 | |
| 210 | message.m_id = record.value(MSG_DB_ID_INDEX).toInt(); |
| 211 | message.m_isRead = record.value(MSG_DB_READ_INDEX).toBool(); |
| 212 | message.m_isImportant = record.value(MSG_DB_IMPORTANT_INDEX).toBool(); |
| 213 | message.m_isDeleted = record.value(MSG_DB_DELETED_INDEX).toBool(); |
| 214 | message.m_feedId = record.value(MSG_DB_FEED_CUSTOM_ID_INDEX).toString(); |
| 215 | message.m_feedTitle = record.value(MSG_DB_FEED_TITLE_INDEX).toString(); |
| 216 | message.m_title = record.value(MSG_DB_TITLE_INDEX).toString(); |
| 217 | message.m_url = record.value(MSG_DB_URL_INDEX).toString(); |
| 218 | message.m_author = record.value(MSG_DB_AUTHOR_INDEX).toString(); |
| 219 | message.m_created = TextFactory::parseDateTime(record.value(MSG_DB_DCREATED_INDEX).value<qint64>()); |
| 220 | message.m_contents = record.value(MSG_DB_CONTENTS_INDEX).toString(); |
| 221 | message.m_enclosures = Enclosures::decodeEnclosuresFromString(record.value(MSG_DB_ENCLOSURES_INDEX).toString()); |
| 222 | message.m_score = record.value(MSG_DB_SCORE_INDEX).toDouble(); |
| 223 | message.m_rtlBehavior = record.value(MSG_DB_FEED_IS_RTL_INDEX).value<RtlBehavior>(); |
| 224 | message.m_accountId = record.value(MSG_DB_ACCOUNT_ID_INDEX).toInt(); |
| 225 | message.m_customId = record.value(MSG_DB_CUSTOM_ID_INDEX).toString(); |
| 226 | message.m_customHash = record.value(MSG_DB_CUSTOM_HASH_INDEX).toString(); |
| 227 | message.m_assignedLabelsIds = record.value(MSG_DB_LABELS_IDS) |
| 228 | .toString() |
| 229 | .split('.', |
| 230 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 231 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 232 | #else |
| 233 | QString::SplitBehavior::SkipEmptyParts); |
| 234 | #endif |
| 235 | |
| 236 | if (result != nullptr) { |
| 237 | *result = true; |
| 238 | } |
| 239 | |
| 240 | return message; |
| 241 | } |
| 242 | |
| 243 | QString Message::generateRawAtomContents(const Message& msg) { |
| 244 | return QSL("<entry>" |
| 245 | "<title>%1</title>" |
| 246 | "<link href=\"%2\" rel=\"alternate\" type=\"text/html\" title=\"%1\"/>" |
| 247 | "<published>%3</published>" |
| 248 | "<author><name>%6</name></author>" |
| 249 | "<updated>%3</updated>" |
| 250 | "<id>%4</id>" |
| 251 | "<summary type=\"html\">%5</summary>" |
| 252 | "</entry>") |
| 253 | .arg(msg.m_title, |
| 254 | msg.m_url, |
| 255 | msg.m_created.toUTC().toString(QSL("yyyy-MM-ddThh:mm:ss")), |
| 256 | msg.m_url, |
| 257 | msg.m_contents.toHtmlEscaped(), |
| 258 | msg.m_author); |
| 259 | } |
| 260 | |
| 261 | QDataStream& operator<<(QDataStream& out, const Message& my_obj) { |
| 262 | out << my_obj.m_accountId << my_obj.m_customHash << my_obj.m_customId << my_obj.m_feedId << my_obj.m_id |
| 263 | << my_obj.m_isImportant << my_obj.m_isRead << my_obj.m_isDeleted << my_obj.m_score << my_obj.m_rtlBehavior; |
| 264 | |
| 265 | return out; |
| 266 | } |
| 267 | |
| 268 | QDataStream& operator>>(QDataStream& in, Message& my_obj) { |
| 269 | int account_id; |
| 270 | QString custom_hash; |
| 271 | QString custom_id; |
| 272 | QString feed_id; |
| 273 | int id; |
| 274 | bool is_important; |
| 275 | bool is_read; |
| 276 | bool is_deleted; |
| 277 | RtlBehavior is_rtl; |
| 278 | double score; |
| 279 | |
| 280 | in >> account_id >> custom_hash >> custom_id >> feed_id >> id >> is_important >> is_read >> is_deleted >> score >> |
| 281 | is_rtl; |
| 282 | |
| 283 | my_obj.m_accountId = account_id; |
| 284 | my_obj.m_customHash = custom_hash; |
| 285 | my_obj.m_customId = custom_id; |
| 286 | my_obj.m_feedId = feed_id; |
| 287 | my_obj.m_id = id; |
| 288 | my_obj.m_isImportant = is_important; |
| 289 | my_obj.m_isRead = is_read; |
| 290 | my_obj.m_isDeleted = is_deleted; |
| 291 | my_obj.m_score = score; |
| 292 | my_obj.m_rtlBehavior = is_rtl; |
| 293 | |
| 294 | return in; |
| 295 | } |
| 296 | |
| 297 | uint qHash(const Message& key, uint seed) { |
| 298 | Q_UNUSED(seed) |
| 299 | return (uint(key.m_accountId) * 10000) + uint(key.m_id); |
| 300 | } |
| 301 | |
| 302 | uint qHash(const Message& key) { |
| 303 | return (uint(key.m_accountId) * 10000) + uint(key.m_id); |
| 304 | } |
| 305 | |
| 306 | MessageCategory::MessageCategory(const QString& title) : QObject(), m_title(title) {} |
| 307 | |
| 308 | MessageCategory::MessageCategory(const MessageCategory& other) { |
| 309 | m_title = other.m_title; |
| 310 | } |
| 311 | |
| 312 | QString MessageCategory::title() const { |
| 313 | return m_title; |
| 314 | } |
| 315 | |
| 316 | MessageCategory& MessageCategory::operator=(const MessageCategory& other) { |
| 317 | m_title = other.m_title; |
| 318 | |
| 319 | return *this; |
| 320 | } |