Merge branch 'mastoapi-entities' into 'main'

Move ditto/interfaces to @ditto/mastoapi/types

See merge request soapbox-pub/ditto!693
This commit is contained in:
Alex Gleason 2025-02-27 00:03:45 +00:00
commit ccf6d2074e
18 changed files with 37 additions and 26 deletions

View file

@ -1,8 +1,8 @@
import { MastodonTranslation } from '@ditto/mastoapi/types';
import { LanguageCode } from 'iso-639-1'; import { LanguageCode } from 'iso-639-1';
import { LRUCache } from 'lru-cache'; import { LRUCache } from 'lru-cache';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { MastodonTranslation } from '@/entities/MastodonTranslation.ts';
/** Translations LRU cache. */ /** Translations LRU cache. */
export const translationCache = new LRUCache<`${LanguageCode}-${string}`, MastodonTranslation>({ export const translationCache = new LRUCache<`${LanguageCode}-${string}`, MastodonTranslation>({

View file

@ -19,7 +19,8 @@ import { hydrateEvents } from '@/storages/hydrate.ts';
import { bech32ToPubkey } from '@/utils.ts'; import { bech32ToPubkey } from '@/utils.ts';
import { addTag, deleteTag, findReplyTag, getTagSet } from '@/utils/tags.ts'; import { addTag, deleteTag, findReplyTag, getTagSet } from '@/utils/tags.ts';
import { getPubkeysBySearch } from '@/utils/search.ts'; import { getPubkeysBySearch } from '@/utils/search.ts';
import { MastodonAccount } from '@/entities/MastodonAccount.ts';
import type { MastodonAccount } from '@ditto/mastoapi/types';
const createAccountSchema = z.object({ const createAccountSchema = z.object({
username: z.string().min(1).max(30).regex(/^[a-z0-9_]+$/i), username: z.string().min(1).max(30).regex(/^[a-z0-9_]+$/i),

View file

@ -1,11 +1,11 @@
import { cachedTranslationsSizeGauge } from '@ditto/metrics'; import { cachedTranslationsSizeGauge } from '@ditto/metrics';
import { MastodonTranslation } from '@ditto/mastoapi/types';
import { logi } from '@soapbox/logi'; import { logi } from '@soapbox/logi';
import { LanguageCode } from 'iso-639-1'; import { LanguageCode } from 'iso-639-1';
import { z } from 'zod'; import { z } from 'zod';
import { AppController } from '@/app.ts'; import { AppController } from '@/app.ts';
import { translationCache } from '@/caches/translationCache.ts'; import { translationCache } from '@/caches/translationCache.ts';
import { MastodonTranslation } from '@/entities/MastodonTranslation.ts';
import { getEvent } from '@/queries.ts'; import { getEvent } from '@/queries.ts';
import { localeSchema } from '@/schema.ts'; import { localeSchema } from '@/schema.ts';
import { parseBody } from '@/utils/api.ts'; import { parseBody } from '@/utils/api.ts';

View file

@ -7,10 +7,12 @@ import { z } from 'zod';
import { AppController } from '@/app.ts'; import { AppController } from '@/app.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
import { generateDateRange, Time } from '@/utils/time.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 { errorJson } from '@/utils/log.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts';
import type { MastodonPreviewCard } from '@ditto/mastoapi/types';
interface TrendHistory { interface TrendHistory {
day: string; day: string;
accounts: string; accounts: string;
@ -23,7 +25,7 @@ interface TrendingHashtag {
history: TrendHistory[]; history: TrendHistory[];
} }
interface TrendingLink extends PreviewCard { interface TrendingLink extends MastodonPreviewCard {
history: TrendHistory[]; history: TrendHistory[];
} }

View file

@ -4,10 +4,11 @@ import linkify from 'linkifyjs';
import { nip19, nip27 } from 'nostr-tools'; import { nip19, nip27 } from 'nostr-tools';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { MastodonMention } from '@/entities/MastodonMention.ts';
import { html } from '@/utils/html.ts'; import { html } from '@/utils/html.ts';
import { getUrlMediaType, isPermittedMediaType } from '@/utils/media.ts'; import { getUrlMediaType, isPermittedMediaType } from '@/utils/media.ts';
import { MastodonMention } from '@ditto/mastoapi/types';
linkify.registerCustomProtocol('nostr', true); linkify.registerCustomProtocol('nostr', true);
linkify.registerCustomProtocol('wss'); linkify.registerCustomProtocol('wss');

View file

@ -1,10 +1,10 @@
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { match } from 'path-to-regexp'; 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 { InstanceMetadata } from '@/utils/instance.ts';
import type { MastodonAccount, MastodonStatus } from '@ditto/mastoapi/types';
export interface MetadataEntities { export interface MetadataEntities {
status?: MastodonStatus; status?: MastodonStatus;
account?: MastodonAccount; account?: MastodonAccount;

View file

@ -6,10 +6,11 @@ import DOMPurify from 'isomorphic-dompurify';
import { unfurl } from 'unfurl.js'; import { unfurl } from 'unfurl.js';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { PreviewCard } from '@/entities/PreviewCard.ts';
import { errorJson } from '@/utils/log.ts'; import { errorJson } from '@/utils/log.ts';
async function unfurlCard(url: string, signal: AbortSignal): Promise<PreviewCard | null> { import type { MastodonPreviewCard } from '@ditto/mastoapi/types';
async function unfurlCard(url: string, signal: AbortSignal): Promise<MastodonPreviewCard | null> {
try { try {
const result = await unfurl(url, { const result = await unfurl(url, {
fetch: (url) => fetch: (url) =>
@ -55,10 +56,10 @@ async function unfurlCard(url: string, signal: AbortSignal): Promise<PreviewCard
} }
/** TTL cache for preview cards. */ /** TTL cache for preview cards. */
const previewCardCache = new TTLCache<string, Promise<PreviewCard | null>>(Conf.caches.linkPreview); const previewCardCache = new TTLCache<string, Promise<MastodonPreviewCard | null>>(Conf.caches.linkPreview);
/** Unfurl card from cache if available, otherwise fetch it. */ /** Unfurl card from cache if available, otherwise fetch it. */
function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Promise<PreviewCard | null> { export function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Promise<MastodonPreviewCard | null> {
const cached = previewCardCache.get(url); const cached = previewCardCache.get(url);
if (cached !== undefined) { if (cached !== undefined) {
return cached; return cached;
@ -69,5 +70,3 @@ function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Prom
return card; return card;
} }
} }
export { type PreviewCard, unfurlCardCached };

View file

@ -2,7 +2,6 @@ import { NSchema as n } from '@nostrify/nostrify';
import { nip19, UnsignedEvent } from 'nostr-tools'; import { nip19, UnsignedEvent } from 'nostr-tools';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { MastodonAccount } from '@/entities/MastodonAccount.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { metadataSchema } from '@/schemas/nostr.ts'; import { metadataSchema } from '@/schemas/nostr.ts';
import { getLnurl } from '@/utils/lnurl.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 { nostrDate, nostrNow, parseNip05 } from '@/utils.ts';
import { renderEmojis } from '@/views/mastodon/emojis.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts';
import type { MastodonAccount } from '@ditto/mastoapi/types';
type ToAccountOpts = { type ToAccountOpts = {
withSource: true; withSource: true;
settingsStore: Record<string, unknown> | undefined; settingsStore: Record<string, unknown> | undefined;

View file

@ -1,4 +1,5 @@
import { MastodonAttachment } from '@/entities/MastodonAttachment.ts'; import { MastodonAttachment } from '@ditto/mastoapi/types';
import { getUrlMediaType } from '@/utils/media.ts'; import { getUrlMediaType } from '@/utils/media.ts';
/** Render Mastodon media attachment. */ /** Render Mastodon media attachment. */

View file

@ -2,9 +2,6 @@ import { NostrEvent, NStore } from '@nostrify/nostrify';
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { Conf } from '@/config.ts'; 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 { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { nostrDate } from '@/utils.ts'; import { nostrDate } from '@/utils.ts';
import { getMediaLinks, parseNoteContent, stripimeta } from '@/utils/note.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 { renderAttachment } from '@/views/mastodon/attachments.ts';
import { renderEmojis } from '@/views/mastodon/emojis.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts';
import { MastodonAttachment, MastodonMention, MastodonStatus } from '@ditto/mastoapi/types';
interface RenderStatusOpts { interface RenderStatusOpts {
viewerPubkey?: string; viewerPubkey?: string;
depth?: number; depth?: number;

View file

@ -5,6 +5,7 @@
"./middleware": "./middleware/mod.ts", "./middleware": "./middleware/mod.ts",
"./pagination": "./pagination/mod.ts", "./pagination": "./pagination/mod.ts",
"./router": "./router/mod.ts", "./router": "./router/mod.ts",
"./test": "./test.ts" "./test": "./test.ts",
"./types": "./types/mod.ts"
} }
} }

View file

@ -1,4 +1,4 @@
export interface PreviewCard { export interface MastodonPreviewCard {
url: string; url: string;
title: string; title: string;
description: string; description: string;

View file

@ -1,11 +1,11 @@
import { MastodonAccount } from '@/entities/MastodonAccount.ts'; import type { MastodonAccount } from './MastodonAccount.ts';
import { MastodonAttachment } from '@/entities/MastodonAttachment.ts'; import type { MastodonAttachment } from './MastodonAttachment.ts';
import { PreviewCard } from '@/entities/PreviewCard.ts'; import type { MastodonPreviewCard } from './MastodonPreviewCard.ts';
export interface MastodonStatus { export interface MastodonStatus {
id: string; id: string;
account: MastodonAccount; account: MastodonAccount;
card: PreviewCard | null; card: MastodonPreviewCard | null;
content: string; content: string;
created_at: string; created_at: string;
in_reply_to_id: string | null; in_reply_to_id: string | null;

View file

@ -1,4 +1,4 @@
import { LanguageCode } from 'iso-639-1'; import type { LanguageCode } from 'iso-639-1';
/** https://docs.joinmastodon.org/entities/Translation/ */ /** https://docs.joinmastodon.org/entities/Translation/ */
export interface MastodonTranslation { export interface MastodonTranslation {

View file

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