diff --git a/locales/index.d.ts b/locales/index.d.ts index f125a9fa53..998d5da5d0 100644 --- a/locales/index.d.ts +++ b/locales/index.d.ts @@ -12221,6 +12221,10 @@ export interface Locale extends ILocale { * Applies a content warning to all posts created by this user. If the post already has a CW, then this is appended to the end. */ "mandatoryCWDescription": string; + /** + * Fetch linked note + */ + "fetchLinkedNote": string; "_processErrors": { /** * Unable to process quote. This post may be missing context. diff --git a/packages/backend/src/server/web/UrlPreviewService.ts b/packages/backend/src/server/web/UrlPreviewService.ts index 19dac1dfb8..cf130a21ec 100644 --- a/packages/backend/src/server/web/UrlPreviewService.ts +++ b/packages/backend/src/server/web/UrlPreviewService.ts @@ -19,6 +19,7 @@ import { MiMeta } from '@/models/Meta.js'; import { RedisKVCache } from '@/misc/cache.js'; import { UtilityService } from '@/core/UtilityService.js'; import type { FastifyRequest, FastifyReply } from 'fastify'; +import { ApDbResolverService } from '@/core/activitypub/ApDbResolverService.js'; @Injectable() export class UrlPreviewService { @@ -38,6 +39,7 @@ export class UrlPreviewService { private httpRequestService: HttpRequestService, private loggerService: LoggerService, private utilityService: UtilityService, + private apDbResolverService: ApDbResolverService, ) { this.logger = this.loggerService.getLogger('url-preview'); this.previewCache = new RedisKVCache(this.redisClient, 'summaly', { @@ -102,12 +104,16 @@ export class UrlPreviewService { } const key = `${url}@${lang}`; - const cached = await this.previewCache.get(key); + const cached = await this.previewCache.get(key) as SummalyResult & { haveNoteLocally?: boolean }; if (cached !== undefined) { this.logger.info(`Returning cache preview of ${key}`); // Cache 7days reply.header('Cache-Control', 'max-age=604800, immutable'); + if (cached.activityPub) { + cached.haveNoteLocally = !! await this.apDbResolverService.getNoteFromApId(cached.activityPub); + } + return cached; } @@ -116,7 +122,7 @@ export class UrlPreviewService { : `Getting preview of ${key} ...`); try { - const summary = this.meta.urlPreviewSummaryProxyUrl + const summary: SummalyResult & { haveNoteLocally?: boolean } = this.meta.urlPreviewSummaryProxyUrl ? await this.fetchSummaryFromProxy(url, this.meta, lang) : await this.fetchSummary(url, this.meta, lang); @@ -135,6 +141,10 @@ export class UrlPreviewService { this.previewCache.set(key, summary); + if (summary.activityPub) { + summary.haveNoteLocally = !! await this.apDbResolverService.getNoteFromApId(summary.activityPub); + } + // Cache 7days reply.header('Cache-Control', 'max-age=604800, immutable'); diff --git a/packages/frontend/src/components/MkUrlPreview.vue b/packages/frontend/src/components/MkUrlPreview.vue index 7fcdc2e5d1..42062e6f48 100644 --- a/packages/frontend/src/components/MkUrlPreview.vue +++ b/packages/frontend/src/components/MkUrlPreview.vue @@ -71,6 +71,11 @@ SPDX-License-Identifier: AGPL-3.0-only {{ i18n.ts.expandTweet }} +
+ + {{ i18n.ts.fetchLinkedNote }} + +
{{ i18n.ts.enablePlayer }} @@ -150,16 +155,21 @@ const embedId = `embed${Math.random().toString().replace(/\D/, '')}`; const tweetHeight = ref(150); const unknownUrl = ref(false); const theNote = ref(null); +const fetchingTheNote = ref(false); onDeactivated(() => { playerEnabled.value = false; }); -watch(activityPub, async (uri) => { +async function fetchNote() { if (!props.showAsQuote) return; - if (!uri) return; + if (!activityPub.value) return; + if (theNote.value) return; + if (fetchingTheNote.value) return; + + fetchingTheNote.value = true; try { - const response = await misskeyApi('ap/show', { uri }); + const response = await misskeyApi('ap/show', { uri: activityPub.value }); if (response.type !== 'Note') return; const theNoteId = response['object'].id; if (theNoteId && props.skipNoteIds && props.skipNoteIds.includes(theNoteId)) { @@ -167,12 +177,16 @@ watch(activityPub, async (uri) => { return; } theNote.value = response['object']; + fetchingTheNote.value = false; } catch (err) { if (_DEV_) { - console.error(`failed to extract note for preview of ${uri}`, err); + console.error(`failed to extract note for preview of ${activityPub.value}`, err); } + activityPub.value = null; + fetchingTheNote.value = false; + theNote.value = null; } -}); + } const requestUrl = new URL(props.url); if (!['http:', 'https:'].includes(requestUrl.protocol)) throw new Error('invalid url'); @@ -199,7 +213,7 @@ window.fetch(`/url?url=${encodeURIComponent(requestUrl.href)}&lang=${versatileLa return res.json(); }) - .then((info: SummalyResult | null) => { + .then((info: SummalyResult & { haveNoteLocally?: boolean } | null) => { if (!info || info.url == null) { fetching.value = false; unknownUrl.value = true; @@ -217,6 +231,9 @@ window.fetch(`/url?url=${encodeURIComponent(requestUrl.href)}&lang=${versatileLa player.value = info.player; sensitive.value = info.sensitive ?? false; activityPub.value = info.activityPub; + if (info.haveNoteLocally) { + fetchNote(); + } }); function adjustTweetHeight(message: MessageEvent) { diff --git a/sharkey-locales/en-US.yml b/sharkey-locales/en-US.yml index 7be3022f24..e25020de8e 100644 --- a/sharkey-locales/en-US.yml +++ b/sharkey-locales/en-US.yml @@ -510,6 +510,7 @@ id: "ID" mandatoryCW: "Force content warning" mandatoryCWDescription: "Applies a content warning to all posts created by this user. If the post already has a CW, then this is appended to the end." +fetchLinkedNote: "Fetch linked note" _processErrors: quoteUnavailable: "Unable to process quote. This post may be missing context."