]> git.cworth.org Git - turbot/blob - turbot/views.py
e05593dc69c1ef6c6e4bd21064d74d1cd8ed3433
[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 input(label, name, placeholder):
33     return {
34         "type": "input",
35         "element": {
36             "type": "plain_text_input",
37             "action_id": name,
38             "placeholder": {
39                 "type": "plain_text",
40                 "text": placeholder,
41             }
42         },
43         "label": {
44             "type": "plain_text",
45             "text": label
46         }
47     }
48
49 def hunt_block(hunt):
50     text = "{}: <#{}>".format(hunt['name'], hunt['channel_id'])
51     return section(text_block(text))
52
53 def home(turb, user_id, body):
54     """Returns a view to be published as the turbot home tab for user_id
55
56     The body argument is a dictionary as provided by the Slack request.
57     The return value is a dictionary suitable to be published to the
58     Slack views_publish API."""
59
60     response = turb.db.Table("hunts").scan()
61     hunts = response['Items']
62
63     return {
64         "type": "home",
65         "blocks": [
66             section(text_block("*Active hunts*")),
67             *[hunt_block(hunt) for hunt in hunts if hunt['active']],
68             actions(button("New hunt", "new_hunt"))
69         ]
70     }
71
72 def new_hunt():
73     """Returns a view to be published as the new_hunt modal"""
74
75     return {
76         "type": "modal",
77         "title": { "type": "plain_text", "text": "New Hunt" },
78         "submit": { "type": "plain_text", "text": "Create" },
79         "blocks": [
80             input("Hunt name", "name", "Name of the hunt"),
81             input("Hunt ID", "slug", "Short prefix for hunt (no spaces)"),
82             input("Hunt URL", "url", "External URL of hunt")
83         ]
84     }