Skip to content

Commit 1457b66

Browse files
authored
Merge pull request #35 from qsweber/quinn/feature_notify_in_channel
feature notify in channel
2 parents 215d633 + f240359 commit 1457b66

File tree

10 files changed

+257
-20
lines changed

10 files changed

+257
-20
lines changed

lunch_buddies/actions/auth.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66
from lunch_buddies.clients.slack import SlackClient
77
from lunch_buddies.clients.http import HttpClient
88
from lunch_buddies.dao.teams import TeamsDao
9+
from lunch_buddies.dao.team_settings import TeamSettingsDao
910
from lunch_buddies.models.teams import Team
11+
from lunch_buddies.models.team_settings import TeamSettings
1012
from lunch_buddies.types import Auth
1113

1214

1315
logger = logging.getLogger(__name__)
1416

1517

16-
def auth(request_form: Auth, teams_dao: TeamsDao, slack_client: SlackClient, http_client: HttpClient) -> None:
18+
def auth(
19+
request_form: Auth,
20+
teams_dao: TeamsDao,
21+
team_settings_dao: TeamSettingsDao,
22+
slack_client: SlackClient,
23+
http_client: HttpClient,
24+
) -> None:
1725
response = json.loads(http_client.get(
1826
url='https://slack.com/api/oauth.access',
1927
params={
@@ -33,6 +41,11 @@ def auth(request_form: Auth, teams_dao: TeamsDao, slack_client: SlackClient, htt
3341

3442
teams_dao.create(team)
3543

44+
team_settings_dao.create(TeamSettings(
45+
team_id=team.team_id,
46+
feature_notify_in_channel=True,
47+
))
48+
3649
slack_client.post_message(
3750
team=team,
3851
channel=response['user_id'],

lunch_buddies/actions/notify_group.py

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
from lunch_buddies.clients.slack import SlackClient
44
from lunch_buddies.dao.polls import PollsDao
55
from lunch_buddies.dao.teams import TeamsDao
6+
from lunch_buddies.dao.team_settings import TeamSettingsDao
67
from lunch_buddies.dao.groups import GroupsDao
78
from lunch_buddies.models.groups import Group
9+
from lunch_buddies.models.teams import Team
10+
from lunch_buddies.models.polls import Poll, Choice
811
from lunch_buddies.types import GroupsToNotifyMessage
912

1013

@@ -13,41 +16,85 @@ def notify_group(
1316
slack_client: SlackClient,
1417
polls_dao: PollsDao,
1518
teams_dao: TeamsDao,
19+
team_settings_dao: TeamSettingsDao,
1620
groups_dao: GroupsDao,
1721
) -> None:
1822
team = teams_dao.read('team_id', message.team_id)[0]
19-
poll = polls_dao.find_by_callback_id(message.team_id, message.callback_id)
23+
poll: Poll = polls_dao.find_by_callback_id(message.team_id, message.callback_id)
2024
choice = [
2125
choice
2226
for choice in poll.choices
2327
if choice.key == message.response
2428
][0]
2529

30+
group = Group(
31+
callback_id=message.callback_id,
32+
user_ids=message.user_ids,
33+
response_key=message.response,
34+
)
35+
36+
groups_dao.create(group)
37+
38+
team_settings_search = team_settings_dao.read('team_id', message.team_id)
39+
if team_settings_search and team_settings_search[0].feature_notify_in_channel:
40+
_notify_in_channel(message, slack_client, team, poll, choice)
41+
else:
42+
_notify_private_group(message, slack_client, team, choice)
43+
44+
return
45+
46+
47+
def _notify_private_group(
48+
message: GroupsToNotifyMessage,
49+
slack_client: SlackClient,
50+
team: Team,
51+
choice: Choice,
52+
):
2653
user_in_charge = random.choice(message.user_ids)
2754
text = (
28-
'Hello! This is your lunch group for today. ' +
55+
'Hello! This is your group for today. ' +
2956
'You all should meet somewhere at `{}`. '.format(choice.time) +
3057
'I am selecting <@{}> to be in charge of picking the location.'.format(user_in_charge)
3158
)
32-
3359
conversation = slack_client.open_conversation(
3460
team=team,
3561
users=','.join(message.user_ids),
3662
)
37-
38-
group = Group(
39-
callback_id=message.callback_id,
40-
user_ids=message.user_ids,
41-
response_key=message.response,
63+
slack_client.post_message(
64+
team=team,
65+
channel=conversation['channel']['id'],
66+
as_user=True,
67+
text=text,
4268
)
4369

44-
groups_dao.create(group)
4570

71+
def _notify_in_channel(
72+
message: GroupsToNotifyMessage,
73+
slack_client: SlackClient,
74+
team: Team,
75+
poll: Poll,
76+
choice: Choice,
77+
):
78+
users_formatted = ', '.join([
79+
'<@{}>'.format(user_id)
80+
for user_id in message.user_ids
81+
])
82+
text = (
83+
'Hey {}!'.format(users_formatted) +
84+
' This is your group for today.' +
85+
' You all should meet somewhere at `{}`.'.format(choice.time)
86+
)
87+
result = slack_client.post_message(
88+
team=team,
89+
channel=poll.channel_id,
90+
as_user=True,
91+
text=text,
92+
)
93+
text = '<@{}> should pick the location.'.format(random.choice(message.user_ids))
4694
slack_client.post_message(
4795
team=team,
48-
channel=conversation['channel']['id'],
96+
channel=poll.channel_id,
4997
as_user=True,
5098
text=text,
99+
thread_ts=result['ts']
51100
)
52-
53-
return

lunch_buddies/actions/tests/test_auth.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from lunch_buddies.clients.http import HttpClient
88
from lunch_buddies.clients.slack import SlackClient
99
from lunch_buddies.dao.teams import TeamsDao
10+
from lunch_buddies.dao.team_settings import TeamSettingsDao
1011
from lunch_buddies.models.teams import Team
1112
from lunch_buddies.types import Auth
1213

@@ -33,11 +34,12 @@ def test_auth(mocker):
3334
auto_spec=True,
3435
return_value=True,
3536
)
36-
mocker.patch.object(
37-
teams_dao,
38-
'_read_internal',
37+
team_settings_dao = TeamSettingsDao()
38+
mocked_team_settings_dao_create_internal = mocker.patch.object(
39+
team_settings_dao,
40+
'_create_internal',
3941
auto_spec=True,
40-
return_value=[]
42+
return_value=True,
4143
)
4244
http_client = HttpClient()
4345
mocked_http_get = mocker.patch.object(
@@ -75,6 +77,7 @@ def test_auth(mocker):
7577
module.auth(
7678
Auth(code='test_code'),
7779
teams_dao,
80+
team_settings_dao,
7881
slack_client,
7982
http_client,
8083
)
@@ -88,6 +91,12 @@ def test_auth(mocker):
8891
'created_at': Decimal(created_at.timestamp()),
8992
},
9093
)
94+
mocked_team_settings_dao_create_internal.assert_called_with(
95+
{
96+
'team_id': 'fake_team_id',
97+
'feature_notify_in_channel': 1,
98+
},
99+
)
91100
mocked_slack_client_post_message.assert_called_with(
92101
team=expected_team,
93102
channel='fake_user_id',

lunch_buddies/actions/tests/test_notify_group.py

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from lunch_buddies.clients.slack import SlackClient
77
from lunch_buddies.dao.polls import PollsDao
88
from lunch_buddies.dao.teams import TeamsDao
9+
from lunch_buddies.dao.team_settings import TeamSettingsDao
910
from lunch_buddies.dao.groups import GroupsDao
1011
from lunch_buddies.models.teams import Team
1112
import lunch_buddies.actions.notify_group as module
@@ -28,6 +29,13 @@ def test_notify_group(mocker):
2829
'created_at': created_at.timestamp(),
2930
}]
3031
)
32+
team_settings_dao = TeamSettingsDao()
33+
mocker.patch.object(
34+
team_settings_dao,
35+
'_read_internal',
36+
auto_spec=True,
37+
return_value=[]
38+
)
3139
polls_dao = PollsDao()
3240
mocker.patch.object(
3341
polls_dao,
@@ -79,6 +87,7 @@ def test_notify_group(mocker):
7987
slack_client,
8088
polls_dao,
8189
teams_dao,
90+
team_settings_dao,
8291
groups_dao,
8392
)
8493

@@ -104,5 +113,123 @@ def test_notify_group(mocker):
104113
),
105114
channel='new_group_message_channel',
106115
as_user=True,
107-
text='Hello! This is your lunch group for today. You all should meet somewhere at `11:45`. I am selecting <@user_id_two> to be in charge of picking the location.',
116+
text='Hello! This is your group for today. You all should meet somewhere at `11:45`. I am selecting <@user_id_two> to be in charge of picking the location.',
117+
)
118+
119+
120+
def test_notify_group_feature_notify_in_channel(mocker):
121+
slack_client = SlackClient()
122+
teams_dao = TeamsDao()
123+
created_at = datetime.now()
124+
mocker.patch.object(
125+
teams_dao,
126+
'_read_internal',
127+
auto_spec=True,
128+
return_value=[{
129+
'team_id': '123',
130+
'access_token': 'fake-token',
131+
'name': 'fake-team-name',
132+
'bot_access_token': 'fake-bot-token',
133+
'created_at': created_at.timestamp(),
134+
}]
135+
)
136+
team_settings_dao = TeamSettingsDao()
137+
mocker.patch.object(
138+
team_settings_dao,
139+
'_read_internal',
140+
auto_spec=True,
141+
return_value=[{
142+
'team_id': '123',
143+
'feature_notify_in_channel': 1,
144+
}]
108145
)
146+
polls_dao = PollsDao()
147+
mocker.patch.object(
148+
polls_dao,
149+
'_read_internal',
150+
auto_spec=True,
151+
return_value=[
152+
{
153+
'team_id': '123',
154+
'created_at': datetime.now().timestamp(),
155+
'channel_id': 'test_channel_id',
156+
'created_by_user_id': 'foo',
157+
'callback_id': 'f0d101f9-9aaa-4899-85c8-aa0a2dbb07cb',
158+
'state': polls_constants.CREATED,
159+
'choices': '[{"key": "yes_1145", "is_yes": true, "time": "11:45", "display_text": "Yes (11:45)"}, {"key": "no", "is_yes": false, "time": "", "display_text": "No"}]',
160+
'group_size': polls_constants.DEFAULT_GROUP_SIZE,
161+
},
162+
]
163+
)
164+
165+
groups_dao = GroupsDao()
166+
mocked_groups_dao_create_internal = mocker.patch.object(
167+
groups_dao,
168+
'_create_internal',
169+
auto_spec=True,
170+
return_value=True,
171+
)
172+
173+
mocked_post_message = mocker.patch.object(
174+
slack_client,
175+
'post_message',
176+
auto_spec=True,
177+
return_value={
178+
'ts': 'fake_thread_ts',
179+
},
180+
)
181+
random.seed(0)
182+
183+
module.notify_group(
184+
GroupsToNotifyMessage(
185+
team_id='123',
186+
callback_id=UUID('f0d101f9-9aaa-4899-85c8-aa0a2dbb07cb'),
187+
response='yes_1145',
188+
user_ids=['user_id_one', 'user_id_two'],
189+
),
190+
slack_client,
191+
polls_dao,
192+
teams_dao,
193+
team_settings_dao,
194+
groups_dao,
195+
)
196+
197+
expected_group = {
198+
'callback_id': 'f0d101f9-9aaa-4899-85c8-aa0a2dbb07cb',
199+
'user_ids': '["user_id_one", "user_id_two"]',
200+
'response_key': 'yes_1145',
201+
}
202+
203+
mocked_groups_dao_create_internal.assert_called_with(
204+
expected_group,
205+
)
206+
207+
assert mocked_post_message.call_count == 2
208+
209+
mocked_post_message.assert_has_calls([
210+
mocker.call(
211+
team=Team(
212+
team_id='123',
213+
access_token='fake-token',
214+
name='fake-team-name',
215+
bot_access_token='fake-bot-token',
216+
created_at=created_at,
217+
),
218+
channel='test_channel_id',
219+
as_user=True,
220+
text='Hey <@user_id_one>, <@user_id_two>! This is your group for today. You all should meet somewhere at `11:45`.',
221+
),
222+
mocker.call(
223+
team=Team(
224+
team_id='123',
225+
access_token='fake-token',
226+
name='fake-team-name',
227+
bot_access_token='fake-bot-token',
228+
created_at=created_at,
229+
),
230+
channel='test_channel_id',
231+
as_user=True,
232+
thread_ts='fake_thread_ts',
233+
text='<@user_id_two> should pick the location.',
234+
)
235+
])

lunch_buddies/app/handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from lunch_buddies.dao.polls import PollsDao
1515
from lunch_buddies.dao.poll_responses import PollResponsesDao
1616
from lunch_buddies.dao.teams import TeamsDao
17+
from lunch_buddies.dao.team_settings import TeamSettingsDao
1718
from lunch_buddies.dao.groups import GroupsDao
1819
from lunch_buddies.actions.check_sqs_ping_sns import check_sqs_and_ping_sns as check_sqs_and_ping_sns_action
1920
from lunch_buddies.actions.create_poll import create_poll as create_poll_action
@@ -35,6 +36,7 @@
3536
polls_dao = PollsDao()
3637
poll_responses_dao = PollResponsesDao()
3738
groups_dao = GroupsDao()
39+
team_settings_dao = TeamSettingsDao()
3840

3941

4042
def captureErrors(func):
@@ -106,7 +108,7 @@ def notify_groups_from_queue(event: dict, context: dict) -> None:
106108
GROUPS_TO_NOTIFY,
107109
None,
108110
notify_group_action,
109-
[slack_client, polls_dao, teams_dao, groups_dao]
111+
[slack_client, polls_dao, teams_dao, team_settings_dao, groups_dao]
110112
)
111113
groups_to_notify_queue_handler.run()
112114

lunch_buddies/app/http.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from lunch_buddies.dao.polls import PollsDao
1414
from lunch_buddies.dao.poll_responses import PollResponsesDao
1515
from lunch_buddies.dao.teams import TeamsDao
16+
from lunch_buddies.dao.team_settings import TeamSettingsDao
1617
from lunch_buddies.dao.groups import GroupsDao
1718
from lunch_buddies.actions.auth import auth as auth_action
1819
from lunch_buddies.actions.bot import bot as bot_action, parse_raw_request
@@ -40,6 +41,7 @@
4041
sns_client = SnsClient()
4142
http_client = HttpClient()
4243
teams_dao = TeamsDao()
44+
team_settings_dao = TeamSettingsDao()
4345
polls_dao = PollsDao()
4446
poll_responses_dao = PollResponsesDao()
4547
groups_dao = GroupsDao()
@@ -166,7 +168,7 @@ def auth_http() -> str:
166168
code=request.args['code'],
167169
)
168170

169-
auth_action(request_form, teams_dao, slack_client, http_client)
171+
auth_action(request_form, teams_dao, team_settings_dao, slack_client, http_client)
170172

171173
return redirect('http://lunchbuddies.quinnweber.com/registration/')
172174

0 commit comments

Comments
 (0)