From b81da2c0d7a029736a3108dfb26524e7cff660d3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 26 Feb 2025 17:58:17 -0600 Subject: [PATCH] Move ditto/interfaces to @ditto/mastoapi/types --- packages/ditto/caches/translationCache.ts | 2 +- packages/ditto/controllers/api/accounts.ts | 3 ++- packages/ditto/controllers/api/translate.ts | 2 +- packages/ditto/controllers/api/trends.ts | 6 ++++-- packages/ditto/utils/note.ts | 3 ++- packages/ditto/utils/og-metadata.ts | 4 ++-- packages/ditto/utils/unfurl.ts | 11 +++++------ packages/ditto/views/mastodon/accounts.ts | 3 ++- packages/ditto/views/mastodon/attachments.ts | 3 ++- packages/ditto/views/mastodon/statuses.ts | 5 ++--- packages/mastoapi/deno.json | 3 ++- .../entities => mastoapi/types}/MastodonAccount.ts | 0 .../entities => mastoapi/types}/MastodonAttachment.ts | 0 .../entities => mastoapi/types}/MastodonMention.ts | 0 .../types/MastodonPreviewCard.ts} | 2 +- .../entities => mastoapi/types}/MastodonStatus.ts | 8 ++++---- .../types}/MastodonTranslation.ts | 2 +- packages/mastoapi/types/mod.ts | 6 ++++++ 18 files changed, 37 insertions(+), 26 deletions(-) rename packages/{ditto/entities => mastoapi/types}/MastodonAccount.ts (100%) rename packages/{ditto/entities => mastoapi/types}/MastodonAttachment.ts (100%) rename packages/{ditto/entities => mastoapi/types}/MastodonMention.ts (100%) rename packages/{ditto/entities/PreviewCard.ts => mastoapi/types/MastodonPreviewCard.ts} (89%) rename packages/{ditto/entities => mastoapi/types}/MastodonStatus.ts (80%) rename packages/{ditto/entities => mastoapi/types}/MastodonTranslation.ts (93%) create mode 100644 packages/mastoapi/types/mod.ts diff --git a/packages/ditto/caches/translationCache.ts b/packages/ditto/caches/translationCache.ts index 7bd27946..2196d22a 100644 --- a/packages/ditto/caches/translationCache.ts +++ b/packages/ditto/caches/translationCache.ts @@ -1,8 +1,8 @@ +import { MastodonTranslation } from '@ditto/mastoapi/types'; import { LanguageCode } from 'iso-639-1'; import { LRUCache } from 'lru-cache'; import { Conf } from '@/config.ts'; -import { MastodonTranslation } from '@/entities/MastodonTranslation.ts'; /** Translations LRU cache. */ export const translationCache = new LRUCache<`${LanguageCode}-${string}`, MastodonTranslation>({ diff --git a/packages/ditto/controllers/api/accounts.ts b/packages/ditto/controllers/api/accounts.ts index ad9dde19..a01c0038 100644 --- a/packages/ditto/controllers/api/accounts.ts +++ b/packages/ditto/controllers/api/accounts.ts @@ -19,7 +19,8 @@ import { hydrateEvents } from '@/storages/hydrate.ts'; import { bech32ToPubkey } from '@/utils.ts'; import { addTag, deleteTag, findReplyTag, getTagSet } from '@/utils/tags.ts'; import { getPubkeysBySearch } from '@/utils/search.ts'; -import { MastodonAccount } from '@/entities/MastodonAccount.ts'; + +import type { MastodonAccount } from '@ditto/mastoapi/types'; const createAccountSchema = z.object({ username: z.string().min(1).max(30).regex(/^[a-z0-9_]+$/i), diff --git a/packages/ditto/controllers/api/translate.ts b/packages/ditto/controllers/api/translate.ts index 7a0f7731..65a95a26 100644 --- a/packages/ditto/controllers/api/translate.ts +++ b/packages/ditto/controllers/api/translate.ts @@ -1,11 +1,11 @@ import { cachedTranslationsSizeGauge } from '@ditto/metrics'; +import { MastodonTranslation } from '@ditto/mastoapi/types'; import { logi } from '@soapbox/logi'; import { LanguageCode } from 'iso-639-1'; import { z } from 'zod'; import { AppController } from '@/app.ts'; import { translationCache } from '@/caches/translationCache.ts'; -import { MastodonTranslation } from '@/entities/MastodonTranslation.ts'; import { getEvent } from '@/queries.ts'; import { localeSchema } from '@/schema.ts'; import { parseBody } from '@/utils/api.ts'; diff --git a/packages/ditto/controllers/api/trends.ts b/packages/ditto/controllers/api/trends.ts index a687c2cc..f7a09b5f 100644 --- a/packages/ditto/controllers/api/trends.ts +++ b/packages/ditto/controllers/api/trends.ts @@ -7,10 +7,12 @@ import { z } from 'zod'; import { AppController } from '@/app.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; import { generateDateRange, Time } from '@/utils/time.ts'; -import { PreviewCard, unfurlCardCached } from '@/utils/unfurl.ts'; +import { unfurlCardCached } from '@/utils/unfurl.ts'; import { errorJson } from '@/utils/log.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; +import type { MastodonPreviewCard } from '@ditto/mastoapi/types'; + interface TrendHistory { day: string; accounts: string; @@ -23,7 +25,7 @@ interface TrendingHashtag { history: TrendHistory[]; } -interface TrendingLink extends PreviewCard { +interface TrendingLink extends MastodonPreviewCard { history: TrendHistory[]; } diff --git a/packages/ditto/utils/note.ts b/packages/ditto/utils/note.ts index 45fcf94a..a17a0f15 100644 --- a/packages/ditto/utils/note.ts +++ b/packages/ditto/utils/note.ts @@ -4,10 +4,11 @@ import linkify from 'linkifyjs'; import { nip19, nip27 } from 'nostr-tools'; import { Conf } from '@/config.ts'; -import { MastodonMention } from '@/entities/MastodonMention.ts'; import { html } from '@/utils/html.ts'; import { getUrlMediaType, isPermittedMediaType } from '@/utils/media.ts'; +import { MastodonMention } from '@ditto/mastoapi/types'; + linkify.registerCustomProtocol('nostr', true); linkify.registerCustomProtocol('wss'); diff --git a/packages/ditto/utils/og-metadata.ts b/packages/ditto/utils/og-metadata.ts index c0e5756c..9dc7d125 100644 --- a/packages/ditto/utils/og-metadata.ts +++ b/packages/ditto/utils/og-metadata.ts @@ -1,10 +1,10 @@ import { nip19 } from 'nostr-tools'; import { match } from 'path-to-regexp'; -import { MastodonAccount } from '@/entities/MastodonAccount.ts'; -import { MastodonStatus } from '@/entities/MastodonStatus.ts'; import { InstanceMetadata } from '@/utils/instance.ts'; +import type { MastodonAccount, MastodonStatus } from '@ditto/mastoapi/types'; + export interface MetadataEntities { status?: MastodonStatus; account?: MastodonAccount; diff --git a/packages/ditto/utils/unfurl.ts b/packages/ditto/utils/unfurl.ts index e2d4f855..31905a04 100644 --- a/packages/ditto/utils/unfurl.ts +++ b/packages/ditto/utils/unfurl.ts @@ -6,10 +6,11 @@ import DOMPurify from 'isomorphic-dompurify'; import { unfurl } from 'unfurl.js'; import { Conf } from '@/config.ts'; -import { PreviewCard } from '@/entities/PreviewCard.ts'; import { errorJson } from '@/utils/log.ts'; -async function unfurlCard(url: string, signal: AbortSignal): Promise { +import type { MastodonPreviewCard } from '@ditto/mastoapi/types'; + +async function unfurlCard(url: string, signal: AbortSignal): Promise { try { const result = await unfurl(url, { fetch: (url) => @@ -55,10 +56,10 @@ async function unfurlCard(url: string, signal: AbortSignal): Promise>(Conf.caches.linkPreview); +const previewCardCache = new TTLCache>(Conf.caches.linkPreview); /** Unfurl card from cache if available, otherwise fetch it. */ -function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Promise { +export function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Promise { const cached = previewCardCache.get(url); if (cached !== undefined) { return cached; @@ -69,5 +70,3 @@ function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Prom return card; } } - -export { type PreviewCard, unfurlCardCached }; diff --git a/packages/ditto/views/mastodon/accounts.ts b/packages/ditto/views/mastodon/accounts.ts index d541e633..827b7921 100644 --- a/packages/ditto/views/mastodon/accounts.ts +++ b/packages/ditto/views/mastodon/accounts.ts @@ -2,7 +2,6 @@ import { NSchema as n } from '@nostrify/nostrify'; import { nip19, UnsignedEvent } from 'nostr-tools'; import { Conf } from '@/config.ts'; -import { MastodonAccount } from '@/entities/MastodonAccount.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { metadataSchema } from '@/schemas/nostr.ts'; import { getLnurl } from '@/utils/lnurl.ts'; @@ -11,6 +10,8 @@ import { getTagSet } from '@/utils/tags.ts'; import { nostrDate, nostrNow, parseNip05 } from '@/utils.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts'; +import type { MastodonAccount } from '@ditto/mastoapi/types'; + type ToAccountOpts = { withSource: true; settingsStore: Record | undefined; diff --git a/packages/ditto/views/mastodon/attachments.ts b/packages/ditto/views/mastodon/attachments.ts index 4e9401fd..b0d2e49c 100644 --- a/packages/ditto/views/mastodon/attachments.ts +++ b/packages/ditto/views/mastodon/attachments.ts @@ -1,4 +1,5 @@ -import { MastodonAttachment } from '@/entities/MastodonAttachment.ts'; +import { MastodonAttachment } from '@ditto/mastoapi/types'; + import { getUrlMediaType } from '@/utils/media.ts'; /** Render Mastodon media attachment. */ diff --git a/packages/ditto/views/mastodon/statuses.ts b/packages/ditto/views/mastodon/statuses.ts index 5957356e..55aa0808 100644 --- a/packages/ditto/views/mastodon/statuses.ts +++ b/packages/ditto/views/mastodon/statuses.ts @@ -2,9 +2,6 @@ import { NostrEvent, NStore } from '@nostrify/nostrify'; import { nip19 } from 'nostr-tools'; import { Conf } from '@/config.ts'; -import { MastodonAttachment } from '@/entities/MastodonAttachment.ts'; -import { MastodonMention } from '@/entities/MastodonMention.ts'; -import { MastodonStatus } from '@/entities/MastodonStatus.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { nostrDate } from '@/utils.ts'; import { getMediaLinks, parseNoteContent, stripimeta } from '@/utils/note.ts'; @@ -14,6 +11,8 @@ import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderAttachment } from '@/views/mastodon/attachments.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts'; +import { MastodonAttachment, MastodonMention, MastodonStatus } from '@ditto/mastoapi/types'; + interface RenderStatusOpts { viewerPubkey?: string; depth?: number; diff --git a/packages/mastoapi/deno.json b/packages/mastoapi/deno.json index b9626b3e..fc976655 100644 --- a/packages/mastoapi/deno.json +++ b/packages/mastoapi/deno.json @@ -5,6 +5,7 @@ "./middleware": "./middleware/mod.ts", "./pagination": "./pagination/mod.ts", "./router": "./router/mod.ts", - "./test": "./test.ts" + "./test": "./test.ts", + "./types": "./types/mod.ts" } } diff --git a/packages/ditto/entities/MastodonAccount.ts b/packages/mastoapi/types/MastodonAccount.ts similarity index 100% rename from packages/ditto/entities/MastodonAccount.ts rename to packages/mastoapi/types/MastodonAccount.ts diff --git a/packages/ditto/entities/MastodonAttachment.ts b/packages/mastoapi/types/MastodonAttachment.ts similarity index 100% rename from packages/ditto/entities/MastodonAttachment.ts rename to packages/mastoapi/types/MastodonAttachment.ts diff --git a/packages/ditto/entities/MastodonMention.ts b/packages/mastoapi/types/MastodonMention.ts similarity index 100% rename from packages/ditto/entities/MastodonMention.ts rename to packages/mastoapi/types/MastodonMention.ts diff --git a/packages/ditto/entities/PreviewCard.ts b/packages/mastoapi/types/MastodonPreviewCard.ts similarity index 89% rename from packages/ditto/entities/PreviewCard.ts rename to packages/mastoapi/types/MastodonPreviewCard.ts index 2decf926..9d625cca 100644 --- a/packages/ditto/entities/PreviewCard.ts +++ b/packages/mastoapi/types/MastodonPreviewCard.ts @@ -1,4 +1,4 @@ -export interface PreviewCard { +export interface MastodonPreviewCard { url: string; title: string; description: string; diff --git a/packages/ditto/entities/MastodonStatus.ts b/packages/mastoapi/types/MastodonStatus.ts similarity index 80% rename from packages/ditto/entities/MastodonStatus.ts rename to packages/mastoapi/types/MastodonStatus.ts index 3bc15f55..019e5a7b 100644 --- a/packages/ditto/entities/MastodonStatus.ts +++ b/packages/mastoapi/types/MastodonStatus.ts @@ -1,11 +1,11 @@ -import { MastodonAccount } from '@/entities/MastodonAccount.ts'; -import { MastodonAttachment } from '@/entities/MastodonAttachment.ts'; -import { PreviewCard } from '@/entities/PreviewCard.ts'; +import type { MastodonAccount } from './MastodonAccount.ts'; +import type { MastodonAttachment } from './MastodonAttachment.ts'; +import type { MastodonPreviewCard } from './MastodonPreviewCard.ts'; export interface MastodonStatus { id: string; account: MastodonAccount; - card: PreviewCard | null; + card: MastodonPreviewCard | null; content: string; created_at: string; in_reply_to_id: string | null; diff --git a/packages/ditto/entities/MastodonTranslation.ts b/packages/mastoapi/types/MastodonTranslation.ts similarity index 93% rename from packages/ditto/entities/MastodonTranslation.ts rename to packages/mastoapi/types/MastodonTranslation.ts index d59b9aad..fa8b99e8 100644 --- a/packages/ditto/entities/MastodonTranslation.ts +++ b/packages/mastoapi/types/MastodonTranslation.ts @@ -1,4 +1,4 @@ -import { LanguageCode } from 'iso-639-1'; +import type { LanguageCode } from 'iso-639-1'; /** https://docs.joinmastodon.org/entities/Translation/ */ export interface MastodonTranslation { diff --git a/packages/mastoapi/types/mod.ts b/packages/mastoapi/types/mod.ts new file mode 100644 index 00000000..48e92474 --- /dev/null +++ b/packages/mastoapi/types/mod.ts @@ -0,0 +1,6 @@ +export type { MastodonAccount } from './MastodonAccount.ts'; +export type { MastodonAttachment } from './MastodonAttachment.ts'; +export type { MastodonMention } from './MastodonMention.ts'; +export type { MastodonPreviewCard } from './MastodonPreviewCard.ts'; +export type { MastodonStatus } from './MastodonStatus.ts'; +export type { MastodonTranslation } from './MastodonTranslation.ts';