From 10e341102040cc741a816b3dd731a5a4e3e59602 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 15 Apr 2018 17:53:25 +0900 Subject: [PATCH] nanka iroiro --- src/queue/index.ts | 4 ++-- src/services/following/create.ts | 4 ++-- src/services/following/delete.ts | 2 +- src/services/note/create.ts | 6 ++--- src/services/note/reaction/create.ts | 35 ++++++++++++++-------------- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/queue/index.ts b/src/queue/index.ts index 4aa1dc032..94efb3634 100644 --- a/src/queue/index.ts +++ b/src/queue/index.ts @@ -19,12 +19,12 @@ export function createHttp(data) { } export function deliver(user, content, to) { - return createHttp({ + createHttp({ type: 'deliver', user, content, to - }); + }).save(); } export default function() { diff --git a/src/services/following/create.ts b/src/services/following/create.ts index 3289e3129..375b02891 100644 --- a/src/services/following/create.ts +++ b/src/services/following/create.ts @@ -58,11 +58,11 @@ export default async function(follower: IUser, followee: IUser, activity?) { if (isLocalUser(follower) && isRemoteUser(followee)) { const content = pack(renderFollow(follower, followee)); - deliver(follower, content, followee.inbox).save(); + deliver(follower, content, followee.inbox); } if (isRemoteUser(follower) && isLocalUser(followee)) { const content = pack(renderAccept(activity)); - deliver(followee, content, follower.inbox).save(); + deliver(followee, content, follower.inbox); } } diff --git a/src/services/following/delete.ts b/src/services/following/delete.ts index 8b6c56816..c0c99fbed 100644 --- a/src/services/following/delete.ts +++ b/src/services/following/delete.ts @@ -57,6 +57,6 @@ export default async function(follower: IUser, followee: IUser, activity?) { if (isLocalUser(follower) && isRemoteUser(followee)) { const content = pack(renderUndo(renderFollow(follower, followee))); - deliver(follower, content, followee.inbox).save(); + deliver(follower, content, followee.inbox); } } diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 603851adb..781337daf 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -136,12 +136,12 @@ export default async (user: IUser, data: { // 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送 if (data.reply && isLocalUser(user) && isRemoteUser(data.reply._user)) { - deliver(user, await render(), data.reply._user.inbox).save(); + deliver(user, await render(), data.reply._user.inbox); } // 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送 if (data.renote && isLocalUser(user) && isRemoteUser(data.renote._user)) { - deliver(user, await render(), data.renote._user.inbox).save(); + deliver(user, await render(), data.renote._user.inbox); } Promise.all(followers.map(async follower => { @@ -153,7 +153,7 @@ export default async (user: IUser, data: { } else { // フォロワーがリモートユーザーかつ投稿者がローカルユーザーなら投稿を配信 if (isLocalUser(user)) { - deliver(user, await render(), follower.inbox).save(); + deliver(user, await render(), follower.inbox); } } })); diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts index 69a14248d..dd3d4be8b 100644 --- a/src/services/note/reaction/create.ts +++ b/src/services/note/reaction/create.ts @@ -9,6 +9,7 @@ import watch from '../watch'; import renderLike from '../../../remote/activitypub/renderer/like'; import { deliver } from '../../../queue'; import pack from '../../../remote/activitypub/renderer'; +import { MongoError } from 'mongodb'; export default async (user: IUser, note: INote, reaction: string) => new Promise(async (res, rej) => { // Myself @@ -16,23 +17,23 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise return rej('cannot react to my note'); } - // if already reacted - const exist = await NoteReaction.findOne({ - noteId: note._id, - userId: user._id - }); - - if (exist !== null) { - return rej('already reacted'); - } - // Create reaction - await NoteReaction.insert({ - createdAt: new Date(), - noteId: note._id, - userId: user._id, - reaction - }); + try { + await NoteReaction.insert({ + createdAt: new Date(), + noteId: note._id, + userId: user._id, + reaction + }); + } catch (e) { + // duplicate key error + if (e instanceof MongoError && e.code === 11000) { + return rej('already reacted'); + } + + console.error(e); + return rej('something happened'); + } res(); @@ -86,7 +87,7 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise // リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送 if (isLocalUser(user) && isRemoteUser(note._user)) { const content = pack(renderLike(user, note)); - deliver(user, content, note._user.inbox).save(); + deliver(user, content, note._user.inbox); } //#endregion });