]> git.cworth.org Git - turbot/blobdiff - turbot/interaction.py
Unify code to rename channel, set channel description, and rename sheet
[turbot] / turbot / interaction.py
index 417066aba650324fd3ff0199311552f94bb16214..0b2f30eec9324c9ba8689a266d9f3ad683e2fd78 100644 (file)
@@ -3,7 +3,12 @@ from turbot.blocks import (
     input_block, section_block, text_block, multi_select_block, checkbox_block
 )
 from turbot.hunt import find_hunt_for_hunt_id, hunt_blocks
-from turbot.puzzle import find_puzzle_for_url, find_puzzle_for_puzzle_id
+from turbot.puzzle import (
+    find_puzzle_for_url,
+    find_puzzle_for_puzzle_id,
+    puzzle_update_channel_and_sheet,
+    puzzle_id_from_name
+)
 import turbot.rot
 import turbot.sheets
 import turbot.slack
@@ -230,14 +235,7 @@ def edit_puzzle_submission(turb, payload, metadata):
     # We need to set the channel topic if any of puzzle name, url,
     # state, status, or solution, has changed. Let's just do that
     # unconditionally here.
-
-    # XXX: What we really want here is a single function that sets the
-    # channel name, the channel topic, and the sheet name. That single
-    # function should be called anywhere there is code changing any of
-    # these things. This function could then also accept an optional
-    # "old_puzzle" argument and avoid changing any of those things
-    # that are unnecessary.
-    set_channel_topic(turb, puzzle)
+    puzzle_update_channel_and_sheet(turb, puzzle)
 
     return lambda_ok
 
@@ -626,7 +624,7 @@ def puzzle_submission(turb, payload, metadata):
                 "Error: A puzzle with this URL already exists.")
 
     # Create a Slack-channel-safe puzzle_id
-    puzzle_id = re.sub(r'[^a-zA-Z0-9_]', '', name).lower()
+    puzzle_id = puzzle_id_from_name(name)
 
     # Create a channel for the puzzle
     hunt_dash_channel = "{}-{}".format(hunt_id, puzzle_id)
@@ -676,41 +674,6 @@ def puzzle_submission(turb, payload, metadata):
 
     return lambda_ok
 
-# XXX: This duplicates functionality eith events.py:set_channel_description
-def set_channel_topic(turb, puzzle):
-    channel_id = puzzle['channel_id']
-    name = puzzle['name']
-    url = puzzle.get('url', None)
-    sheet_url = puzzle.get('sheet_url', None)
-    state = puzzle.get('state', None)
-    status = puzzle['status']
-
-    description = ''
-
-    if status == 'solved':
-        description += "SOLVED: `{}` ".format('`, `'.join(puzzle['solution']))
-
-    description += name
-
-    links = []
-    if url:
-        links.append("<{}|Puzzle>".format(url))
-    if sheet_url:
-        links.append("<{}|Sheet>".format(sheet_url))
-
-    if len(links):
-        description += "({})".format(', '.join(links))
-
-    if state:
-        description += " {}".format(state)
-
-    # Slack only allows 250 characters for a topic
-    if len(description) > 250:
-        description = description[:247] + "..."
-
-    turb.slack_client.conversations_setTopic(channel=channel_id,
-                                             topic=description)
-
 def state(turb, body, args):
     """Implementation of the /state command
 
@@ -729,7 +692,7 @@ def state(turb, body, args):
     puzzle['state'] = args
     turb.table.put_item(Item=puzzle)
 
-    set_channel_topic(turb, puzzle)
+    puzzle_update_channel_and_sheet(turb, puzzle)
 
     return lambda_ok
 
@@ -774,19 +737,7 @@ def solved(turb, body, args):
     )
 
     # And update the puzzle's description
-    set_channel_topic(turb, puzzle)
-
-    # And rename the sheet to suffix with "-SOLVED"
-    turbot.sheets.renameSheet(turb, puzzle['sheet_url'],
-                              puzzle['name'] + "-SOLVED")
-
-    # Finally, rename the Slack channel to add the suffix '-solved'
-    channel_name = "{}-{}-solved".format(
-        puzzle['hunt_id'],
-        puzzle['puzzle_id'])
-    turb.slack_client.conversations_rename(
-        channel=puzzle['channel_id'],
-        name=channel_name)
+    puzzle_update_channel_and_sheet(turb, puzzle)
 
     return lambda_ok