diff --git a/src/ssb.db.c b/src/ssb.db.c index 961702f9..d075f2a7 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -894,7 +894,7 @@ bool tf_ssb_db_blob_has(sqlite3* db, const char* id) { bool result = false; sqlite3_stmt* statement; - const char* query = "SELECT COUNT(*) FROM blobs WHERE id = ?1"; + const char* query = "SELECT COUNT(*) FROM blobs LEFT OUTER JOIN blocks ON blobs.id = blocks.id WHERE blobs.id = ?1 AND blocks.id IS NULL"; if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) @@ -911,7 +911,7 @@ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_ bool result = false; sqlite3_stmt* statement; sqlite3* db = tf_ssb_acquire_db_reader(ssb); - const char* query = "SELECT content FROM blobs WHERE id = ?1"; + const char* query = "SELECT content FROM blobs LEFT OUTER JOIN blocks ON blobs.id = blocks.id WHERE blobs.id = ?1 AND blocks.id IS NULL"; if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) @@ -1169,7 +1169,8 @@ bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, i if (out_message_id) { - const char* query = "SELECT id, sequence FROM messages WHERE author = ?1 ORDER BY sequence DESC LIMIT 1"; + const char* query = "SELECT messages.id, messages.sequence FROM messages LEFT OUTER JOIN blocks ON messages.id = blocks.id WHERE author = ?1 AND blocks.id IS NULL ORDER " + "BY messages.sequence DESC LIMIT 1"; if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) @@ -1193,7 +1194,8 @@ bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, i } else { - const char* query = "SELECT max_sequence FROM messages_stats WHERE author = ?1"; + const char* query = "SELECT messages_stats.max_sequence FROM messages_stats LEFT OUTER JOIN blocks ON messages_stats.author = blocks.id WHERE messages_stats.author = ?1 " + "AND blocks.id IS NULL"; if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) @@ -2946,5 +2948,6 @@ void tf_ssb_db_get_blocks(sqlite3* db, void (*callback)(const char* id, double t { callback((const char*)sqlite3_column_text(statement, 0), sqlite3_column_double(statement, 1), user_data); } + sqlite3_finalize(statement); } } diff --git a/src/ssb.ebt.c b/src/ssb.ebt.c index d9722f41..9c68fc59 100644 --- a/src/ssb.ebt.c +++ b/src/ssb.ebt.c @@ -221,14 +221,6 @@ static void _ebt_add_to_clock(ebt_get_clock_t* work, const char* id, int64_t val } } -static bool _is_blocked(tf_ssb_t* ssb, const char* id) -{ - sqlite3* db = tf_ssb_acquire_db_reader(ssb); - bool blocked = tf_ssb_db_is_blocked(db, id); - tf_ssb_release_db_reader(ssb, db); - return blocked; -} - static void _tf_ssb_ebt_get_send_clock_work(tf_ssb_connection_t* connection, void* user_data) { ebt_get_clock_t* work = user_data; @@ -247,10 +239,7 @@ static void _tf_ssb_ebt_get_send_clock_work(tf_ssb_connection_t* connection, voi for (int i = 0; visible[i]; i++) { int32_t sequence = 0; - if (!_is_blocked(ssb, visible[i])) - { - tf_ssb_db_get_latest_message_by_author(ssb, visible[i], &sequence, NULL, 0); - } + tf_ssb_db_get_latest_message_by_author(ssb, visible[i], &sequence, NULL, 0); sequences = tf_resize_vec(sequences, (i + 1) * sizeof(int32_t)); sequences[i] = sequence; } @@ -270,14 +259,11 @@ static void _tf_ssb_ebt_get_send_clock_work(tf_ssb_connection_t* connection, voi char id[k_id_base64_len] = ""; if (tf_ssb_connection_get_id(connection, id, sizeof(id))) { - if (!_is_blocked(ssb, id)) - { - int32_t sequence = 0; - tf_ssb_db_get_latest_message_by_author(ssb, id, &sequence, NULL, 0); - uv_mutex_lock(&work->ebt->mutex); - _ebt_add_to_clock(work, id, sequence, true, true); - uv_mutex_unlock(&work->ebt->mutex); - } + int32_t sequence = 0; + tf_ssb_db_get_latest_message_by_author(ssb, id, &sequence, NULL, 0); + uv_mutex_lock(&work->ebt->mutex); + _ebt_add_to_clock(work, id, sequence, true, true); + uv_mutex_unlock(&work->ebt->mutex); } /* Also respond with what we know about all requested identities. */ @@ -301,10 +287,7 @@ static void _tf_ssb_ebt_get_send_clock_work(tf_ssb_connection_t* connection, voi { for (int i = 0; i < requested_count; i++) { - if (!_is_blocked(ssb, requested[i].id)) - { - tf_ssb_db_get_latest_message_by_author(ssb, requested[i].id, &requested[i].value, NULL, 0); - } + tf_ssb_db_get_latest_message_by_author(ssb, requested[i].id, &requested[i].value, NULL, 0); } uv_mutex_lock(&work->ebt->mutex); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index cb629f97..1fa31f8b 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -1903,12 +1903,15 @@ void tf_ssb_test_blocks(const tf_test_options_t* options) } JS_FreeValue(context, obj); + assert(tf_ssb_db_blob_get(ssb, blob_id, NULL, NULL)); + sqlite3* db = tf_ssb_acquire_db_writer(ssb); tf_ssb_db_add_block(db, message_id); assert(tf_ssb_db_is_blocked(db, message_id)); /* Blocked already, because the blocked message references it. */ assert(tf_ssb_db_is_blocked(db, blob_id)); tf_ssb_db_add_block(db, blob_id); + assert(!tf_ssb_db_blob_get(ssb, blob_id, NULL, NULL)); tf_ssb_db_add_block(db, id); assert(tf_ssb_db_is_blocked(db, id)); tf_ssb_db_remove_block(db, blob_id); @@ -1918,6 +1921,7 @@ void tf_ssb_test_blocks(const tf_test_options_t* options) assert(!tf_ssb_db_is_blocked(db, blob_id)); assert(!tf_ssb_db_is_blocked(db, id)); tf_ssb_release_db_writer(ssb, db); + assert(tf_ssb_db_blob_get(ssb, blob_id, NULL, NULL)); tf_ssb_destroy(ssb);