1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
/**
* This file is part of JS8Call.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* (C) 2018 Jordan Sherer <kn4crd@gmail.com> - All Rights Reserved
*
**/
#include "Inbox.h"
#include "DriftingDateTime.h"
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(inbox_js8)
namespace {
constexpr char SCHEMA[] =
"CREATE TABLE IF NOT EXISTS inbox_v1 ("
" id INTEGER PRIMARY KEY AUTOINCREMENT, "
" blob TEXT"
");"
"CREATE INDEX IF NOT EXISTS idx_inbox_v1__type ON"
" inbox_v1(json_extract(blob, '$.type'));"
"CREATE INDEX IF NOT EXISTS idx_inbox_v1__params_from ON"
" inbox_v1(json_extract(blob, '$.params.FROM'));"
"CREATE INDEX IF NOT EXISTS idx_inbox_v1__params_to ON"
" inbox_v1(json_extract(blob, '$.params.TO'));"
"CREATE TABLE IF NOT EXISTS inbox_group_recip_v1 ("
" id INTEGER PRIMARY KEY AUTOINCREMENT, "
" msg_id INTEGER, "
" callsign VARCHAR(255), "
" FOREIGN KEY(msg_id) REFERENCES inbox_v1(id) ON DELETE CASCADE"
");"
"CREATE INDEX IF NOT EXISTS idx_inbox_group_recip_v1__callsign ON"
" inbox_group_recip_v1(callsign);";
// Attempt to retrieve a Message object previously serialized as a
// JSON object to the specified column; will throw on failure to
// deserialize the object.
auto get_column_message(sqlite3_stmt *const stmt, int const iCol) {
return Message::fromJson(
QByteArray((const char *)sqlite3_column_text(stmt, iCol),
sqlite3_column_bytes(stmt, iCol)));
}
} // namespace
Inbox::Inbox(QString path) : path_{path}, db_{nullptr} {}
Inbox::~Inbox() { close(); }
/**
* Low-Level Interface
**/
bool Inbox::isOpen() { return db_ != nullptr; }
bool Inbox::open() {
int rc = sqlite3_open(path_.toLocal8Bit().data(), &db_);
if (rc != SQLITE_OK) {
close();
return false;
}
rc = sqlite3_exec(db_, SCHEMA, nullptr, nullptr, nullptr);
if (rc != SQLITE_OK) {
return false;
}
return true;
}
void Inbox::close() {
if (db_) {
sqlite3_close(db_);
db_ = nullptr;
}
}
QString Inbox::error() {
if (db_) {
return QString::fromLocal8Bit(sqlite3_errmsg(db_));
}
return "";
}
int Inbox::count(QString type, QString query, QString match) {
if (!isOpen()) {
return -1;
}
const char *sql = "SELECT COUNT(*) FROM inbox_v1 "
"WHERE json_extract(blob, '$.type') = ? "
"AND json_extract(blob, ?) LIKE ?;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return -1;
}
auto t8 = type.toLocal8Bit();
auto q8 = query.toLocal8Bit();
auto m8 = match.toLocal8Bit();
rc = sqlite3_bind_text(stmt, 1, t8.data(), -1, nullptr);
rc = sqlite3_bind_text(stmt, 2, q8.data(), -1, nullptr);
rc = sqlite3_bind_text(stmt, 3, m8.data(), -1, nullptr);
int count = 0;
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
count = sqlite3_column_int(stmt, 0);
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return -1;
}
return count;
}
QList<QPair<int, Message>> Inbox::values(QString type, QString query,
QString match, int offset, int limit) {
if (!isOpen()) {
return {};
}
const char *sql = "SELECT id, blob FROM inbox_v1 "
"WHERE json_extract(blob, '$.type') = ? "
"AND json_extract(blob, ?) LIKE ? "
"ORDER BY id ASC "
"LIMIT ? OFFSET ?;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return {};
}
auto t8 = type.toLocal8Bit();
auto q8 = query.toLocal8Bit();
auto m8 = match.toLocal8Bit();
rc = sqlite3_bind_text(stmt, 1, t8.data(), -1, nullptr);
rc = sqlite3_bind_text(stmt, 2, q8.data(), -1, nullptr);
rc = sqlite3_bind_text(stmt, 3, m8.data(), -1, nullptr);
rc = sqlite3_bind_int(stmt, 4, limit);
rc = sqlite3_bind_int(stmt, 5, offset);
// qCDebug(inbox_js8) << "exec" << sqlite3_expanded_sql(stmt);
QList<QPair<int, Message>> v;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
try {
v.append(
{sqlite3_column_int(stmt, 0), get_column_message(stmt, 1)});
} catch (...) {
continue;
}
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return {};
}
return v;
}
Message Inbox::value(int key) {
if (!isOpen()) {
return {};
}
const char *sql = "SELECT blob FROM inbox_v1 WHERE id = ? LIMIT 1;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return {};
}
rc = sqlite3_bind_int(stmt, 1, key);
Message m;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
try {
m = get_column_message(stmt, 0);
} catch (...) {
continue;
}
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return {};
}
return m;
}
int Inbox::append(Message value) {
if (!isOpen()) {
return -1;
}
const char *sql = "INSERT INTO inbox_v1 (blob) VALUES (?);";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return -2;
}
auto j8 = value.toJson();
rc = sqlite3_bind_text(stmt, 1, j8.data(), -1, nullptr);
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return -1;
}
return sqlite3_last_insert_rowid(db_);
}
bool Inbox::set(int key, Message value) {
if (!isOpen()) {
return false;
}
const char *sql = "UPDATE inbox_v1 SET blob = ? WHERE id = ?;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return false;
}
auto j8 = value.toJson();
rc = sqlite3_bind_text(stmt, 1, j8.data(), -1, nullptr);
rc = sqlite3_bind_int(stmt, 2, key);
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return false;
}
return true;
}
bool Inbox::del(int key) {
if (!isOpen()) {
return false;
}
const char *sql = "DELETE FROM inbox_v1 WHERE id = ?;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return false;
}
rc = sqlite3_bind_int(stmt, 1, key);
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return false;
}
return true;
}
int Inbox::getLookaheadMessageIdForCallsign(const QString &callsign,
int afterMsgId) {
if (!isOpen()) {
return -1;
}
const char *sql = "SELECT inbox_v1.id, inbox_v1.blob FROM inbox_v1 "
"WHERE inbox_v1.id > ? "
"AND json_extract(blob, '$.type') = 'STORE' "
"AND json_extract(blob, '$.params.TO') LIKE ? "
"ORDER BY inbox_v1.id ASC "
"LIMIT ? OFFSET ?;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return -1;
}
auto c8 = callsign.toLocal8Bit();
rc = sqlite3_bind_int(stmt, 1, afterMsgId);
rc = sqlite3_bind_text(stmt, 2, c8.data(), -1, nullptr);
rc = sqlite3_bind_int(stmt, 3, 10);
rc = sqlite3_bind_int(stmt, 4, 0);
// qCDebug(inbox_js8) << "exec " << sqlite3_expanded_sql(stmt);
int next_id = -1;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
Message m;
int i = sqlite3_column_int(stmt, 0);
auto msg = QByteArray((const char *)sqlite3_column_text(stmt, 1),
sqlite3_column_bytes(stmt, 1));
m = Message::fromJson(msg);
auto params = m.params();
auto text = params.value("TEXT").toString().trimmed();
if (!text.isEmpty()) {
next_id = i;
break;
}
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return -1;
}
return next_id;
}
/**
* High-Level Interface
**/
int Inbox::countUnreadFrom(QString from) {
return count("UNREAD", "$.params.FROM", from);
}
QPair<int, Message> Inbox::firstUnreadFrom(QString from) {
auto v = values("UNREAD", "$.params.FROM", from, 0, 1);
if (v.isEmpty()) {
return {};
}
return v.first();
}
QMap<QString, int> Inbox::getGroupMessageCounts() {
if (!isOpen()) {
return {};
}
QMap<QString, int> messageCounts;
const char *sql = "SELECT count(id) as msg_count, json_extract(blob, "
"'$.params.TO') as group_name FROM inbox_v1 "
"WHERE json_extract(blob, '$.type') = 'STORE' "
"AND group_name LIKE '@%' "
"AND json_extract(blob, '$.params.UTC') > ? "
"GROUP BY group_name";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return messageCounts;
}
// Set a floor or 48 hours for group message retrieval
// TODO: date formatting with the "yyyy-MM-dd HH:mm:ss" string happens
// elsewhere as well, centralize
// TODO: possibly make the date floor configurable
auto d8 = DriftingDateTime::currentDateTimeUtc()
.addDays(-2)
.toString("yyyy-MM-dd HH:mm:ss")
.toLocal8Bit();
rc = sqlite3_bind_text(stmt, 1, d8.data(), -1, nullptr);
// qCDebug(inbox_js8) << "exec " << sqlite3_expanded_sql(stmt);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
Message m;
int count = sqlite3_column_int(stmt, 0);
auto group = sqlite3_column_text(stmt, 1);
messageCounts.insert(
QString::fromLocal8Bit(reinterpret_cast<const char *>(group)),
count);
}
rc = sqlite3_finalize(stmt);
return messageCounts;
}
bool Inbox::markGroupMsgDeliveredForCallsign(int msgId, QString callsign) {
if (!isOpen()) {
return false;
}
const char *sql = "SELECT count(id) as msg_count FROM inbox_group_recip_v1 "
"WHERE msg_id = ? AND callsign = ? LIMIT 1;";
sqlite3_stmt *exists_stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &exists_stmt, nullptr);
if (rc != SQLITE_OK) {
return false;
}
auto cs8 = callsign.toLocal8Bit();
rc = sqlite3_bind_int(exists_stmt, 1, msgId);
rc = sqlite3_bind_text(exists_stmt, 2, cs8.data(), -1, nullptr);
bool recordExists = false;
rc = sqlite3_step(exists_stmt);
int count = sqlite3_column_int(exists_stmt, 0);
recordExists = (count > 0);
rc = sqlite3_finalize(exists_stmt);
if (!recordExists) {
sql =
"INSERT INTO inbox_group_recip_v1 (msg_id, callsign) VALUES (?,?);";
sqlite3_stmt *insert_stmt;
rc = sqlite3_prepare_v2(db_, sql, -1, &insert_stmt, nullptr);
if (rc != SQLITE_OK) {
return false;
}
rc = sqlite3_bind_int(insert_stmt, 1, msgId);
rc = sqlite3_bind_text(insert_stmt, 2, cs8.data(), -1, nullptr);
rc = sqlite3_step(insert_stmt);
rc = sqlite3_finalize(insert_stmt);
if (rc != SQLITE_OK) {
return false;
}
}
return true;
}
int Inbox::getNextGroupMessageIdForCallsign(const QString &group_name,
const QString &callsign) {
if (!isOpen()) {
return -1;
}
const char *sql = "SELECT inbox_v1.id, inbox_v1.blob FROM inbox_v1 "
"LEFT JOIN inbox_group_recip_v1 ON "
"(inbox_group_recip_v1.msg_id=inbox_v1.id AND "
"inbox_group_recip_v1.callsign = ?) "
"WHERE json_extract(blob, '$.type') = 'STORE' "
"AND json_extract(blob, '$.params.TO') LIKE ? "
"AND json_extract(blob, '$.params.UTC') > ? "
"AND inbox_group_recip_v1.id IS NULL "
"ORDER BY inbox_v1.id ASC "
"LIMIT ? OFFSET ?;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return -1;
}
auto c8 = callsign.toLocal8Bit();
auto g8 = group_name.toLocal8Bit();
// Set a floor or 48 hours for group message retrieval
// TODO: date formatting with the "yyyy-MM-dd HH:mm:ss" string happens
// elsewhere as well, centralize
// TODO: possibly make the date floor configurable
auto d8 = DriftingDateTime::currentDateTimeUtc()
.addDays(-2)
.toString("yyyy-MM-dd HH:mm:ss")
.toLocal8Bit();
rc = sqlite3_bind_text(stmt, 1, c8.data(), -1, nullptr);
rc = sqlite3_bind_text(stmt, 2, g8.data(), -1, nullptr);
rc = sqlite3_bind_text(stmt, 3, d8.data(), -1, nullptr);
rc = sqlite3_bind_int(stmt, 4, 10);
rc = sqlite3_bind_int(stmt, 5, 0);
// qCDebug(inbox_js8) << "exec " << sqlite3_expanded_sql(stmt);
int next_id = -1;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
Message m;
int i = sqlite3_column_int(stmt, 0);
auto msg = QByteArray((const char *)sqlite3_column_text(stmt, 1),
sqlite3_column_bytes(stmt, 1));
m = Message::fromJson(msg);
auto params = m.params();
auto text = params.value("TEXT").toString().trimmed();
if (!text.isEmpty()) {
next_id = i;
break;
}
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return -1;
}
return next_id;
}
int Inbox::getLookaheadGroupMessageIdForCallsign(const QString &group_name,
const QString &callsign,
int afterMsgId) {
if (!isOpen()) {
return -1;
}
const char *sql = "SELECT inbox_v1.id, inbox_v1.blob FROM inbox_v1 "
"LEFT JOIN inbox_group_recip_v1 ON "
"(inbox_group_recip_v1.msg_id=inbox_v1.id AND "
"inbox_group_recip_v1.callsign = ?) "
"WHERE inbox_v1.id > ? "
"AND json_extract(blob, '$.type') = 'STORE' "
"AND json_extract(blob, '$.params.TO') LIKE ? "
"AND json_extract(blob, '$.params.UTC') > ? "
"AND inbox_group_recip_v1.id IS NULL "
"ORDER BY inbox_v1.id ASC "
"LIMIT ? OFFSET ?;";
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
return -1;
}
auto c8 = callsign.toLocal8Bit();
auto g8 = group_name.toLocal8Bit();
// Set a floor or 48 hours for group message retrieval
// TODO: date formatting with the "yyyy-MM-dd HH:mm:ss" string happens
// elsewhere as well, centralize
// TODO: possibly make the date floor configurable
auto d8 = DriftingDateTime::currentDateTimeUtc()
.addDays(-2)
.toString("yyyy-MM-dd HH:mm:ss")
.toLocal8Bit();
rc = sqlite3_bind_text(stmt, 1, c8.data(), -1, nullptr);
rc = sqlite3_bind_int(stmt, 2, afterMsgId);
rc = sqlite3_bind_text(stmt, 3, g8.data(), -1, nullptr);
rc = sqlite3_bind_text(stmt, 4, d8.data(), -1, nullptr);
rc = sqlite3_bind_int(stmt, 5, 10);
rc = sqlite3_bind_int(stmt, 6, 0);
qCDebug(inbox_js8) << "exec " << sqlite3_expanded_sql(stmt);
int next_id = -1;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
Message m;
int i = sqlite3_column_int(stmt, 0);
auto msg = QByteArray((const char *)sqlite3_column_text(stmt, 1),
sqlite3_column_bytes(stmt, 1));
m = Message::fromJson(msg);
auto params = m.params();
auto text = params.value("TEXT").toString().trimmed();
if (!text.isEmpty()) {
next_id = i;
break;
}
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
return -1;
}
return next_id;
}
Q_LOGGING_CATEGORY(inbox_js8, "inbox.js8", QtWarningMsg)
|