Skip to content

Commit

Permalink
Merge pull request #6 from koonwen/koonwen/get_history
Browse files Browse the repository at this point in the history
add get_history api call
  • Loading branch information
sewenthy authored Oct 3, 2024
2 parents fc45eac + a74b5b0 commit e6bc519
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module type S = sig
conversation:conversations_replies_req ->
conversations_replies_res slack_response Lwt.t

val get_history :
ctx:Context.t ->
conversation:conversations_history_req ->
conversations_history_res slack_response Lwt.t

val get_conversations_info :
ctx:Context.t ->
conversation:conversations_info_req ->
Expand Down
4 changes: 4 additions & 0 deletions lib/api_local.ml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ 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
Expand Down
23 changes: 23 additions & 0 deletions lib/api_remote.ml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ let get_replies ~(ctx : Context.t) ~(conversation : Slack_t.conversations_replie
~name:(sprintf "conversations.replies (%s, %s)" conversation.channel conversation.ts)
~ctx `GET api_path Slack_j.read_conversations_replies_res

let www_form_of_conversation_history_req (conversation : Slack_t.conversations_history_req) =
let fields =
[
Some ("channel", conversation.channel);
string_field_val conversation.cursor "cursor";
bool_field_val conversation.include_all_metadata "include_all_metadata";
bool_field_val conversation.inclusive "inclusive";
string_field_val conversation.latest "latest";
int_field_val conversation.limit "limit";
string_field_val conversation.oldest "oldest";
]
in
list_filter_opt fields

let get_history ~(ctx : Context.t) ~(conversation : Slack_t.conversations_history_req) =
log#info "getting conversations history of %s" conversation.channel;
let data = www_form_of_conversation_history_req conversation in
let args = Web.make_url_args data in
let api_path = sprintf "conversations.history?%s" args in
request_token_auth
~name:(sprintf "conversations.history (%s)" conversation.channel)
~ctx `POST api_path Slack_j.read_conversations_history_res

let www_form_of_conversations_info_req (conversation : Slack_t.conversations_info_req) =
let fields =
[
Expand Down
27 changes: 27 additions & 0 deletions lib/slack.atd
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ type conversations_replies_req <ocaml attr="deriving make"> = {
?oldest: string nullable;
}

type reactions = {
name: string;
}

(* NB:
- thread_ts is present only if it's a thread reply.
*)
Expand All @@ -212,6 +216,7 @@ type conversation_reply = {
?last_read: string nullable;
?unread_count: int nullable;
?parent_user_id: string nullable;
?reactions : reactions list nullable
}

type conversations_replies_res = {
Expand All @@ -221,6 +226,28 @@ type conversations_replies_res = {
~response_metadata <ocaml default="[]">: (string * json) list <json repr="object">;
}

type conversations_history_req <ocaml attr="deriving make"> = {
channel: string;
?cursor: string nullable;
?include_all_metadata: bool nullable;
?inclusive: bool nullable;
?latest: string nullable;
?limit: int nullable;
?oldest: string nullable;
}

type conversation_history = {
inherit conversation_reply;
?subtype : string nullable
}

type conversations_history_res = {
?messages: conversation_history list nullable;
ok: bool;
?has_more: bool nullable;
~response_metadata <ocaml default="[]">: (string * json) list <json repr="object">;
}

type user_info_req <ocaml attr="deriving make"> = {
user: string;
?include_locale: bool nullable;
Expand Down
177 changes: 177 additions & 0 deletions lib_test/slack-api-cache/C049XFXK286_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"ok": true,
"messages": [
{
"subtype": "channel_join",
"user": "U07NMN2LQUR",
"text": "<@U07NMN2LQUR> has joined the channel",
"inviter": "U07NBD2PZ36",
"type": "message",
"ts": "1727925675.995379"
},
{
"user": "U07NBD2PZ36",
"type": "message",
"ts": "1727771931.938959",
"client_msg_id": "07794752-e790-4c98-8462-663198f71444",
"text": "ds",
"team": "T07MUBTEWGP",
"thread_ts": "1727771931.938959",
"reply_count": 6,
"reply_users_count": 2,
"latest_reply": "1727940191.965199",
"reply_users": [
"U07NBD2PZ36",
"U07NMN2LQUR"
],
"is_locked": false,
"subscribed": false,
"blocks": [
{
"type": "rich_text",
"block_id": "35ic7",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "ds"
}
]
}
]
}
]
},
{
"user": "U07NBD2PZ36",
"type": "message",
"ts": "1727771579.590079",
"client_msg_id": "271b4f6d-55a1-47a2-beca-c0984e158ff0",
"text": "dse",
"team": "T07MUBTEWGP",
"blocks": [
{
"type": "rich_text",
"block_id": "ErrCA",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "dse"
}
]
}
]
}
],
"reactions": [
{
"name": "raised_hands",
"users": [
"U07NBD2PZ36"
],
"count": 1
}
]
},
{
"user": "U07NBD2PZ36",
"type": "message",
"ts": "1727771467.008679",
"client_msg_id": "0926085e-cb8d-4308-8587-ec887cc33996",
"text": "ds",
"team": "T07MUBTEWGP",
"blocks": [
{
"type": "rich_text",
"block_id": "35ic7",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "ds"
}
]
}
]
}
],
"reactions": [
{
"name": "eyes",
"users": [
"U07NBD2PZ36"
],
"count": 1
}
]
},
{
"user": "U07NBD2PZ36",
"type": "message",
"ts": "1727770954.187009",
"client_msg_id": "e8174cad-6da4-4fc8-8215-d44f1ef6127f",
"text": "test message",
"team": "T07MUBTEWGP",
"blocks": [
{
"type": "rich_text",
"block_id": "DrcyD",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "test message"
}
]
}
]
}
],
"reactions": [
{
"name": "white_check_mark",
"users": [
"U07NBD2PZ36"
],
"count": 1
}
]
},
{
"subtype": "channel_join",
"user": "U07PCFRHVGX",
"text": "<@U07PCFRHVGX> has joined the channel",
"inviter": "U07NBD2PZ36",
"type": "message",
"ts": "1727770932.264889"
},
{
"subtype": "channel_join",
"user": "U07NMN2LQUR",
"text": "<@U07NMN2LQUR> has joined the channel",
"inviter": "U07NBD2PZ36",
"type": "message",
"ts": "1727412663.419999"
},
{
"subtype": "channel_join",
"user": "U07NBD2PZ36",
"text": "<@U07NBD2PZ36> has joined the channel",
"type": "message",
"ts": "1726799277.028339"
}
],
"has_more": false,
"pin_count": 0,
"channel_actions_ts": null,
"channel_actions_count": 0
}
17 changes: 17 additions & 0 deletions lib_test/slack-api-cache/C04CXBYNC68_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"ok": true,
"latest": "1512085950.000216",
"messages": [
{
"type": "message",
"user": "U123ABC456",
"text": "I find you punny and would like to smell your nose letter",
"ts": "1512085950.000216"
}
],
"has_more": true,
"pin_count": 0,
"response_metadata": {
"next_cursor": "bmV4dF90czoxNTEyMzU2NTI2MDAwMTMw"
}
}
4 changes: 4 additions & 0 deletions lib_test/slack-api-cache/D049WPTCGMC_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ok": false,
"error": "channel_not_found"
}
81 changes: 81 additions & 0 deletions lib_test/slack_payloads.expected
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,87 @@ get_conversations_info--------channel: D049WPTCGMC--------
"channel": { "id": "D049WPTCGMC", "is_im": true, "user": "U046XN0M2R5" }
}
channel type is: direct message
get_conversations_history--------channel: C049XFXK286--------
{
"messages": [
{
"type": "message",
"ts": "1727925675.995379",
"user": "U07NMN2LQUR",
"text": "<@U07NMN2LQUR> has joined the channel",
"subtype": "channel_join"
},
{
"type": "message",
"ts": "1727771931.938959",
"thread_ts": "1727771931.938959",
"user": "U07NBD2PZ36",
"text": "ds",
"reply_count": 6,
"subscribed": false
},
{
"type": "message",
"ts": "1727771579.590079",
"user": "U07NBD2PZ36",
"text": "dse",
"reactions": [ { "name": "raised_hands" } ]
},
{
"type": "message",
"ts": "1727771467.008679",
"user": "U07NBD2PZ36",
"text": "ds",
"reactions": [ { "name": "eyes" } ]
},
{
"type": "message",
"ts": "1727770954.187009",
"user": "U07NBD2PZ36",
"text": "test message",
"reactions": [ { "name": "white_check_mark" } ]
},
{
"type": "message",
"ts": "1727770932.264889",
"user": "U07PCFRHVGX",
"text": "<@U07PCFRHVGX> has joined the channel",
"subtype": "channel_join"
},
{
"type": "message",
"ts": "1727412663.419999",
"user": "U07NMN2LQUR",
"text": "<@U07NMN2LQUR> has joined the channel",
"subtype": "channel_join"
},
{
"type": "message",
"ts": "1726799277.028339",
"user": "U07NBD2PZ36",
"text": "<@U07NBD2PZ36> has joined the channel",
"subtype": "channel_join"
}
],
"ok": true,
"has_more": false
}
get_conversations_history--------channel: C04CXBYNC68--------
{
"messages": [
{
"type": "message",
"ts": "1512085950.000216",
"user": "U123ABC456",
"text": "I find you punny and would like to smell your nose letter"
}
],
"ok": true,
"has_more": true,
"response_metadata": { "next_cursor": "bmV4dF90czoxNTEyMzU2NTI2MDAwMTMw" }
}
get_conversations_history--------channel: D049WPTCGMC--------
{ "ok": false }
get_conversations_replies--------channel: C047C6ECFNX--------
joining #C047C6ECFNX...
{
Expand Down
Loading

0 comments on commit e6bc519

Please sign in to comment.