Skip to content

Commit

Permalink
Merge pull request #51 from pm5/fix-notify
Browse files Browse the repository at this point in the history
fix: slack-notify cli
  • Loading branch information
Leonidas-from-XIV authored Jan 25, 2024
2 parents 87fbd00 + c4fe7b1 commit afc683c
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 34 deletions.
22 changes: 11 additions & 11 deletions src/lib/slacko.ml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ type channel_obj = {
creator: user;
is_archived: bool;
is_general: bool;
name_normalized: string;
is_member: bool;
members: user list;
topic: topic_obj;
Expand All @@ -273,6 +274,7 @@ type conversation_obj = {
creator: user;
is_archived: bool;
is_general: bool;
name_normalized: string;
is_member: bool;
topic: topic_obj;
purpose: topic_obj;
Expand Down Expand Up @@ -746,9 +748,9 @@ let maybe fn = function
| None -> None

(* nonpublic types for conversion in list types *)
type channels_list_obj = {
channels: channel_obj list
} [@@deriving of_yojson]
type conversations_list_obj = {
channels: conversation_obj list
} [@@deriving of_yojson { strict = false }]

type users_list_obj = {
members: user_obj list
Expand All @@ -762,15 +764,15 @@ type im_list_obj = {
ims: im_obj list;
} [@@deriving of_yojson]

let channels_list ?exclude_archived session =
api_request "channels.list"
let conversations_list ?exclude_archived session =
api_request "conversations.list"
|> optionally_add "exclude_archived" @@ maybe string_of_bool @@ exclude_archived
|> query session
>|= function
| `Json_response d ->
(match d |> channels_list_obj_of_yojson with
(match d |> conversations_list_obj_of_yojson with
| Ok x -> `Success x.channels
| Error x -> `ParseFailure x)
| Error e -> `ParseFailure e)
| #parsed_auth_error as res -> res
| _ -> `Unknown_error

Expand Down Expand Up @@ -809,11 +811,9 @@ let lookupk session (listfn : 'a listfn) filterfn k =
let id_of_channel session = function
| ChannelId id -> Lwt.return @@ `Found id
| ChannelName name ->
let base = String.sub name 1 @@ String.length name - 1 in
lookupk session channels_list (fun (x:channel_obj) -> x.name = base) @@ function
lookupk session conversations_list (fun (x:conversation_obj) -> x.name = name || x.name_normalized = name) @@ function
| [] -> `Channel_not_found
| [{id = ChannelId s; _}] -> `Found s
| [_] -> failwith "Bad result from channel id lookup."
| [{id = s; _}] -> `Found s
| _::_::_ -> failwith "Too many results from channel id lookup."

(* like id_of_channel but does not resolve names to ids *)
Expand Down
4 changes: 3 additions & 1 deletion src/lib/slacko.mli
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ type channel_obj = {
creator: user;
is_archived: bool;
is_general: bool;
name_normalized: string;
is_member: bool;
members: user list;
topic: topic_obj;
Expand All @@ -380,6 +381,7 @@ type conversation_obj = {
creator: user;
is_archived: bool;
is_general: bool;
name_normalized: string;
is_member: bool;
topic: topic_obj;
purpose: topic_obj;
Expand Down Expand Up @@ -761,7 +763,7 @@ val channels_kick: session -> channel -> user -> [ `Success | parsed_auth_error
val channels_leave: session -> channel -> [ `Success of channel_leave_obj | parsed_auth_error | channel_error | archive_error | leave_general_error | `User_is_restricted | bot_error ] Lwt.t

(** Lists all channels in a Slack team. *)
val channels_list: ?exclude_archived:bool -> session -> [ `Success of channel_obj list | parsed_auth_error ] Lwt.t
val conversations_list: ?exclude_archived:bool -> session -> [ `Success of conversation_obj list | parsed_auth_error ] Lwt.t

(** Sets the read cursor in a channel. *)
val channels_mark: session -> channel -> timestamp -> [ `Success | parsed_auth_error | channel_error | archive_error | not_in_channel_error ] Lwt.t
Expand Down
46 changes: 46 additions & 0 deletions test/abbrtypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,52 @@ let abbr_channel_obj (chan : Slacko.channel_obj) = {
type abbr_channel_obj_list = abbr_channel_obj list
[@@deriving show, yojson]

type abbr_conversation_obj = {
(* id: conversation; *)
name: string;
is_channel: bool;
created: Timestamp.t;
(* creator: user; *)
is_archived: bool;
is_general: bool;
is_member: bool;
(* members: user list; *)
topic: abbr_topic_obj;
purpose: abbr_topic_obj;
last_read: Timestamp.t option [@default None];
(* latest: json option [@default None]; *)
unread_count: int option [@default None];
unread_count_display: int option [@default None];
num_members: int option [@default None];
} [@@deriving show, yojson { strict = false }]

let abbr_conversation_obj (conversation : Slacko.conversation_obj) = {
name = conversation.Slacko.name;
is_channel = conversation.Slacko.is_channel;
created = conversation.Slacko.created;
is_archived = conversation.Slacko.is_archived;
is_general = conversation.Slacko.is_general;
is_member = conversation.Slacko.is_member;
topic = abbr_topic_obj conversation.Slacko.topic;
purpose = abbr_topic_obj conversation.Slacko.purpose;
last_read = conversation.Slacko.last_read;
unread_count = conversation.Slacko.unread_count;
unread_count_display = conversation.Slacko.unread_count_display;
num_members = conversation.Slacko.num_members;
}

type abbr_conversation_obj_list = abbr_conversation_obj list
[@@deriving show]

type abbr_conversation_list_obj = {
channels: abbr_conversation_obj list
} [@@deriving show, yojson { strict = false }]

let abbr_conversation_obj_list_of_yojson json =
match abbr_conversation_list_obj_of_yojson json with
| Ok obj -> Ok obj.channels
| (Error _) as err -> err

type abbr_message_obj = {
type': string [@key "type"];
ts: Timestamp.t;
Expand Down
119 changes: 119 additions & 0 deletions test/conversations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"channels": [
{
"id": "C3XTJPLFL",
"name": "archivable_channel",
"name_normalized": "archivable_channel",
"is_channel": true,
"created": 1485679967,
"creator": "U3UMJU868",
"is_archived": false,
"is_general": false,
"is_member": true,
"topic": {
"value": "",
"creator": "",
"last_set": 0
},
"purpose": {
"value": "",
"creator": "",
"last_set": 0
},
"previous_names": [],
"num_members": 1
},
{
"id": "C3XTHDCTC",
"name": "archived_channel",
"name_normalized": "archived_channel",
"is_channel": true,
"created": 1485678562,
"creator": "U3UMJU868",
"is_archived": true,
"is_general": false,
"is_member": false,
"topic": {
"value": "",
"creator": "",
"last_set": 0
},
"purpose": {
"value": "",
"creator": "",
"last_set": 0
},
"previous_names": [],
"num_members": 0
},
{
"id": "C3UK9TS3C",
"name": "general",
"name_normalized": "general",
"is_channel": true,
"created": 1484993283,
"creator": "U3UMJU868",
"is_archived": false,
"is_general": true,
"is_member": true,
"topic": {
"value": "Company-wide announcements and work-based matters",
"creator": "",
"last_set": 0
},
"purpose": {
"value": "This channel is for team-wide communication and announcements. All team members are in this channel.",
"creator": "",
"last_set": 0
},
"previous_names": [],
"num_members": 1
},
{
"id": "C3TTWNCTA",
"name": "random",
"name_normalized": "random",
"is_channel": true,
"created": 1484993283,
"creator": "U3UMJU868",
"is_archived": false,
"is_general": false,
"is_member": true,
"topic": {
"value": "Non-work banter and water cooler conversation",
"creator": "",
"last_set": 0
},
"purpose": {
"value": "A place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber you'd prefer to keep out of more focused work-related channels.",
"creator": "",
"last_set": 0
},
"previous_names": [],
"num_members": 1
},
{
"id": "C3V9V3E9L",
"name": "slackobot",
"name_normalized": "slackobot",
"is_channel": true,
"created": 1484993366,
"creator": "U3UMJU868",
"is_archived": false,
"is_general": false,
"is_member": true,
"topic": {
"value": "",
"creator": "",
"last_set": 0
},
"purpose": {
"value": "Testing slackobot.",
"creator": "U3UMJU868",
"last_set": 1484993366
},
"previous_names": [],
"num_members": 1
}
]
}
9 changes: 7 additions & 2 deletions test/fake_slack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let im_slackbot = "D3UMJU8VA"


let channels_json = Yojson.Safe.from_file "channels.json"
let conversations_json = Yojson.Safe.from_file "conversations.json"
let new_channel_json = Yojson.Safe.from_file "new_channel.json"
let authed_json = Yojson.Safe.from_file "authed.json"
let random_history_json = Yojson.Safe.from_file "random_history.json"
Expand Down Expand Up @@ -101,8 +102,8 @@ let channels_archive req _body =

let channels_create req _body =
match get_arg "name" req with
| "#general" | "#random" -> reply_err "name_taken" []
| "#new_channel" | _ -> reply_ok ["channel", new_channel_json]
| "general" | "random" -> reply_err "name_taken" []
| "new_channel" | _ -> reply_ok ["channel", new_channel_json]

let channels_history req _body =
(* TODO: Check various filtering params. *)
Expand Down Expand Up @@ -141,6 +142,9 @@ let users_list _req _body =
(* TODO: Check presence param. *)
reply_ok (json_fields users_json)

let conversations_list _req _body =
reply_ok (json_fields conversations_json)

(* Dispatcher, etc. *)

let server ?(port=7357) ~stop () =
Expand All @@ -158,6 +162,7 @@ let server ?(port=7357) ~stop () =
| "/api/im.history" -> check_auth im_history
| "/api/im.list" -> check_auth im_list
| "/api/users.list" -> check_auth users_list
| "/api/conversations.list" -> check_auth conversations_list
| _ -> bad_path
in
handler req body
Expand Down
1 change: 1 addition & 0 deletions test/new_channel.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"U3UMJU868"
],
"name": "new_channel",
"name_normalized": "new_channel",
"previous_names": [],
"purpose": {
"creator": "",
Expand Down
Loading

0 comments on commit afc683c

Please sign in to comment.