Skip to content

Commit

Permalink
add bookmarks apis
Browse files Browse the repository at this point in the history
  • Loading branch information
koonwen committed Nov 8, 2024
1 parent 17b632e commit bec9bf9
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ module type S = sig
val get_user : ctx:Context.t -> user:user_info_req -> user_info_res slack_response Lwt.t
val list_users : ctx:Context.t -> req:list_users_req -> list_users_res slack_response Lwt.t

(* bookmarks *)
val add_bookmark : ctx:Context.t -> req:add_bookmark_req -> add_bookmark_res slack_response Lwt.t
val edit_bookmark : ctx:Context.t -> req:edit_bookmark_req -> edit_bookmark_res slack_response Lwt.t
val list_bookmarks : ctx:Context.t -> req:list_bookmarks_req -> list_bookmarks_res slack_response Lwt.t
val remove_bookmark : ctx:Context.t -> req:remove_bookmark_req -> remove_bookmark_res slack_response Lwt.t

(* misc *)
val send_auth_test : ctx:Context.t -> unit -> auth_test_res slack_response Lwt.t
end
20 changes: 20 additions & 0 deletions lib/api_local.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ let list_users ~ctx:_ ~(req : Slack_t.list_users_req) =
let url = Filename.concat cache_dir (sprintf "%s_list_users" @@ Option.get req.cursor) in
with_cache_file url Slack_j.list_users_res_of_string

let add_bookmark ~ctx:_ ~(req : Slack_t.add_bookmark_req) =
printf "adding bookmark (title %s, type %s) to channel_id #%s...\n" req.title req.type_ req.channel_id;
let url = Filename.concat cache_dir (sprintf "%s_%s_%s_add_bookmark" req.channel_id req.title req.type_) in
with_cache_file url Slack_j.add_bookmark_res_of_string

let edit_bookmark ~ctx:_ ~(req : Slack_t.edit_bookmark_req) =
printf "editting bookmark %s at channel_id #%s...\n" req.bookmark_id req.channel_id;
let url = Filename.concat cache_dir (sprintf "%s_%s_edit_bookmark" req.bookmark_id req.channel_id) in
with_cache_file url Slack_j.edit_bookmark_res_of_string

let list_bookmarks ~ctx:_ ~(req : Slack_t.list_bookmarks_req) =
printf "listing bookmarks at channel_id #%s...\n" req.channel_id;
let url = Filename.concat cache_dir (sprintf "%s_list_bookmarks" req.channel_id) in
with_cache_file url Slack_j.list_bookmarks_res_of_string

let remove_bookmark ~ctx:_ ~(req : Slack_t.remove_bookmark_req) =
printf "removing bookmark %s at channel_id #%s...\n" req.bookmark_id req.channel_id;
let url = Filename.concat cache_dir (sprintf "%s_%s_remove_bookmark" req.bookmark_id req.channel_id) in
with_cache_file url Slack_j.remove_bookmark_res_of_string

let send_auth_test ~ctx:_ () =
Lwt.return
@@ Ok ({ url = ""; team = ""; user = ""; team_id = ""; user_id = "test_slack_user" } : Slack_t.auth_test_res)
38 changes: 37 additions & 1 deletion lib/api_remote.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ let upload_file ~(ctx : Context.t) ~(file : Slack_t.files_upload_req) =
~name:(sprintf "file.upload (%s)" @@ Option.default "<NO CHANNEL>" file.channels)
~body `POST "files.upload" Slack_j.read_files_upload_res

(** [join_conversation ctx channel ] will join the token owner
(** [join_conversation ctx channel ] will join the token owner
[ctx.secrets.slack_access_token] to the [channel]. *)
let join_conversation ~(ctx : Context.t) ~(channel : Slack_t.conversations_join_req) =
log#info "joining channel %s" channel.channel;
Expand Down Expand Up @@ -284,5 +284,41 @@ let list_users ~(ctx : Context.t) ~(req : Slack_t.list_users_req) =
~name:(sprintf "users.list (%s)" @@ Slack_j.string_of_list_users_req req)
~ctx `GET api_path Slack_j.read_list_users_res

