forked from cory/tildefriends
		
	ssb: Remove the weird option to make the server account follow you. Now that this account is admin-controlled, it's unnecessary.
This commit is contained in:
		| @@ -1,5 +1,5 @@ | |||||||
| { | { | ||||||
| 	"type": "tildefriends-app", | 	"type": "tildefriends-app", | ||||||
| 	"emoji": "🦀", | 	"emoji": "🦀", | ||||||
| 	"previous": "&20LHLqRX51iuC74SlJ+u76sH9cyptDyuR3/bkqE4yhA=.sha256" | 	"previous": "&vne+VDF5TCAlv/9HqNIt5Zgkf+WNwwm+XiG/xwlBrnk=.sha256" | ||||||
| } | } | ||||||
|   | |||||||
| @@ -21,9 +21,6 @@ tfrpc.register(async function createIdentity() { | |||||||
| tfrpc.register(async function getServerIdentity() { | tfrpc.register(async function getServerIdentity() { | ||||||
| 	return ssb.getServerIdentity(); | 	return ssb.getServerIdentity(); | ||||||
| }); | }); | ||||||
| tfrpc.register(async function setServerFollowingMe(id, following) { |  | ||||||
| 	return ssb.setServerFollowingMe(id, following); |  | ||||||
| }); |  | ||||||
| tfrpc.register(async function getIdentities() { | tfrpc.register(async function getIdentities() { | ||||||
| 	return ssb.getIdentities(); | 	return ssb.getIdentities(); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -11,7 +11,6 @@ class TfProfileElement extends LitElement { | |||||||
| 			id: {type: String}, | 			id: {type: String}, | ||||||
| 			users: {type: Object}, | 			users: {type: Object}, | ||||||
| 			size: {type: Number}, | 			size: {type: Number}, | ||||||
| 			server_follows_me: {type: Boolean}, |  | ||||||
| 			following: {type: Boolean}, | 			following: {type: Boolean}, | ||||||
| 			blocking: {type: Boolean}, | 			blocking: {type: Boolean}, | ||||||
| 		}; | 		}; | ||||||
| @@ -27,7 +26,6 @@ class TfProfileElement extends LitElement { | |||||||
| 		this.id = null; | 		this.id = null; | ||||||
| 		this.users = {}; | 		this.users = {}; | ||||||
| 		this.size = 0; | 		this.size = 0; | ||||||
| 		this.server_follows_me = undefined; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async load() { | 	async load() { | ||||||
| @@ -63,26 +61,6 @@ class TfProfileElement extends LitElement { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async initial_load() { |  | ||||||
| 		this.server_follows_me = undefined; |  | ||||||
| 		let server_id = await tfrpc.rpc.getServerIdentity(); |  | ||||||
| 		let followed = await tfrpc.rpc.query( |  | ||||||
| 			` |  | ||||||
| 			SELECT json_extract(content, '$.following') AS following |  | ||||||
| 			FROM messages |  | ||||||
| 			WHERE author = ? AND |  | ||||||
| 			json_extract(content, '$.type') = 'contact' AND |  | ||||||
| 			json_extract(content, '$.contact') = ? ORDER BY sequence DESC LIMIT 1 |  | ||||||
| 		`, |  | ||||||
| 			[server_id, this.whoami] |  | ||||||
| 		); |  | ||||||
| 		let is_followed = false; |  | ||||||
| 		for (let row of followed) { |  | ||||||
| 			is_followed = row.following != 0; |  | ||||||
| 		} |  | ||||||
| 		this.server_follows_me = is_followed; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	modify(change) { | 	modify(change) { | ||||||
| 		tfrpc.rpc | 		tfrpc.rpc | ||||||
| 			.appendMessage( | 			.appendMessage( | ||||||
| @@ -175,31 +153,11 @@ class TfProfileElement extends LitElement { | |||||||
| 		input.click(); | 		input.click(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async server_follow_me(follow) { |  | ||||||
| 		try { |  | ||||||
| 			await tfrpc.rpc.setServerFollowingMe(this.whoami, follow); |  | ||||||
| 		} catch (e) { |  | ||||||
| 			console.log(e); |  | ||||||
| 		} |  | ||||||
| 		try { |  | ||||||
| 			await this.initial_load(); |  | ||||||
| 		} catch (e) { |  | ||||||
| 			console.log(e); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	copy_id() { | 	copy_id() { | ||||||
| 		navigator.clipboard.writeText(this.id); | 		navigator.clipboard.writeText(this.id); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	render() { | 	render() { | ||||||
| 		if ( |  | ||||||
| 			this.id == this.whoami && |  | ||||||
| 			this.editing && |  | ||||||
| 			this.server_follows_me === undefined |  | ||||||
| 		) { |  | ||||||
| 			this.initial_load(); |  | ||||||
| 		} |  | ||||||
| 		this.load(); | 		this.load(); | ||||||
| 		let self = this; | 		let self = this; | ||||||
| 		let profile = this.users[this.id] || {}; | 		let profile = this.users[this.id] || {}; | ||||||
| @@ -216,22 +174,6 @@ class TfProfileElement extends LitElement { | |||||||
| 		let block; | 		let block; | ||||||
| 		if (this.id === this.whoami) { | 		if (this.id === this.whoami) { | ||||||
| 			if (this.editing) { | 			if (this.editing) { | ||||||
| 				let server_follow; |  | ||||||
| 				if (this.server_follows_me === true) { |  | ||||||
| 					server_follow = html`<button |  | ||||||
| 						class="w3-button w3-theme-d1" |  | ||||||
| 						@click=${() => this.server_follow_me(false)} |  | ||||||
| 					> |  | ||||||
| 						Server, Stop Following Me |  | ||||||
| 					</button>`; |  | ||||||
| 				} else if (this.server_follows_me === false) { |  | ||||||
| 					server_follow = html`<button |  | ||||||
| 						class="w3-button w3-theme-d1" |  | ||||||
| 						@click=${() => this.server_follow_me(true)} |  | ||||||
| 					> |  | ||||||
| 						Server, Follow Me |  | ||||||
| 					</button>`; |  | ||||||
| 				} |  | ||||||
| 				edit = html` | 				edit = html` | ||||||
| 					<button | 					<button | ||||||
| 						id="save_profile" | 						id="save_profile" | ||||||
| @@ -243,7 +185,6 @@ class TfProfileElement extends LitElement { | |||||||
| 					<button class="w3-button w3-theme-d1" @click=${this.discard_edits}> | 					<button class="w3-button w3-theme-d1" @click=${this.discard_edits}> | ||||||
| 						Discard | 						Discard | ||||||
| 					</button> | 					</button> | ||||||
| 					${server_follow} |  | ||||||
| 				`; | 				`; | ||||||
| 			} else { | 			} else { | ||||||
| 				edit = html`<button | 				edit = html`<button | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								core/core.js
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								core/core.js
									
									
									
									
									
								
							| @@ -577,19 +577,6 @@ async function getProcessBlob(blobId, key, options) { | |||||||
| 					); | 					); | ||||||
| 				} | 				} | ||||||
| 			}; | 			}; | ||||||
| 			imports.ssb.setServerFollowingMe = function (id, following) { |  | ||||||
| 				if ( |  | ||||||
| 					process.credentials && |  | ||||||
| 					process.credentials.session && |  | ||||||
| 					process.credentials.session.name |  | ||||||
| 				) { |  | ||||||
| 					return ssb.setServerFollowingMe( |  | ||||||
| 						process.credentials.session.name, |  | ||||||
| 						id, |  | ||||||
| 						following |  | ||||||
| 					); |  | ||||||
| 				} |  | ||||||
| 			}; |  | ||||||
| 			imports.ssb.swapWithServerIdentity = function (id) { | 			imports.ssb.swapWithServerIdentity = function (id) { | ||||||
| 				if ( | 				if ( | ||||||
| 					process.credentials && | 					process.credentials && | ||||||
|   | |||||||
							
								
								
									
										106
									
								
								src/ssb.js.c
									
									
									
									
									
								
							
							
						
						
									
										106
									
								
								src/ssb.js.c
									
									
									
									
									
								
							| @@ -251,111 +251,6 @@ static JSValue _tf_ssb_deleteIdentity(JSContext* context, JSValueConst this_val, | |||||||
| 	return result; | 	return result; | ||||||
| } | } | ||||||
|  |  | ||||||
| static JSValue _set_server_following_internal(tf_ssb_t* ssb, JSValueConst this_val, const char* id, bool follow) |  | ||||||
| { |  | ||||||
| 	JSContext* context = tf_ssb_get_context(ssb); |  | ||||||
| 	JSValue message = JS_NewObject(context); |  | ||||||
| 	JSValue server_user = JS_NewString(context, ":admin"); |  | ||||||
| 	char server_id_buffer[k_id_base64_len] = { 0 }; |  | ||||||
| 	tf_ssb_whoami(ssb, server_id_buffer, sizeof(server_id_buffer)); |  | ||||||
| 	JSValue server_id = JS_NewString(context, server_id_buffer); |  | ||||||
| 	JS_SetPropertyStr(context, message, "type", JS_NewString(context, "contact")); |  | ||||||
| 	JS_SetPropertyStr(context, message, "contact", JS_NewString(context, id)); |  | ||||||
| 	JS_SetPropertyStr(context, message, "following", JS_NewBool(context, follow)); |  | ||||||
| 	JSValue args[] = { |  | ||||||
| 		server_user, |  | ||||||
| 		server_id, |  | ||||||
| 		message, |  | ||||||
| 	}; |  | ||||||
| 	JSValue result = _tf_ssb_appendMessageWithIdentity(context, this_val, tf_countof(args), args); |  | ||||||
| 	JS_FreeValue(context, server_id); |  | ||||||
| 	JS_FreeValue(context, server_user); |  | ||||||
| 	JS_FreeValue(context, message); |  | ||||||
| 	return result; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| typedef struct _set_server_following_me_t |  | ||||||
| { |  | ||||||
| 	const char* user; |  | ||||||
| 	const char* key; |  | ||||||
| 	bool follow; |  | ||||||
| 	JSValue this_val; |  | ||||||
| 	JSValue promise[2]; |  | ||||||
| 	bool error_does_not_own_key; |  | ||||||
| 	bool append_message; |  | ||||||
| } set_server_following_me_t; |  | ||||||
|  |  | ||||||
| static void _tf_ssb_set_server_following_me_work(tf_ssb_t* ssb, void* user_data) |  | ||||||
| { |  | ||||||
| 	set_server_following_me_t* work = user_data; |  | ||||||
| 	if (!tf_ssb_db_identity_get_private_key(ssb, work->user, work->key, NULL, 0)) |  | ||||||
| 	{ |  | ||||||
| 		work->error_does_not_own_key = true; |  | ||||||
| 	} |  | ||||||
| 	else |  | ||||||
| 	{ |  | ||||||
| 		char server_id[k_id_base64_len] = { 0 }; |  | ||||||
| 		tf_ssb_whoami(ssb, server_id, sizeof(server_id)); |  | ||||||
| 		const char* server_id_ptr = server_id; |  | ||||||
| 		const char** current_following = tf_ssb_db_following_deep_ids(ssb, &server_id_ptr, 1, 1); |  | ||||||
| 		bool is_following = false; |  | ||||||
| 		for (const char** it = current_following; *it; it++) |  | ||||||
| 		{ |  | ||||||
| 			if (strcmp(work->key, *it) == 0) |  | ||||||
| 			{ |  | ||||||
| 				is_following = true; |  | ||||||
| 				break; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		tf_free(current_following); |  | ||||||
| 		work->append_message = (work->follow && !is_following) || (!work->follow && is_following); |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| static void _tf_ssb_set_server_following_me_after_work(tf_ssb_t* ssb, int status, void* user_data) |  | ||||||
| { |  | ||||||
| 	set_server_following_me_t* work = user_data; |  | ||||||
| 	JSContext* context = tf_ssb_get_context(ssb); |  | ||||||
| 	JSValue result = JS_UNDEFINED; |  | ||||||
| 	if (work->error_does_not_own_key) |  | ||||||
| 	{ |  | ||||||
| 		result = JS_ThrowInternalError(context, "User %s does not own key %s.", work->user, work->key); |  | ||||||
| 	} |  | ||||||
| 	else if (work->append_message) |  | ||||||
| 	{ |  | ||||||
| 		result = _set_server_following_internal(ssb, work->this_val, work->key, work->follow); |  | ||||||
| 	} |  | ||||||
| 	JS_FreeCString(context, work->key); |  | ||||||
| 	JS_FreeCString(context, work->user); |  | ||||||
| 	JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result); |  | ||||||
| 	JS_FreeValue(context, result); |  | ||||||
| 	tf_util_report_error(context, error); |  | ||||||
| 	JS_FreeValue(context, error); |  | ||||||
| 	JS_FreeValue(context, work->promise[0]); |  | ||||||
| 	JS_FreeValue(context, work->promise[1]); |  | ||||||
| 	JS_FreeValue(context, work->this_val); |  | ||||||
| 	tf_free(work); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| static JSValue _tf_ssb_set_server_following_me(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) |  | ||||||
| { |  | ||||||
| 	tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); |  | ||||||
| 	JSValue result = JS_UNDEFINED; |  | ||||||
| 	if (ssb) |  | ||||||
| 	{ |  | ||||||
| 		set_server_following_me_t* work = tf_malloc(sizeof(set_server_following_me_t)); |  | ||||||
| 		*work = (set_server_following_me_t) { |  | ||||||
| 			.user = JS_ToCString(context, argv[0]), |  | ||||||
| 			.key = JS_ToCString(context, argv[1]), |  | ||||||
| 			.follow = JS_ToBool(context, argv[2]), |  | ||||||
| 			.this_val = JS_DupValue(context, this_val), |  | ||||||
| 		}; |  | ||||||
| 		result = JS_NewPromiseCapability(context, work->promise); |  | ||||||
| 		tf_ssb_run_work(ssb, _tf_ssb_set_server_following_me_work, _tf_ssb_set_server_following_me_after_work, work); |  | ||||||
| 	} |  | ||||||
| 	return result; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| typedef struct _swap_with_server_identity_t | typedef struct _swap_with_server_identity_t | ||||||
| { | { | ||||||
| 	char server_id[k_id_base64_len]; | 	char server_id[k_id_base64_len]; | ||||||
| @@ -2562,7 +2457,6 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) | |||||||
| 	JS_SetPropertyStr(context, object, "createIdentity", JS_NewCFunction(context, _tf_ssb_createIdentity, "createIdentity", 1)); | 	JS_SetPropertyStr(context, object, "createIdentity", JS_NewCFunction(context, _tf_ssb_createIdentity, "createIdentity", 1)); | ||||||
| 	JS_SetPropertyStr(context, object, "addIdentity", JS_NewCFunction(context, _tf_ssb_addIdentity, "addIdentity", 2)); | 	JS_SetPropertyStr(context, object, "addIdentity", JS_NewCFunction(context, _tf_ssb_addIdentity, "addIdentity", 2)); | ||||||
| 	JS_SetPropertyStr(context, object, "deleteIdentity", JS_NewCFunction(context, _tf_ssb_deleteIdentity, "deleteIdentity", 2)); | 	JS_SetPropertyStr(context, object, "deleteIdentity", JS_NewCFunction(context, _tf_ssb_deleteIdentity, "deleteIdentity", 2)); | ||||||
| 	JS_SetPropertyStr(context, object, "setServerFollowingMe", JS_NewCFunction(context, _tf_ssb_set_server_following_me, "setServerFollowingMe", 3)); |  | ||||||
| 	JS_SetPropertyStr(context, object, "swapWithServerIdentity", JS_NewCFunction(context, _tf_ssb_swap_with_server_identity, "swapWithServerIdentity", 2)); | 	JS_SetPropertyStr(context, object, "swapWithServerIdentity", JS_NewCFunction(context, _tf_ssb_swap_with_server_identity, "swapWithServerIdentity", 2)); | ||||||
| 	JS_SetPropertyStr(context, object, "getIdentities", JS_NewCFunction(context, _tf_ssb_getIdentities, "getIdentities", 1)); | 	JS_SetPropertyStr(context, object, "getIdentities", JS_NewCFunction(context, _tf_ssb_getIdentities, "getIdentities", 1)); | ||||||
| 	JS_SetPropertyStr(context, object, "getPrivateKey", JS_NewCFunction(context, _tf_ssb_getPrivateKey, "getPrivateKey", 2)); | 	JS_SetPropertyStr(context, object, "getPrivateKey", JS_NewCFunction(context, _tf_ssb_getPrivateKey, "getPrivateKey", 2)); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user