Skip to content

Commit 381d10a

Browse files
committed
refactor mentee claim/unclaim to class based message
1 parent 114c8a5 commit 381d10a

3 files changed

Lines changed: 125 additions & 68 deletions

File tree

pybot/endpoints/slack/actions/mentor_request.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import json
2+
import logging
23

34
from sirbot import SirBot
45
from slack import methods
56
from slack.actions import Action
67

7-
from pybot.endpoints.slack.message_templates.mentor_request import MentorRequest
8-
from pybot.endpoints.slack.utils.action_messages import base_response, mentor_details_dialog, \
9-
mentee_unclaimed_attachment, mentee_claimed_attachment
8+
from pybot.endpoints.slack.message_templates.mentor_request import MentorRequest, MentorRequestClaim
9+
from pybot.endpoints.slack.utils.action_messages import mentor_details_dialog
10+
11+
logger = logging.getLogger(__name__)
1012

1113

1214
async def mentor_request_submit(action: Action, app: SirBot):
@@ -123,34 +125,19 @@ async def claim_mentee(action: Action, app: SirBot):
123125
slack = app.plugins['slack'].api
124126
airtable = app.plugins['airtable'].api
125127

126-
update_airtable = True
127-
clicker_id = action['user']['id']
128-
request_record = action['actions'][0]['name']
129-
click_type = action['actions'][0]['value']
130-
131-
response = base_response(action)
132-
133-
user_info = await slack.query(methods.USERS_INFO, dict(user=clicker_id))
134-
clicker_email = user_info['user']['profile']['email']
128+
event = MentorRequestClaim(action, slack, airtable)
129+
if event.is_claim():
130+
user_info = await slack.query(methods.USERS_INFO, {'user': event.clicker})
131+
clicker_email = user_info['user']['profile']['email']
135132

136-
if click_type == 'mentee_claimed':
137-
records = await airtable.find_records(table_name='Mentors', field='Email', value=clicker_email)
138-
mentor_id = records[0]['id'] if records else ''
133+
mentor_records = await airtable.find_records(table_name='Mentors', field='Email', value=clicker_email)
134+
mentor_id = mentor_records[0]['id'] if mentor_records else False
139135

140-
if mentor_id:
141-
attachment = mentee_claimed_attachment(clicker_id, request_record)
142-
else:
143-
update_airtable = False
144-
attachment = action['original_message']['attachments']
145-
attachment[0]['text'] = f":warning: <@{clicker_id}>'s slack Email not found in Mentor table. :warning:"
136+
await event.claim_request(mentor_id)
146137
else:
147-
mentor_id = ''
148-
attachment = mentee_unclaimed_attachment(clicker_id, request_record)
138+
await event.unclaim_request()
149139

150-
response['attachments'] = attachment
140+
await event.update_message()
151141

152-
await slack.query(methods.CHAT_UPDATE, response)
153-
if update_airtable:
154-
await airtable.update_request(request_record, mentor_id)
155-
except Exception as ex:
156-
print(ex)
142+
except Exception:
143+
logger.exception('Exception while updating claim')

pybot/endpoints/slack/message_templates/mentor_request.py

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from typing import MutableMapping, Optional
2+
13
from slack import methods
24
from slack.actions import Action
35
from slack.io.abc import SlackAPI
46

7+
from pybot.endpoints.slack.utils.action_messages import now
58
from pybot.plugins.airtable.api import AirtableAPI
69

710
SERVICE_INDEX = 0
@@ -18,6 +21,12 @@ def __init__(self, action: Action, channel=None):
1821
self.action = action
1922
self.channel = channel
2023

24+
def __getitem__(self, item):
25+
return self.action[item]
26+
27+
def __setitem__(self, key, value):
28+
self.action[key] = value
29+
2130
@classmethod
2231
def selected_option(cls, attachment):
2332
action = attachment['actions'][0]
@@ -27,7 +36,7 @@ def selected_option(cls, attachment):
2736

2837
@property
2938
def attachments(self):
30-
return self.action['attachments']
39+
return self['attachments']
3140

3241
@property
3342
def service(self):
@@ -91,7 +100,7 @@ def details(self, new_details):
91100
def update_params(self):
92101
return {
93102
'channel': self.channel,
94-
'ts': self.action['ts'],
103+
'ts': self['ts'],
95104
'attachments': self.attachments
96105
}
97106

@@ -139,11 +148,108 @@ def submission_complete(self, slack: SlackAPI):
139148
done_attachment['text'] = 'Request submitted successfully!'
140149
done_attachment['actions'] = [{'type': 'button', 'text': 'Dismiss', 'name': 'cancel', 'value': 'cancel'}]
141150

142-
self.action['attachments'] = [done_attachment]
151+
self['attachments'] = [done_attachment]
143152
return self.update(slack)
144153

145154
def clear_skillsets(self):
146155
self.attachments[SKILLSET_INDEX]['text'] = ''
147156

