]> git.cworth.org Git - turbot/blobdiff - turbot/events.py
Convert /puzzle puzzle creation to all-one-table database schema
[turbot] / turbot / events.py
index 86a3c837d15f2f28485c13c7ae7a913c454f803c..d7019944696f63a782270a8a20305a1e2c31499c 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"))
         ]
@@ -124,7 +140,7 @@ def hunt_channel_created(turb, channel_name, channel_id):
         KeyConditionExpression=Key("channel_id").eq(channel_id)
     )
     if 'Items' not in response:
-        print("Warning: Cannot find channel_id {} in hunts table. "
+        print("Warning: Cannot find channel_id {} in turbot table. "
               .format(channel_id) + "Letting Slack retry this event")
         return lambda_error
 
@@ -191,28 +207,25 @@ def set_channel_description(turb, puzzle):
     turb.slack_client.conversations_setTopic(channel=channel_id,
                                              topic=description)
 
-def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
+def puzzle_channel_created(turb, channel_name, channel_id):
     """Creates sheet and invites user for a newly-created puzzle channel"""
 
-    hunt_id = puzzle_channel_name.split('-')[0]
-
     # First see if we can find an entry for this puzzle in the database.
     # If not, simply return an error and let Slack retry
-    response = turb.table.get_item(
-        Key={'channel_id': puzzle_channel_id},
-        ConsistentRead=True
+    response = turb.table.query(
+        IndexName="channel_id_index",
+        KeyConditionExpression=Key("channel_id").eq(channel_id),
     )
-    if 'Item' not in response:
-        print("Warning: Cannot find channel_id {} in {} table. "
-              .format(puzzle_channel_id, hunt_id)
-              + "Letting Slack retry this event")
+    if 'Items' not in response:
+        print("Warning: Cannot find channel_id {} in turbot table. "
+              .format(channel_id) + "Letting Slack retry this event")
         return lambda_error
 
-    item = response['Item']
+    item = response['Items'][0]
 
     if 'sheet_url' in item:
         print("Info: channel_id {} already has sheet_url {}. Exiting."
-              .format(puzzle_channel_id, item['sheet_url']))
+              .format(channel_id, item['sheet_url']))
         return lambda_success
 
     # Before launching into sheet creation, indicate that we're doing this
@@ -220,28 +233,28 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
     # and Slack retries the event, that next event will see this 'pending'
     # 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)
+    item['channel_url'] = channel_url(channel_id)
+    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)
 
     # Lookup and invite all users from this hunt to this new puzzle
-    hunts_table = turb.db.Table('hunts')
-    response = hunts_table.scan(
-        FilterExpression='hunt_id = :hunt_id',
-        ExpressionAttributeValues={':hunt_id': hunt_id}
-    )
-
-    if 'Items' in response:
-
+    #    hunts_table = turb.db.Table('hunts')
+    #    response = hunts_table.scan(
+    #        FilterExpression='hunt_id = :hunt_id',
+    #        ExpressionAttributeValues={':hunt_id': hunt_id}
+    #    )
+    #
+    #    if 'Items' in response:
+    if False:
         hunt_channel_id = response['Items'][0]['channel_id']
 
         # Find all members of the hunt channel
@@ -252,7 +265,7 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
         members = [m for m in members if m != TURBOT_USER_ID]
 
         slack_send_message(
-            turb.slack_client, puzzle_channel_id,
+            turb.slack_client, channel_id,
             "Inviting all members from the hunt channel: "
             + "<#{}>".format(hunt_channel_id))
 
@@ -260,7 +273,7 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
         cursor = 0
         while cursor < len(members):
             turb.slack_client.conversations_invite(
-                channel=puzzle_channel_id,
+                channel=channel_id,
                 users=members[cursor:cursor + 500])
             cursor += 500
 
@@ -304,7 +317,7 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
     )
 
     turb.slack_client.chat_postMessage(
-        channel=puzzle_channel_id,
+        channel=channel_id,
         text="New puzzle: {}".format(item['name']),
         blocks=[
             section_block(text_block(welcome_msg)),