Skip to content

Commit 892458d

Browse files
committed
add support for slack users.list method
1 parent 0dc6c1d commit 892458d

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

lib/api.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module type Slack = sig
2525
unit ->
2626
lookup_user_res slack_response Lwt.t
2727

28+
val list_users : ?cursor:string -> ?limit:int -> ctx:Context.t -> unit -> list_users_res slack_response Lwt.t
2829
val send_notification : ctx:Context.t -> msg:post_message_req -> unit slack_response Lwt.t
2930

3031
val send_chat_unfurl

lib/api_local.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Filename = Stdlib.Filename
55
module Sys = Stdlib.Sys
66

77
let cwd = Sys.getcwd ()
8-
let cache_dir = Filename.concat cwd "github-api-cache"
8+
let github_cache_dir = Filename.concat cwd "github-api-cache"
9+
let slack_cache_dir = Filename.concat cwd "slack-api-cache"
910

1011
(** return the file with a function f applied unless the file is empty;
1112
empty file:this is needed to simulate 404 returns from github *)
@@ -29,7 +30,7 @@ and its Github_j.<kind>_of_string function.
2930
NB: please save the cache file in the same format *)
3031
let get_repo_member_cache ~(repo : Github_t.repository) ~kind ~ref_ ~of_string =
3132
let file = clean_forward_slashes (sprintf "%s_%s_%s" repo.full_name kind ref_) in
32-
let url = Filename.concat cache_dir file in
33+
let url = Filename.concat github_cache_dir file in
3334
with_cache_file url of_string
3435

3536
module Github : Api.Github = struct
@@ -56,6 +57,7 @@ end
5657
(** The base implementation for local check payload debugging and mocking tests *)
5758
module Slack_base : Api.Slack = struct
5859
let lookup_user ?cache:_ ~ctx:_ ~cfg:_ ~email:_ () = Lwt.return @@ Error "undefined for local setup"
60+
let list_users ?cursor:_ ?limit:_ ~ctx:_ () = Lwt.return @@ Error "undefined for local setup"
5961
let send_notification ~ctx:_ ~msg:_ = Lwt.return @@ Error "undefined for local setup"
6062
let send_chat_unfurl ~ctx:_ ~channel:_ ~ts:_ ~unfurls:_ () = Lwt.return @@ Error "undefined for local setup"
6163
let send_auth_test ~ctx:_ () = Lwt.return @@ Error "undefined for local setup"
@@ -72,11 +74,16 @@ module Slack : Api.Slack = struct
7274
Slack_t.id = sprintf "id[%s]" email;
7375
name = sprintf "name[%s]" email;
7476
real_name = sprintf "real_name[%s]" email;
77+
profile = { email = Some email };
7578
}
7679
in
7780
let mock_response = { Slack_t.user = mock_user } in
7881
Lwt.return @@ Ok mock_response
7982

83+
let list_users ?cursor:_ ?limit:_ ~ctx:_ () =
84+
let url = Filename.concat slack_cache_dir "users-list" in
85+
with_cache_file url Slack_j.list_users_res_of_string
86+
8087
let send_notification ~ctx:_ ~msg =
8188
let json = msg |> Slack_j.string_of_post_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
8289
Printf.printf "will notify #%s\n" msg.channel;

lib/api_remote.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ module Slack : Api.Slack = struct
149149
| Some user -> Lwt.return_ok user
150150
| None -> lookup_user' ~ctx ~cfg ~email ()
151151

152+
let list_users ?cursor ?limit ~(ctx : Context.t) () =
153+
let cursor_option = Option.map (fun c -> "cursor", c) cursor in
154+
let limit_option = Option.map (fun l -> "limit", Int.to_string l) limit in
155+
let url_args = Web.make_url_args @@ List.filter_map id [ cursor_option; limit_option ] in
156+
request_token_auth ~name:"list users" ~ctx `GET (sprintf "users.list?%s" url_args) Slack_j.read_list_users_res
157+
152158
(** [send_notification ctx msg] notifies [msg.channel] with the payload [msg];
153159
uses web API with access token if available, or with webhook otherwise *)
154160
let send_notification ~(ctx : Context.t) ~(msg : Slack_t.post_message_req) =

lib/slack.atd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,19 @@ type lookup_user_res = {
7474
user: user;
7575
}
7676

77+
type profile = {
78+
?email: string nullable
79+
}
80+
7781
type user = {
7882
id: string;
7983
name: string;
8084
real_name: string;
85+
profile: profile
86+
}
87+
88+
type list_users_res = {
89+
members: user list;
8190
}
8291

8392
type link_shared_link = {

0 commit comments

Comments
 (0)