]> git.cworth.org Git - turbot/blob - turbot/views.py
Add list of active hunts to the turbot Home tab
[turbot] / turbot / views.py
1 def text(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 home(turb, user_id, body):
33     """Returns a view to be published as the turbot home tab for user_id
34
35     The body argument is a dictionary as provided by the Slack request.
36     The return value is a dictionary suitable to be published to the
37     Slack views_publish API."""
38
39     response = turb.db.Table("hunts").scan()
40     hunts = response['Items']
41
42     return {
43         "type": "home",
44         "blocks": [
45             section(text("*Active hunts*")),
46             *[section(text(hunt['name'])) for hunt in hunts if hunt['active']],
47             actions(button("New hunt", "new_hunt"))
48         ]
49     }