]> git.cworth.org Git - turbot/blobdiff - turbot/events.py
Home: Update query for finding puzzles to new all-one-table schema
[turbot] / turbot / events.py
index 86a3c837d15f2f28485c13c7ae7a913c454f803c..bbc00b63bccf8b74c578ed0f3e4d6e3372c91e8e 100644 (file)
@@ -58,7 +58,12 @@ def hunt_block(turb, hunt):
     hunt_id = hunt['hunt_id']
     channel_id = hunt['channel_id']
 
-    response = turb.db.Table(hunt_id).scan()
+    response = turb.table.query(
+        KeyConditionExpression=(
+            Key('PK').eq('hunt-{}'.format(hunt_id)) &
+            Key('SK').begins_with('puzzle-')
+        )
+    )
     puzzles = response['Items']
 
     hunt_text = "*<{}|{}>*".format(channel_url(channel_id), name)
@@ -77,7 +82,9 @@ def home(turb, user_id):
 
     # Behave cleanly if there is no "turbot" table at all yet.
     try:
-        response = turb.table.scan()
+        response = turb.table.scan(
+            IndexName="hunt_id_index",
+        )
         hunts = response['Items']
     except Exception:
         hunts = []
@@ -91,11 +98,20 @@ def home(turb, user_id):
             continue
         hunt_blocks += hunt_block(turb, hunt)
 
+    if len(hunt_blocks):
+        hunt_blocks = [
+            section_block(text_block("*Hunts you belong to*")),
+            divider_block(),
+            * hunt_blocks
+        ]
+    else:
+        hunt_blocks = [
+            section_block(text_block("You do not belong to any hunts"))
+        ]
+
     return {
         "type": "home",
         "blocks": [
-            section_block(text_block("*Hunts you belong to*")),
-            divider_block(),
             * hunt_blocks,
             actions_block(button_block("New hunt", "new_hunt"))
         ]
@@ -221,14 +237,14 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
     # string and cleanly return (eliminating all future retries).
     item['sheet_url'] = 'pending'
     item['channel_url'] = channel_url(puzzle_channel_id)
-    puzzle_table.put_item(Item=item)
+    turb.table.put_item(Item=item)
 
     # Create a sheet for the puzzle
     sheet = sheets_create_for_puzzle(turb, item)
 
     # Update the database with the URL of the sheet
     item['sheet_url'] = sheet['url']
-    puzzle_table.put_item(Item=item)
+    turb.table.put_item(Item=item)
 
     # Get the new sheet_url into the channel description
     set_channel_description(turb, item)