]> git.cworth.org Git - turbot/blob - turbot/views.py
Add a link each hunt's channel in the Home view
[turbot] / turbot / views.py
1 def text_block(body):
2     return {
3         "text": {
4             "type": "mrkdwn",
5             "text": body
6         }
7     }
8
9 def section(block):
10     return {
11         "type": "section",
12         **block
13     }
14
15 def actions(*elements):
16     return {
17         "type": "actions",
18         "elements": list(elements)
19     }
20
21 def button(label, name):
22     return {
23         "type": "button",
24         "text": {
25             "type": "plain_text",
26             "text": label,
27             "emoji": True
28         },
29         "value": name
30     }
31
32 def hunt_block(hunt):
33     text = "{}: <#{}>".format(hunt['name'], hunt['channel'])
34     return section(text_block(text))
35
36 def home(turb, user_id, body):
37     """Returns a view to be published as the turbot home tab for user_id
38
39     The body argument is a dictionary as provided by the Slack request.
40     The return value is a dictionary suitable to be published to the
41     Slack views_publish API."""
42
43     response = turb.db.Table("hunts").scan()
44     hunts = response['Items']
45
46     return {
47         "type": "home",
48         "blocks": [
49             section(text_block("*Active hunts*")),
50             *[hunt_block(hunt) for hunt in hunts if hunt['active']],
51             actions(button("New hunt", "new_hunt"))
52         ]
53     }