From 6ac92ac4b86a2e9aeac55b7e1259a9dedcb7e379 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 10 Sep 2018 02:43:16 +0900 Subject: [PATCH] Fix #2321 --- src/models/note.ts | 127 +++++++++++++++++++----------------- src/services/note/create.ts | 9 ++- 2 files changed, 75 insertions(+), 61 deletions(-) diff --git a/src/models/note.ts b/src/models/note.ts index 181ebecf2..624bdbdfe 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -162,6 +162,66 @@ export async function deleteNote(note: string | mongo.ObjectID | INote) { console.log(`Note: deleted ${n._id}`); } +export const hideNote = async (packedNote: any, meId: mongo.ObjectID) => { + let hide = false; + + // visibility が private かつ投稿者のIDが自分のIDではなかったら非表示 + if (packedNote.visibility == 'private' && (meId == null || !meId.equals(packedNote.userId))) { + hide = true; + } + + // visibility が specified かつ自分が指定されていなかったら非表示 + if (packedNote.visibility == 'specified') { + if (meId == null) { + hide = true; + } else if (meId.equals(packedNote.userId)) { + hide = false; + } else { + // 指定されているかどうか + const specified = packedNote.visibleUserIds.some((id: mongo.ObjectID) => id.equals(meId)); + + if (specified) { + hide = false; + } else { + hide = true; + } + } + } + + // visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示 + if (packedNote.visibility == 'followers') { + if (meId == null) { + hide = true; + } else if (meId.equals(packedNote.userId)) { + hide = false; + } else { + // フォロワーかどうか + const following = await Following.findOne({ + followeeId: packedNote.userId, + followerId: meId + }); + + if (following == null) { + hide = true; + } else { + hide = false; + } + } + } + + if (hide) { + packedNote.fileIds = []; + packedNote.files = []; + packedNote.text = null; + packedNote.poll = null; + packedNote.cw = null; + packedNote.tags = []; + packedNote.tagsLower = []; + packedNote.geo = null; + packedNote.isHidden = true; + } +}; + /** * Pack a note for API response * @@ -174,11 +234,13 @@ export const pack = async ( note: string | mongo.ObjectID | INote, me?: string | mongo.ObjectID | IUser, options?: { - detail: boolean + detail?: boolean; + skipHide?: boolean; } ) => { const opts = Object.assign({ - detail: true + detail: true, + skipHide: false }, options); // Me @@ -207,52 +269,6 @@ export const pack = async ( if (!_note) throw `invalid note arg ${note}`; - let hide = false; - - // visibility が private かつ投稿者のIDが自分のIDではなかったら非表示 - if (_note.visibility == 'private' && (meId == null || !meId.equals(_note.userId))) { - hide = true; - } - - // visibility が specified かつ自分が指定されていなかったら非表示 - if (_note.visibility == 'specified') { - if (meId == null) { - hide = true; - } else if (meId.equals(_note.userId)) { - hide = false; - } else { - // 指定されているかどうか - const specified = _note.visibleUserIds.some((id: mongo.ObjectID) => id.equals(meId)); - - if (specified) { - hide = false; - } else { - hide = true; - } - } - } - - // visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示 - if (_note.visibility == 'followers') { - if (meId == null) { - hide = true; - } else if (meId.equals(_note.userId)) { - hide = false; - } else { - // フォロワーかどうか - const following = await Following.findOne({ - followeeId: _note.userId, - followerId: meId - }); - - if (following == null) { - hide = true; - } else { - hide = false; - } - } - } - const id = _note._id; // Rename _id to id @@ -274,7 +290,7 @@ export const pack = async ( } // Populate files - _note.files = hide ? [] : Promise.all(_note.fileIds.map((fileId: mongo.ObjectID) => + _note.files = Promise.all(_note.fileIds.map((fileId: mongo.ObjectID) => packFile(fileId) )); @@ -304,7 +320,7 @@ export const pack = async ( } // Poll - if (meId && _note.poll && !hide) { + if (meId && _note.poll) { _note.poll = (async poll => { const vote = await PollVote .findOne({ @@ -349,15 +365,8 @@ export const pack = async ( _note.text = _note.text.replace(/な/g, 'にゃ').replace(/ナ/g, 'ニャ').replace(/ナ/g, 'ニャ'); } - if (hide) { - _note.fileIds = []; - _note.text = null; - _note.poll = null; - _note.cw = null; - _note.tags = []; - _note.tagsLower = []; - _note.geo = null; - _note.isHidden = true; + if (!opts.skipHide) { + await hideNote(_note, meId); } return _note; diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 7062bc481..ede3a0101 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -447,6 +447,11 @@ async function publishToUserLists(note: INote, noteObj: any) { } async function publishToFollowers(note: INote, noteObj: any, user: IUser, noteActivity: any) { + const detailPackedNote = await pack(note, null, { + detail: true, + skipHide: true + }); + const followers = await Following.find({ followeeId: note.userId }); @@ -465,10 +470,10 @@ async function publishToFollowers(note: INote, noteObj: any, user: IUser, noteAc } // Publish event to followers stream - publishUserStream(following.followerId, 'note', noteObj); + publishUserStream(following.followerId, 'note', detailPackedNote); if (isRemoteUser(user) || note.visibility != 'public') { - publishHybridTimelineStream(following.followerId, noteObj); + publishHybridTimelineStream(following.followerId, detailPackedNote); } } else { // フォロワーがリモートユーザーかつ投稿者がローカルユーザーなら投稿を配信