2018-04-09 04:08:56 +09:00
|
|
|
import Resolver from '../../resolver';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { IRemoteUser } from '../../../../models/entities/user';
|
2018-04-17 16:05:50 +09:00
|
|
|
import { createNote, fetchNote } from '../../models/note';
|
2020-01-30 18:58:13 +09:00
|
|
|
import { getApId, IObject, ICreate } from '../../type';
|
2019-09-09 22:46:45 +09:00
|
|
|
import { getApLock } from '../../../../misc/app-lock';
|
2020-05-09 08:21:42 +09:00
|
|
|
import { extractDbHost } from '../../../../misc/convert-host';
|
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) {
|
|
|
|
|
if (e.statusCode >= 400 && e.statusCode < 500) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|