( messageId: string, userServers: DiscordAPIServer[] | null, )
| 226 | >; |
| 227 | |
| 228 | export async function findMessageResultPage( |
| 229 | messageId: string, |
| 230 | userServers: DiscordAPIServer[] | null, |
| 231 | ) { |
| 232 | const targetMessage = await findMessageById(messageId); |
| 233 | if (!targetMessage) { |
| 234 | return null; |
| 235 | } |
| 236 | // Declare as const to make TypeScript not yell at us when used in arrow functions |
| 237 | const threadId = getThreadIdOfMessage(targetMessage); |
| 238 | // TODO: These should maybe be a different error code |
| 239 | const parentId = getParentChannelOfMessage(targetMessage); |
| 240 | |
| 241 | if (!parentId) { |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | const startTime = Date.now(); |
| 246 | |
| 247 | const [result, rootMessage, messages] = await Promise.all([ |
| 248 | db.query.dbChannels.findFirst({ |
| 249 | where: eq(dbChannels.id, threadId ?? parentId), |
| 250 | with: { |
| 251 | server: true, |
| 252 | parent: true, |
| 253 | }, |
| 254 | }), |
| 255 | threadId ? findMessageByIdWithDiscordAccount(threadId) : undefined, |
| 256 | db.query.dbMessages.findMany({ |
| 257 | where: and( |
| 258 | eq(dbMessages.channelId, threadId ?? parentId), |
| 259 | !threadId ? gte(dbMessages.id, targetMessage.id) : undefined, |
| 260 | ), |
| 261 | orderBy: asc(dbMessages.id), |
| 262 | limit: !threadId ? NUMBER_OF_CHANNEL_MESSAGES_TO_LOAD : undefined, |
| 263 | columns: { |
| 264 | id: true, |
| 265 | content: true, |
| 266 | questionId: true, |
| 267 | serverId: true, |
| 268 | authorId: true, |
| 269 | channelId: true, |
| 270 | parentChannelId: true, |
| 271 | childThreadId: true, |
| 272 | embeds: true, |
| 273 | }, |
| 274 | with: { |
| 275 | author: { |
| 276 | with: { |
| 277 | userServerSettings: { |
| 278 | where: eq(dbUserServerSettings.serverId, targetMessage.serverId), |
| 279 | }, |
| 280 | }, |
| 281 | }, |
| 282 | attachments: true, |
| 283 | reference: { |
| 284 | with: { |
| 285 | author: { |
no test coverage detected