@@ -17,6 +17,7 @@ import { DebounceLoader } from '@/misc/loader.js';
1717import { IdService } from '@/core/IdService.js' ;
1818import { shouldHideNoteByTime } from '@/misc/should-hide-note-by-time.js' ;
1919import { ReactionsBufferingService } from '@/core/ReactionsBufferingService.js' ;
20+ import { CacheService } from '@/core/CacheService.js' ;
2021import type { OnModuleInit } from '@nestjs/common' ;
2122import type { CustomEmojiService } from '../CustomEmojiService.js' ;
2223import type { ReactionService } from '../ReactionService.js' ;
@@ -66,6 +67,7 @@ export class NoteEntityService implements OnModuleInit {
6667 private reactionService : ReactionService ;
6768 private reactionsBufferingService : ReactionsBufferingService ;
6869 private idService : IdService ;
70+ private cacheService : CacheService ;
6971 private noteLoader = new DebounceLoader ( this . findNoteOrFail ) ;
7072
7173 constructor (
@@ -101,6 +103,7 @@ export class NoteEntityService implements OnModuleInit {
101103 //private reactionService: ReactionService,
102104 //private reactionsBufferingService: ReactionsBufferingService,
103105 //private idService: IdService,
106+ //private cacheService: CacheService,
104107 ) {
105108 }
106109
@@ -111,6 +114,7 @@ export class NoteEntityService implements OnModuleInit {
111114 this . reactionService = this . moduleRef . get ( 'ReactionService' ) ;
112115 this . reactionsBufferingService = this . moduleRef . get ( 'ReactionsBufferingService' ) ;
113116 this . idService = this . moduleRef . get ( 'IdService' ) ;
117+ this . cacheService = this . moduleRef . get ( 'CacheService' ) ;
114118 }
115119
116120 @bindThis
@@ -125,75 +129,65 @@ export class NoteEntityService implements OnModuleInit {
125129 }
126130
127131 @bindThis
128- private async hideNote ( packedNote : Packed < 'Note' > , meId : MiUser [ 'id' ] | null ) : Promise < void > {
129- if ( meId === packedNote . userId ) return ;
130-
132+ public async shouldHideNote ( packedNote : Packed < 'Note' > , meId : MiUser [ 'id' ] | null ) : Promise < boolean > {
133+ if ( meId === packedNote . userId ) return false ;
131134 // TODO: isVisibleForMe を使うようにしても良さそう(型違うけど)
132- let hide = false ;
133135
134136 if ( packedNote . user . requireSigninToViewContents && meId == null ) {
135- hide = true ;
137+ return true ;
136138 }
137139
138- if ( ! hide ) {
139- const hiddenBefore = packedNote . user . makeNotesHiddenBefore ;
140- if ( shouldHideNoteByTime ( hiddenBefore , packedNote . createdAt ) ) {
141- hide = true ;
142- }
140+ const hiddenBefore = packedNote . user . makeNotesHiddenBefore ;
141+ if ( shouldHideNoteByTime ( hiddenBefore , packedNote . createdAt ) ) {
142+ return true ;
143143 }
144144
145145 // visibility が specified かつ自分が指定されていなかったら非表示
146- if ( ! hide ) {
147- if ( packedNote . visibility === 'specified' ) {
148- if ( meId == null ) {
149- hide = true ;
150- } else {
151- // 指定されているかどうか
152- const specified = packedNote . visibleUserIds ! . some ( id => meId === id ) ;
146+ if ( packedNote . visibility === 'specified' ) {
147+ if ( meId == null ) {
148+ return true ;
149+ } else {
150+ // 指定されているかどうか
151+ const specified = packedNote . visibleUserIds ! . some ( id => meId === id ) ;
153152
154- if ( ! specified ) {
155- hide = true ;
156- }
153+ if ( ! specified ) {
154+ return true ;
157155 }
158156 }
159157 }
160158
161159 // visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
162- if ( ! hide ) {
163- if ( packedNote . visibility === 'followers' ) {
164- if ( meId == null ) {
165- hide = true ;
166- } else if ( packedNote . reply && ( meId === packedNote . reply . userId ) ) {
167- // 自分の投稿に対するリプライ
168- hide = false ;
169- } else if ( packedNote . mentions && packedNote . mentions . some ( id => meId === id ) ) {
170- // 自分へのメンション
171- hide = false ;
172- } else {
173- // フォロワーかどうか
174- // TODO: 当関数呼び出しごとにクエリが走るのは重そうだからなんとかする
175- const isFollowing = await this . followingsRepository . exists ( {
176- where : {
177- followeeId : packedNote . userId ,
178- followerId : meId ,
179- } ,
180- } ) ;
181-
182- hide = ! isFollowing ;
160+ if ( packedNote . visibility === 'followers' ) {
161+ if ( meId == null ) {
162+ return true ;
163+ } else if ( packedNote . reply && ( meId === packedNote . reply . userId ) ) {
164+ // 自分の投稿に対するリプライ
165+ return false ;
166+ } else if ( packedNote . mentions && packedNote . mentions . some ( id => meId === id ) ) {
167+ // 自分へのメンション
168+ return false ;
169+ } else {
170+ // フォロワーかどうか
171+ const followings = await this . cacheService . userFollowingsCache . fetch ( meId ) ;
172+ if ( ! Object . hasOwn ( followings , packedNote . userId ) ) {
173+ return true ;
183174 }
184175 }
185176 }
186177
187- if ( hide ) {
188- packedNote . visibleUserIds = undefined ;
189- packedNote . fileIds = [ ] ;
190- packedNote . files = [ ] ;
191- packedNote . text = null ;
192- packedNote . poll = undefined ;
193- packedNote . cw = null ;
194- packedNote . isHidden = true ;
195- // TODO: hiddenReason みたいなのを提供しても良さそう
196- }
178+ return false ;
179+ }
180+
181+ @bindThis
182+ public hideNote ( packedNote : Packed < 'Note' > ) : void {
183+ packedNote . visibleUserIds = undefined ;
184+ packedNote . fileIds = [ ] ;
185+ packedNote . files = [ ] ;
186+ packedNote . text = null ;
187+ packedNote . poll = undefined ;
188+ packedNote . cw = null ;
189+ packedNote . isHidden = true ;
190+ // TODO: hiddenReason みたいなのを提供しても良さそう
197191 }
198192
199193 @bindThis
@@ -278,7 +272,7 @@ export class NoteEntityService implements OnModuleInit {
278272
279273 @bindThis
280274 public async isVisibleForMe ( note : MiNote , meId : MiUser [ 'id' ] | null ) : Promise < boolean > {
281- // This code must always be synchronized with the checks in generateVisibilityQuery.
275+ // This code must always be synchronized with the checks in QueryService. generateVisibilityQuery.
282276 // visibility が specified かつ自分が指定されていなかったら非表示
283277 if ( note . visibility === 'specified' ) {
284278 if ( meId == null ) {
@@ -468,8 +462,8 @@ export class NoteEntityService implements OnModuleInit {
468462
469463 this . treatVisibility ( packed ) ;
470464
471- if ( ! opts . skipHide ) {
472- await this . hideNote ( packed , meId ) ;
465+ if ( ! opts . skipHide && await this . shouldHideNote ( packed , meId ) ) {
466+ this . hideNote ( packed ) ;
473467 }
474468
475469 return packed ;
0 commit comments