forked from FoundKeyGang/FoundKey
activitypub: handle incoming Update Note activities
Changelog: Added
This commit is contained in:
parent
5c3e7c132a
commit
42b555e5e4
2 changed files with 54 additions and 1 deletions
|
@ -1,9 +1,10 @@
|
||||||
import { IRemoteUser } from '@/models/entities/user.js';
|
import { IRemoteUser } from '@/models/entities/user.js';
|
||||||
import { getApId, getApType, IUpdate, isActor } from '@/remote/activitypub/type.js';
|
import { getApId, getOneApId, getApType, IUpdate, isActor, isPost } from '@/remote/activitypub/type.js';
|
||||||
import { apLogger } from '@/remote/activitypub/logger.js';
|
import { apLogger } from '@/remote/activitypub/logger.js';
|
||||||
import { updateQuestion } from '@/remote/activitypub/models/question.js';
|
import { updateQuestion } from '@/remote/activitypub/models/question.js';
|
||||||
import { Resolver } from '@/remote/activitypub/resolver.js';
|
import { Resolver } from '@/remote/activitypub/resolver.js';
|
||||||
import { updatePerson } from '@/remote/activitypub/models/person.js';
|
import { updatePerson } from '@/remote/activitypub/models/person.js';
|
||||||
|
import { update as updateNote } from '@/remote/activitypub/kernel/update/note.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updateアクティビティを捌きます
|
* Updateアクティビティを捌きます
|
||||||
|
@ -30,6 +31,8 @@ export default async (actor: IRemoteUser, activity: IUpdate, resolver: Resolver)
|
||||||
} else if (getApType(object) === 'Question') {
|
} else if (getApType(object) === 'Question') {
|
||||||
await updateQuestion(object, resolver).catch(e => console.log(e));
|
await updateQuestion(object, resolver).catch(e => console.log(e));
|
||||||
return 'ok: Question updated';
|
return 'ok: Question updated';
|
||||||
|
} else if (isPost(object)) {
|
||||||
|
return await updateNote(actor, object, resolver);
|
||||||
} else {
|
} else {
|
||||||
return `skip: Unknown type: ${getApType(object)}`;
|
return `skip: Unknown type: ${getApType(object)}`;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
import { IRemoteUser } from '@/models/entities/user.js';
|
||||||
|
import { getApId } from '@/remote/activitypub/type.js';
|
||||||
|
import { Resolver } from '@/remote/activitypub/resolver.js';
|
||||||
|
import { Notes } from '@/models/index.js';
|
||||||
|
import createNote from '@/remote/activitypub/kernel/create/note.js';
|
||||||
|
import { getApLock } from '@/misc/app-lock.js';
|
||||||
|
import { updateNote } from '@/remote/activitypub/models/note.js';
|
||||||
|
|
||||||
|
export async function update(actor: IRemoteUser, note: IObject, resolver: Resolver): Promise<string> {
|
||||||
|
// check whether note exists
|
||||||
|
const uri = getApId(note);
|
||||||
|
const exists = await Notes.findOneBy({ uri });
|
||||||
|
|
||||||
|
if (exists == null) {
|
||||||
|
// does not yet exist, handle as if this was a create activity
|
||||||
|
// and since this is not a direct creation, handle it silently
|
||||||
|
createNote(resolver, actor, note, true);
|
||||||
|
|
||||||
|
const unlock = await getApLock(uri);
|
||||||
|
try {
|
||||||
|
// if creating was successful...
|
||||||
|
const existsNow = await Notes.findOneByOrFail({ uri });
|
||||||
|
// set the updatedAt timestamp since the note was changed
|
||||||
|
await Notes.update(existsNow.id, { updatedAt: new Date() });
|
||||||
|
return 'ok: unknown note created and marked as updated';
|
||||||
|
} catch (e) {
|
||||||
|
return `skip: updated note unknown and creating rejected: ${e.message}`;
|
||||||
|
} finally {
|
||||||
|
unlock();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// check that actor is authorized to update this note
|
||||||
|
if (actor.id !== exists.userId) {
|
||||||
|
return 'skip: actor not authorized to update Note';
|
||||||
|
}
|
||||||
|
// this does not redo the checks from the Create Note kernel
|
||||||
|
// since if the note made it into the database, we assume
|
||||||
|
// those checks must have been passed before.
|
||||||
|
|
||||||
|
const unlock = await getApLock(uri);
|
||||||
|
try {
|
||||||
|
await updateNote(note, actor, resolver);
|
||||||
|
return 'ok: note updated';
|
||||||
|
} catch (e) {
|
||||||
|
return `skip: update note rejected: ${e.message}`;
|
||||||
|
} finally {
|
||||||
|
unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue