@@ -238,6 +238,23 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
238
238
Lwt. return_unit
239
239
240
240
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#warn " failed to save state file %s : %s" path msg;
251
+ Lwt. return_unit)
252
+ in
253
+ Lwt. return_some user_id
254
+ | Error msg ->
255
+ log#warn " failed to query slack auth.test : %s" msg;
256
+ Lwt. return_none
257
+ in
241
258
let process link =
242
259
match Github. gh_link_of_string link with
243
260
| None -> Lwt. return_none
@@ -259,7 +276,14 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
259
276
| Ok commit -> Lwt. return_some @@ (link, Slack_message. populate_commit repo commit)
260
277
)
261
278
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
262
284
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"
263
287
else begin
264
288
let links = List. map event.links ~f: (fun l -> l.url) in
265
289
let % lwt unfurls = List. map links ~f: process |> Lwt. all |> Lwt. map List. filter_opt |> Lwt. map StringMap. of_list in
0 commit comments