summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/web/UrlPreviewService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/server/web/UrlPreviewService.ts')
-rw-r--r--packages/backend/src/server/web/UrlPreviewService.ts67
1 files changed, 52 insertions, 15 deletions
diff --git a/packages/backend/src/server/web/UrlPreviewService.ts b/packages/backend/src/server/web/UrlPreviewService.ts
index c6a96e94cb..8f8f08a305 100644
--- a/packages/backend/src/server/web/UrlPreviewService.ts
+++ b/packages/backend/src/server/web/UrlPreviewService.ts
@@ -5,6 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { summaly } from '@misskey-dev/summaly';
+import { SummalyResult } from '@misskey-dev/summaly/built/summary.js';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { MetaService } from '@/core/MetaService.js';
@@ -14,6 +15,7 @@ import { query } from '@/misc/prelude/url.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import { ApiError } from '@/server/api/error.js';
+import { MiMeta } from '@/models/Meta.js';
import type { FastifyRequest, FastifyReply } from 'fastify';
@Injectable()
@@ -62,24 +64,25 @@ export class UrlPreviewService {
const meta = await this.metaService.fetch();
- this.logger.info(meta.summalyProxy
+ if (!meta.urlPreviewEnabled) {
+ reply.code(403);
+ return {
+ error: new ApiError({
+ message: 'URL preview is disabled',
+ code: 'URL_PREVIEW_DISABLED',
+ id: '58b36e13-d2f5-0323-b0c6-76aa9dabefb8',
+ }),
+ };
+ }
+
+ this.logger.info(meta.urlPreviewSummaryProxyUrl
? `(Proxy) Getting preview of ${url}@${lang} ...`
: `Getting preview of ${url}@${lang} ...`);
+
try {
- const summary = meta.summalyProxy ?
- await this.httpRequestService.getJson<ReturnType<typeof summaly>>(`${meta.summalyProxy}?${query({
- url: url,
- lang: lang ?? 'ja-JP',
- })}`)
- :
- await summaly(url, {
- followRedirects: false,
- lang: lang ?? 'ja-JP',
- agent: this.config.proxy ? {
- http: this.httpRequestService.httpAgent,
- https: this.httpRequestService.httpsAgent,
- } : undefined,
- });
+ const summary = meta.urlPreviewSummaryProxyUrl
+ ? await this.fetchSummaryFromProxy(url, meta, lang)
+ : await this.fetchSummary(url, meta, lang);
this.logger.succ(`Got preview of ${url}: ${summary.title}`);
@@ -100,6 +103,7 @@ export class UrlPreviewService {
return summary;
} catch (err) {
this.logger.warn(`Failed to get preview of ${url}: ${err}`);
+
reply.code(422);
reply.header('Cache-Control', 'max-age=86400, immutable');
return {
@@ -111,4 +115,37 @@ export class UrlPreviewService {
};
}
}
+
+ private fetchSummary(url: string, meta: MiMeta, lang?: string): Promise<SummalyResult> {
+ const agent = this.config.proxy
+ ? {
+ http: this.httpRequestService.httpAgent,
+ https: this.httpRequestService.httpsAgent,
+ }
+ : undefined;
+
+ return summaly(url, {
+ followRedirects: false,
+ lang: lang ?? 'ja-JP',
+ agent: agent,
+ userAgent: meta.urlPreviewUserAgent ?? undefined,
+ operationTimeout: meta.urlPreviewTimeout,
+ contentLengthLimit: meta.urlPreviewMaximumContentLength,
+ contentLengthRequired: meta.urlPreviewRequireContentLength,
+ });
+ }
+
+ private fetchSummaryFromProxy(url: string, meta: MiMeta, lang?: string): Promise<SummalyResult> {
+ const proxy = meta.urlPreviewSummaryProxyUrl!;
+ const queryStr = query({
+ url: url,
+ lang: lang ?? 'ja-JP',
+ userAgent: meta.urlPreviewUserAgent ?? undefined,
+ operationTimeout: meta.urlPreviewTimeout,
+ contentLengthLimit: meta.urlPreviewMaximumContentLength,
+ contentLengthRequired: meta.urlPreviewRequireContentLength,
+ });
+
+ return this.httpRequestService.getJson<SummalyResult>(`${proxy}?${queryStr}`);
+ }
}