diff --git a/src/ssb.db.c b/src/ssb.db.c index 153df218..d9510626 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -981,27 +981,52 @@ bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, i bool found = false; sqlite3_stmt* statement; sqlite3* db = tf_ssb_acquire_db_reader(ssb); - const char* query = "SELECT id, sequence FROM messages WHERE author = ?1 ORDER BY sequence DESC LIMIT 1"; - if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) + + if (out_message_id) { - if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) + const char* query = "SELECT id, sequence FROM messages WHERE author = ?1 ORDER BY sequence DESC LIMIT 1"; + if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) { - if (out_sequence) + if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) { - *out_sequence = sqlite3_column_int64(statement, 1); + if (out_sequence) + { + *out_sequence = sqlite3_column_int64(statement, 1); + } + if (out_message_id) + { + strncpy(out_message_id, (const char*)sqlite3_column_text(statement, 0), out_message_id_size - 1); + } + found = true; } - if (out_message_id) - { - strncpy(out_message_id, (const char*)sqlite3_column_text(statement, 0), out_message_id_size - 1); - } - found = true; + sqlite3_finalize(statement); + } + else + { + tf_printf("%s: prepare failed: %s\n", __FUNCTION__, sqlite3_errmsg(db)); } - sqlite3_finalize(statement); } else { - tf_printf("%s: prepare failed: %s\n", __FUNCTION__, sqlite3_errmsg(db)); + const char* query = "SELECT max_sequence FROM messages_stats WHERE author = ?1"; + if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) + { + if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) + { + if (out_sequence) + { + *out_sequence = sqlite3_column_int64(statement, 0); + } + found = true; + } + sqlite3_finalize(statement); + } + else + { + tf_printf("%s: prepare failed: %s\n", __FUNCTION__, sqlite3_errmsg(db)); + } } + tf_ssb_release_db_reader(ssb, db); return found; }