summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2025-03-15 19:05:28 +0000
committerdakkar <dakkar@thenautilus.net>2025-03-15 19:17:36 +0000
commitd0a074ac899d4bf2f4613ebf34d3650fc80ab2fc (patch)
treef93aedf3533081f4d980c6ee4c2ef6e3f14404ca
parentmerge: Add external url warning to url previews (!944) (diff)
downloadsharkey-d0a074ac899d4bf2f4613ebf34d3650fc80ab2fc.tar.gz
sharkey-d0a074ac899d4bf2f4613ebf34d3650fc80ab2fc.tar.bz2
sharkey-d0a074ac899d4bf2f4613ebf34d3650fc80ab2fc.zip
fetch linked notes manually, unless we have them in DB - fixes 1006
-rw-r--r--locales/index.d.ts4
-rw-r--r--packages/backend/src/server/web/UrlPreviewService.ts14
-rw-r--r--packages/frontend/src/components/MkUrlPreview.vue29
-rw-r--r--sharkey-locales/en-US.yml1
4 files changed, 40 insertions, 8 deletions
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<SummalyResult>(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
<i class="ti ti-brand-x"></i> {{ i18n.ts.expandTweet }}
</MkButton>
</div>
+ <div v-if="showAsQuote && activityPub && !theNote && !fetchingTheNote" :class="$style.action">
+ <MkButton :small="true" inline @click="fetchNote()">
+ <i class="ti ti-note"></i> {{ i18n.ts.fetchLinkedNote }}
+ </MkButton>
+ </div>
<div v-if="!playerEnabled && player.url" :class="$style.action">
<MkButton :small="true" inline @click="playerEnabled = true">
<i class="ti ti-player-play"></i> {{ 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<Misskey.entities.Note | null>(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."