Skip to content

Commit 8220710

Browse files
committed
slack: don't unfurl own msg links
1 parent fa92c93 commit 8220710

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/action.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,23 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
238238
Lwt.return_unit
239239

240240
let process_link_shared_event (ctx : Context.t) (event : Slack_t.link_shared_event) =
241+
let fetch_bot_user_id () =
242+
match%lwt Slack_api.send_auth_test ~ctx () with
243+
| Ok { user_id; _ } ->
244+
ctx.state.bot_user_id <- Some user_id;
245+
let%lwt () =
246+
Option.value_map ctx.state_filepath ~default:Lwt.return_unit ~f:(fun path ->
247+
match%lwt State.save ctx.state path with
248+
| Ok () -> Lwt.return_unit
249+
| Error msg ->
250+
log#error "%s" msg;
251+
Lwt.return_unit)
252+
in
253+
Lwt.return_some user_id
254+
| Error msg ->
255+
log#error "%s" msg;
256+
Lwt.return_none
257+
in
241258
let process link =
242259
match Github.gh_link_of_string link with
243260
| None -> Lwt.return_none
@@ -259,7 +276,14 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
259276
| Ok commit -> Lwt.return_some @@ (link, Slack_message.populate_commit repo commit)
260277
)
261278
in
279+
let%lwt bot_user_id =
280+
match ctx.state.bot_user_id with
281+
| Some id -> Lwt.return_some id
282+
| None -> fetch_bot_user_id ()
283+
in
262284
if List.length event.links > 2 then Lwt.return "ignored: more than two links present"
285+
else if Option.value_map bot_user_id ~default:false ~f:(String.equal event.user) then
286+
Lwt.return "ignored: is bot user"
263287
else begin
264288
let links = List.map event.links ~f:(fun l -> l.url) in
265289
let%lwt unfurls = List.map links ~f:process |> Lwt.all |> Lwt.map List.filter_opt |> Lwt.map StringMap.of_list in

lib/state.atd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ type pipeline_statuses = branch_statuses map_as_object
1111

1212
(* The serializable runtime state of the bot *)
1313
type state = {
14-
pipeline_statuses <ocaml mutable>: pipeline_statuses
14+
pipeline_statuses <ocaml mutable>: pipeline_statuses;
15+
?bot_user_id <ocaml mutable>: string nullable;
1516
}

lib/state.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open Base
22
open Common
33
open Devkit
44

5-
let empty : State_t.state = { pipeline_statuses = StringMap.empty }
5+
let empty : State_t.state = { pipeline_statuses = StringMap.empty; bot_user_id = None }
66

77
let refresh_pipeline_status (state : State_t.state) ~pipeline ~(branches : Github_t.branch list) ~status =
88
let update_pipeline_status branch_statuses =

0 commit comments

Comments
 (0)