Delete nostr-to-mastoapi.ts, add notifications.ts and relationships.ts

This commit is contained in:
Alex Gleason 2023-10-06 15:40:34 -05:00
parent d49c63bb1a
commit 45d42f7ea4
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 58 additions and 55 deletions

View File

@ -11,9 +11,9 @@ import { isFollowing, lookupAccount, nostrNow, Time } from '@/utils.ts';
import { paginated, paginationSchema, parseBody } from '@/utils/web.ts'; import { paginated, paginationSchema, parseBody } from '@/utils/web.ts';
import { createEvent } from '@/utils/web.ts'; import { createEvent } from '@/utils/web.ts';
import { renderEventAccounts } from '@/views.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 { renderStatus } from '@/views/mastodon/statuses.ts';
import { accountFromPubkey, toRelationship } from '@/views/nostr-to-mastoapi.ts';
const usernameSchema = z const usernameSchema = z
.string().min(1).max(30) .string().min(1).max(30)
@ -117,7 +117,7 @@ const relationshipsController: AppController = async (c) => {
return c.json({ error: 'Missing `id[]` query parameters.' }, 422); 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); return c.json(result);
}; };
@ -222,7 +222,7 @@ const followController: AppController = async (c) => {
}, c); }, c);
} }
const relationship = await toRelationship(sourcePubkey, targetPubkey); const relationship = await renderRelationship(sourcePubkey, targetPubkey);
return c.json(relationship); return c.json(relationship);
}; };

View File

@ -2,7 +2,7 @@ import { type AppController } from '@/app.ts';
import * as mixer from '@/mixer.ts'; import * as mixer from '@/mixer.ts';
import { Time } from '@/utils.ts'; import { Time } from '@/utils.ts';
import { paginated, paginationSchema } from '@/utils/web.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 notificationsController: AppController = async (c) => {
const pubkey = c.get('pubkey')!; const pubkey = c.get('pubkey')!;
@ -13,7 +13,7 @@ const notificationsController: AppController = async (c) => {
{ timeout: Time.seconds(3) }, { 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); return paginated(c, events, statuses);
}; };

View File

@ -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 };

View File

@ -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 };

View File

@ -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 };