| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "core/messageobject.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "database/databasefactory.h" |
| 7 | #include "database/databasequeries.h" |
| 8 | #include "definitions/definitions.h" |
| 9 | #include "definitions/globals.h" |
| 10 | #include "services/abstract/labelsnode.h" |
| 11 | |
| 12 | #include <QRandomGenerator> |
| 13 | #include <QSqlDatabase> |
| 14 | #include <QSqlError> |
| 15 | #include <QSqlQuery> |
| 16 | |
| 17 | MessageObject::MessageObject(QSqlDatabase* db, Feed* feed, ServiceRoot* account, bool is_new_message, QObject* parent) |
| 18 | : QObject(parent), m_db(db), m_feed(feed), m_account(account), m_message(nullptr), |
| 19 | m_runningAfterFetching(is_new_message) { |
| 20 | |
| 21 | m_feedCustomId = m_feed != nullptr ? m_feed->customId() : QString::number(NO_PARENT_CATEGORY); |
| 22 | m_accountId = m_account != nullptr ? m_account->accountId() : NO_PARENT_CATEGORY; |
| 23 | m_availableLabels = m_account != nullptr ? m_account->labelsNode()->labels() : QList<Label*>(); |
| 24 | } |
| 25 | |
| 26 | void MessageObject::setMessage(Message* message) { |
| 27 | m_message = message; |
| 28 | } |
| 29 | |
| 30 | bool MessageObject::isAlreadyInDatabase(DuplicateCheck attribute_check) const { |
| 31 | return isDuplicateWithAttribute(attribute_check); |
| 32 | } |
| 33 | |
| 34 | bool MessageObject::isDuplicate(DuplicateCheck attribute_check) const { |
| 35 | return isDuplicateWithAttribute(attribute_check); |
| 36 | } |
| 37 | |
| 38 | bool MessageObject::isDuplicateWithAttribute(MessageObject::DuplicateCheck attribute_check) const { |
| 39 | // Check database according to duplication attribute_check. |
| 40 | QSqlQuery q(*m_db); |
| 41 | QStringList where_clauses; |
| 42 | QVector<QPair<QString, QVariant>> bind_values; |
| 43 | |
| 44 | // Now we construct the query according to parameter. |
| 45 | if (Globals::hasFlag(attribute_check, DuplicateCheck::SameTitle)) { |
| 46 | where_clauses.append(QSL("title = :title")); |
| 47 | bind_values.append({QSL(":title"), title()}); |
| 48 | } |
| 49 | |
| 50 | if (Globals::hasFlag(attribute_check, DuplicateCheck::SameUrl)) { |
| 51 | where_clauses.append(QSL("url = :url")); |
| 52 | bind_values.append({QSL(":url"), url()}); |
| 53 | } |
| 54 | |
| 55 | if (Globals::hasFlag(attribute_check, DuplicateCheck::SameAuthor)) { |
| 56 | where_clauses.append(QSL("author = :author")); |
| 57 | bind_values.append({QSL(":author"), author()}); |
| 58 | } |
| 59 | |
| 60 | if (Globals::hasFlag(attribute_check, DuplicateCheck::SameDateCreated)) { |
| 61 | where_clauses.append(QSL("date_created = :date_created")); |
| 62 | bind_values.append({QSL(":date_created"), created().toMSecsSinceEpoch()}); |
| 63 | } |
| 64 | |
| 65 | if (Globals::hasFlag(attribute_check, DuplicateCheck::SameCustomId)) { |
| 66 | where_clauses.append(QSL("custom_id = :custom_id")); |
| 67 | bind_values.append({QSL(":custom_id"), customId()}); |
| 68 | } |
| 69 | |
| 70 | where_clauses.append(QSL("account_id = :account_id")); |
| 71 | bind_values.append({QSL(":account_id"), accountId()}); |
| 72 | |
| 73 | // If we have already message stored in DB, then we also must |
| 74 | // make sure that we do not match the message against itself. |
| 75 | if (!runningFilterWhenFetching() && m_message->m_id > 0) { |
| 76 | where_clauses.append(QSL("id != :id")); |
| 77 | bind_values.append({QSL(":id"), QString::number(m_message->m_id)}); |
| 78 | } |
| 79 | |
| 80 | if (!Globals::hasFlag(attribute_check, DuplicateCheck::AllFeedsSameAccount)) { |
| 81 | // Limit to current feed. |
| 82 | where_clauses.append(QSL("feed = :feed")); |
| 83 | bind_values.append({QSL(":feed"), feedCustomId()}); |
| 84 | } |
| 85 | |
| 86 | QString full_query = QSL("SELECT COUNT(*) FROM Messages WHERE ") + where_clauses.join(QSL(" AND ")) + QSL(";"); |
| 87 | |
| 88 | qDebugNN << LOGSEC_MESSAGEMODEL |
| 89 | << "Prepared query for MSG duplicate identification is:" << QUOTE_W_SPACE_DOT(full_query); |
| 90 | |
| 91 | q.setForwardOnly(true); |
| 92 | q.prepare(full_query); |
| 93 | |
| 94 | for (const auto& bind : bind_values) { |
| 95 | q.bindValue(bind.first, bind.second); |
| 96 | } |
| 97 | |
| 98 | if (q.exec() && q.next()) { |
| 99 | qDebugNN << LOGSEC_DB << "Executed SQL for message duplicates check:" |
| 100 | << QUOTE_W_SPACE_DOT(DatabaseFactory::lastExecutedQuery(q)); |
| 101 | |
| 102 | if (q.value(0).toInt() > 0) { |
| 103 | // Whoops, we have the "same" message in database. |
| 104 | qDebugNN << LOGSEC_CORE << "Message" << QUOTE_W_SPACE(title()) << "was identified as duplicate by filter script."; |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | else if (q.lastError().isValid()) { |
| 109 | qWarningNN << LOGSEC_CORE << "Error when checking for duplicate messages via filtering system, error:" |
| 110 | << QUOTE_W_SPACE_DOT(q.lastError().text()); |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | bool MessageObject::assignLabel(const QString& label_custom_id) const { |
| 117 | // NOTE: This is now not needed as the underlying bug was fixed in DB layer. |
| 118 | /* |
| 119 | if (m_message->m_id <= 0 && m_message->m_customId.isEmpty()) { |
| 120 | return false; |
| 121 | } |
| 122 | */ |
| 123 | |
| 124 | Label* lbl = boolinq::from(m_availableLabels).firstOrDefault([label_custom_id](Label* lbl) { |
| 125 | return lbl->customId() == label_custom_id; |
| 126 | }); |
| 127 | |
| 128 | if (lbl != nullptr) { |
| 129 | if (!m_message->m_assignedLabels.contains(lbl)) { |
| 130 | m_message->m_assignedLabels.append(lbl); |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | else { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | bool MessageObject::deassignLabel(const QString& label_custom_id) const { |
| 141 | // NOTE: This is now not needed as the underlying bug was fixed in DB layer. |
| 142 | /* |
| 143 | if (m_message->m_id <= 0 && m_message->m_customId.isEmpty()) { |
| 144 | return false; |
| 145 | } |
| 146 | */ |
| 147 | |
| 148 | Label* lbl = boolinq::from(m_message->m_assignedLabels).firstOrDefault([label_custom_id](Label* lbl) { |
| 149 | return lbl->customId() == label_custom_id; |
| 150 | }); |
| 151 | |
| 152 | if (lbl != nullptr) { |
| 153 | m_message->m_assignedLabels.removeAll(lbl); |
| 154 | return true; |
| 155 | } |
| 156 | else { |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | QString MessageObject::findLabelId(const QString& label_title) const { |
| 162 | Label* found_lbl = boolinq::from(m_availableLabels).firstOrDefault([label_title](Label* lbl) { |
| 163 | return lbl->title().toLower() == label_title.toLower(); |
| 164 | }); |
| 165 | |
| 166 | if (found_lbl == nullptr) { |
| 167 | qWarningNN << LOGSEC_CORE << "Label with title" << QUOTE_W_SPACE(label_title) << "not found."; |
| 168 | } |
| 169 | |
| 170 | return found_lbl != nullptr ? found_lbl->customId() : QString(); |
| 171 | } |
| 172 | |
| 173 | QString MessageObject::createLabelId(const QString& title, const QString& hex_color) { |
| 174 | QString lbl_id = findLabelId(title); |
| 175 | |
| 176 | if (!lbl_id.isEmpty()) { |
| 177 | // Label exists. |
| 178 | return lbl_id; |
| 179 | } |
| 180 | |
| 181 | if (!Globals::hasFlag(m_account->supportedLabelOperations(), ServiceRoot::LabelOperation::Adding)) { |
| 182 | qWarningNN << LOGSEC_CORE << "This account does not support creating labels."; |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
| 186 | Label* new_lbl = nullptr; |
| 187 | |
| 188 | try { |
| 189 | auto rnd_color = TextFactory::generateRandomColor(); |
| 190 | |
| 191 | new_lbl = new Label(title, hex_color.isEmpty() ? rnd_color : hex_color); |
| 192 | QSqlDatabase db = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 193 | |
| 194 | DatabaseQueries::createLabel(db, new_lbl, m_account->accountId()); |
| 195 | m_account->requestItemReassignment(new_lbl, m_account->labelsNode()); |
| 196 | |
| 197 | m_availableLabels.append(new_lbl); |
| 198 | |
| 199 | return new_lbl->customId(); |
| 200 | } |
| 201 | catch (const ApplicationException& ex) { |
| 202 | qCriticalNN << LOGSEC_CORE << "Cannot create label:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 203 | |
| 204 | if (new_lbl != nullptr) { |
| 205 | new_lbl->deleteLater(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return {}; |
| 210 | } |
| 211 | |
| 212 | void MessageObject::addEnclosure(const QString& url, const QString& mime_type) const { |
| 213 | m_message->m_enclosures.append(Enclosure(url, mime_type)); |
| 214 | } |
| 215 | |
| 216 | QString MessageObject::title() const { |
| 217 | return m_message->m_title; |
| 218 | } |
| 219 | |
| 220 | void MessageObject::setTitle(const QString& title) { |
| 221 | m_message->m_title = title; |
| 222 | } |
| 223 | |
| 224 | QString MessageObject::url() const { |
| 225 | return m_message->m_url; |
| 226 | } |
| 227 | |
| 228 | void MessageObject::setUrl(const QString& url) { |
| 229 | m_message->m_url = url; |
| 230 | } |
| 231 | |
| 232 | QString MessageObject::author() const { |
| 233 | return m_message->m_author; |
| 234 | } |
| 235 | |
| 236 | void MessageObject::setAuthor(const QString& author) { |
| 237 | m_message->m_author = author; |
| 238 | } |
| 239 | |
| 240 | QString MessageObject::contents() const { |
| 241 | return m_message->m_contents; |
| 242 | } |
| 243 | |
| 244 | void MessageObject::setContents(const QString& contents) { |
| 245 | m_message->m_contents = contents; |
| 246 | } |
| 247 | |
| 248 | QString MessageObject::rawContents() const { |
| 249 | return m_message->m_rawContents; |
| 250 | } |
| 251 | |
| 252 | void MessageObject::setRawContents(const QString& raw_contents) { |
| 253 | m_message->m_rawContents = raw_contents; |
| 254 | } |
| 255 | |
| 256 | QDateTime MessageObject::created() const { |
| 257 | return m_message->m_created; |
| 258 | } |
| 259 | |
| 260 | void MessageObject::setCreated(const QDateTime& created) { |
| 261 | m_message->m_created = created; |
| 262 | } |
| 263 | |
| 264 | bool MessageObject::createdIsMadeup() const { |
| 265 | return !m_message->m_createdFromFeed; |
| 266 | } |
| 267 | |
| 268 | void MessageObject::setCreatedIsMadeup(bool madeup) { |
| 269 | m_message->m_createdFromFeed = !madeup; |
| 270 | } |
| 271 | |
| 272 | bool MessageObject::isRead() const { |
| 273 | return m_message->m_isRead; |
| 274 | } |
| 275 | |
| 276 | void MessageObject::setIsRead(bool is_read) { |
| 277 | m_message->m_isRead = is_read; |
| 278 | } |
| 279 | |
| 280 | bool MessageObject::isImportant() const { |
| 281 | return m_message->m_isImportant; |
| 282 | } |
| 283 | |
| 284 | void MessageObject::setIsImportant(bool is_important) { |
| 285 | m_message->m_isImportant = is_important; |
| 286 | } |
| 287 | |
| 288 | bool MessageObject::isDeleted() const { |
| 289 | return m_message->m_isDeleted; |
| 290 | } |
| 291 | |
| 292 | void MessageObject::setIsDeleted(bool is_deleted) { |
| 293 | m_message->m_isDeleted = is_deleted; |
| 294 | } |
| 295 | |
| 296 | double MessageObject::score() const { |
| 297 | return m_message->m_score; |
| 298 | } |
| 299 | |
| 300 | void MessageObject::setScore(double score) { |
| 301 | m_message->m_score = score; |
| 302 | } |
| 303 | |
| 304 | QString MessageObject::feedCustomId() const { |
| 305 | if (m_feedCustomId.isEmpty() || m_feedCustomId == QString::number(NO_PARENT_CATEGORY)) { |
| 306 | return m_message->m_feedId; |
| 307 | } |
| 308 | else { |
| 309 | return m_feedCustomId; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | int MessageObject::accountId() const { |
| 314 | return m_accountId; |
| 315 | } |
| 316 | |
| 317 | QString MessageObject::customId() const { |
| 318 | return m_message->m_customId; |
| 319 | } |
| 320 | |
| 321 | void MessageObject::setCustomId(const QString& custom_id) { |
| 322 | m_message->m_customId = custom_id; |
| 323 | } |
| 324 | |
| 325 | int MessageObject::id() const { |
| 326 | return m_message->m_id; |
| 327 | } |
| 328 | |
| 329 | QList<Label*> MessageObject::assignedLabels() const { |
| 330 | return m_message->m_assignedLabels; |
| 331 | } |
| 332 | |
| 333 | QList<Label*> MessageObject::availableLabels() const { |
| 334 | return m_availableLabels; |
| 335 | } |
| 336 | |
| 337 | QList<MessageCategory> MessageObject::categories() const { |
| 338 | return m_message->m_categories; |
| 339 | } |
| 340 | |
| 341 | bool MessageObject::runningFilterWhenFetching() const { |
| 342 | return m_runningAfterFetching; |
| 343 | } |
| 344 | |
| 345 | bool MessageObject::hasEnclosures() const { |
| 346 | return !m_message->m_enclosures.isEmpty(); |
| 347 | } |