Skip to content

Commit 70a0334

Browse files
fix: do not recreate group if redis is empty (#114)
* fix: do not recreate group if redis is empty * fix: fixed the fix * fix: add ttl * perf: fill redis once to avoid too many backend calls * fix: remove ttl, otherwise we lose track this Redis collection will be manually deleted * fix: void the fillRedis promise --------- Co-authored-by: Lorenzo Corallo <lorenzo.corallo@outlook.it> Co-authored-by: Lorenzo Corallo <66379281+lorenzocorallo@users.noreply.github.com>
1 parent cc1dcd3 commit 70a0334

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/middlewares/bot-membership-handler.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ export class BotMembershipHandler<C extends TelemetryContextFlavor<Context>> ext
4747
constructor() {
4848
super("bot_membership_handler")
4949

50+
void this.fillRedis()
51+
5052
// TEMP: this is for initial migration from previous bot
5153
this.composer.fork().filter(predicate, async (ctx, next) => {
5254
if (ctx.chat.type === "private") return next()
5355

5456
const redisCheck = await this.TEMP_redis.has(ctx.chat.id.toString())
55-
if (redisCheck) {
56-
const backendGroup = await api.tg.groups.getById.query({ telegramId: ctx.chat.id }).catch(() => null)
57-
if (backendGroup !== null) return next()
58-
}
57+
if (redisCheck) return next()
5958

6059
const me = await ctx.getChatMember(ctx.me.id).catch(() => ({ status: "undefined" }))
6160
if (me.status !== "administrator") {
@@ -93,6 +92,11 @@ export class BotMembershipHandler<C extends TelemetryContextFlavor<Context>> ext
9392
})
9493
}
9594

95+
private async fillRedis() {
96+
const dbGroups = await api.tg.groups.getAll.query()
97+
await Promise.all(dbGroups.map((g) => this.TEMP_redis.write(g.telegramId.toString(), g.telegramId)))
98+
}
99+
96100
private static isJoin<C extends Context>(ctx: MemberContext<C>): boolean {
97101
const oldStatusCheck = ["left", "kicked"].includes(ctx.myChatMember.old_chat_member.status)
98102
const newStatusCheck = joinEvent[ctx.myChatMember.chat.type].includes(ctx.myChatMember.new_chat_member.status)

0 commit comments

Comments
 (0)