(
data: BaseMessageWithRelations[],
opts?: {
ignoreChecks?: boolean;
},
)
| 118 | } |
| 119 | |
| 120 | export async function upsertManyMessages( |
| 121 | data: BaseMessageWithRelations[], |
| 122 | opts?: { |
| 123 | ignoreChecks?: boolean; |
| 124 | }, |
| 125 | ) { |
| 126 | if (data.length === 0) return Promise.resolve([]); |
| 127 | const authorIds = new Set(data.map((msg) => msg.authorId)); |
| 128 | |
| 129 | // Todo: make one query for all of these |
| 130 | if (!opts?.ignoreChecks) { |
| 131 | const [ignoredAccounts, userServerSettings] = await Promise.all([ |
| 132 | findManyIgnoredDiscordAccountsById(Array.from(authorIds)), |
| 133 | findManyUserServerSettings( |
| 134 | data.map((msg) => ({ |
| 135 | userId: msg.authorId, |
| 136 | serverId: msg.serverId, |
| 137 | })), |
| 138 | ), |
| 139 | ]); |
| 140 | const userServerSettingsLookup = new Map( |
| 141 | userServerSettings.map((uss) => [`${uss.userId}-${uss.serverId}`, uss]), |
| 142 | ); |
| 143 | |
| 144 | const ignoredAccountIds = new Set(ignoredAccounts.map((i) => i.id)); |
| 145 | data = data.filter((msg) => { |
| 146 | if (ignoredAccountIds.has(msg.authorId)) { |
| 147 | return false; |
| 148 | } |
| 149 | if ( |
| 150 | userServerSettingsLookup.get(`${msg.authorId}-${msg.serverId}`)?.flags |
| 151 | .messageIndexingDisabled |
| 152 | ) { |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | }); |
| 157 | } |
| 158 | const chunkSize = 100; |
| 159 | const chunks = []; |
| 160 | for (let i = 0; i < data.length; i += chunkSize) { |
| 161 | chunks.push(data.slice(i, i + chunkSize)); |
| 162 | } |
| 163 | for await (const chunk of chunks) { |
| 164 | await fastUpsertManyMessages(chunk); |
| 165 | } |
| 166 | return data; |
| 167 | } |
| 168 | |
| 169 | export async function fastUpsertManyMessages(data: BaseMessageWithRelations[]) { |
| 170 | const attachments = new Set<typeof dbAttachments.$inferInsert>(); |
no test coverage detected