BlueGit

ict rusty-teams

Files

rusty-teams / crates / graph / src / target.rs

target.rs

target.rs 5.0 KiB Download Raw
1use crate::urls;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub 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
18impl 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 undo_soft_delete_url(&self, user_id: &str) -> String {
80 match self {
81 MessageTarget::Chat {
82 chat_id,
83 message_id,
84 } => urls::undo_soft_delete(user_id, chat_id, message_id),
85 channel => format!("{}/undoSoftDelete", channel.url()),
86 }
87 }
88
89 pub(crate) fn reaction_url(&self, action: &str) -> String {
90 format!("{}/{action}", self.url())
91 }
92
93 pub(crate) fn react_scope(&self) -> &'static str {
94 match self {
95 MessageTarget::Chat { .. } => "Chat.ReadWrite",
96 MessageTarget::Channel { .. } => "ChannelMessage.Send",
97 }
98 }
99
100 pub(crate) fn write_scope(&self) -> &'static str {
101 match self {
102 MessageTarget::Chat { .. } => "Chat.ReadWrite",
103 MessageTarget::Channel { .. } => "ChannelMessage.ReadWrite",
104 }
105 }
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn channel_urls_cover_root_and_reply() {
114 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
115 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
116 assert_eq!(
117 root.url(),
118 "https://graph.microsoft.com/v1.0/teams/t/channels/19%3Ac%40thread.tacv2/messages/m1"
119 );
120 assert_eq!(
121 reply.reaction_url("setReaction"),
122 "https://graph.microsoft.com/v1.0/teams/t/channels/19%3Ac%40thread.tacv2/messages/m1/replies/r1/setReaction"
123 );
124 assert!(
125 root.soft_delete_url("u")
126 .ends_with("/messages/m1/softDelete")
127 );
128 assert!(!root.soft_delete_url("u").contains("/users/"));
129 }
130
131 #[test]
132 fn chat_soft_delete_goes_through_the_user() {
133 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
134 assert_eq!(
135 chat.soft_delete_url("u1"),
136 "https://graph.microsoft.com/v1.0/users/u1/chats/19%3Aa%40thread.v2/messages/m1/softDelete"
137 );
138 assert_eq!(
139 chat.undo_soft_delete_url("u1"),
140 "https://graph.microsoft.com/v1.0/users/u1/chats/19%3Aa%40thread.v2/messages/m1/undoSoftDelete"
141 );
142 assert!(
143 MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"))
144 .undo_soft_delete_url("u")
145 .ends_with("/messages/m1/replies/r1/undoSoftDelete")
146 );
147 assert!(
148 chat.reaction_url("unsetReaction")
149 .ends_with("/messages/m1/unsetReaction")
150 );
151 }
152
153 #[test]
154 fn scopes_follow_the_documented_permissions() {
155 let channel = MessageTarget::channel("t", "c", "m", None);
156 assert_eq!(channel.react_scope(), "ChannelMessage.Send");
157 assert_eq!(channel.write_scope(), "ChannelMessage.ReadWrite");
158 assert_eq!(
159 MessageTarget::chat("c", "m").write_scope(),
160 "Chat.ReadWrite"
161 );
162 }
163}