allow Update(Note) and Update(Poll) to implicitly create missing notes

This commit is contained in:
Hazelnoot 2024-11-14 14:54:46 -05:00
parent 2bbccde2ce
commit 9d3321fca4
2 changed files with 23 additions and 4 deletions

View File

@ -388,7 +388,7 @@ export class ApInboxService {
}
@bindThis
private async create(actor: MiRemoteUser, activity: ICreate, resolver?: Resolver): Promise<string | void> {
private async create(actor: MiRemoteUser, activity: ICreate | IUpdate, resolver?: Resolver): Promise<string | void> {
const uri = getApId(activity);
this.logger.info(`Create: ${uri}`);
@ -423,14 +423,14 @@ export class ApInboxService {
});
if (isPost(object)) {
await this.createNote(resolver, actor, object, false, activity);
await this.createNote(resolver, actor, object, false);
} else {
return `Unknown type: ${getApType(object)}`;
}
}
@bindThis
private async createNote(resolver: Resolver, actor: MiRemoteUser, note: IObject, silent = false, activity?: ICreate): Promise<string> {
private async createNote(resolver: Resolver, actor: MiRemoteUser, note: IObject, silent = false): Promise<string> {
const uri = getApId(note);
if (typeof note === 'object') {
@ -789,7 +789,7 @@ export class ApInboxService {
}
@bindThis
private async update(actor: MiRemoteUser, activity: IUpdate, resolver?: Resolver): Promise<string> {
private async update(actor: MiRemoteUser, activity: IUpdate, resolver?: Resolver): Promise<string | void> {
if (actor.uri !== activity.actor) {
return 'skip: invalid actor';
}
@ -808,9 +808,19 @@ export class ApInboxService {
await this.apPersonService.updatePerson(actor.uri, resolver, object);
return 'ok: Person updated';
} else if (getApType(object) === 'Question') {
// If we get an Update(Question) for a note that doesn't exist, then create it instead
if (!await this.apNoteService.hasNote(object)) {
return await this.create(actor, activity, resolver);
}
await this.apQuestionService.updateQuestion(object, actor, resolver).catch(err => console.error(err));
return 'ok: Question updated';
} else if (isPost(object)) {
// If we get an Update(Note) for a note that doesn't exist, then create it instead
if (!await this.apNoteService.hasNote(object)) {
return await this.create(actor, activity, resolver);
}
await this.apNoteService.updateNote(object, actor, resolver).catch(err => console.error(err));
return 'ok: Note updated';
} else {

View File

@ -142,6 +142,15 @@ export class ApNoteService {
return await this.apDbResolverService.getNoteFromApId(object);
}
/**
* Returns true if the provided object / ID exists in the local database.
*/
@bindThis
public async hasNote(object: string | IObject | [string | IObject]): Promise<boolean> {
const uri = getApId(object);
return await this.notesRepository.existsBy({ uri });
}
/**
* Noteを作成します
*/