2021-08-19 21:55:45 +09:00
|
|
|
import Resolver from '../../resolver';
|
|
|
|
|
import { IRemoteUser } from '@/models/entities/user';
|
|
|
|
|
import { createNote, fetchNote } from '../../models/note';
|
|
|
|
|
import { getApId, IObject, ICreate } from '../../type';
|
|
|
|
|
import { getApLock } from '@/misc/app-lock';
|
|
|
|
|
import { extractDbHost } from '@/misc/convert-host';
|
2021-10-16 17:16:24 +09:00
|
|
|
import { StatusError } from '@/misc/fetch';
|
2018-04-09 04:08:56 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 投稿作成アクティビティを捌きます
|
|
|
|
|
*/
|
2020-05-09 08:21:42 +09:00
|
|
|
export default async function(resolver: Resolver, actor: IRemoteUser, note: IObject, silent = false, activity?: ICreate): Promise<string> {
|
2019-09-09 22:46:45 +09:00
|
|
|
const uri = getApId(note);
|
|
|
|
|
|
2020-05-09 08:21:42 +09:00
|
|
|
if (typeof note === 'object') {
|
|
|
|
|
if (actor.uri !== note.attributedTo) {
|
|
|
|
|
return `skip: actor.uri !== note.attributedTo`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof note.id === 'string') {
|
|
|
|
|
if (extractDbHost(actor.uri) !== extractDbHost(note.id)) {
|
|
|
|
|
return `skip: host in actor.uri !== note.id`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 22:46:45 +09:00
|
|
|
const unlock = await getApLock(uri);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const exist = await fetchNote(note);
|
2020-05-09 08:21:42 +09:00
|
|
|
if (exist) return 'skip: note exists';
|
|
|
|
|
|
|
|
|
|
await createNote(note, resolver, silent);
|
|
|
|
|
return 'ok';
|
|
|
|
|
} catch (e) {
|
2021-10-16 17:16:24 +09:00
|
|
|
if (e instanceof StatusError && e.isClientError) {
|
2020-05-09 08:21:42 +09:00
|
|
|
return `skip ${e.statusCode}`;
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
2019-09-09 22:46:45 +09:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
unlock();
|
2018-04-09 04:08:56 +09:00
|
|
|
}
|
|
|
|
|
}
|