Skip to content

Commit 0946b97

Browse files
authored
Merge pull request #49 from swdotcom/migrate-auth-endpoints
migrate auth endpoints and remove local integrations
2 parents fad3ae7 + 458f6b6 commit 0946b97

File tree

7 files changed

+36
-269
lines changed

7 files changed

+36
-269
lines changed

Default.sublime-commands

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
{
1919
"command": "show_tree_view",
20-
"caption": "Code Time: More Options"
20+
"caption": "Code Time: Menu"
2121
},
2222
{
2323
"command": "switch_account",

Main.sublime-menu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"command": "show_tree_view",
30-
"caption": "More Options"
30+
"caption": "Menu"
3131
},
3232
{
3333
"command": "switch_account",

lib/CommonUtil.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,6 @@ def getSoftwareDirName():
173173
def sublime_variables(view):
174174
view.window().extract_variables()
175175

176-
def getIntegrationsFile():
177-
file = getSoftwareDir(True)
178-
return os.path.join(file, 'integrations.json')
179-
180-
def getIntegrations():
181-
try:
182-
with open(getIntegrationsFile(), encoding='utf-8') as integrationsFile:
183-
loadedIntegrationsFile = json.load(integrationsFile)
184-
return loadedIntegrationsFile
185-
except Exception:
186-
return []
187-
188-
def syncIntegrations(integrations_data):
189-
content = json.dumps(integrations_data)
190-
191-
integrationsFile = getIntegrationsFile()
192-
with open(integrationsFile, 'w') as f:
193-
f.write(content)
194-
195176
def getWebUrl():
196177
return getValue("software_dashboard_url", "https://app.software.com")
197178

lib/SlackManager.py

Lines changed: 18 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -19,222 +19,39 @@
1919
pendingCallback = None
2020

2121
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 []
2531

2632
def hasSlackWorkspaces():
2733
return True if len(getSlackWorkspaces()) > 0 else False
2834

2935
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()
4537

4638
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()
5940

41+
t = Timer(10, refetchSlackConnectStatusLazily, [40])
42+
t.start()
6043

61-
api_endpoint = getApiEndpoint()
44+
def launchSlackSettings():
6245
scheme = "https"
63-
if('localhost' in api_endpoint):
46+
if('localhost' in getApiEndpoint()):
6447
scheme = "http"
65-
url = scheme + "://" + api_endpoint + "/auth/slack?" + urlencode(params)
48+
url = scheme + "://" + getWebUrl() + "data_sources/integration_types/slack"
6649
webbrowser.open(url)
6750

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-
14951
#######################################################################################
15052
# PRIVATE METHODS
15153
#######################################################################################
15254

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-
23855
def refetchSlackConnectStatusLazily(try_count=40):
23956
foundSlackAuth = getSlackAuth()
24057
if (foundSlackAuth is False):
@@ -249,12 +66,8 @@ def refetchSlackConnectStatusLazily(try_count=40):
24966
sublime.message_dialog("Successfully connected to Slack")
25067

25168
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)
25770

258-
return foundNewIntegration
71+
return hasSlackWorkspaces()
25972

26073

lib/SoftwareHttp.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
lastMsg = None
1010
windowView = None
1111
version = None
12+
currentUser = None
1213

1314
def toggleStatus():
1415
global lastMsg
@@ -170,17 +171,24 @@ def createAnonymousUser():
170171
logIt("Code Time: Unable to complete anonymous user creation: %s" % ex)
171172
return None
172173

173-
def getUser():
174+
def getUser(refreshUser=True):
175+
global currentUser
176+
177+
if refreshUser is False and currentUser is not None:
178+
return currentUser
179+
174180
jwt = getItem("jwt")
175181
if (jwt):
176182
api = "/users/me"
177183
response = requestIt("GET", api, None, jwt)
178184
if (isResponseOk(response)):
179185
try:
180186
responseObj = json.loads(response.read().decode('utf-8'))
181-
user = responseObj.get("data", None)
182-
return user
187+
currentUser = responseObj.get("data", None)
188+
return currentUser
183189
except Exception as ex:
184190
logIt("Code Time: Unable to retrieve user: %s" % ex)
185191
return None
186192

193+
194+

0 commit comments

Comments
 (0)