(
ids: string[],
opts: {
includeMessageCount?: boolean;
} = {},
)
| 176 | } |
| 177 | |
| 178 | export async function findManyChannelsById( |
| 179 | ids: string[], |
| 180 | opts: { |
| 181 | includeMessageCount?: boolean; |
| 182 | } = {}, |
| 183 | ): Promise<ChannelWithFlags[]> { |
| 184 | if (ids.length === 0) return Promise.resolve([]); |
| 185 | const data = await dbReplica.query.dbChannels.findMany({ |
| 186 | where: inArray(dbChannels.id, ids), |
| 187 | }); |
| 188 | const withFlags = data.map(addFlagsToChannel); |
| 189 | let threadMessageCountLookup: Map<string, number | undefined> | undefined = |
| 190 | undefined; |
| 191 | const isThreadType = (t: ChannelType) => |
| 192 | t === ChannelType.PublicThread || |
| 193 | t === ChannelType.PrivateThread || |
| 194 | t === ChannelType.AnnouncementThread; |
| 195 | if (opts.includeMessageCount) { |
| 196 | const threadIds = withFlags |
| 197 | .filter((c) => isThreadType(c.type)) |
| 198 | .map((c) => c.id); |
| 199 | const threadMessageCounts = await findManyChannelMessagesCounts(threadIds); |
| 200 | threadMessageCountLookup = new Map( |
| 201 | threadMessageCounts.map((x) => [x.channelId, x.count]), |
| 202 | ); |
| 203 | } |
| 204 | return withFlags.map((c) => { |
| 205 | let messageCount: number | undefined = undefined; |
| 206 | if (opts.includeMessageCount) { |
| 207 | if (isThreadType(c.type)) { |
| 208 | messageCount = threadMessageCountLookup?.get(c.id); |
| 209 | } else { |
| 210 | messageCount = NUMBER_OF_CHANNEL_MESSAGES_TO_LOAD; |
| 211 | } |
| 212 | } |
| 213 | return { |
| 214 | ...c, |
| 215 | messageCount, |
| 216 | }; |
| 217 | }); |
| 218 | } |
| 219 | |
| 220 | export async function createChannel(data: z.infer<typeof zChannelCreate>) { |
| 221 | const combinedData = applyChannelSettingsChangesSideEffects({ |
no test coverage detected