let add_bookmark ~(ctx : Context.t) ~(req : Slack_t.add_bookmark_req) =
log#info "adding bookmark %s" @@ Slack_j.string_of_add_bookmark_req req;
let data = Slack_j.string_of_add_bookmark_req req in
let body = `Raw ("application/json", data) in
let api_path = sprintf "bookmark.add" in
request_token_auth
~name:(sprintf "bookmark.add (%s)" @@ Slack_j.string_of_add_bookmark_req req)
~ctx ~body `POST api_path Slack_j.read_add_bookmark_res

let edit_bookmark ~(ctx : Context.t) ~(req : Slack_t.edit_bookmark_req) =
log#info "editting bookmark %s" @@ Slack_j.string_of_edit_bookmark_req req;
let data = Slack_j.string_of_edit_bookmark_req req in
let body = `Raw ("application/json", data) in
let api_path = sprintf "bookmark.edit" in
request_token_auth
~name:(sprintf "bookmark.edit (%s)" @@ Slack_j.string_of_edit_bookmark_req req)
~ctx ~body `POST api_path Slack_j.read_edit_bookmark_res

let list_bookmarks ~(ctx : Context.t) ~(req : Slack_t.list_bookmarks_req) =
log#info "getting bookmarks %s" @@ Slack_j.string_of_list_bookmarks_req req;
let data = Slack_j.string_of_list_bookmarks_req req in
let body = `Raw ("application/json", data) in
let api_path = sprintf "bookmarks.list" in
request_token_auth
~name:(sprintf "bookmarks.list (%s)" @@ Slack_j.string_of_list_bookmarks_req req)
~ctx ~body `POST api_path Slack_j.read_list_bookmarks_res

let remove_bookmark ~(ctx : Context.t) ~(req : Slack_t.remove_bookmark_req) =
log#info "removing bookmark %s" @@ Slack_j.string_of_remove_bookmark_req req;
let data = Slack_j.string_of_remove_bookmark_req req in
let body = `Raw ("application/json", data) in
let api_path = sprintf "bookmark.remove" in
request_token_auth
~name:(sprintf "bookmark.remove (%s)" @@ Slack_j.string_of_remove_bookmark_req req)
~ctx ~body `POST api_path Slack_j.read_remove_bookmark_res

