Skip to content

Commit

Permalink
Update Note
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Nov 24, 2022
1 parent 8fbb6be commit e05ec93
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/remote/activitypub/kernel/update/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { IRemoteUser } from '../../../../models/user';
import { IUpdate, isActor, getApType } from '../../type';
import { IUpdate, isActor, isPost, getApType } from '../../type';
import { apLogger } from '../../logger';
import { updateQuestion } from '../../models/question';
import Resolver from '../../resolver';
import { updatePerson } from '../../models/person';
import updateNote from './note';

/**
* Updateアクティビティを捌きます
Expand All @@ -28,6 +29,8 @@ export default async (actor: IRemoteUser, activity: IUpdate): Promise<string> =>
} else if (getApType(object) === 'Question') { // isQuestionだとNoteを含んでいる
await updateQuestion(object).catch(e => console.log(e));
return `ok: Question updated`;
} else if (isPost(object)) {
return await updateNote(actor, object);
} else {
return `skip: Unknown type: ${getApType(object)}`;
}
Expand Down
55 changes: 55 additions & 0 deletions src/remote/activitypub/kernel/update/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { IRemoteUser } from '../../../../models/user';
import deleteNode from '../../../../services/note/delete';
import { apLogger } from '../../logger';
import { getApLock } from '../../../../misc/app-lock';
import DbResolver from '../../db-resolver';
import { getApId, IPost } from '../../type';
import { extractApHost } from '../../../../misc/convert-host';
import { createNote } from '../../models/note';
import { StatusError } from '../../../../misc/fetch';

const logger = apLogger;

export default async function(actor: IRemoteUser, note: IPost): Promise<string> {
if (typeof note === 'object') {
if (actor.uri !== note.attributedTo) {
return `skip: actor.uri !== note.attributedTo`;
}

if (typeof note.id === 'string') {
if (extractApHost(note.id) !== extractApHost(actor.uri)) {
return `skip: host in actor.uri !== host in note.id`;
}
}
}

const uri = getApId(note);

logger.info(`Update the Note: ${uri}`);

const unlock = await getApLock(uri);

try {
const dbResolver = new DbResolver();
const old = await dbResolver.getNoteFromApId(uri);

if (!old) return 'skip: old note is not found';

if (!old.userId.equals(actor._id)) {
return '投稿をUpdateしようとしているユーザーは投稿の作成者ではありません';
}

await deleteNode(actor, old);

const n = await createNote(note);
return n ? 'ok' : 'skip';
} catch (e) {
if (e instanceof StatusError && e.isClientError) {
return `skip ${e.statusCode}`;
} else {
throw e;
}
} finally {
unlock();
}
}
3 changes: 2 additions & 1 deletion src/services/note/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export default async function(user: IUser, note: INote, quiet = false) {
renoteId: null,
poll: null,
geo: null,
cw: null
cw: null,
uri: null,
}
});

Expand Down

0 comments on commit e05ec93

Please sign in to comment.