fix: fix language property in the Mastodon API

This commit is contained in:
P. Reis 2024-10-07 16:05:22 -03:00
parent bbbce958d9
commit 4712cb1d80
3 changed files with 21 additions and 2 deletions

View file

@ -1,4 +1,5 @@
import { NostrEvent } from '@nostrify/nostrify';
import { LanguageCode } from 'iso-639-1';
/** Ditto internal stats for the event's author. */
export interface AuthorStats {
@ -43,4 +44,6 @@ export interface DittoEvent extends NostrEvent {
zap_sender?: DittoEvent | string;
zap_amount?: number;
zap_message?: string;
/** Language of the event (kind 1s are more accurate). */
language?: LanguageCode;
}

View file

@ -1,5 +1,6 @@
// deno-lint-ignore-file require-await
import { LanguageCode } from 'iso-639-1';
import { NPostgres, NPostgresSchema } from '@nostrify/db';
import { NIP50, NKinds, NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
import { Stickynotes } from '@soapbox/stickynotes';
@ -12,6 +13,7 @@ import { RelayError } from '@/RelayError.ts';
import { isNostrId, isURL } from '@/utils.ts';
import { abortError } from '@/utils/abort.ts';
import { purifyEvent } from '@/utils/purify.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
/** Function to decide whether or not to index a tag. */
type TagCondition = ({ event, count, value }: {
@ -175,7 +177,7 @@ class EventsDB extends NPostgres {
override async query(
filters: NostrFilter[],
opts: { signal?: AbortSignal; timeout?: number; limit?: number } = {},
): Promise<NostrEvent[]> {
): Promise<DittoEvent[]> {
filters = await this.expandFilters(filters);
for (const filter of filters) {
@ -199,6 +201,20 @@ class EventsDB extends NPostgres {
return super.query(filters, { ...opts, timeout: opts.timeout ?? this.opts.timeout });
}
/** Parse an event row from the database. */
protected override parseEventRow(row: Pick<DittoTables['nostr_events'], keyof NostrEvent | 'language'>): DittoEvent {
return {
id: row.id,
kind: row.kind,
pubkey: row.pubkey,
content: row.content,
created_at: Number(row.created_at),
tags: row.tags,
sig: row.sig,
language: (row.language || undefined) as LanguageCode,
};
}
/** Delete events based on filters from the database. */
override async remove(filters: NostrFilter[], opts: { signal?: AbortSignal; timeout?: number } = {}): Promise<void> {
this.console.debug('DELETE', JSON.stringify(filters));

View file

@ -113,7 +113,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
sensitive: !!cw,
spoiler_text: (cw ? cw[1] : subject?.[1]) || '',
visibility: 'public',
language: event.tags.find((tag) => tag[0] === 'l' && tag[2] === 'ISO-639-1')?.[1] || null,
language: event.language ?? null,
replies_count: event.event_stats?.replies_count ?? 0,
reblogs_count: event.event_stats?.reposts_count ?? 0,
favourites_count: event.event_stats?.reactions['+'] ?? 0,