-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_local.ml
175 lines (146 loc) · 8.26 KB
/
api_local.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
(** Module for mocking test requests to slack--will output on Stdio *)
open Common
open Printf
let cwd = Sys.getcwd ()
let cache_dir = Filename.concat cwd "slack-api-cache"
(** return the file with a function f applied unless the file is empty;
empty file:this is needed to simulate 404 returns from github *)
let with_cache_file url f =
match get_local_file url with
| "" -> Lwt.return_error (`Other "empty file")
| file -> Lwt.return_ok (f file)
| exception Slack_lib_error e -> Lwt.return_error (`Other e)
let default_post_message_res : Slack_t.post_message_res = { channel = "SOME_RETURN_POST_CHANNEL_ID"; ts = "SOME_TS" }
let default_update_message_res : Slack_t.update_message_res =
{ channel = "SOME_RETURN_UPDATE_CHANNEL_ID"; ts = "SOME_TS" }
let default_files_res : Slack_t.file =
{
id = "SOME_FILE_ID";
name = None;
title = None;
mimetype = None;
filetype = None;
pretty_type = None;
user = None;
size = None;
channels = [];
ims = [];
groups = [];
permalink = None;
permalink_public = None;
}
let default_files_upload_res : Slack_t.files_upload_res = { ok = true; file = default_files_res }
let send_message ~ctx:_ ~msg =
let json = msg |> Slack_j.string_of_post_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
printf "will notify #%s\n" msg.channel;
printf "%s\n" json;
Lwt.return_ok { default_post_message_res with channel = msg.channel }
let send_message_webhook ~ctx:_ ~url ~msg =
let json = msg |> Slack_j.string_of_post_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
printf "will notify #%s\n" url;
printf "%s\n" json;
Lwt.return_ok ()
let update_message ~ctx:_ ~msg =
let json = msg |> Slack_j.string_of_update_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
printf "will update #%s at %s \n" msg.channel msg.ts;
printf "%s\n" json;
Lwt.return_ok { default_update_message_res with channel = msg.channel }
let upload_file ~ctx:_ ~file =
let json = file |> Slack_j.string_of_files_upload_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
match file.channels with
| Some channels ->
printf "will update #%s\n" channels;
printf "%s\n" json;
Lwt.return_ok { default_files_upload_res with file = { default_files_res with channels = [ channels ] } }
| None -> Lwt.return_error (`Other "invalid file upload")
let join_conversation ~ctx:_ ~(channel : Slack_t.conversations_join_req) =
printf "joining #%s...\n" channel.channel;
let url = Filename.concat cache_dir (sprintf "%s_join" channel.channel) in
with_cache_file url Slack_j.conversations_join_res_of_string
let send_chat_unfurl ~ctx:_ ~(req : Slack_t.chat_unfurl_req) =
let data = req |> Slack_j.string_of_chat_unfurl_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
printf "will unfurl in #%s\n" req.channel;
printf "%s\n" data;
Lwt.return_ok ()
let update_usergroup_users ~ctx:_ ~(usergroup : Slack_t.update_usergroups_users_req) =
printf "updating #%s...\n" usergroup.usergroup;
let url = Filename.concat cache_dir (sprintf "%s_usergroup_users" usergroup.usergroup) in
with_cache_file url Slack_j.update_usergroups_users_res_of_string
let get_replies ~ctx:_ ~(conversation : Slack_t.conversations_replies_req) =
let url = Filename.concat cache_dir (sprintf "%s_%s_replies" conversation.channel conversation.ts) in
with_cache_file url Slack_j.conversations_replies_res_of_string
let get_history ~ctx:_ ~(conversation : Slack_t.conversations_history_req) =
let url = Filename.concat cache_dir (sprintf "%s_history" conversation.channel) in
with_cache_file url Slack_j.conversations_history_res_of_string
let get_conversations_info ~ctx:_ ~(conversation : Slack_t.conversations_info_req) =
let url = Filename.concat cache_dir conversation.channel in
with_cache_file url Slack_j.conversations_info_res_of_string
let get_user ~ctx:_ ~(user : Slack_t.user_info_req) =
let url = Filename.concat cache_dir user.user in
with_cache_file url Slack_j.user_info_res_of_string
let list_usergroups ~ctx:_ ~(req : Slack_t.list_usergroups_req) =
let url = Filename.concat cache_dir (sprintf "%s_usergroups_list" @@ Option.get req.team_id) in
with_cache_file url Slack_j.list_usergroups_res_of_string
let list_usergroup_users ~ctx:_ ~(usergroup : Slack_t.list_usergroup_users_req) =
printf "listing #%s...\n" usergroup.usergroup;
let url = Filename.concat cache_dir (sprintf "%s_list_usergroup_users" usergroup.usergroup) in
with_cache_file url Slack_j.list_usergroup_users_res_of_string
let list_users ~ctx:_ ~(req : Slack_t.list_users_req) =
printf "listing at cursor #%s...\n" @@ Option.get req.cursor;
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 open_views ~ctx:_ ~(req : Slack_t.open_views_req) =
match req.trigger_id, req.interactivity_pointer with
| Some _, Some _ -> Lwt.return_error (`Other "Both trigger_id and interactivity_pointer found")
| None, None -> Lwt.return_error (`Other "Both trigger_id and interactivity_pointer not found")
| Some id, None ->
printf "opening views at trigger_id #%s...\n" id;
let url = Filename.concat cache_dir (sprintf "%s_open_views" id) in
with_cache_file url Slack_j.open_views_res_of_string
| None, Some ptr ->
printf "opening views at interactivity_pointer #%s...\n" ptr;
let url = Filename.concat cache_dir (sprintf "%s_open_views" ptr) in
with_cache_file url Slack_j.open_views_res_of_string
let push_views ~ctx:_ ~(req : Slack_t.push_views_req) =
match req.trigger_id, req.interactivity_pointer with
| Some _, Some _ -> Lwt.return_error (`Other "Both trigger_id and interactivity_pointer found")
| None, None -> Lwt.return_error (`Other "Both trigger_id and interactivity_pointer not found")
| Some id, None ->
printf "pushing views at trigger_id #%s...\n" id;
let url = Filename.concat cache_dir (sprintf "%s_push_views" id) in
with_cache_file url Slack_j.push_views_res_of_string
| None, Some ptr ->
printf "pushing views at interactivity_pointer #%s...\n" ptr;
let url = Filename.concat cache_dir (sprintf "%s_push_views" ptr) in
with_cache_file url Slack_j.push_views_res_of_string
let update_views ~ctx:_ ~(req : Slack_t.update_views_req) =
match req.external_id, req.view_id with
| Some _, Some _ -> Lwt.return_error (`Other "Both external_id and view_id found")
| None, None -> Lwt.return_error (`Other "Both external_id and view_id not found")
| Some id, None ->
printf "updating views at external_id #%s...\n" id;
let url = Filename.concat cache_dir (sprintf "%s_update_views" id) in
with_cache_file url Slack_j.update_views_res_of_string
| None, Some ptr ->
printf "updating views at view_id #%s...\n" ptr;
let url = Filename.concat cache_dir (sprintf "%s_update_views" ptr) in
with_cache_file url Slack_j.update_views_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)