]> git.cworth.org Git - turbot/blobdiff - turbot/interaction.py
Add a state field to the "edit hunt" form
[turbot] / turbot / interaction.py
index cbb8befe2bcb3eb4ebf3df0d7f3165fcf1f5e128..25307c22a26aec26084da1b623ebaee2acec069f 100644 (file)
@@ -5,7 +5,8 @@ from turbot.blocks import (
 from turbot.hunt import (
     find_hunt_for_hunt_id,
     hunt_blocks,
-    hunt_puzzles_for_hunt_id
+    hunt_puzzles_for_hunt_id,
+    hunt_update_topic
 )
 from turbot.puzzle import (
     find_puzzle_for_url,
@@ -216,6 +217,10 @@ def edit_puzzle(turb, puzzle, trigger_id):
                         "New round(s) this puzzle belongs to " +
                         "(comma separated)",
                         optional=True),
+            input_block("Tag(s)", "tags",
+                        "Tags for this puzzle (comma separated)",
+                        initial_value=", ".join(puzzle.get("tags", [])),
+                        optional=True),
             input_block("State", "state",
                         "State of this puzzle (partial progress, next steps)",
                         initial_value=puzzle.get("state", None),
@@ -273,6 +278,7 @@ def edit_puzzle_submission(turb, payload, metadata):
         if rounds:
             puzzle['rounds'] = rounds
     new_rounds = state['new_rounds']['new_rounds']['value']
+    tags = state['tags']['tags']['value']
     puzzle_state = state['state']['state']['value']
     if puzzle_state:
         puzzle['state'] = puzzle_state
@@ -283,9 +289,10 @@ def edit_puzzle_submission(turb, payload, metadata):
     puzzle['solution'] = []
     solution = state['solution']['solution']['value']
     if solution:
-        puzzle['solution'] = [
+        # Construct a list from a set to avoid any duplicates
+        puzzle['solution'] = list({
             sol.strip() for sol in solution.split(',')
-        ]
+        })
 
     # Verify that there's a solution if the puzzle is mark solved
     if puzzle['status'] == 'solved' and not puzzle['solution']:
@@ -314,13 +321,31 @@ def edit_puzzle_submission(turb, payload, metadata):
                 }
             )
 
+    # Process any tags
+    puzzle['tags'] = []
+    if tags:
+        for tag in tags.split(','):
+            # Drop any leading/trailing spaces from the tag
+            tag = tag.strip().upper()
+            # Ignore any empty string
+            if not len(tag):
+                continue
+            # Reject a tag that is not alphabetic or underscore A-Z_
+            if not re.match(r'^[A-Z0-9_]*$', tag):
+                return submission_error(
+                    "tags",
+                    "Error: Tags can only contain letters, numbers, "
+                    + "and the underscore character."
+                )
+            puzzle['tags'].append(tag)
+
     # Get old puzzle from the database (to determine what's changed)
     old_puzzle = find_puzzle_for_sort_key(turb,
                                           puzzle['hunt_id'],
                                           puzzle['SK'])
 
     # If we are changing puzzle type (meta -> plain or plain -> meta)
-    # the the sort key has to change, so compute the new one and delete
+    # then the sort key has to change, so compute the new one and delete
     # the old item from the database.
     #
     # XXX: We should really be using a transaction here to combine the
@@ -348,9 +373,20 @@ def edit_puzzle_submission(turb, payload, metadata):
         turb.slack_client, puzzle['channel_id'],
         edit_message, blocks=blocks)
 
+    # Advertize any tag additions to the hunt
+    hunt = find_hunt_for_hunt_id(turb, puzzle['hunt_id'])
+
+    new_tags = set(puzzle['tags']) - set(old_puzzle['tags'])
+    if new_tags:
+        message = "Puzzle <{}|{}> has been tagged: {}".format(
+            puzzle['channel_url'],
+            puzzle['name'],
+            ", ".join(['`{}`'.format(t) for t in new_tags])
+        )
+        slack_send_message(turb.slack_client, hunt['channel_id'], message)
+
     # Also inform the hunt if the puzzle's solved status changed
     if puzzle['status'] != old_puzzle['status']:
-        hunt = find_hunt_for_hunt_id(turb, puzzle['hunt_id'])
         if puzzle['status'] == 'solved':
             message = "Puzzle <{}|{}> has been solved!".format(
                 puzzle['channel_url'],
@@ -424,6 +460,10 @@ def edit_hunt(turb, hunt, trigger_id):
             input_block("Hunt URL", "url", "External URL of hunt",
                         initial_value=hunt.get("url", None),
                         optional=True),
+            input_block("State", "state",
+                        "State of the hunt (goals, upcoming meetings, etc.)",
+                        initial_value=hunt.get("state", None),
+                        optional=True),
             checkbox_block("Is this hunt active?", "Active", "active",
                            checked=(hunt.get('active', False)))
         ]
@@ -462,6 +502,9 @@ def edit_hunt_submission(turb, payload, metadata):
     if url:
         hunt['url'] = url
 
+    hunt_state = state['state']['state']['value']
+    if hunt_state:
+        hunt['state'] = hunt_state
     if state['active']['active']['selected_options']:
         hunt['active'] = True
     else:
