11import prisma from '~/prisma/prisma'
2+ import { markdownToText } from '~/utils/markdownToText'
23import type {
34 SearchResultTopic ,
45 SearchResultGalgame ,
56 SearchResultUser ,
6- SearchResultReply
7+ SearchResultReplyTarget ,
8+ SearchResultReply ,
9+ SearchResultComment
710} from '~/types/api/search'
811
912export const searchTopic = async (
@@ -14,10 +17,10 @@ export const searchTopic = async (
1417 const data = await prisma . topic . findMany ( {
1518 where : {
1619 status : { not : 1 } ,
17- category : { in : keywords } ,
18- tag : { hasSome : keywords } ,
1920 AND : keywords . map ( ( kw ) => ( {
2021 OR : [
22+ { category : { in : keywords } } ,
23+ { tag : { hasSome : keywords } } ,
2124 { title : { contains : kw , mode : 'insensitive' } } ,
2225 { content : { contains : kw , mode : 'insensitive' } }
2326 ]
@@ -82,16 +85,10 @@ export const searchGalgame = async (
8285 orderBy : { created : 'desc' } ,
8386 where : {
8487 status : { not : 1 } ,
85- vndb_id : { in : keywords } ,
86- tag : {
87- some : {
88- tag : {
89- name : { in : keywords }
90- }
91- }
92- } ,
9388 AND : keywords . map ( ( kw ) => ( {
9489 OR : [
90+ { vndb_id : { in : keywords } } ,
91+ { tag : { some : { tag : { name : { in : keywords } } } } } ,
9592 { name_en_us : { contains : kw , mode : 'insensitive' } } ,
9693 { name_ja_jp : { contains : kw , mode : 'insensitive' } } ,
9794 { name_zh_cn : { contains : kw , mode : 'insensitive' } } ,
@@ -200,11 +197,22 @@ export const searchReply = async (
200197 avatar : true
201198 }
202199 } ,
203- target_user : {
200+ target : {
201+ orderBy : {
202+ target_reply : { floor : 'asc' }
203+ } ,
204204 select : {
205- id : true ,
206- name : true ,
207- avatar : true
205+ content : true ,
206+ target_reply : {
207+ select : {
208+ id : true ,
209+ floor : true ,
210+ content : true ,
211+ user : {
212+ select : { id : true , name : true , avatar : true }
213+ }
214+ }
215+ }
208216 }
209217 } ,
210218 topic : {
@@ -215,14 +223,33 @@ export const searchReply = async (
215223 }
216224 } )
217225
218- const replies : SearchResultReply [ ] = data . map ( ( reply ) => ( {
219- topicId : reply . topic_id ,
220- topicTitle : reply . topic . title ,
221- content : reply . content . slice ( 0 , 233 ) ,
222- user : reply . user ,
223- targetUser : reply . target_user ,
224- created : reply . created
225- } ) )
226+ const replies : SearchResultReply [ ] = data . map ( ( reply ) => {
227+ const targets : SearchResultReplyTarget [ ] = reply . target . map (
228+ ( targetRelation ) => {
229+ const targetReply = targetRelation . target_reply
230+ const content = markdownToText ( targetRelation . content )
231+ const contentPreviewText = markdownToText ( targetReply . content )
232+ return {
233+ id : targetReply . id ,
234+ user : targetReply . user ,
235+ content : content . slice ( 0 , 150 ) + ( content . length > 150 ? '...' : '' ) ,
236+ contentPreview :
237+ contentPreviewText . slice ( 0 , 150 ) +
238+ ( contentPreviewText . length > 150 ? '...' : '' )
239+ }
240+ }
241+ )
242+
243+ return {
244+ topicId : reply . topic_id ,
245+ topicTitle : reply . topic . title ,
246+ content : markdownToText ( reply . content ) . slice ( 0 , 233 ) ,
247+ user : reply . user ,
248+ floor : reply . floor ,
249+ targets,
250+ created : reply . created
251+ }
252+ } )
226253
227254 return replies
228255}
@@ -262,7 +289,7 @@ export const searchComment = async (
262289 }
263290 } )
264291
265- const comments : SearchResultReply [ ] = data . map ( ( comment ) => ( {
292+ const comments : SearchResultComment [ ] = data . map ( ( comment ) => ( {
266293 topicId : comment . topic_id ,
267294 topicTitle : comment . topic . title ,
268295 content : comment . content . slice ( 0 , 233 ) ,
0 commit comments