From 66277041143630c382fcf5d9ab585151c0c1ec58 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 28 Apr 2024 17:07:41 -0300 Subject: [PATCH 1/2] feat: save user preferences (kind 30078) & encrypt it --- src/controllers/api/accounts.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 02897993..f6440fee 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -18,6 +18,7 @@ import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderRelationship } from '@/views/mastodon/relationships.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; +import { APISigner } from '@/signers/APISigner.ts'; const usernameSchema = z .string().min(1).max(30) @@ -186,6 +187,7 @@ const updateCredentialsSchema = z.object({ bot: z.boolean().optional(), discoverable: z.boolean().optional(), nip05: z.string().optional(), + pleroma_settings_store: z.object({ soapbox_fe: z.object({ themeMode: z.string() }).passthrough() }).optional(), }); const updateCredentialsController: AppController = async (c) => { @@ -225,6 +227,16 @@ const updateCredentialsController: AppController = async (c) => { tags: [], }, c); + const soapbox_fe = result.data.pleroma_settings_store?.soapbox_fe; + if (soapbox_fe) { + const signer = new APISigner(c); + await createEvent({ + kind: 30078, + tags: [['d', 'pub.ditto.preferences']], + content: await signer.nip44.encrypt(pubkey, JSON.stringify(soapbox_fe)), + }, c); + } + const account = await renderAccount(event, { withSource: true }); return c.json(account); }; From bb82df14c6c4d0cf9612a6556fbcdb994517ddbb Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 28 Apr 2024 21:42:57 -0300 Subject: [PATCH 2/2] refactor: user preference in create & verify credentials --- src/controllers/api/accounts.ts | 39 ++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index f6440fee..5f0840e3 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -50,7 +50,21 @@ const verifyCredentialsController: AppController = async (c) => { const event = await getAuthor(pubkey, { relations: ['author_stats'] }); if (event) { - return c.json(await renderAccount(event, { withSource: true })); + const account = await renderAccount(event, { withSource: true }); + + const [userPreferencesEvent] = await eventsDB.query([{ + authors: [pubkey], + kinds: [30078], + '#d': ['pub.ditto.pleroma_settings_store'], + limit: 1, + }]); + if (userPreferencesEvent) { + const signer = new APISigner(c); + const userPreference = JSON.parse(await signer.nip44.decrypt(pubkey, userPreferencesEvent.content)); + (account.pleroma as any).settings_store = userPreference; + } + + return c.json(account); } else { return c.json(await accountFromPubkey(pubkey, { withSource: true })); } @@ -187,7 +201,7 @@ const updateCredentialsSchema = z.object({ bot: z.boolean().optional(), discoverable: z.boolean().optional(), nip05: z.string().optional(), - pleroma_settings_store: z.object({ soapbox_fe: z.object({ themeMode: z.string() }).passthrough() }).optional(), + pleroma_settings_store: z.object({ soapbox_fe: z.record(z.string(), z.unknown()) }).optional(), }); const updateCredentialsController: AppController = async (c) => { @@ -227,17 +241,30 @@ const updateCredentialsController: AppController = async (c) => { tags: [], }, c); - const soapbox_fe = result.data.pleroma_settings_store?.soapbox_fe; - if (soapbox_fe) { + const pleroma_frontend = result.data.pleroma_settings_store; + if (pleroma_frontend) { const signer = new APISigner(c); await createEvent({ kind: 30078, - tags: [['d', 'pub.ditto.preferences']], - content: await signer.nip44.encrypt(pubkey, JSON.stringify(soapbox_fe)), + tags: [['d', 'pub.ditto.pleroma_settings_store']], + content: await signer.nip44.encrypt(pubkey, JSON.stringify(pleroma_frontend)), }, c); } const account = await renderAccount(event, { withSource: true }); + + const [userPreferencesEvent] = await eventsDB.query([{ + authors: [pubkey], + kinds: [30078], + '#d': ['pub.ditto.pleroma_settings_store'], + limit: 1, + }]); + if (userPreferencesEvent) { + const signer = new APISigner(c); + const userPreference = JSON.parse(await signer.nip44.decrypt(pubkey, userPreferencesEvent.content)); + (account.pleroma as any).settings_store = userPreference; + } + return c.json(account); };