(opts: {
offset: number;
limit?: number;
})
| 12 | import { findManyServersById } from './server'; |
| 13 | import { JSONParse, JSONStringify } from './utils/json-big'; |
| 14 | export async function listPublicThreads(opts: { |
| 15 | offset: number; |
| 16 | limit?: number; |
| 17 | }) { |
| 18 | const { limit = 50000 } = opts; |
| 19 | |
| 20 | let start = Date.now(); |
| 21 | const threads = await dbReplica.query.dbChannels.findMany({ |
| 22 | where: eq(dbChannels.type, ChannelType.PublicThread), |
| 23 | offset: limit * opts.offset, |
| 24 | limit: limit, |
| 25 | }); |
| 26 | let end = Date.now(); |
| 27 | console.log(`findAllThreadsForSitemap took ${end - start}ms`); |
| 28 | |
| 29 | const parents = await findManyChannelsById( |
| 30 | threads.filter((t) => t.parentId !== null).map((t) => t.parentId!), |
| 31 | ); |
| 32 | const parentLookup = new Map(parents.map((p) => [p.id, p])); |
| 33 | const filterThreads = threads.filter( |
| 34 | (t) => parentLookup.get(t.parentId ?? '')?.flags.indexingEnabled, |
| 35 | ); |
| 36 | |
| 37 | const questionIds = filterThreads.map((c) => c.id); |
| 38 | |
| 39 | start = Date.now(); |
| 40 | const questions = |
| 41 | questionIds.length > 0 |
| 42 | ? await findManyMessagesWithAuthors(questionIds, { |
| 43 | excludePrivateMessages: true, |
| 44 | }) |
| 45 | : []; |
| 46 | const questionLookup = new Map(questions.map((q) => [q.id, q])); |
| 47 | |
| 48 | const servers = await findManyServersById( |
| 49 | filterThreads.map((c) => c.serverId), |
| 50 | ); |
| 51 | const serverLookup = new Map(servers.map((s) => [s.id, s])); |
| 52 | end = Date.now(); |
| 53 | console.log(`findAllThreadsForSitemap messages took ${end - start}ms`); |
| 54 | |
| 55 | return { |
| 56 | threads: filterThreads.filter( |
| 57 | (t) => |
| 58 | questionLookup.has(t.id) && |
| 59 | !serverLookup.get(t.serverId)?.customDomain && |
| 60 | !serverLookup.get(t.serverId)?.kickedTime, |
| 61 | ), |
| 62 | hasMore: threads.length === limit, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | // todo: deduplicate |
| 67 | type Snowflake = string; |
no test coverage detected