let send_auth_test ~(ctx : Context.t) () =
request_token_auth ~name:"retrieve bot information" ~ctx `POST "auth.test" Slack_j.read_auth_test_res
54 changes: 54 additions & 0 deletions lib/slack.atd
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,60 @@ type list_usergroup_users_res = {
users : string list;
}

type add_bookmark_req <ocaml attr="deriving make"> = {
channel_id: string;
title: string;
type_ <json name = "type"> : string;
?emoji : string nullable;
?entity_id : string nullable;
?link : string nullable;
?parent_id : string nullable
}

type add_bookmark_res = {
ok: bool;
bookmark: bookmark
}

type edit_bookmark_req <ocaml attr="deriving make"> = {
channel_id: string;
bookmark_id: string;
?emoji : string nullable;
?link : string nullable;
?title : string nullable
}

type edit_bookmark_res = {
ok: bool;
bookmark: bookmark
}

type list_bookmarks_req <ocaml attr="deriving make"> = {
channel_id: string;
}

type list_bookmarks_res = {
ok: bool;
bookmarks: bookmark list
}

type remove_bookmark_req <ocaml attr="deriving make"> = {
channel_id: string;
bookmark_id: string;
?quip_section_id: string nullable
}

type remove_bookmark_res = {
ok: bool;
}

type bookmark = {
id: string;
channel_id: string;
title: string;
link: string;
}

type unfurl_block = {
blocks: message_block list;
}
Expand Down
20 changes: 20 additions & 0 deletions lib_test/slack-api-cache/C1RQ000_Bk033XFJ9BTJ_edit_bookmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"ok": true,
"bookmark": {
"id": "Bk033XFJ9BTJ",
"channel_id": "C1RQ000",
"title": "bookmark-1",
"link": "https://google.com",
"emoji": ":clap:",
"icon_url": "https://www.google.com/favicon.ico",
"type": "link",
"entity_id": null,
"date_created": 1644956055,
"date_updated": 0,
"rank": "g",
"last_updated_by_user_id": "U0334B6G6G5",
"last_updated_by_team_id": "T018DF03GHY",
"shortcut_id": null,
"app_id": null
}
}
3 changes: 3 additions & 0 deletions lib_test/slack-api-cache/C1RQ000_Bk033XFJ9BTJ_remove_bookmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ok": true
}
20 changes: 20 additions & 0 deletions lib_test/slack-api-cache/C1RQ000__edit_bookmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"ok": true,
"bookmark": {
"id": "Bk033XFJ9BTJ",
"channel_id": "C1RQ000",
"title": "bookmark-1",
"link": "https://google.com",
"emoji": ":clap:",
"icon_url": "https://www.google.com/favicon.ico",
"type": "link",
"entity_id": null,
"date_created": 1644956055,
"date_updated": 0,
"rank": "g",
"last_updated_by_user_id": "U0334B6G6G5",
"last_updated_by_team_id": "T018DF03GHY",
"shortcut_id": null,
"app_id": null
}
}
20 changes: 20 additions & 0 deletions lib_test/slack-api-cache/C1RQ000_bookmark-1_link_add_bookmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"ok": true,
"bookmark": {
"id": "Bk033XFJ9BTJ",
"channel_id": "C1RQ000",
"title": "bookmark-1",
"link": "https://google.com",
"emoji": ":clap:",
"icon_url": "https://www.google.com/favicon.ico",
"type": "link",
"entity_id": null,
"date_created": 1644956055,
"date_updated": 0,
"rank": "g",
"last_updated_by_user_id": "U0334B6G6G5",
"last_updated_by_team_id": "T018DF03GHY",
"shortcut_id": null,
"app_id": null
}
}
22 changes: 22 additions & 0 deletions lib_test/slack-api-cache/C1RQ000_list_bookmarks
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"ok": true,
"bookmarks": [
{
"id": "Bk123ABC4DEF",
"channel_id": "C1RQ000",
"title": "bookmark-1",
"link": "https://google.com",
"emoji": ":clap:",
"icon_url": "https://www.google.com/favicon.ico",
"type": "link",
"entity_id": null,
"date_created": 1644956055,
"date_updated": 0,
"rank": "g",
"last_updated_by_user_id": "U0123A1B1C1",
"last_updated_by_team_id": "T012AB34CDE",
"shortcut_id": null,
"app_id": null
}
]
}
38 changes: 38 additions & 0 deletions lib_test/slack_payloads.expected
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,44 @@ listing at cursor #dXNlcjpVMDQ2WE4wTTJSNQ==...
],
"response_metadata": { "next_cursor": "" }
}
add_bookmark_to_channel_id_of--------channel_id: C1RQ000--------
adding bookmark (title bookmark-1, type link) to channel_id #C1RQ000...
{
"ok": true,
"bookmark": {
"id": "Bk033XFJ9BTJ",
"channel_id": "C1RQ000",
"title": "bookmark-1",
"link": "https://google.com"
}
}
edit_bookmark_to_channel_id_of--------channel_id: Bk033XFJ9BTJ--------
editting bookmark C1RQ000 at channel_id #Bk033XFJ9BTJ...
{
"ok": true,
"bookmark": {
"id": "Bk033XFJ9BTJ",
"channel_id": "C1RQ000",
"title": "bookmark-1",
"link": "https://google.com"
}
}
list_bookmarks_to_channel_id_of--------channel_id: C1RQ000--------
listing bookmarks at channel_id #C1RQ000...
{
"ok": true,
"bookmarks": [
{
"id": "Bk123ABC4DEF",
"channel_id": "C1RQ000",
"title": "bookmark-1",
"link": "https://google.com"
}
]
}
remove_bookmark_to_channel_id_of--------channel_id: Bk033XFJ9BTJ--------
removing bookmark C1RQ000 at channel_id #Bk033XFJ9BTJ...
{ "ok": true }
===== file mock-slack-events/link_shared.json =====
{
"token": "",
Expand Down
73 changes: 73 additions & 0 deletions lib_test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,75 @@ let process_list_users cursor =
log#error "failed to get users at cursor %s: %s" cursor (Slack_j.string_of_slack_api_error e);
Lwt.return_unit

let add_bookmarks_list = [ "C1RQ000", "bookmark-1", "link" ]

let process_add_bookmark (channel_id, title, type_) =
Printf.printf "add_bookmark_to_channel_id_of--------channel_id: %s--------\n" channel_id;
let ctx = Context.empty_ctx () in
let req : Slack_t.add_bookmark_req = Slack_j.make_add_bookmark_req ~channel_id ~title ~type_ () in
match%lwt Api.add_bookmark ~ctx ~req with
| Ok res ->
let json = res |> Slack_j.string_of_add_bookmark_res |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
print_endline json;
Lwt.return_unit
| Error e ->
log#error "failed to add bookmark (channel %s, title %s, type %s): %s" channel_id title type_
(Slack_j.string_of_slack_api_error e);
Lwt.return_unit

let edit_bookmarks_id_list = [ "C1RQ000", "Bk033XFJ9BTJ" ]

let process_edit_bookmark (bookmark_id, channel_id) =
Printf.printf "edit_bookmark_to_channel_id_of--------channel_id: %s--------\n" channel_id;
let ctx = Context.empty_ctx () in
let req : Slack_t.edit_bookmark_req = Slack_j.make_edit_bookmark_req ~channel_id ~bookmark_id () in
match%lwt Api.edit_bookmark ~ctx ~req with
| Ok res ->
let json =
res |> Slack_j.string_of_edit_bookmark_res |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string
in
print_endline json;
Lwt.return_unit
| Error e ->
log#error "failed to edit bookmark (bookmark %s, channel %s): %s" bookmark_id channel_id
(Slack_j.string_of_slack_api_error e);
Lwt.return_unit

let list_bookmarks_id_list = [ "C1RQ000" ]

let process_list_bookmarks channel_id =
Printf.printf "list_bookmarks_to_channel_id_of--------channel_id: %s--------\n" channel_id;
let ctx = Context.empty_ctx () in
let req : Slack_t.list_bookmarks_req = Slack_j.make_list_bookmarks_req ~channel_id in
match%lwt Api.list_bookmarks ~ctx ~req with
| Ok res ->
let json =
res |> Slack_j.string_of_list_bookmarks_res |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string
in
print_endline json;
Lwt.return_unit
| Error e ->
log#error "failed to list bookmarks in channel %s: %s" channel_id (Slack_j.string_of_slack_api_error e);
Lwt.return_unit

let remove_bookmarks_id_list = [ "C1RQ000", "Bk033XFJ9BTJ" ]

let process_remove_bookmark (bookmark_id, channel_id) =
Printf.printf "remove_bookmark_to_channel_id_of--------channel_id: %s--------\n" channel_id;
let ctx = Context.empty_ctx () in
let req : Slack_t.remove_bookmark_req = Slack_j.make_remove_bookmark_req ~channel_id ~bookmark_id () in
match%lwt Api.remove_bookmark ~ctx ~req with
| Ok res ->
let json =
res |> Slack_j.string_of_remove_bookmark_res |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string
in
print_endline json;
Lwt.return_unit
| Error e ->
log#error "failed to remove bookmark (bookmark %s, channel %s): %s" bookmark_id channel_id
(Slack_j.string_of_slack_api_error e);
Lwt.return_unit

let mock_slack_event_dir = "mock-slack-events"

let get_mock_slack_events () =
Expand Down Expand Up @@ -303,6 +372,10 @@ let () =
let%lwt () = Lwt_list.iter_s process_list_usergroups list_usergroups_team_id_list in
let%lwt () = Lwt_list.iter_s process_list_usergroup_users list_usergroup_users_usergroup_id_list in
let%lwt () = Lwt_list.iter_s process_list_users list_users_cursors_list in
let%lwt () = Lwt_list.iter_s process_add_bookmark add_bookmarks_list in
let%lwt () = Lwt_list.iter_s process_edit_bookmark edit_bookmarks_id_list in
let%lwt () = Lwt_list.iter_s process_list_bookmarks list_bookmarks_id_list in
let%lwt () = Lwt_list.iter_s process_remove_bookmark remove_bookmarks_id_list in
let%lwt () = Lwt_list.iter_s process_events slack_events in
let%lwt () = Lwt_list.iter_s process_interactions slack_interactions in
Lwt.return_unit
Expand Down

0 comments on commit bec9bf9

Please sign in to comment.