@@ -487,6 +530,9 @@ def edit_hunt_submission(turb, payload, metadata):
         turb.slack_client, hunt['channel_id'],
         edit_message, blocks=blocks)
 
+    # Update channel topic and description
+    hunt_update_topic(turb, hunt)
+
     return lambda_ok
 
 def new_hunt_command(turb, body):
@@ -964,7 +1010,10 @@ def new_puzzle(turb, body):
             input_block("New round(s)", "new_rounds",
                         "New round(s) this puzzle belongs to " +
                         "(comma separated)",
-                        optional=True)
+                        optional=True),
+            input_block("Tag(s)", "tags",
+                        "Tags for this puzzle (comma separated)",
+                        optional=True),
         ]
     }
 
@@ -1006,6 +1055,7 @@ def new_puzzle_submission(turb, payload, metadata):
     else:
         rounds = []
     new_rounds = state['new_rounds']['new_rounds']['value']
+    tags = state['tags']['tags']['value']
 
     # Create a Slack-channel-safe puzzle_id
     puzzle['puzzle_id'] = puzzle_id_from_name(puzzle['name'])
@@ -1041,6 +1091,24 @@ def new_puzzle_submission(turb, payload, metadata):
                 }
             )
 
+    # Process any tags
+    puzzle['tags'] = []
+    if tags:
+        for tag in tags.split(','):
+            # Drop any leading/trailing spaces from the tag
+            tag = tag.strip().upper()
+            # Ignore any empty string
+            if not len(tag):
+                continue
+            # Reject a tag that is not alphabetic or underscore A-Z_
+            if not re.match(r'^[A-Z0-9_]*$', tag):
+                return submission_error(
+                    "tags",
+                    "Error: Tags can only contain letters, numbers, "
+                    + "and the underscore character."
+                )
+            puzzle['tags'].append(tag)
+
     if rounds:
         puzzle['rounds'] = rounds
 
@@ -1158,6 +1226,17 @@ def tag(turb, body, args):
 
     puzzle_update_channel_and_sheet(turb, puzzle, old_puzzle=old_puzzle)
 
+    # Advertize any tag additions to the hunt
+    new_tags = set(puzzle['tags']) - set(old_puzzle['tags'])
+    if new_tags:
+        hunt = find_hunt_for_hunt_id(turb, puzzle['hunt_id'])
+        message = "Puzzle <{}|{}> has been tagged: {}".format(
+            puzzle['channel_url'],
+            puzzle['name'],
+            ", ".join(['`{}`'.format(t) for t in new_tags])
+        )
+        slack_send_message(turb.slack_client, hunt['channel_id'], message)
+
     return lambda_ok
 
 commands["/tag"] = tag
@@ -1184,7 +1263,10 @@ def solved(turb, body, args):
 
     # Set the status and solution fields in the database
     puzzle['status'] = 'solved'
-    puzzle['solution'].append(args)
+
+    # Don't append a duplicate solution
+    if args not in puzzle['solution']:
+        puzzle['solution'].append(args)
     if 'state' in puzzle:
         del puzzle['state']
     turb.table.put_item(Item=puzzle)
@@ -1192,7 +1274,7 @@ def solved(turb, body, args):
     # Report the solution to the puzzle's channel
     slack_send_message(
         turb.slack_client, channel_id,
-        "Puzzle mark solved by <@{}>: `{}`".format(user_id, args))
+        "Puzzle marked solved by <@{}>: `{}`".format(user_id, args))
 
     # Also report the solution to the hunt channel
     hunt = find_hunt_for_hunt_id(turb, puzzle['hunt_id'])
@@ -1210,6 +1292,50 @@ def solved(turb, body, args):
 
 commands["/solved"] = solved
 
+def delete(turb, body, args):
+    """Implementation of the /delete command
+
+    The argument to this command is the ID of a hunt.
+
+    The command will report an error if the specified hunt is active.
+
+    If the hunt is inactive, this command will archive all channels
+    from the hunt.
+    """
+
+    if not args:
+        return bot_reply("Error, no hunt provided. Usage: `/delete HUNT_ID`")
+
+    hunt_id = args
+    hunt = find_hunt_for_hunt_id(turb, hunt_id)
+
+    if not hunt:
+        return bot_reply("Error, no hunt named \"{}\" exists.".format(hunt_id))
+
+    if hunt['active']:
+        return bot_reply(
+            "Error, refusing to delete active hunt \"{}\".".format(hunt_id)
+        )
+
+    if hunt['hunt_id'] != hunt_id:
+        return bot_reply(
+            "Error, expected hunt ID of \"{}\" but found \"{}\".".format(
+                hunt_id, hunt['hunt_id']
+            )
+        )
+
+    puzzles = hunt_puzzles_for_hunt_id(turb, hunt_id)
+
+    for puzzle in puzzles:
+        channel_id = puzzle['channel_id']
+        turb.slack_client.conversations_archive(channel=channel_id)
+
+    turb.slack_client.conversations_archive(channel=hunt['channel_id'])
+
+    return lambda_ok
+
+commands["/delete"] = delete
+
 def hunt(turb, body, args):
     """Implementation of the /hunt command