Merge develop into feature/search-drive-via-alt-text

This commit is contained in:
KevinWh0 2024-07-16 23:02:15 +02:00
commit 5e65b6d01d
318 changed files with 1986 additions and 1489 deletions

View file

@ -112,6 +112,7 @@
"content-disposition": "0.5.4",
"date-fns": "2.30.0",
"deep-email-validator": "0.1.21",
"fast-xml-parser": "^4.4.0",
"fastify": "4.26.2",
"fastify-multer": "^2.0.3",
"fastify-raw-body": "4.3.0",

View file

@ -5,7 +5,7 @@
import { URL } from 'node:url';
import { Injectable } from '@nestjs/common';
import { query as urlQuery } from '@/misc/prelude/url.js';
import { XMLParser } from 'fast-xml-parser';
import { HttpRequestService } from '@/core/HttpRequestService.js';
import { bindThis } from '@/decorators.js';
@ -22,6 +22,8 @@ export type IWebFinger = {
const urlRegex = /^https?:\/\//;
const mRegex = /^([^@]+)@(.*)/;
const defaultProtocol = process.env.MISSKEY_WEBFINGER_USE_HTTP?.toLowerCase() === 'true' ? 'http' : 'https';
@Injectable()
export class WebfingerService {
constructor(
@ -31,25 +33,76 @@ export class WebfingerService {
@bindThis
public async webfinger(query: string): Promise<IWebFinger> {
const url = this.genUrl(query);
const hostMetaUrl = this.queryToHostMetaUrl(query);
const template = await this.fetchWebFingerTemplateFromHostMeta(hostMetaUrl) ?? this.queryToWebFingerTemplate(query);
const url = this.genUrl(query, template);
return await this.httpRequestService.getJson<IWebFinger>(url, 'application/jrd+json, application/json');
}
@bindThis
private genUrl(query: string): string {
private genUrl(query: string, template: string): string {
if (template.indexOf('{uri}') < 0) throw new Error(`Invalid webFingerUrl: ${template}`);
if (query.match(urlRegex)) {
return template.replace('{uri}', encodeURIComponent(query));
}
const m = query.match(mRegex);
if (m) {
return template.replace('{uri}', encodeURIComponent(`acct:${query}`));
}
throw new Error(`Invalid query (${query})`);
}
@bindThis
private queryToWebFingerTemplate(query: string): string {
if (query.match(urlRegex)) {
const u = new URL(query);
return `${u.protocol}//${u.hostname}/.well-known/webfinger?` + urlQuery({ resource: query });
return `${u.protocol}//${u.hostname}/.well-known/webfinger?resource={uri}`;
}
const m = query.match(mRegex);
if (m) {
const hostname = m[2];
const useHttp = process.env.MISSKEY_WEBFINGER_USE_HTTP && process.env.MISSKEY_WEBFINGER_USE_HTTP.toLowerCase() === 'true';
return `http${useHttp ? '' : 's'}://${hostname}/.well-known/webfinger?${urlQuery({ resource: `acct:${query}` })}`;
return `${defaultProtocol}//${hostname}/.well-known/webfinger?resource={uri}`;
}
throw new Error(`Invalid query (${query})`);
}
@bindThis
private queryToHostMetaUrl(query: string): string {
if (query.match(urlRegex)) {
const u = new URL(query);
return `${u.protocol}//${u.hostname}/.well-known/host-meta`;
}
const m = query.match(mRegex);
if (m) {
const hostname = m[2];
return `${defaultProtocol}://${hostname}/.well-known/host-meta`;
}
throw new Error(`Invalid query (${query})`);
}
@bindThis
private async fetchWebFingerTemplateFromHostMeta(url: string): Promise<string | null> {
try {
const res = await this.httpRequestService.getHtml(url, 'application/xrd+xml');
const options = {
ignoreAttributes: false,
isArray: (_name: string, jpath: string) => jpath === 'XRD.Link',
};
const parser = new XMLParser(options);
const hostMeta = parser.parse(res);
const template = (hostMeta['XRD']['Link'] as Array<any>).filter(p => p['@_rel'] === 'lrdd')[0]['@_template'];
return template.indexOf('{uri}') < 0 ? null : template;
} catch (err) {
console.error(`error while request host-meta for ${url}`);
return null;
}
}
}

View file

@ -6,9 +6,10 @@
import ms from 'ms';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UsersRepository, BlockingsRepository } from '@/models/_.js';
import type { UsersRepository, BlockingsRepository, MutingsRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { UserBlockingService } from '@/core/UserBlockingService.js';
import { UserMutingService } from '@/core/UserMutingService.js';
import { DI } from '@/di-symbols.js';
import { GetterService } from '@/server/api/GetterService.js';
import { ApiError } from '../../error.js';
@ -69,9 +70,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
@Inject(DI.blockingsRepository)
private blockingsRepository: BlockingsRepository,
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,
private userEntityService: UserEntityService,
private getterService: GetterService,
private userBlockingService: UserBlockingService,
private userMutingService: UserMutingService,
) {
super(meta, paramDef, async (ps, me) => {
const blocker = await this.usersRepository.findOneByOrFail({ id: me.id });
@ -99,7 +104,19 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw new ApiError(meta.errors.alreadyBlocking);
}
await this.userBlockingService.block(blocker, blockee);
await Promise.all([
this.userBlockingService.block(blocker, blockee),
this.mutingsRepository.exists({
where: {
muterId: blocker.id,
muteeId: blockee.id,
},
}).then(exists => {
if (!exists) {
this.userMutingService.mute(blocker, blockee, null);
}
}),
]);
return await this.userEntityService.pack(blockee.id, blocker, {
schema: 'UserDetailedNotMe',

View file

@ -6,9 +6,10 @@
import ms from 'ms';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { UsersRepository, BlockingsRepository } from '@/models/_.js';
import type { UsersRepository, BlockingsRepository, MutingsRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { UserBlockingService } from '@/core/UserBlockingService.js';
import { UserMutingService } from '@/core/UserMutingService.js';
import { DI } from '@/di-symbols.js';
import { GetterService } from '@/server/api/GetterService.js';
import { ApiError } from '../../error.js';
@ -69,9 +70,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
@Inject(DI.blockingsRepository)
private blockingsRepository: BlockingsRepository,
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,
private userEntityService: UserEntityService,
private getterService: GetterService,
private userBlockingService: UserBlockingService,
private userMutingService: UserMutingService,
) {
super(meta, paramDef, async (ps, me) => {
const blocker = await this.usersRepository.findOneByOrFail({ id: me.id });
@ -100,7 +105,17 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}
// Delete blocking
await this.userBlockingService.unblock(blocker, blockee);
await Promise.all([
this.userBlockingService.unblock(blocker, blockee),
this.mutingsRepository.findOneBy({
muterId: blocker.id,
muteeId: blockee.id,
}).then(exists => {
if (exists) {
this.userMutingService.unmute([exists]);
}
}),
]);
return await this.userEntityService.pack(blockee.id, blocker, {
schema: 'UserDetailedNotMe',

View file

@ -17,20 +17,33 @@ import { bindThis } from '@/decorators.js';
import { ApiError } from '@/server/api/error.js';
import { MiMeta } from '@/models/Meta.js';
import type { FastifyRequest, FastifyReply } from 'fastify';
import * as Redis from 'ioredis';
import { RedisKVCache } from '@/misc/cache.js';
@Injectable()
export class UrlPreviewService {
private logger: Logger;
private previewCache: RedisKVCache<SummalyResult>;
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.redis)
private redisClient: Redis.Redis,
private metaService: MetaService,
private httpRequestService: HttpRequestService,
private loggerService: LoggerService,
) {
this.logger = this.loggerService.getLogger('url-preview');
this.previewCache = new RedisKVCache<SummalyResult>(this.redisClient, 'summaly', {
lifetime: 1000 * 86400,
memoryCacheLifetime: 1000 * 10 * 60,
fetcher: (key: string) => { throw new Error('the UrlPreview cache should never fetch'); },
toRedisConverter: (value) => JSON.stringify(value),
fromRedisConverter: (value) => JSON.parse(value),
});
}
@bindThis
@ -75,9 +88,19 @@ export class UrlPreviewService {
};
}
const key = `${url}@${lang}`;
const cached = await this.previewCache.get(key);
if (cached !== undefined) {
this.logger.info(`Returning cache preview of ${key}`);
// Cache 7days
reply.header('Cache-Control', 'max-age=604800, immutable');
return cached;
}
this.logger.info(meta.urlPreviewSummaryProxyUrl
? `(Proxy) Getting preview of ${url}@${lang} ...`
: `Getting preview of ${url}@${lang} ...`);
? `(Proxy) Getting preview of ${key} ...`
: `Getting preview of ${key} ...`);
try {
const summary = meta.urlPreviewSummaryProxyUrl
@ -97,6 +120,8 @@ export class UrlPreviewService {
summary.icon = this.wrap(summary.icon);
summary.thumbnail = this.wrap(summary.thumbnail);
this.previewCache.set(key, summary);
// Cache 7days
reply.header('Cache-Control', 'max-age=604800, immutable');