diff --git a/src/ssb.db.c b/src/ssb.db.c index 0d1de3f9..d48962b2 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -2918,3 +2918,21 @@ void tf_ssb_db_remove_block(sqlite3* db, const char* id) sqlite3_finalize(statement); } } + +bool tf_ssb_db_is_blocked(sqlite3* db, const char* id) +{ + bool is_blocked = false; + sqlite3_stmt* statement = NULL; + if (sqlite3_prepare_v2(db, "SELECT 1 FROM blocks WHERE id = ?", -1, &statement, NULL) == SQLITE_OK) + { + if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK) + { + if (sqlite3_step(statement) == SQLITE_ROW) + { + is_blocked = true; + } + } + sqlite3_finalize(statement); + } + return is_blocked; +} diff --git a/src/ssb.db.h b/src/ssb.db.h index 5aef1a43..dfde0790 100644 --- a/src/ssb.db.h +++ b/src/ssb.db.h @@ -631,4 +631,12 @@ void tf_ssb_db_add_block(sqlite3* db, const char* id); */ void tf_ssb_db_remove_block(sqlite3* db, const char* id); +/** +** Check if an ID is blocked on this instance. +** @param db The database. +** @param id The account, message, or blob ID to check. +** @return true if the id is blocked. +*/ +bool tf_ssb_db_is_blocked(sqlite3* db, const char* id); + /** @} */ diff --git a/src/ssb.ebt.c b/src/ssb.ebt.c index 9c68fc59..d9722f41 100644 --- a/src/ssb.ebt.c +++ b/src/ssb.ebt.c @@ -221,6 +221,14 @@ 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; @@ -239,7 +247,10 @@ 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; - tf_ssb_db_get_latest_message_by_author(ssb, visible[i], &sequence, NULL, 0); + if (!_is_blocked(ssb, visible[i])) + { + 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; } @@ -259,11 +270,14 @@ 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))) { - 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); + 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); + } } /* Also respond with what we know about all requested identities. */ @@ -287,7 +301,10 @@ static void _tf_ssb_ebt_get_send_clock_work(tf_ssb_connection_t* connection, voi { for (int i = 0; i < requested_count; i++) { - tf_ssb_db_get_latest_message_by_author(ssb, requested[i].id, &requested[i].value, NULL, 0); + if (!_is_blocked(ssb, requested[i].id)) + { + 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.rpc.c b/src/ssb.rpc.c index b874015b..4ffe6ddd 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -155,7 +155,7 @@ static void _tf_ssb_rpc_blobs_has_work(tf_ssb_connection_t* connection, void* us blobs_has_work_t* work = user_data; tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection); sqlite3* db = tf_ssb_acquire_db_reader(ssb); - work->found = tf_ssb_db_blob_has(db, work->id); + work->found = tf_ssb_db_blob_has(db, work->id) && !tf_ssb_db_is_blocked(db, work->id); tf_ssb_release_db_reader(ssb, db); } diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 6c23b393..cb629f97 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -1905,11 +1905,18 @@ void tf_ssb_test_blocks(const tf_test_options_t* options) 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); tf_ssb_db_add_block(db, id); + assert(tf_ssb_db_is_blocked(db, id)); tf_ssb_db_remove_block(db, blob_id); tf_ssb_db_remove_block(db, message_id); tf_ssb_db_remove_block(db, id); + assert(!tf_ssb_db_is_blocked(db, message_id)); + assert(!tf_ssb_db_is_blocked(db, blob_id)); + assert(!tf_ssb_db_is_blocked(db, id)); tf_ssb_release_db_writer(ssb, db); tf_ssb_destroy(ssb);