]> git.cworth.org Git - turbot/blobdiff - turbot/events.py
Convert /puzzle puzzle creation to all-one-table database schema
[turbot] / turbot / events.py
index bbc00b63bccf8b74c578ed0f3e4d6e3372c91e8e..d7019944696f63a782270a8a20305a1e2c31499c 100644 (file)
@@ -140,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
 
@@ -207,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
@@ -236,7 +233,7 @@ 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)
+    item['channel_url'] = channel_url(channel_id)
     turb.table.put_item(Item=item)
 
     # Create a sheet for the puzzle
@@ -250,14 +247,14 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
     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
@@ -268,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))
 
@@ -276,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
 
@@ -320,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)),