]> git.cworth.org Git - turbot/blobdiff - turbot/events.py
Limit Turbot home display to hunts a user belongs to
[turbot] / turbot / events.py
index 2c1e58b21d65df96e9e64e58a56eb5e346cf587a..1b15f0cdd32b918c141ab13466b74a0de208c8d8 100644 (file)
@@ -3,6 +3,7 @@ from turbot.blocks import (
 )
 import turbot.sheets
 import turbot.slack
+from turbot.slack import slack_send_message, slack_channel_members
 
 TURBOT_USER_ID = 'U01B9QM4P9R'
 
@@ -82,13 +83,17 @@ def home(turb, user_id):
 
     hunt_blocks = []
     for hunt in hunts:
-        if hunt['active']:
-            hunt_blocks += hunt_block(turb, hunt)
+        if not hunt['active']:
+            continue
+        if user_id not in slack_channel_members(turb.slack_client,
+                                                hunt['channel_id']):
+            continue
+        hunt_blocks += hunt_block(turb, hunt)
 
     return {
         "type": "home",
         "blocks": [
-            section_block(text_block("*Active hunts*")),
+            section_block(text_block("*Hunts you belong to*")),
             divider_block(),
             * hunt_blocks,
             actions_block(button_block("New hunt", "new_hunt"))
@@ -142,22 +147,21 @@ def hunt_channel_created(turb, channel_name, channel_id):
     hunts_table.put_item(Item=item)
 
     # Also, let the channel users know what we are up to
-    turb.slack_client.chat_postMessage(
-        channel=channel_id,
-        text="Welcome to the channel for the {} hunt! ".format(item['name'])
+    slack_send_message(
+        turb.slack_client, channel_id,
+        "Welcome to the channel for the {} hunt! ".format(item['name'])
         + "Please wait a minute or two while I create some backend resources.")
 
-    # Create a sheet for the channel
-    sheet = turbot.sheets.sheets_create(turb, channel_name)
+    # Create a sheet for the hunt
+    sheet = turbot.sheets.sheets_create(turb, item['name'])
 
     # Update the database with the URL of the sheet
     item['sheet_url'] = sheet['url']
     hunts_table.put_item(Item=item)
 
     # Message the channel with the URL of the sheet
-    turb.slack_client.chat_postMessage(channel=channel_id,
-                                       text="Sheet created for this hunt: {}"
-                                       .format(sheet['url']))
+    slack_send_message(turb.slack_client, channel_id,
+                       "Sheet created for this hunt: {}".format(sheet['url']))
 
     # Create a database table for this hunt's puzzles
     table = turb.db.create_table(
@@ -182,9 +186,9 @@ def hunt_channel_created(turb, channel_name, channel_id):
     hunts_table.put_item(Item=item)
 
     # Message the hunt channel that the database is ready
-    turb.slack_client.chat_postMessage(
-        channel=channel_id,
-        text="Thank you for waiting. This hunt is now ready to begin! "
+    slack_send_message(
+        turb.slack_client, channel_id,
+        "Thank you for waiting. This hunt is now ready to begin! "
         + "Use `/puzzle` to create puzzles for the hunt.")
 
     return lambda_success
@@ -243,10 +247,11 @@ 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)
 
     # Create a sheet for the puzzle
-    sheet = turbot.sheets.sheets_create_for_puzzle(turb, puzzle_channel_name)
+    sheet = turbot.sheets.sheets_create_for_puzzle(turb, item)
 
     # Update the database with the URL of the sheet
     item['sheet_url'] = sheet['url']
@@ -255,6 +260,7 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
     # Get the new sheet_url into the channel description
     set_channel_description(turb, item)
 
+    # Finally, 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',
@@ -272,9 +278,9 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id):
         # Filter out Turbot's own ID to avoid inviting itself
         members = [m for m in members if m != TURBOT_USER_ID]
 
-        turb.slack_client.chat_postMessage(
-            channel=puzzle_channel_id,
-            text="Inviting all members from the hunt channel:  {}"
+        slack_send_message(
+            turb.slack_client, puzzle_channel_id,
+            "Inviting all members from the hunt channel:  {}"
             .format(hunt_id))
 
         # Invite those members to the puzzle channel (in chunks of 500)