merge: fetch linked notes manually, unless we have them in DB - fixes 1006 (!945)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/945

Closes #1006

Approved-by: Marie <github@yuugi.dev>
Approved-by: Hazelnoot <acomputerdog@gmail.com>
This commit is contained in:
Hazelnoot 2025-03-15 20:46:28 +00:00
commit e3d949ced6
4 changed files with 52 additions and 20 deletions

4
locales/index.d.ts vendored
View file

@ -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.

View file

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

View file

@ -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,29 +155,38 @@ 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) => {
if (!props.showAsQuote) return;
if (!uri) return;
try {
const response = await misskeyApi('ap/show', { uri });
if (response.type !== 'Note') return;
const theNoteId = response['object'].id;
if (theNoteId && props.skipNoteIds && props.skipNoteIds.includes(theNoteId)) {
hidePreview.value = true;
return;
}
theNote.value = response['object'];
} catch (err) {
if (_DEV_) {
console.error(`failed to extract note for preview of ${uri}`, err);
}
async function fetchNote() {
if (!props.showAsQuote) return;
if (!activityPub.value) return;
if (theNote.value) return;
if (fetchingTheNote.value) return;
fetchingTheNote.value = true;
try {
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)) {
hidePreview.value = true;
return;
}
});
theNote.value = response['object'];
fetchingTheNote.value = false;
} catch (err) {
if (_DEV_) {
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) {

View file

@ -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."