19
19
pendingCallback = None
20
20
21
21
def getSlackWorkspaces ():
22
- integrations = getIntegrations ()
23
- workspaces = [x for x in integrations if (x ['name' ].lower () == 'slack' and x ['status' ].lower () == 'active' )]
24
- return workspaces if workspaces is not None and len (workspaces ) > 0 else []
22
+ currentUser = getUser (False )
23
+
24
+ if (currentUser ):
25
+ integrations = currentUser .get ("integration_connections" , [])
26
+
27
+ workspaces = [x for x in integrations if (x ['integration_type_id' ] == 14 and x ['status' ].lower () == 'active' )]
28
+ return workspaces if workspaces is not None and len (workspaces ) > 0 else []
29
+
30
+ return []
25
31
26
32
def hasSlackWorkspaces ():
27
33
return True if len (getSlackWorkspaces ()) > 0 else False
28
34
29
35
def disconnectSlackWorkspace ():
30
- result = checkSlackConnection (False )
31
- if (result is False ):
32
- # show a prompt there are no slack workspaces to disconnect
33
- sublime .message_dialog ("No Slack workspaces found to disconnect" )
34
-
35
- # set the pending callback
36
- global pendingCallback
37
- pendingCallback = disconnectSlackWorkspaceCallback
38
- showSlackWorkspaceSelection ()
39
-
40
- def disconnectSlackWorkspaceCallback (workspace ):
41
- if (workspace is not None ):
42
- removeSlackIntegration (workspace ['authId' ])
43
- else :
44
- clearPendingCallback ()
36
+ launchSlackSettings ()
45
37
46
38
def connectSlackWorkspace ():
47
- is_registered = checkRegistration (True )
48
- if (is_registered is False ):
49
- return
50
-
51
- params = {}
52
- params ["plugin" ] = getPluginType ()
53
- params ["plugin_uuid" ] = getPluginUuid ()
54
- params ["pluginVersion" ] = getVersion ()
55
- params ["plugin_id" ] = getPluginId ()
56
- params ["auth_callback_state" ] = getAuthCallbackState ()
57
- params ["integrate" ] = "slack"
58
- params ["plugin_token" ] = getItem ("jwt" )
39
+ launchSlackSettings ()
59
40
41
+ t = Timer (10 , refetchSlackConnectStatusLazily , [40 ])
42
+ t .start ()
60
43
61
- api_endpoint = getApiEndpoint ()
44
+ def launchSlackSettings ():
62
45
scheme = "https"
63
- if ('localhost' in api_endpoint ):
46
+ if ('localhost' in getApiEndpoint () ):
64
47
scheme = "http"
65
- url = scheme + "://" + api_endpoint + "/auth /slack?" + urlencode ( params )
48
+ url = scheme + "://" + getWebUrl () + "data_sources/integration_types /slack"
66
49
webbrowser .open (url )
67
50
68
- t = Timer (10 , refetchSlackConnectStatusLazily , [40 ])
69
- t .start ()
70
-
71
- def getSlackDnDInfo ():
72
- workspaces = getSlackWorkspaces ()
73
- for i in range (len (workspaces )):
74
- workspace = workspaces [i ]
75
- resp = api_call ('dnd.info' , {'token' : workspace ["access_token" ]})
76
- if (resp ['ok' ] is True ):
77
- # return the 1st one
78
- return resp
79
-
80
- return None
81
-
82
- def getSlackStatus ():
83
- workspaces = getSlackWorkspaces ()
84
- for i in range (len (workspaces )):
85
- workspace = workspaces [i ]
86
- resp = api_call ('users.profile.get' , {'token' : workspace ["access_token" ]})
87
- if (resp ['ok' ] is True ):
88
- # return the 1st one
89
- return resp
90
-
91
- return None
92
-
93
- def getSlackPresence ():
94
- workspaces = getSlackWorkspaces ()
95
- for i in range (len (workspaces )):
96
- workspace = workspaces [i ]
97
- resp = api_call ('users.getPresence' , {'token' : workspace ["access_token" ]})
98
- if (resp ['ok' ] is True ):
99
- # return the 1st one
100
- return resp
101
-
102
- return None
103
-
104
- # accepted states: "auto" or "away"
105
- def toggleSlackPresence (state ):
106
- is_registered = checkRegistration (True )
107
- if (is_registered is False ):
108
- return
109
-
110
- is_connected = checkSlackConnection (True )
111
- if (is_connected is False ):
112
- return
113
-
114
- updated = False
115
- workspaces = getSlackWorkspaces ()
116
- for i in range (len (workspaces )):
117
- workspace = workspaces [i ]
118
- resp = api_call ('users.setPresence' , {'token' : workspace ["access_token" ], 'presence' : state })
119
- if (resp ['ok' ] is True ):
120
- updated = True
121
-
122
- if (updated is True ):
123
- sublime .message_dialog ("Slack presence updated" )
124
-
125
- def updateSlackStatusText (message ):
126
- updated = False
127
- workspaces = getSlackWorkspaces ()
128
- for i in range (len (workspaces )):
129
- workspace = workspaces [i ]
130
- resp = api_call ('users.profile.set' , {'token' : workspace ["access_token" ], 'profile' : {'status_text' : message , 'status_emoji' : "" , 'status_expiration' : 0 }})
131
- if (resp ['ok' ] is True ):
132
- updated = True
133
-
134
- if (updated is True ):
135
- sublime .message_dialog ("Slack status message updated" )
136
-
137
- def clearSlackStatusText ():
138
- updated = False
139
- workspaces = getSlackWorkspaces ()
140
- for i in range (len (workspaces )):
141
- workspace = workspaces [i ]
142
- resp = api_call ('users.profile.set' , {'token' : workspace ["access_token" ], 'profile' : {'status_text' : "" , 'status_emoji' : "" }})
143
- if (resp ['ok' ] is True ):
144
- updated = True
145
-
146
- if (updated is True ):
147
- sublime .message_dialog ("Slack status message cleared" )
148
-
149
51
#######################################################################################
150
52
# PRIVATE METHODS
151
53
#######################################################################################
152
54
153
- # done
154
- def showSlackWorkspaceSelection ():
155
- workspaces = getSlackWorkspaces ()
156
- # create the options
157
- options = []
158
- for i in range (len (workspaces )):
159
- workspace = workspaces [i ]
160
- options .append (workspace ['team_domain' ] + " (" + workspace ['team_name' ] + ")" )
161
-
162
- # show a prompt of which workspace to get the access token from
163
- sublime .active_window ().show_quick_panel (options , showSlackWorkspaceSelectionHandler )
164
-
165
- # done
166
- def showSlackWorkspaceSelectionHandler (result_idx ):
167
- # -1 means nothing was selected
168
- if (result_idx == - 1 ):
169
- global pendingCallback
170
- pendingCallback = None
171
- return
172
-
173
- workspaces = getSlackWorkspaces ()
174
- if (len (workspaces ) > result_idx ):
175
- # perform the waiting callback
176
- pendingCallback (workspaces [result_idx ])
177
- else :
178
- clearPendingCallback ()
179
-
180
- # done
181
- def checkSlackConnection (show_connect = True ):
182
- if (hasSlackWorkspaces () is False ):
183
- clearPendingCallback ()
184
- if (show_connect is True ):
185
- # show the prompt
186
- options = ['Connect a Slack workspace to continue' , 'Not now' ]
187
- sublime .active_window ().show_quick_panel (options , connectSlackPromptHandler )
188
- return False
189
- else :
190
- return True
191
-
192
- # done
193
- def connectSlackPromptHandler (result_idx ):
194
- # zero means they've selected to connect slack
195
- if (result_idx != 0 ):
196
- clearPendingCallback ()
197
- else :
198
- # connect
199
- connectSlackWorkspace ()
200
-
201
- # done
202
- def removeSlackIntegration (auth_id ):
203
- new_workspaces = [x for x in getSlackWorkspaces () if (x ['authId' ] != auth_id )]
204
- syncIntegrations (new_workspaces )
205
- clearPendingCallback ()
206
-
207
- def clearPendingCallback ():
208
- global pendingCallback
209
- pendingCallback = None
210
-
211
- def checkRegistration (show_signup = True ):
212
- name = getItem ("name" )
213
- if (name is None ):
214
- clearPendingCallback ()
215
- if (show_signup is True ):
216
- # show the signup confirm
217
- options = ['Connecting Slack requires a registered account. Sign up or log in to continue.' , 'Not now' ]
218
- sublime .active_window ().show_quick_panel (options , signupPromptHandler )
219
- return False
220
-
221
- return True
222
-
223
- def signupPromptHandler (result_idx ):
224
- # zero means they've selected to sign up
225
- if (result_idx == 0 ):
226
- # show the sign up flow
227
- signupOptions = ['Google' , 'GitHub' , 'Email' ]
228
- sublime .active_window ().show_quick_panel (signupOptions , authSelectionHandler )
229
-
230
- def authSelectionHandler (result_idx ):
231
- if (result_idx == 0 ):
232
- launchLoginUrl ('google' , False )
233
- elif (result_idx == 1 ):
234
- launchLoginUrl ('github' , False )
235
- elif (result_idx == 2 ):
236
- launchLoginUrl ('software' , False )
237
-
238
55
def refetchSlackConnectStatusLazily (try_count = 40 ):
239
56
foundSlackAuth = getSlackAuth ()
240
57
if (foundSlackAuth is False ):
@@ -249,12 +66,8 @@ def refetchSlackConnectStatusLazily(try_count=40):
249
66
sublime .message_dialog ("Successfully connected to Slack" )
250
67
251
68
def getSlackAuth ():
252
- foundNewIntegration = False
253
- userState = getUserRegistrationState (True )
254
-
255
- if (userState ["user" ] is not None ):
256
- foundNewIntegration = updateSlackIntegrationsFromUser (userState ["user" ])
69
+ curentUser = getUser (True )
257
70
258
- return foundNewIntegration
71
+ return hasSlackWorkspaces ()
259
72
260
73
0 commit comments