Skip to content

Commit f978a03

Browse files
author
Martin Jambon
committed
Make sure to clear Slack credentials in the absence of a bot token in
another spot.
1 parent 96cba34 commit f978a03

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

slack_ws.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ let connect_team esper_uid =
4343
(* Ensure this gets started right away, in order to reserve
4444
the entry in the Slack_ws_conn.connections table. *)
4545
Slack_ws_conn.keep_connected
46+
esper_uid
4647
slack_teamid
4748
(fun () ->
4849
logf `Debug "Create input handler";

slack_ws_conn.ml

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -232,27 +232,30 @@ type connection_result =
232232
| Giveup
233233
| Connected of connection
234234

235-
let create_connection slack_teamid input_handler handle_permanent_failure =
235+
let create_connection uid slack_teamid input_handler handle_permanent_failure =
236236
logf `Debug "Create websocket connection for Slack team %s"
237237
(Slack_api_teamid.to_string slack_teamid);
238238
catch
239239
(fun () ->
240-
Slack_user.get_bot_access_token slack_teamid >>= fun access_token ->
241-
Slack_api.rtm_start access_token >>= fun resp ->
242-
match Slack_util.extract_result resp with
240+
Slack_user.get_bot_access_token_opt ~uid slack_teamid >>= function
243241
| None ->
244-
return Retry
245-
| Some x ->
246-
let ws_url = x.Slack_api_t.url in
247-
let waiting_for_pong = ref None in
248-
create_websocket_connection ws_url
249-
input_handler waiting_for_pong >>= fun send ->
250-
let conn = {
251-
conn_id = slack_teamid;
252-
send;
253-
waiting_for_pong;
254-
} in
255-
return (Connected conn)
242+
return Giveup
243+
| Some access_token ->
244+
Slack_api.rtm_start access_token >>= fun resp ->
245+
match Slack_util.extract_result resp with
246+
| None ->
247+
return Retry
248+
| Some x ->
249+
let ws_url = x.Slack_api_t.url in
250+
let waiting_for_pong = ref None in
251+
create_websocket_connection ws_url
252+
input_handler waiting_for_pong >>= fun send ->
253+
let conn = {
254+
conn_id = slack_teamid;
255+
send;
256+
waiting_for_pong;
257+
} in
258+
return (Connected conn)
256259
)
257260
(fun e ->
258261
if is_permanent_failure e then (
@@ -268,6 +271,7 @@ let get_connection slack_teamid =
268271
with Not_found -> None
269272

270273
let obtain_connection
274+
uid
271275
slack_teamid
272276
create_input_handler
273277
handle_permanent_failure =
@@ -278,6 +282,7 @@ let obtain_connection
278282
| None ->
279283
let input_handler = create_input_handler () in
280284
create_connection
285+
uid
281286
slack_teamid
282287
input_handler
283288
handle_permanent_failure >>= fun result ->
@@ -410,7 +415,8 @@ let rec check_connection_until_failure slack_teamid =
410415
still works, and create a new connection if the previous one
411416
is no longer usable, and so on.
412417
*)
413-
let keep_connected slack_teamid create_input_handler handle_permanent_failure =
418+
let keep_connected
419+
uid slack_teamid create_input_handler handle_permanent_failure =
414420
if connection_existed slack_teamid then (
415421
(* assume that another keep_connected job already exists *)
416422
interrupt_sleeper slack_teamid;
@@ -423,7 +429,7 @@ let keep_connected slack_teamid create_input_handler handle_permanent_failure =
423429
let connect () =
424430
Apputil_error.catch_and_report "Slack WS keep_connected"
425431
(fun () ->
426-
obtain_connection slack_teamid
432+
obtain_connection uid slack_teamid
427433
create_input_handler
428434
handle_permanent_failure
429435
>>= function
@@ -474,6 +480,7 @@ let keep_connected slack_teamid create_input_handler handle_permanent_failure =
474480
*)
475481
let test () =
476482
keep_connected
483+
(Uid.make ())
477484
(Slack_api_teamid.of_string "T19BY0R8X")
478485
(fun () send s ->
479486
Printf.printf "Received %S\n%!" s;

0 commit comments

Comments
 (0)