-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: retry pending members -> concurrent
fix: only store groups that the bot can manage chore: add types to the schema fix: readd batch member add since they will be checked regularly (having an approved member that isn't for a small period of time is fine)
- Loading branch information
1 parent
098595a
commit 14863f0
Showing
7 changed files
with
142 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { and, eq } from "drizzle-orm"; | ||
import * as schema from "../db/schema"; | ||
import { db } from "../db"; | ||
import { bot } from "../lib/xmtp/client"; | ||
import type { GroupMember, GroupMemberStatus } from "../db/schema"; | ||
|
||
export async function retryPendingMembers(groupId?: string) { | ||
// - get the pending members | ||
const pendingMembers = await db.query.groupMembers.findMany({ | ||
where: (fields, { eq }) => | ||
groupId | ||
? and(eq(fields.groupId, groupId), eq(fields.status, "pending")) | ||
: eq(fields.status, "pending"), | ||
}); | ||
|
||
await Promise.allSettled(pendingMembers.map(retryAddMember)); | ||
} | ||
|
||
/** | ||
* Retry adding a status `pending` member to the group & update the status if successful | ||
* */ | ||
async function retryAddMember({ | ||
id, | ||
groupId, | ||
chainAwareAddress, | ||
}: GroupMember): Promise<GroupMemberStatus | undefined> { | ||
const address = chainAwareAddress.split(":").at(-1); | ||
if (!id || !groupId || !address) return undefined; | ||
|
||
try { | ||
console.log(`adding ${address} to group ${groupId}`); | ||
await bot.addMembers(groupId, [address]); | ||
await db | ||
.update(schema.groupMembers) | ||
.set({ status: "approved" as const }) | ||
.where( | ||
and( | ||
eq(schema.groupMembers.id, id), | ||
eq(schema.groupMembers.chainAwareAddress, chainAwareAddress), | ||
), | ||
); | ||
return "approved"; | ||
} catch (e) { | ||
console.error(`failed to add ${address} to group ${groupId}`); | ||
// - no need to update the status as we will retry this on the next run | ||
return "pending"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters