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. * 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; "mandatoryCWDescription": string;
/**
* Fetch linked note
*/
"fetchLinkedNote": string;
"_processErrors": { "_processErrors": {
/** /**
* Unable to process quote. This post may be missing context. * 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 { RedisKVCache } from '@/misc/cache.js';
import { UtilityService } from '@/core/UtilityService.js'; import { UtilityService } from '@/core/UtilityService.js';
import type { FastifyRequest, FastifyReply } from 'fastify'; import type { FastifyRequest, FastifyReply } from 'fastify';
import { ApDbResolverService } from '@/core/activitypub/ApDbResolverService.js';
@Injectable() @Injectable()
export class UrlPreviewService { export class UrlPreviewService {
@ -38,6 +39,7 @@ export class UrlPreviewService {
private httpRequestService: HttpRequestService, private httpRequestService: HttpRequestService,
private loggerService: LoggerService, private loggerService: LoggerService,
private utilityService: UtilityService, private utilityService: UtilityService,
private apDbResolverService: ApDbResolverService,
) { ) {
this.logger = this.loggerService.getLogger('url-preview'); this.logger = this.loggerService.getLogger('url-preview');
this.previewCache = new RedisKVCache<SummalyResult>(this.redisClient, 'summaly', { this.previewCache = new RedisKVCache<SummalyResult>(this.redisClient, 'summaly', {
@ -102,12 +104,16 @@ export class UrlPreviewService {
} }
const key = `${url}@${lang}`; 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) { if (cached !== undefined) {
this.logger.info(`Returning cache preview of ${key}`); this.logger.info(`Returning cache preview of ${key}`);
// Cache 7days // Cache 7days
reply.header('Cache-Control', 'max-age=604800, immutable'); reply.header('Cache-Control', 'max-age=604800, immutable');
if (cached.activityPub) {
cached.haveNoteLocally = !! await this.apDbResolverService.getNoteFromApId(cached.activityPub);
}
return cached; return cached;
} }
@ -116,7 +122,7 @@ export class UrlPreviewService {
: `Getting preview of ${key} ...`); : `Getting preview of ${key} ...`);
try { try {
const summary = this.meta.urlPreviewSummaryProxyUrl const summary: SummalyResult & { haveNoteLocally?: boolean } = this.meta.urlPreviewSummaryProxyUrl
? await this.fetchSummaryFromProxy(url, this.meta, lang) ? await this.fetchSummaryFromProxy(url, this.meta, lang)
: await this.fetchSummary(url, this.meta, lang); : await this.fetchSummary(url, this.meta, lang);
@ -135,6 +141,10 @@ export class UrlPreviewService {
this.previewCache.set(key, summary); this.previewCache.set(key, summary);
if (summary.activityPub) {
summary.haveNoteLocally = !! await this.apDbResolverService.getNoteFromApId(summary.activityPub);
}
// Cache 7days // Cache 7days
reply.header('Cache-Control', 'max-age=604800, immutable'); 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 }} <i class="ti ti-brand-x"></i> {{ i18n.ts.expandTweet }}
</MkButton> </MkButton>
</div> </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"> <div v-if="!playerEnabled && player.url" :class="$style.action">
<MkButton :small="true" inline @click="playerEnabled = true"> <MkButton :small="true" inline @click="playerEnabled = true">
<i class="ti ti-player-play"></i> {{ i18n.ts.enablePlayer }} <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 tweetHeight = ref(150);
const unknownUrl = ref(false); const unknownUrl = ref(false);
const theNote = ref<Misskey.entities.Note | null>(null); const theNote = ref<Misskey.entities.Note | null>(null);
const fetchingTheNote = ref(false);
onDeactivated(() => { onDeactivated(() => {
playerEnabled.value = false; playerEnabled.value = false;
}); });
watch(activityPub, async (uri) => { async function fetchNote() {
if (!props.showAsQuote) return; if (!props.showAsQuote) return;
if (!uri) return; if (!activityPub.value) return;
try { if (theNote.value) return;
const response = await misskeyApi('ap/show', { uri }); if (fetchingTheNote.value) return;
if (response.type !== 'Note') return;
const theNoteId = response['object'].id; fetchingTheNote.value = true;
if (theNoteId && props.skipNoteIds && props.skipNoteIds.includes(theNoteId)) { try {
hidePreview.value = true; const response = await misskeyApi('ap/show', { uri: activityPub.value });
return; if (response.type !== 'Note') return;
} const theNoteId = response['object'].id;
theNote.value = response['object']; if (theNoteId && props.skipNoteIds && props.skipNoteIds.includes(theNoteId)) {
} catch (err) { hidePreview.value = true;
if (_DEV_) { return;
console.error(`failed to extract note for preview of ${uri}`, err);
}
} }
}); 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); const requestUrl = new URL(props.url);
if (!['http:', 'https:'].includes(requestUrl.protocol)) throw new Error('invalid 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(); return res.json();
}) })
.then((info: SummalyResult | null) => { .then((info: SummalyResult & { haveNoteLocally?: boolean } | null) => {
if (!info || info.url == null) { if (!info || info.url == null) {
fetching.value = false; fetching.value = false;
unknownUrl.value = true; unknownUrl.value = true;
@ -217,6 +231,9 @@ window.fetch(`/url?url=${encodeURIComponent(requestUrl.href)}&lang=${versatileLa
player.value = info.player; player.value = info.player;
sensitive.value = info.sensitive ?? false; sensitive.value = info.sensitive ?? false;
activityPub.value = info.activityPub; activityPub.value = info.activityPub;
if (info.haveNoteLocally) {
fetchNote();
}
}); });
function adjustTweetHeight(message: MessageEvent) { function adjustTweetHeight(message: MessageEvent) {

View file

@ -510,6 +510,7 @@ id: "ID"
mandatoryCW: "Force content warning" 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." 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: _processErrors:
quoteUnavailable: "Unable to process quote. This post may be missing context." quoteUnavailable: "Unable to process quote. This post may be missing context."