Skip to content

Commit

Permalink
Add group import
Browse files Browse the repository at this point in the history
  • Loading branch information
BjoernPetersen committed Dec 15, 2024
1 parent fa0839a commit b15b04f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ enabledChats:
crons:
- schedule: "0 13 * * *"
command: send-polls
suspend:
- schedule: "0 0 * * *"
command: close-polls
suspend: true
- schedule: "0 0 * * *"
command: import-users
suspend: true
- schedule: "0 0 * * *"
command: import-user-groups
suspend: true
13 changes: 13 additions & 0 deletions src/bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ async def _import_users(dynamo: DynamoClient, database: Database) -> None:
await database.close()


async def _import_user_groups(dynamo: DynamoClient, database: Database) -> None:
await database.open()
try:
async for user_id, group_id in dynamo.list_user_groups():
_logger.info("Linking user %d to group %d", user_id, group_id)
await database.add_to_group(user_id=user_id, group_id=group_id)
finally:
await database.close()


def main() -> None:
config, database = initialize()

Expand All @@ -60,6 +70,9 @@ def main() -> None:
case "import-users":
_logger.info("Importing users")
asyncio.run(_import_users(DynamoClient(config.aws), database))
case "import-user-groups":
_logger.info("Importing user groups")
asyncio.run(_import_user_groups(DynamoClient(config.aws), database))
case other:
_logger.error("Unknown operation mode: %s", other)
sys.exit(1)
Expand Down
12 changes: 12 additions & 0 deletions src/bot/dynamo.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ async def list_users(self) -> AsyncIterable[User]:
response = await table.scan(ExclusiveStartKey=last_key)
for item in response.get("Items"):
yield self._build_user(item)

async def list_user_groups(self) -> AsyncIterable[tuple[int, int]]:
async with self._session.resource("dynamodb") as resource:
table = await resource.Table("mischebot-groups")
response = await table.scan()
for item in response.get("Items"):
yield int(item["user_id"]), int(item["group_id"])

while last_key := response.get("LastEvaluatedKey"):
response = await table.scan(ExclusiveStartKey=last_key)
for item in response.get("Items"):
yield int(item["user_id"]), int(item["group_id"])

0 comments on commit b15b04f

Please sign in to comment.