| 1 | use crate::urls; |
| 2 | |
| 3 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 4 | pub enum MessageTarget { |
| 5 | Chat { |
| 6 | chat_id: String, |
| 7 | message_id: String, |
| 8 | }, |
| 9 | /// `root_id` is `Some` when `message_id` is a reply in the thread of `root_id`. |
| 10 | Channel { |
| 11 | team_id: String, |
| 12 | channel_id: String, |
| 13 | message_id: String, |
| 14 | root_id: Option<String>, |
| 15 | }, |
| 16 | } |
| 17 | |
| 18 | impl MessageTarget { |
| 19 | pub fn chat(chat_id: &str, message_id: &str) -> Self { |
| 20 | MessageTarget::Chat { |
| 21 | chat_id: chat_id.to_owned(), |
| 22 | message_id: message_id.to_owned(), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn channel( |
| 27 | team_id: &str, |
| 28 | channel_id: &str, |
| 29 | message_id: &str, |
| 30 | root_id: Option<&str>, |
| 31 | ) -> Self { |
| 32 | MessageTarget::Channel { |
| 33 | team_id: team_id.to_owned(), |
| 34 | channel_id: channel_id.to_owned(), |
| 35 | message_id: message_id.to_owned(), |
| 36 | root_id: root_id.map(str::to_owned), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn message_id(&self) -> &str { |
| 41 | match self { |
| 42 | MessageTarget::Chat { message_id, .. } | MessageTarget::Channel { message_id, .. } => { |
| 43 | message_id |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub(crate) fn url(&self) -> String { |
| 49 | match self { |
| 50 | MessageTarget::Chat { |
| 51 | chat_id, |
| 52 | message_id, |
| 53 | } => urls::chat_message(chat_id, message_id), |
| 54 | MessageTarget::Channel { |
| 55 | team_id, |
| 56 | channel_id, |
| 57 | message_id, |
| 58 | root_id: None, |
| 59 | } => urls::channel_message(team_id, channel_id, message_id), |
| 60 | MessageTarget::Channel { |
| 61 | team_id, |
| 62 | channel_id, |
| 63 | message_id, |
| 64 | root_id: Some(root_id), |
| 65 | } => urls::channel_reply(team_id, channel_id, root_id, message_id), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub(crate) fn soft_delete_url(&self, user_id: &str) -> String { |
| 70 | match self { |
| 71 | MessageTarget::Chat { |
| 72 | chat_id, |
| 73 | message_id, |
| 74 | } => urls::soft_delete(user_id, chat_id, message_id), |
| 75 | channel => format!("{}/softDelete", channel.url()), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub(crate) fn reaction_url(&self, action: &str) -> String { |
| 80 | format!("{}/{action}", self.url()) |
| 81 | } |
| 82 | |
| 83 | pub(crate) fn react_scope(&self) -> &'static str { |
| 84 | match self { |
| 85 | MessageTarget::Chat { .. } => "Chat.ReadWrite", |
| 86 | MessageTarget::Channel { .. } => "ChannelMessage.Send", |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub(crate) fn write_scope(&self) -> &'static str { |
| 91 | match self { |
| 92 | MessageTarget::Chat { .. } => "Chat.ReadWrite", |
| 93 | MessageTarget::Channel { .. } => "ChannelMessage.ReadWrite", |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[cfg(test)] |
| 99 | mod tests { |
| 100 | use super::*; |
| 101 | |
| 102 | #[test] |
| 103 | fn channel_urls_cover_root_and_reply() { |
| 104 | let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None); |
| 105 | let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1")); |
| 106 | assert_eq!( |
| 107 | root.url(), |
| 108 | "https://graph.microsoft.com/v1.0/teams/t/channels/19%3Ac%40thread.tacv2/messages/m1" |
| 109 | ); |
| 110 | assert_eq!( |
| 111 | reply.reaction_url("setReaction"), |
| 112 | "https://graph.microsoft.com/v1.0/teams/t/channels/19%3Ac%40thread.tacv2/messages/m1/replies/r1/setReaction" |
| 113 | ); |
| 114 | assert!( |
| 115 | root.soft_delete_url("u") |
| 116 | .ends_with("/messages/m1/softDelete") |
| 117 | ); |
| 118 | assert!(!root.soft_delete_url("u").contains("/users/")); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn chat_soft_delete_goes_through_the_user() { |
| 123 | let chat = MessageTarget::chat("19:a@thread.v2", "m1"); |
| 124 | assert_eq!( |
| 125 | chat.soft_delete_url("u1"), |
| 126 | "https://graph.microsoft.com/v1.0/users/u1/chats/19%3Aa%40thread.v2/messages/m1/softDelete" |
| 127 | ); |
| 128 | assert!( |
| 129 | chat.reaction_url("unsetReaction") |
| 130 | .ends_with("/messages/m1/unsetReaction") |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn scopes_follow_the_documented_permissions() { |
| 136 | let channel = MessageTarget::channel("t", "c", "m", None); |
| 137 | assert_eq!(channel.react_scope(), "ChannelMessage.Send"); |
| 138 | assert_eq!(channel.write_scope(), "ChannelMessage.ReadWrite"); |
| 139 | assert_eq!( |
| 140 | MessageTarget::chat("c", "m").write_scope(), |
| 141 | "Chat.ReadWrite" |
| 142 | ); |
| 143 | } |
| 144 | } |