diff --git a/core/core.js b/core/core.js index 554e1ce8..6535e9ea 100644 --- a/core/core.js +++ b/core/core.js @@ -17,6 +17,8 @@ let gProcesses = {}; let gStatsTimer = false; /** Effectively a process ID. */ let g_handler_index = 0; +/** Whether updating accounts information is currently scheduled. */ +let g_update_accounts_scheduled; /** Time between pings, in milliseconds. */ const k_ping_interval = 60 * 1000; @@ -275,18 +277,23 @@ async function getProcessBlob(blobId, key, options) { }, }; process.sendIdentities = async function () { - process.app.send( - Object.assign( - { - action: 'identities', - }, - await ssb_internal.getIdentityInfo( - process?.credentials?.session?.name, - options?.packageOwner, - options?.packageName - ) - ) + let identities = await ssb_internal.getIdentityInfo( + process?.credentials?.session?.name, + options?.packageOwner, + options?.packageName ); + let json = JSON.stringify(identities); + if (process._last_sent_identities !== json) { + process.app.send( + Object.assign( + { + action: 'identities', + }, + identities + ) + ); + process._last_sent_identities = json; + } }; process.setActiveIdentity = async function (identity) { if ( @@ -624,11 +631,27 @@ async function getProcessBlob(blobId, key, options) { return process; } +/** + * Send any changed account information. + */ +function updateAccounts() { + let promises = []; + for (let process of Object.values(gProcesses)) { + promises.push(process.sendIdentities()); + } + return Promise.all(promises); +} + /** * SSB message added callback. */ ssb_internal.addEventListener('message', function () { broadcastEvent('onMessage', [...arguments]); + + if (!g_update_accounts_scheduled) { + setTimeout(updateAccounts, 1000); + g_update_accounts_scheduled = true; + } }); ssb_internal.addEventListener('blob', function () { diff --git a/src/ssb.db.c b/src/ssb.db.c index 9e62316b..06edd7de 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -623,6 +623,12 @@ static char* _tf_ssb_db_get_message_blob_wants(sqlite3* db, int64_t rowid) return result; } +typedef enum _message_type_t +{ + k_message_type_other, + k_message_type_post, +} message_type_t; + typedef struct _message_store_t { char id[k_id_base64_len]; @@ -635,6 +641,7 @@ typedef struct _message_store_t const char* content; size_t length; + message_type_t type; bool out_stored; char* out_blob_wants; @@ -717,26 +724,25 @@ static void _tf_ssb_db_store_message_after_work(tf_ssb_t* ssb, int status, void* } #if TARGET_OS_IPHONE - JSContext* context = tf_ssb_get_context(ssb); - JSValue content = JS_ParseJSON(context, store->content, strlen(store->content), NULL); - if (JS_IsObject(content)) + if (store->type == k_message_type_post) { - JSValue type_value = JS_GetPropertyStr(context, content, "type"); - const char* type = JS_ToCString(context, type_value); - if (type && strcmp(type, "post") == 0) + JSContext* context = tf_ssb_get_context(ssb); + JSValue content = JS_ParseJSON(context, store->content, strlen(store->content), NULL); + if (JS_IsObject(content)) { + JSValue type_value = JS_GetPropertyStr(context, content, "type"); + const char* type = JS_ToCString(context, type_value); JSValue text_value = JS_GetPropertyStr(context, content, "text"); const char* text = JS_ToCString(context, text_value); - void tf_notify_message_added_ios(const char* identifier, const char* title, const char* content); tf_notify_message_added_ios(store->id, type, text); JS_FreeCString(context, text); JS_FreeValue(context, text_value); + JS_FreeCString(context, type); + JS_FreeValue(context, type_value); } - JS_FreeCString(context, type); - JS_FreeValue(context, type_value); + JS_FreeValue(context, content); } - JS_FreeValue(context, content); #endif if (store->callback) @@ -800,6 +806,16 @@ void tf_ssb_db_store_message( JS_FreeValue(context, timestampval); JSValue contentval = JS_GetPropertyStr(context, val, "content"); + JSValue typeval = JS_IsObject(contentval) ? JS_GetPropertyStr(context, contentval, "type") : JS_UNDEFINED; + const char* type = JS_IsString(typeval) ? JS_ToCString(context, typeval) : NULL; + message_type_t message_type = k_message_type_other; + if (type) + { + message_type = strcmp(type, "post") == 0 ? k_message_type_post : k_message_type_other; + } + JS_FreeCString(context, type); + JS_FreeValue(context, typeval); + JSValue content = JS_JSONStringify(context, contentval, JS_NULL, JS_NULL); size_t content_len; const char* contentstr = JS_ToCStringLen(context, &content_len, content); @@ -811,6 +827,7 @@ void tf_ssb_db_store_message( .sequence = sequence, .timestamp = timestamp, .content = contentstr, + .type = message_type, .length = content_len, .flags = flags,