|
|
|
@ -49,7 +49,7 @@ static bool _tf_ssb_db_has_rows(sqlite3* db, const char* query)
|
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
int result = SQLITE_OK;
|
|
|
|
|
while ((result = sqlite3_step(statement)) == SQLITE_ROW)
|
|
|
|
@ -96,7 +96,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
|
|
|
|
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
int auto_vacuum = 0;
|
|
|
|
|
if (sqlite3_prepare(db, "PRAGMA auto_vacuum", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "PRAGMA auto_vacuum", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
|
{
|
|
|
|
@ -415,7 +415,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
|
|
|
|
bool need_add_flags = true;
|
|
|
|
|
bool need_convert_timestamp_to_real = false;
|
|
|
|
|
|
|
|
|
|
if (sqlite3_prepare(db, "PRAGMA table_info(messages)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "PRAGMA table_info(messages)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
int result = SQLITE_OK;
|
|
|
|
|
while ((result = sqlite3_step(statement)) == SQLITE_ROW)
|
|
|
|
@ -464,7 +464,7 @@ static bool _tf_ssb_db_previous_message_exists(sqlite3* db, const char* author,
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT COUNT(*), id != ?3 AS is_mismatch FROM messages WHERE author = ?1 AND sequence = ?2", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT COUNT(*), id != ?3 AS is_mismatch FROM messages WHERE author = ?1 AND sequence = ?2", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, sequence - 1) == SQLITE_OK &&
|
|
|
|
|
sqlite3_bind_text(statement, 3, previous, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
@ -489,7 +489,7 @@ static int64_t _tf_ssb_db_store_message_raw(sqlite3* db, const char* id, const c
|
|
|
|
|
const char* query = "INSERT INTO messages (id, previous, author, sequence, timestamp, content, hash, signature, flags) VALUES (?, ?, ?, ?, ?, jsonb(?), "
|
|
|
|
|
"?, ?, ?) ON CONFLICT DO NOTHING";
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK &&
|
|
|
|
|
(previous ? sqlite3_bind_text(statement, 2, previous, -1, NULL) : sqlite3_bind_null(statement, 2)) == SQLITE_OK &&
|
|
|
|
@ -539,7 +539,7 @@ static char* _tf_ssb_db_get_message_blob_wants(tf_ssb_t* ssb, int64_t rowid)
|
|
|
|
|
char* result = NULL;
|
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
|
|
if (sqlite3_prepare(db,
|
|
|
|
|
if (sqlite3_prepare_v2(db,
|
|
|
|
|
"SELECT DISTINCT json.value FROM messages, json_tree(messages.content) AS json LEFT OUTER JOIN blobs ON json.value = blobs.id WHERE messages.rowid = ?1 AND "
|
|
|
|
|
"json.value LIKE '&%%.sha256' AND length(json.value) = ?2 AND blobs.content IS NULL",
|
|
|
|
|
-1, &statement, NULL) == SQLITE_OK)
|
|
|
|
@ -776,7 +776,7 @@ bool tf_ssb_db_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
const char* query = "SELECT json(content) FROM messages WHERE id = ?";
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
@ -805,7 +805,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";
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
@ -822,7 +822,7 @@ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
const char* query = "SELECT content FROM blobs WHERE id = ?1";
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
@ -852,8 +852,8 @@ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_
|
|
|
|
|
void tf_ssb_db_add_blob_wants(sqlite3* db, const char* id)
|
|
|
|
|
{
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "INSERT OR REPLACE INTO blob_wants_cache (id, timestamp) VALUES (?, unixepoch() * 1000) ON CONFLICT DO UPDATE SET timestamp = excluded.timestamp", -1,
|
|
|
|
|
&statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO blob_wants_cache (id, timestamp) VALUES (?, unixepoch() * 1000) ON CONFLICT DO UPDATE SET timestamp = excluded.timestamp",
|
|
|
|
|
-1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -978,7 +978,8 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char*
|
|
|
|
|
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "INSERT INTO blobs (id, content, created) VALUES (?1, ?2, CAST(strftime('%s') AS INTEGER)) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "INSERT INTO blobs (id, content, created) VALUES (?1, ?2, CAST(strftime('%s') AS INTEGER)) ON CONFLICT DO NOTHING", -1, &statement, NULL) ==
|
|
|
|
|
SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_bind_blob(statement, 2, blob, size, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -1020,7 +1021,7 @@ bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* aut
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
const char* query = "SELECT id, previous, timestamp, json(content), hash, signature, flags FROM messages WHERE author = ?1 AND sequence = ?2";
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, sequence) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
|
{
|
|
|
|
@ -1083,7 +1084,7 @@ 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";
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
@ -1107,7 +1108,7 @@ 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";
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
@ -1256,7 +1257,7 @@ JSValue tf_ssb_db_visit_query(tf_ssb_t* ssb, const char* query, const JSValue bi
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader_restricted(ssb);
|
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, query, -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
JSValue bind_result = _tf_ssb_sqlite_bind_json(context, db, statement, binds);
|
|
|
|
|
if (JS_IsUndefined(bind_result))
|
|
|
|
@ -1317,7 +1318,7 @@ int tf_ssb_db_identity_get_count_for_user(tf_ssb_t* ssb, const char* user)
|
|
|
|
|
int count = 0;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT COUNT(*) FROM identities WHERE user = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT COUNT(*) FROM identities WHERE user = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -1363,7 +1364,7 @@ bool tf_ssb_db_identity_add(tf_ssb_t* ssb, const char* user, const char* public_
|
|
|
|
|
bool added = false;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "INSERT INTO identities (user, public_key, private_key) VALUES (?, ?, ?) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "INSERT INTO identities (user, public_key, private_key) VALUES (?, ?, ?) ON CONFLICT DO NOTHING", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, public_key, -1, NULL) == SQLITE_OK &&
|
|
|
|
|
sqlite3_bind_text(statement, 3, private_key, -1, NULL) == SQLITE_OK)
|
|
|
|
@ -1386,7 +1387,7 @@ bool tf_ssb_db_identity_delete(tf_ssb_t* ssb, const char* user, const char* publ
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
tf_printf("deleting [%s] [%s]\n", user, public_key);
|
|
|
|
|
if (sqlite3_prepare(db, "DELETE FROM identities WHERE user = ? AND public_key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "DELETE FROM identities WHERE user = ? AND public_key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, public_key, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -1406,7 +1407,7 @@ void tf_ssb_db_identity_visit(tf_ssb_t* ssb, const char* user, void (*callback)(
|
|
|
|
|
{
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT public_key FROM identities WHERE user = ? ORDER BY public_key", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT public_key FROM identities WHERE user = ? ORDER BY public_key", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -1424,7 +1425,7 @@ void tf_ssb_db_identity_visit_all(tf_ssb_t* ssb, void (*callback)(const char* id
|
|
|
|
|
{
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT public_key FROM identities ORDER BY public_key", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT public_key FROM identities ORDER BY public_key", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
while (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
|
{
|
|
|
|
@ -1440,7 +1441,7 @@ const char* tf_ssb_db_get_user_for_identity(tf_ssb_t* ssb, const char* public_ke
|
|
|
|
|
const char* result = NULL;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT user FROM identities WHERE public_key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT user FROM identities WHERE public_key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, (public_key && *public_key == '@') ? public_key + 1 : public_key, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -1472,7 +1473,7 @@ bool tf_ssb_db_identity_get_private_key(tf_ssb_t* ssb, const char* user, const c
|
|
|
|
|
}
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT private_key FROM identities WHERE user = ? AND public_key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT private_key FROM identities WHERE user = ? AND public_key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK &&
|
|
|
|
|
sqlite3_bind_text(statement, 2, (public_key && *public_key == '@') ? public_key + 1 : public_key, -1, NULL) == SQLITE_OK)
|
|
|
|
@ -1699,7 +1700,7 @@ static sqlite3_stmt* _make_following_statement(sqlite3* db)
|
|
|
|
|
{
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
|
|
|
|
|
if (sqlite3_prepare(db,
|
|
|
|
|
if (sqlite3_prepare_v2(db,
|
|
|
|
|
"SELECT content ->> '$.contact' AS contact, content ->> '$.following', content ->> '$.blocking' "
|
|
|
|
|
"FROM messages "
|
|
|
|
|
"WHERE author = ? AND content ->> '$.type' = 'contact' AND contact IS NOT NULL "
|
|
|
|
@ -1849,7 +1850,7 @@ JSValue tf_ssb_db_get_message_by_id(tf_ssb_t* ssb, const char* id, bool is_keys)
|
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT previous, author, id, sequence, timestamp, hash, json(content), signature, flags FROM messages WHERE id = ?", -1, &statement, NULL) ==
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT previous, author, id, sequence, timestamp, hash, json(content), signature, flags FROM messages WHERE id = ?", -1, &statement, NULL) ==
|
|
|
|
|
SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK)
|
|
|
|
@ -1887,7 +1888,7 @@ tf_ssb_db_stored_connection_t* tf_ssb_db_get_stored_connections(tf_ssb_t* ssb, i
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT host, port, key FROM connections ORDER BY host, port, key", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT host, port, key FROM connections ORDER BY host, port, key", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
while (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
|
{
|
|
|
|
@ -1911,7 +1912,7 @@ void tf_ssb_db_forget_stored_connection(tf_ssb_t* ssb, const char* address, int
|
|
|
|
|
{
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "DELETE FROM connections WHERE host = ? AND port = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "DELETE FROM connections WHERE host = ? AND port = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, address, -1, NULL) != SQLITE_OK || sqlite3_bind_int(statement, 2, port) != SQLITE_OK ||
|
|
|
|
|
sqlite3_bind_text(statement, 3, pubkey, -1, NULL) != SQLITE_OK || sqlite3_step(statement) != SQLITE_DONE)
|
|
|
|
@ -1928,7 +1929,7 @@ bool tf_ssb_db_get_account_password_hash(tf_ssb_t* ssb, const char* name, char*
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT value ->> '$.password' FROM properties WHERE id = 'auth' AND key = 'user:' || ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT value ->> '$.password' FROM properties WHERE id = 'auth' AND key = 'user:' || ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, name, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -1963,7 +1964,7 @@ bool tf_ssb_db_set_account_password(uv_loop_t* loop, sqlite3* db, JSContext* con
|
|
|
|
|
const char* user_string = JS_ToCStringLen(context, &user_length, user_json);
|
|
|
|
|
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES ('auth', 'user:' || ?, ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES ('auth', 'user:' || ?, ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, name, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, user_string, user_length, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -1988,7 +1989,7 @@ bool tf_ssb_db_register_account(uv_loop_t* loop, sqlite3* db, JSContext* context
|
|
|
|
|
if (registration_allowed)
|
|
|
|
|
{
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = 'auth' AND key = 'users'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT value FROM properties WHERE id = 'auth' AND key = 'users'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
|
{
|
|
|
|
@ -2007,7 +2008,7 @@ bool tf_ssb_db_register_account(uv_loop_t* loop, sqlite3* db, JSContext* context
|
|
|
|
|
JS_FreeValue(context, users_array);
|
|
|
|
|
size_t value_length = 0;
|
|
|
|
|
const char* value = JS_ToCStringLen(context, &value_length, json);
|
|
|
|
|
if (sqlite3_prepare(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES ('auth', 'users', ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES ('auth', 'users', ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, value, value_length, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2028,7 +2029,7 @@ const char* tf_ssb_db_get_property(tf_ssb_t* ssb, const char* id, const char* ke
|
|
|
|
|
char* result = NULL;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT value FROM properties WHERE id = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, key, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2051,7 +2052,7 @@ bool tf_ssb_db_set_property(tf_ssb_t* ssb, const char* id, const char* key, cons
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES (?, ?, ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO properties (id, key, value) VALUES (?, ?, ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, key, -1, NULL) == SQLITE_OK &&
|
|
|
|
|
sqlite3_bind_text(statement, 3, value, -1, NULL) == SQLITE_OK)
|
|
|
|
@ -2069,7 +2070,7 @@ bool tf_ssb_db_remove_property(tf_ssb_t* ssb, const char* id, const char* key)
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "DELETE FROM properties WHERE id = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "DELETE FROM properties WHERE id = ? AND key = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, key, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2086,7 +2087,7 @@ bool tf_ssb_db_remove_value_from_array_property(tf_ssb_t* ssb, const char* id, c
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db,
|
|
|
|
|
if (sqlite3_prepare_v2(db,
|
|
|
|
|
"UPDATE properties SET value = json_remove(properties.value, entry.fullkey) FROM json_each(properties.value) AS entry WHERE properties.id = ? AND properties.key = ? "
|
|
|
|
|
"AND entry.value = ?",
|
|
|
|
|
-1, &statement, NULL) == SQLITE_OK)
|
|
|
|
@ -2107,7 +2108,7 @@ bool tf_ssb_db_add_value_to_array_property(tf_ssb_t* ssb, const char* id, const
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db,
|
|
|
|
|
if (sqlite3_prepare_v2(db,
|
|
|
|
|
"INSERT INTO properties (id, key, value) VALUES (?1, ?2, json_array(?3)) ON CONFLICT DO UPDATE SET value = json_insert(properties.value, '$[#]', ?3) WHERE "
|
|
|
|
|
"properties.id = ?1 AND properties.key = ?2 AND NOT EXISTS (SELECT 1 FROM json_each(properties.value) AS entry WHERE entry.value = ?3)",
|
|
|
|
|
-1, &statement, NULL) == SQLITE_OK)
|
|
|
|
@ -2127,7 +2128,7 @@ bool tf_ssb_db_identity_get_active(sqlite3* db, const char* user, const char* pa
|
|
|
|
|
{
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
bool found = false;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = ? AND key = 'id:' || ? || ':' || ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT value FROM properties WHERE id = ? AND key = 'id:' || ? || ':' || ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, package_owner, -1, NULL) == SQLITE_OK &&
|
|
|
|
|
sqlite3_bind_text(statement, 3, package_name, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
@ -2154,7 +2155,7 @@ static void _tf_ssb_db_resolve_index_work(tf_ssb_t* ssb, void* user_data)
|
|
|
|
|
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT json_extract(value, '$.index_map') FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT json_extract(value, '$.index_map') FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
|
{
|
|
|
|
@ -2185,7 +2186,7 @@ static void _tf_ssb_db_resolve_index_work(tf_ssb_t* ssb, void* user_data)
|
|
|
|
|
|
|
|
|
|
if (!request->path)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT json_extract(value, '$.index') FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT json_extract(value, '$.index') FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_step(statement) == SQLITE_ROW)
|
|
|
|
|
{
|
|
|
|
@ -2230,7 +2231,7 @@ static void _tf_ssb_db_set_flags(tf_ssb_t* ssb, const char* message_id, int flag
|
|
|
|
|
{
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(db, "UPDATE messages SET flags = ? WHERE id = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "UPDATE messages SET flags = ? WHERE id = ?", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_int(statement, 1, flags) == SQLITE_OK && sqlite3_bind_text(statement, 2, message_id, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2319,7 +2320,7 @@ bool tf_ssb_db_user_has_permission(tf_ssb_t* ssb, sqlite3* db, const char* id, c
|
|
|
|
|
bool has_permission = false;
|
|
|
|
|
sqlite3* reader = db ? db : tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
if (sqlite3_prepare(reader,
|
|
|
|
|
if (sqlite3_prepare_v2(reader,
|
|
|
|
|
"SELECT COUNT(*) FROM properties, json_each(properties.value -> 'permissions' -> ?) AS permission WHERE properties.id = 'core' AND properties.key = 'settings' AND "
|
|
|
|
|
"permission.value = ?",
|
|
|
|
|
-1, &statement, NULL) == SQLITE_OK)
|
|
|
|
@ -2342,7 +2343,7 @@ bool tf_ssb_db_get_global_setting_bool(sqlite3* db, const char* name, bool* out_
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT json_extract(value, '$.' || ?) FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT json_extract(value, '$.' || ?) FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, name, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2369,7 +2370,7 @@ bool tf_ssb_db_get_global_setting_int64(sqlite3* db, const char* name, int64_t*
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT json_extract(value, '$.' || ?) FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT json_extract(value, '$.' || ?) FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, name, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2396,7 +2397,7 @@ bool tf_ssb_db_get_global_setting_string(sqlite3* db, const char* name, char* ou
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT json_extract(value, '$.' || ?) FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT json_extract(value, '$.' || ?) FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, name, -1, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2429,7 +2430,7 @@ bool tf_ssb_db_set_global_setting_from_string(sqlite3* db, const char* name, cha
|
|
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db,
|
|
|
|
|
if (sqlite3_prepare_v2(db,
|
|
|
|
|
"INSERT INTO properties (id, key, value) VALUES ('core', 'settings', json_object(?1, ?2)) ON CONFLICT DO UPDATE SET value = json_set(value, '$.' || ?1, ?2)", -1,
|
|
|
|
|
&statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2468,7 +2469,7 @@ const char* tf_ssb_db_get_profile(sqlite3* db, const char* id)
|
|
|
|
|
{
|
|
|
|
|
const char* result = NULL;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db,
|
|
|
|
|
if (sqlite3_prepare_v2(db,
|
|
|
|
|
"SELECT json(json_group_object(key, value)) FROM (SELECT fields.key, RANK() OVER (PARTITION BY fields.key ORDER BY messages.sequence DESC) AS rank, fields.value FROM "
|
|
|
|
|
"messages, json_each(messages.content) AS fields WHERE messages.author = ? AND messages.content ->> '$.type' = 'about' AND messages.content ->> '$.about' = "
|
|
|
|
|
"messages.author AND NOT fields.key IN ('about', 'type')) WHERE rank = 1",
|
|
|
|
@ -2494,7 +2495,7 @@ const char* tf_ssb_db_get_profile_name(sqlite3* db, const char* id)
|
|
|
|
|
{
|
|
|
|
|
const char* result = NULL;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db,
|
|
|
|
|
if (sqlite3_prepare_v2(db,
|
|
|
|
|
"SELECT name FROM (SELECT messages.author, RANK() OVER (PARTITION BY messages.author ORDER BY messages.sequence DESC) AS author_rank, "
|
|
|
|
|
"messages.content ->> 'name' AS name FROM messages WHERE messages.author = ? "
|
|
|
|
|
"AND messages.content ->> '$.type' = 'about' AND content ->> 'about' = messages.author AND name IS NOT NULL) "
|
|
|
|
@ -2520,7 +2521,7 @@ const char* tf_ssb_db_get_profile_name(sqlite3* db, const char* id)
|
|
|
|
|
static void _tf_ssb_db_invite_cleanup(sqlite3* db)
|
|
|
|
|
{
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "DELETE FROM invites WHERE use_count = 0 OR (expires > 0 AND expires < ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "DELETE FROM invites WHERE use_count = 0 OR (expires > 0 AND expires < ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_int64(statement, 1, (int64_t)time(NULL)) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2572,7 +2573,7 @@ bool tf_ssb_db_generate_invite(sqlite3* db, const char* id, const char* host, in
|
|
|
|
|
|
|
|
|
|
bool inserted = false;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "INSERT INTO invites (invite_public_key, account, use_count, expires) VALUES (?, ?, ?, ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "INSERT INTO invites (invite_public_key, account, use_count, expires) VALUES (?, ?, ?, ?)", -1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, public, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, id, -1, NULL) == SQLITE_OK &&
|
|
|
|
|
sqlite3_bind_int(statement, 3, use_count) == SQLITE_OK &&
|
|
|
|
@ -2593,8 +2594,8 @@ bool tf_ssb_db_use_invite(sqlite3* db, const char* id)
|
|
|
|
|
{
|
|
|
|
|
bool used = false;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "UPDATE invites SET use_count = use_count - 1 WHERE invite_public_key = ? AND (expires < 0 OR expires >= ?) AND (use_count > 0 OR use_count = -1)", -1,
|
|
|
|
|
&statement, NULL) == SQLITE_OK)
|
|
|
|
|
if (sqlite3_prepare_v2(db, "UPDATE invites SET use_count = use_count - 1 WHERE invite_public_key = ? AND (expires < 0 OR expires >= ?) AND (use_count > 0 OR use_count = -1)",
|
|
|
|
|
-1, &statement, NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, (int64_t)time(NULL)) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
@ -2612,7 +2613,7 @@ bool tf_ssb_db_has_invite(sqlite3* db, const char* id)
|
|
|
|
|
{
|
|
|
|
|
bool has = false;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
if (sqlite3_prepare(db, "SELECT COUNT(*) FROM invites WHERE invite_public_key = ? AND (expires < 0 OR expires >= ?) AND (use_count > 0 OR use_count = -1)", -1, &statement,
|
|
|
|
|
if (sqlite3_prepare_v2(db, "SELECT COUNT(*) FROM invites WHERE invite_public_key = ? AND (expires < 0 OR expires >= ?) AND (use_count > 0 OR use_count = -1)", -1, &statement,
|
|
|
|
|
NULL) == SQLITE_OK)
|
|
|
|
|
{
|
|
|
|
|
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, (int64_t)time(NULL)) == SQLITE_OK)
|
|
|
|
@ -2653,7 +2654,7 @@ tf_ssb_identity_info_t* tf_ssb_db_get_identity_info(tf_ssb_t* ssb, const char* u
|
|
|
|
|
|
|
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
|
sqlite3_stmt* statement = NULL;
|
|
|
|
|
int result = sqlite3_prepare(db,
|
|
|
|
|
int result = sqlite3_prepare_v2(db,
|
|
|
|
|
"SELECT author, name FROM ( "
|
|
|
|
|
" SELECT "
|
|
|
|
|
" messages.author, "
|
|
|
|
|