(serverId: string)
| 35 | import { zServerPublic } from './zodSchemas/serverSchemas'; |
| 36 | |
| 37 | export async function findQuestionsForSitemap(serverId: string) { |
| 38 | const res = await db.query.dbServers.findFirst({ |
| 39 | where: and(eq(dbServers.id, serverId)), |
| 40 | with: { |
| 41 | channels: { |
| 42 | where: and( |
| 43 | or( |
| 44 | eq(dbChannels.type, ChannelType.GuildAnnouncement), |
| 45 | eq(dbChannels.type, ChannelType.GuildText), |
| 46 | eq(dbChannels.type, ChannelType.GuildForum), |
| 47 | ), |
| 48 | sql`${dbChannels.bitfield} & ${channelBitfieldValues.indexingEnabled} > 0`, |
| 49 | ), |
| 50 | with: { |
| 51 | threads: { |
| 52 | orderBy: desc(dbChannels.id), |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | }, |
| 57 | }); |
| 58 | |
| 59 | if (!res) return null; |
| 60 | const questionIds = res.channels.flatMap((c) => c.threads.map((t) => t.id)); |
| 61 | |
| 62 | const questions = |
| 63 | questionIds.length > 0 |
| 64 | ? await db.query.dbMessages.findMany({ |
| 65 | where: and( |
| 66 | eq(dbMessages.serverId, serverId), |
| 67 | inArray(dbMessages.id, questionIds), |
| 68 | ), |
| 69 | columns: { |
| 70 | id: true, |
| 71 | authorId: true, |
| 72 | }, |
| 73 | with: { |
| 74 | author: { |
| 75 | with: { |
| 76 | userServerSettings: true, |
| 77 | }, |
| 78 | }, |
| 79 | }, |
| 80 | }) |
| 81 | : []; |
| 82 | |
| 83 | const questionLookup = new Map(questions.map((m) => [m.id, m])); |
| 84 | const areAllServerMessagesPublic = |
| 85 | addFlagsToServer(res).flags.considerAllMessagesPublic; |
| 86 | return { |
| 87 | questions: res.channels.flatMap((c) => |
| 88 | c.threads |
| 89 | .map((t) => { |
| 90 | const question = questionLookup.get(t.id); |
| 91 | // Drizzle doesn't mark relations as optional, so we have to do this |
| 92 | if ( |
| 93 | !question || |
| 94 | !question.author || |
no test coverage detected