148157
def update(self, slack: SlackAPI):
149158
return slack.query(methods.CHAT_UPDATE, self.update_params)
159+
160+
161+
class MentorRequestClaim(Action):
162+
163+
def __init__(self, raw_action: MutableMapping, slack: SlackAPI, airtable: AirtableAPI):
164+
super().__init__(raw_action)
165+
self.slack = slack
166+
self.airtable = airtable
167+
self.attachment = raw_action['original_message']['attachments'][0]
168+
self.should_update = True
169+
170+
@property
171+
def trigger(self) -> dict:
172+
return self['actions'][0]
173+
174+
@property
175+
def click_type(self) -> str:
176+
return self.trigger['value']
177+
178+
def is_claim(self):
179+
return self.click_type == 'mentee_claimed'
180+
181+
@property
182+
def record(self) -> str:
183+
""" Airtable record ID for the mentor request """
184+
return self.trigger['name']
185+
186+
@property
187+
def clicker(self):
188+
return self['user']['id']
189+
190+
@property
191+
def attachment(self):
192+
return self['original_message']['attachments'][0]
193+
194+
@attachment.setter
195+
def attachment(self, value):
196+
self['original_message']['attachments'][0] = value
197+
198+
def claim_request(self, mentor_record):
199+
if mentor_record:
200+
self.attachment = self.mentee_claimed_attachment()
201+
else:
202+
self.attachment['text'] = f":warning: <@{self.clicker}>'s slack Email not found in Mentor table. :warning:"
203+
self.should_update = False
204+
205+
return self.update_airtable(mentor_record)
206+
207+
def unclaim_request(self):
208+
self.attachment = self.mentee_unclaimed_attachment()
209+
return self.update_airtable('')
210+
211+
async def update_airtable(self, mentor_id: Optional[str]):
212+
if mentor_id:
213+
return self.airtable.update_request(self.record, mentor_id)
214+
215+
async def update_message(self):
216+
response = {
217+
'channel': self['channel']['id'],
218+
'ts': self['message_ts'],
219+
'attachments': self['original_message']['attachments']
220+
}
221+
await self.slack.query(methods.CHAT_UPDATE, response)
222+
223+
def mentee_claimed_attachment(self) -> dict:
224+
return {
225+
"text": f":100: Request claimed by <@{self.clicker}>:100:\n"
226+
f"<!date^{now()}^Claimed at {{date_num}} {{time_secs}}|Failed to parse time>",
227+
"fallback": "",
228+
"color": "#3AA3E3",
229+
"callback_id": "claim_mentee",
230+
"attachment_type": "default",
231+
"actions": [{
232+
'name': f'{self.record}',
233+
"text": f"Reset claim",
234+
"type": "button",
235+
"style": "danger",
236+
"value": "reset_claim_mentee",
237+
}]
238+
}
239+
240+
def mentee_unclaimed_attachment(self) -> dict:
241+
return {
242+
'text': f"Reset by <@{self.clicker}> at"
243+
f" <!date^{now()}^ {{date_num}} {{time_secs}}|Failed to parse time>",
244+
'fallback': '',
245+
'color': '#3AA3E3',
246+
'callback_id': 'claim_mentee',
247+
'attachment_type': 'default',
248+
'actions': [{
249+
'name': f'{self.record}',
250+
'text': 'Claim Mentee',
251+
'type': 'button',
252+
'style': 'primary',
253+
'value': 'mentee_claimed'
254+
}]
255+
}

pybot/endpoints/slack/utils/action_messages.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -250,42 +250,6 @@ def mentor_details_dialog(action):
250250
}
251251

252252

253-
def mentee_claimed_attachment(user_id: str, record: str) -> List[dict]:
254-
return [{
255-
"text": f":100: Request claimed by <@{user_id}>:100:\n"
256-
f"<!date^{now()}^Claimed at {{date_num}} {{time_secs}}|Failed to parse time>",
257-
"fallback": "",
258-
"color": "#3AA3E3",
259-
"callback_id": "claim_mentee",
260-
"attachment_type": "default",
261-
"actions": [{
262-
'name': f'{record}',
263-
"text": f"Reset claim",
264-
"type": "button",
265-
"style": "danger",
266-
"value": "reset_claim_mentee",
267-
}]
268-
}]
269-
270-
271-
def mentee_unclaimed_attachment(user_id: str, record: str) -> List[dict]:
272-
return [{
273-
'text': f"Reset by <@{user_id}> at"
274-
f" <!date^{now()}^ {{date_num}} {{time_secs}}|Failed to parse time>",
275-
'fallback': '',
276-
'color': '#3AA3E3',
277-
'callback_id': 'claim_mentee',
278-
'attachment_type': 'default',
279-
'actions': [{
280-
'name': f'{record}',
281-
'text': 'Claim Mentee',
282-
'type': 'button',
283-
'style': 'primary',
284-
'value': 'mentee_claimed'
285-
}]
286-
}]
287-
288-
289253
def new_suggestion_text(user_id: str, suggestion: str) -> str:
290254
return f":exclamation:<@{user_id}> just submitted a suggestion for a help topic:exclamation:\n-- {suggestion}"
291255

0 commit comments

Comments
 (0)