| 4 | import { Logger } from '@linen/types'; |
| 5 | |
| 6 | export async function getThreads( |
| 7 | channels: Record<string, ChannelType>, |
| 8 | logger: Logger |
| 9 | ) { |
| 10 | const sitemapPremium: Record<string, UrlType[]> = {}; |
| 11 | const sitemapFree: UrlType[] = []; |
| 12 | |
| 13 | let incrementId = 0; |
| 14 | logger.time('query-threads'); |
| 15 | do { |
| 16 | const threads = await prisma.threads.findMany({ |
| 17 | select: { |
| 18 | messages: { |
| 19 | select: { |
| 20 | sentAt: true, |
| 21 | body: true, |
| 22 | author: { select: { isBot: true } }, |
| 23 | reactions: { select: { count: true } }, |
| 24 | }, |
| 25 | orderBy: { sentAt: 'asc' }, |
| 26 | }, |
| 27 | incrementId: true, |
| 28 | slug: true, |
| 29 | channelId: true, |
| 30 | sentAt: true, |
| 31 | viewCount: true, |
| 32 | messageCount: true, |
| 33 | firstManagerReplyAt: true, |
| 34 | }, |
| 35 | where: { incrementId: { gt: incrementId }, hidden: false }, |
| 36 | orderBy: { incrementId: 'asc' }, |
| 37 | take: batchSize, |
| 38 | }); |
| 39 | |
| 40 | const searchKeywords = await prisma.$queryRaw< |
| 41 | { incrementId: number; searchKeywords: number }[] |
| 42 | >` |
| 43 | select t."incrementId", |
| 44 | sum( length(m.textsearchable_index_col) ) as "searchKeywords" |
| 45 | from threads t |
| 46 | left join messages m on t.id = m."threadId" |
| 47 | where t."incrementId" in (${Prisma.join(threads.map((t) => t.incrementId))}) |
| 48 | group by t."incrementId" |
| 49 | `; |
| 50 | |
| 51 | const discardedThreads = threads.map((thread) => { |
| 52 | const account = channels[thread.channelId]?.account; |
| 53 | |
| 54 | if ( |
| 55 | channels[thread.channelId] && // skip, channel not in the list |
| 56 | thread.messages.length && // skip, thread without messages |
| 57 | !!thread.messages[0].author && // skip messages from unknown user (missing userId) |
| 58 | !thread.messages[0].author.isBot // skip, messages from bot or missing user |
| 59 | ) { |
| 60 | const body = thread.messages |
| 61 | .map((m) => m.body) |
| 62 | .join(' ') |
| 63 | .replace(/\s+/g, ' '); // clean up dup space |