(opts: {
idOrVanityUrl: string;
limit?: number;
selectedChannel: string | undefined;
page: number;
})
| 116 | } |
| 117 | |
| 118 | export async function findServerWithCommunityPageData(opts: { |
| 119 | idOrVanityUrl: string; |
| 120 | limit?: number; |
| 121 | selectedChannel: string | undefined; |
| 122 | page: number; |
| 123 | }) { |
| 124 | const { idOrVanityUrl, limit = NUMBER_OF_THREADS_TO_LOAD } = opts; |
| 125 | let serverId = idOrVanityUrl; |
| 126 | try { |
| 127 | BigInt(idOrVanityUrl); |
| 128 | } catch (e) { |
| 129 | const found = await db.query.dbServers.findFirst({ |
| 130 | where: eq(dbServers.vanityUrl, idOrVanityUrl), |
| 131 | }); |
| 132 | if (!found) return null; |
| 133 | serverId = found.id; |
| 134 | } |
| 135 | |
| 136 | const offset = (opts.page > 0 ? opts.page - 1 : opts.page) * limit; |
| 137 | // eslint-disable-next-line prefer-const |
| 138 | let [found, questions] = await Promise.all([ |
| 139 | db.query.dbServers.findFirst({ |
| 140 | where: eq(dbServers.id, serverId), |
| 141 | with: { |
| 142 | channels: { |
| 143 | where: and( |
| 144 | or( |
| 145 | eq(dbChannels.type, ChannelType.GuildAnnouncement), |
| 146 | eq(dbChannels.type, ChannelType.GuildText), |
| 147 | eq(dbChannels.type, ChannelType.GuildForum), |
| 148 | ), |
| 149 | sql`${dbChannels.bitfield} & ${channelBitfieldValues.indexingEnabled} > 0`, |
| 150 | ), |
| 151 | }, |
| 152 | }, |
| 153 | }), |
| 154 | opts.selectedChannel |
| 155 | ? db.query.dbChannels.findMany({ |
| 156 | where: eq(dbChannels.parentId, opts.selectedChannel), |
| 157 | orderBy: desc(dbChannels.id), |
| 158 | offset, |
| 159 | limit: limit * 10, // Allow buffer room if some threads are private |
| 160 | }) |
| 161 | : [], |
| 162 | ]); |
| 163 | if (!found || found.kickedTime != null) return null; |
| 164 | const channels = found.channels |
| 165 | .map((c) => zChannelPublic.parse(c)) |
| 166 | .sort((a, b) => { |
| 167 | if ( |
| 168 | a.type === ChannelType.GuildForum.valueOf() && |
| 169 | b.type !== ChannelType.GuildForum.valueOf() |
| 170 | ) |
| 171 | return -1; |
| 172 | if ( |
| 173 | a.type !== ChannelType.GuildForum.valueOf() && |
| 174 | b.type === ChannelType.GuildForum.valueOf() |
| 175 | ) |
no test coverage detected