backend: fix ApInboxService

This commit is contained in:
fly_mc 2024-11-18 23:27:31 +08:00
parent fc2f68f1d6
commit 9263c485c4

View file

@ -5,6 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { In } from 'typeorm';
import * as Bull from 'bullmq';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { UserFollowingService } from '@/core/UserFollowingService.js';
@ -38,6 +39,7 @@ import { ApPersonService } from './models/ApPersonService.js';
import { ApQuestionService } from './models/ApQuestionService.js';
import type { Resolver } from './ApResolverService.js';
import type { IAccept, IAdd, IAnnounce, IBlock, ICreate, IDelete, IFlag, IFollow, ILike, IObject, IReject, IRemove, IUndo, IUpdate, IMove, IPost } from './type.js';
import { fromTuple } from '@/misc/from-tuple.js';
@Injectable()
export class ApInboxService {
@ -84,6 +86,7 @@ export class ApInboxService {
private apQuestionService: ApQuestionService,
private queueService: QueueService,
private globalEventService: GlobalEventService,
private federatedInstanceService: FederatedInstanceService,
) {
this.logger = this.apLoggerService.logger;
}
@ -251,7 +254,8 @@ export class ApInboxService {
}
if (activity.target === actor.featured) {
const note = await this.apNoteService.resolveNote(activity.object);
const object = fromTuple(activity.object);
const note = await this.apNoteService.resolveNote(object);
if (note == null) return 'note not found';
await this.notePiningService.addPinned(actor, note.id);
return;
@ -268,11 +272,12 @@ export class ApInboxService {
const resolver = this.apResolverService.createResolver();
if (!activity.object) return 'skip: activity has no object property';
const targetUri = getApId(activity.object);
const activityObject = fromTuple(activity.object);
if (!activityObject) return 'skip: activity has no object property';
const targetUri = getApId(activityObject);
if (targetUri.startsWith('bear:')) return 'skip: bearcaps url not supported.';
const target = await resolver.resolve(activity.object).catch(e => {
const target = await resolver.resolve(activityObject).catch(e => {
this.logger.error(`Resolution failed: ${e}`);
return e;
});
@ -367,29 +372,30 @@ export class ApInboxService {
this.logger.info(`Create: ${uri}`);
if (!activity.object) return 'skip: activity has no object property';
const targetUri = getApId(activity.object);
const activityObject = fromTuple(activity.object);
if (!activityObject) return 'skip: activity has no object property';
const targetUri = getApId(activityObject);
if (targetUri.startsWith('bear:')) return 'skip: bearcaps url not supported.';
// copy audiences between activity <=> object.
if (typeof activity.object === 'object') {
const to = unique(concat([toArray(activity.to), toArray(activity.object.to)]));
const cc = unique(concat([toArray(activity.cc), toArray(activity.object.cc)]));
if (typeof activityObject === 'object') {
const to = unique(concat([toArray(activity.to), toArray(activityObject.to)]));
const cc = unique(concat([toArray(activity.cc), toArray(activityObject.cc)]));
activity.to = to;
activity.cc = cc;
activity.object.to = to;
activity.object.cc = cc;
activityObject.to = to;
activityObject.cc = cc;
}
// If there is no attributedTo, use Activity actor.
if (typeof activity.object === 'object' && !activity.object.attributedTo) {
activity.object.attributedTo = activity.actor;
if (typeof activityObject === 'object' && !activityObject.attributedTo) {
activityObject.attributedTo = activity.actor;
}
const resolver = this.apResolverService.createResolver();
const object = await resolver.resolve(activity.object).catch(e => {
const object = await resolver.resolve(activityObject).catch(e => {
this.logger.error(`Resolution failed: ${e}`);
throw e;
});
@ -445,15 +451,15 @@ export class ApInboxService {
// 削除対象objectのtype
let formerType: string | undefined;
if (typeof activity.object === 'string') {
const activityObject = fromTuple(activity.object);
if (typeof activityObject === 'string') {
// typeが不明だけど、どうせ消えてるのでremote resolveしない
formerType = undefined;
} else {
const object = activity.object;
if (isTombstone(object)) {
formerType = toSingle(object.formerType);
if (isTombstone(activityObject)) {
formerType = toSingle(activityObject.formerType);
} else {
formerType = toSingle(object.type);
formerType = toSingle(activityObject.type);
}
}
@ -607,7 +613,8 @@ export class ApInboxService {
}
if (activity.target === actor.featured) {
const note = await this.apNoteService.resolveNote(activity.object);
const activityObject = fromTuple(activity.object);
const note = await this.apNoteService.resolveNote(activityObject);
if (note == null) return 'note not found';
await this.notePiningService.removePinned(actor, note.id);
return;
@ -770,9 +777,9 @@ export class ApInboxService {
} else if (getApType(object) === 'Question') {
await this.apQuestionService.updateQuestion(object, resolver).catch(err => console.error(err));
return 'ok: Question updated';
} else if (isPost(object)) {
await this.apNoteService.updateNote(object, resolver);
return 'ok: Post updated';
} else if (getApType(object) === 'Note') {
await this.apNoteService.updateNote(object, resolver).catch(err => console.error(err));
return 'ok: Note updated';
} else {
return `skip: Unknown type: ${getApType(object)}`;
}