1+ from typing import MutableMapping , Optional
2+
13from slack import methods
24from slack .actions import Action
35from slack .io .abc import SlackAPI
46
7+ from pybot .endpoints .slack .utils .action_messages import now
58from pybot .plugins .airtable .api import AirtableAPI
69
710SERVICE_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+ }
0 commit comments