diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index feb6dfe..9ac9f57 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -11,9 +11,9 @@ import { isFollowing, lookupAccount, nostrNow, Time } from '@/utils.ts'; import { paginated, paginationSchema, parseBody } from '@/utils/web.ts'; import { createEvent } from '@/utils/web.ts'; import { renderEventAccounts } from '@/views.ts'; -import { renderAccount } from '@/views/mastodon/accounts.ts'; +import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; +import { renderRelationship } from '@/views/mastodon/relationships.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; -import { accountFromPubkey, toRelationship } from '@/views/nostr-to-mastoapi.ts'; const usernameSchema = z .string().min(1).max(30) @@ -117,7 +117,7 @@ const relationshipsController: AppController = async (c) => { return c.json({ error: 'Missing `id[]` query parameters.' }, 422); } - const result = await Promise.all(ids.data.map((id) => toRelationship(pubkey, id))); + const result = await Promise.all(ids.data.map((id) => renderRelationship(pubkey, id))); return c.json(result); }; @@ -222,7 +222,7 @@ const followController: AppController = async (c) => { }, c); } - const relationship = await toRelationship(sourcePubkey, targetPubkey); + const relationship = await renderRelationship(sourcePubkey, targetPubkey); return c.json(relationship); }; diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index c5f3e7e..89f59f7 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -2,7 +2,7 @@ import { type AppController } from '@/app.ts'; import * as mixer from '@/mixer.ts'; import { Time } from '@/utils.ts'; import { paginated, paginationSchema } from '@/utils/web.ts'; -import { toNotification } from '@/views/nostr-to-mastoapi.ts'; +import { renderNotification } from '@/views/mastodon/notifications.ts'; const notificationsController: AppController = async (c) => { const pubkey = c.get('pubkey')!; @@ -13,7 +13,7 @@ const notificationsController: AppController = async (c) => { { timeout: Time.seconds(3) }, ); - const statuses = await Promise.all(events.map((event) => toNotification(event, pubkey))); + const statuses = await Promise.all(events.map((event) => renderNotification(event, pubkey))); return paginated(c, events, statuses); }; diff --git a/src/views/mastodon/notifications.ts b/src/views/mastodon/notifications.ts new file mode 100644 index 0000000..59e5665 --- /dev/null +++ b/src/views/mastodon/notifications.ts @@ -0,0 +1,26 @@ +import { type Event } from '@/deps.ts'; +import { nostrDate } from '@/utils.ts'; +import { accountFromPubkey } from '@/views/mastodon/accounts.ts'; +import { renderStatus } from '@/views/mastodon/statuses.ts'; + +function renderNotification(event: Event, viewerPubkey?: string) { + switch (event.kind) { + case 1: + return renderNotificationMention(event as Event<1>, viewerPubkey); + } +} + +async function renderNotificationMention(event: Event<1>, viewerPubkey?: string) { + const status = await renderStatus(event, viewerPubkey); + if (!status) return; + + return { + id: event.id, + type: 'mention', + created_at: nostrDate(event.created_at).toISOString(), + account: status.account, + status: status, + }; +} + +export { accountFromPubkey, renderNotification }; diff --git a/src/views/mastodon/relationships.ts b/src/views/mastodon/relationships.ts new file mode 100644 index 0000000..91d33ea --- /dev/null +++ b/src/views/mastodon/relationships.ts @@ -0,0 +1,26 @@ +import { getFollows } from '@/queries.ts'; +import { isFollowing } from '@/utils.ts'; + +async function renderRelationship(sourcePubkey: string, targetPubkey: string) { + const [source, target] = await Promise.all([ + getFollows(sourcePubkey), + getFollows(targetPubkey), + ]); + + return { + id: targetPubkey, + following: source ? isFollowing(source, targetPubkey) : false, + showing_reblogs: true, + notifying: false, + followed_by: target ? isFollowing(target, sourcePubkey) : false, + blocking: false, + blocked_by: false, + muting: false, + muting_notifications: false, + requested: false, + domain_blocking: false, + endorsed: false, + }; +} + +export { renderRelationship }; diff --git a/src/views/nostr-to-mastoapi.ts b/src/views/nostr-to-mastoapi.ts deleted file mode 100644 index bdad739..0000000 --- a/src/views/nostr-to-mastoapi.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { type Event } from '@/deps.ts'; -import { getFollows } from '@/queries.ts'; -import { isFollowing, nostrDate } from '@/utils.ts'; -import { accountFromPubkey } from '@/views/mastodon/accounts.ts'; -import { renderStatus } from '@/views/mastodon/statuses.ts'; - -async function toRelationship(sourcePubkey: string, targetPubkey: string) { - const [source, target] = await Promise.all([ - getFollows(sourcePubkey), - getFollows(targetPubkey), - ]); - - return { - id: targetPubkey, - following: source ? isFollowing(source, targetPubkey) : false, - showing_reblogs: true, - notifying: false, - followed_by: target ? isFollowing(target, sourcePubkey) : false, - blocking: false, - blocked_by: false, - muting: false, - muting_notifications: false, - requested: false, - domain_blocking: false, - endorsed: false, - }; -} - -function toNotification(event: Event, viewerPubkey?: string) { - switch (event.kind) { - case 1: - return toNotificationMention(event as Event<1>, viewerPubkey); - } -} - -async function toNotificationMention(event: Event<1>, viewerPubkey?: string) { - const status = await renderStatus(event, viewerPubkey); - if (!status) return; - - return { - id: event.id, - type: 'mention', - created_at: nostrDate(event.created_at).toISOString(), - account: status.account, - status: status, - }; -} - -export { accountFromPubkey, toNotification, toRelationship };