forked from cory/tildefriends
Work in progress moving SSB RPC handlers into javascript.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3657 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
36
src/ssb.db.c
36
src/ssb.db.c
@ -35,6 +35,11 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
||||
" created INTEGER"
|
||||
")",
|
||||
NULL, NULL, NULL);
|
||||
sqlite3_exec(db,
|
||||
"CREATE TABLE IF NOT EXISTS blob_wants ("
|
||||
" id TEXT PRIMARY KEY"
|
||||
")",
|
||||
NULL, NULL, NULL);
|
||||
sqlite3_exec(db,
|
||||
"CREATE TABLE IF NOT EXISTS properties ("
|
||||
" id TEXT,"
|
||||
@ -75,6 +80,7 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
|
||||
|
||||
sqlite3* db = tf_ssb_get_db(ssb);
|
||||
sqlite3_stmt* statement;
|
||||
int64_t last_row_id = -1;
|
||||
const char* query = "INSERT INTO messages (id, previous, author, sequence, timestamp, content, hash, signature) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING";
|
||||
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK &&
|
||||
@ -90,12 +96,37 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
|
||||
printf("%s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
stored = r == SQLITE_DONE && sqlite3_changes(db) != 0;
|
||||
if (stored)
|
||||
{
|
||||
last_row_id = sqlite3_last_insert_rowid(db);
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
} else {
|
||||
printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
if (last_row_id != -1)
|
||||
{
|
||||
query = "INSERT INTO blob_wants (id) 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 ON CONFLICT DO NOTHING RETURNING id";
|
||||
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) {
|
||||
if (sqlite3_bind_int64(statement, 1, last_row_id) == SQLITE_OK &&
|
||||
sqlite3_bind_int(statement, 2, BLOB_ID_LEN - 1) == SQLITE_OK) {
|
||||
int r = SQLITE_OK;
|
||||
while ((r = sqlite3_step(statement)) == SQLITE_ROW)
|
||||
{
|
||||
tf_ssb_notify_blob_want_added(ssb, (const char*)sqlite3_column_text(statement, 0));
|
||||
}
|
||||
if (r != SQLITE_DONE) {
|
||||
printf("%s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
} else {
|
||||
printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
}
|
||||
|
||||
JS_FreeValue(context, previousval);
|
||||
JS_FreeCString(context, author);
|
||||
JS_FreeValue(context, authorval);
|
||||
@ -319,7 +350,10 @@ static int _tf_ssb_sqlite_authorizer(void* user_data, int action_code, const cha
|
||||
case SQLITE_FUNCTION:
|
||||
return SQLITE_OK;
|
||||
case SQLITE_READ:
|
||||
return strcmp(arg0, "messages") == 0 ? SQLITE_OK : SQLITE_DENY;
|
||||
return
|
||||
(strcmp(arg0, "messages") == 0 ||
|
||||
strcmp(arg0, "blob_wants") == 0)
|
||||
? SQLITE_OK : SQLITE_DENY;
|
||||
break;
|
||||
}
|
||||
return SQLITE_DENY;
|
||||
|
Reference in New Issue
Block a user