BlueGit

ict rusty-teams

Files

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

writes.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
writes.rs 6.1 KiB Download Raw
1use serde_json::{Value, json};
2use session::{GRAPH, Method, Scope};
3
4use crate::client::Graph;
5use crate::error::Result;
6use crate::models::{Chat, Message};
7use crate::outgoing::{OutgoingMention, message_body};
8use crate::target::MessageTarget;
9use crate::urls;
10
11fn conversation_member(user_id: &str) -> Value {
12 json!({
13 "@odata.type": "#microsoft.graph.aadUserConversationMember",
14 "roles": ["owner"],
15 "user@odata.bind": format!("{GRAPH}/v1.0/users('{user_id}')"),
16 })
17}
18
19impl Graph {
20 pub async fn send_chat_message(
21 &self,
22 chat_id: &str,
23 html: &str,
24 mentions: &[OutgoingMention],
25 ) -> Result<Message> {
26 self.write_for_message(
27 Method::Post,
28 &urls::chat_message_collection(chat_id),
29 "Chat.ReadWrite",
30 Some(message_body(html, mentions)),
31 )
32 .await
33 }
34
35 pub async fn send_channel_message(
36 &self,
37 team_id: &str,
38 channel_id: &str,
39 html: &str,
40 subject: Option<&str>,
41 mentions: &[OutgoingMention],
42 ) -> Result<Message> {
43 let mut body = message_body(html, mentions);
44 if let Some(subject) = subject.filter(|subject| !subject.is_empty()) {
45 body["subject"] = json!(subject);
46 }
47 self.write_for_message(
48 Method::Post,
49 &urls::channel_post(team_id, channel_id),
50 "ChannelMessage.Send",
51 Some(body),
52 )
53 .await
54 }
55
56 pub async fn reply_to_channel_message(
57 &self,
58 team_id: &str,
59 channel_id: &str,
60 message_id: &str,
61 html: &str,
62 mentions: &[OutgoingMention],
63 ) -> Result<Message> {
64 self.write_for_message(
65 Method::Post,
66 &urls::channel_reply_post(team_id, channel_id, message_id),
67 "ChannelMessage.Send",
68 Some(message_body(html, mentions)),
69 )
70 .await
71 }
72
73 /// Beta API `chats/{id}/messages/replyWithQuote`; Teams renders the quote from the `messageReference` attachment.
74 pub async fn reply_with_quote(
75 &self,
76 chat_id: &str,
77 quoted_message_id: &str,
78 html: &str,
79 mentions: &[OutgoingMention],
80 ) -> Result<Message> {
81 let body = json!({
82 "messageIds": [quoted_message_id],
83 "replyMessage": message_body(html, mentions),
84 });
85 self.write_for_message(
86 Method::Post,
87 &urls::reply_with_quote(chat_id),
88 "Chat.ReadWrite",
89 Some(body),
90 )
91 .await
92 }
93
94 pub async fn mark_chat_read(
95 &self,
96 chat_id: &str,
97 user_id: &str,
98 tenant_id: &str,
99 ) -> Result<()> {
100 let body = json!({"user": {"id": user_id, "tenantId": tenant_id}});
101 self.write(
102 Method::Post,
103 &urls::mark_chat_read(chat_id),
104 "Chat.ReadWrite",
105 Some(body),
106 )
107 .await
108 }
109
110 pub async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
111 self.write(
112 Method::Post,
113 &target.reaction_url("setReaction"),
114 target.react_scope(),
115 Some(json!({"reactionType": reaction_type})),
116 )
117 .await
118 }
119
120 pub async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
121 self.write(
122 Method::Post,
123 &target.reaction_url("unsetReaction"),
124 target.react_scope(),
125 Some(json!({"reactionType": reaction_type})),
126 )
127 .await
128 }
129
130 pub async fn edit_message(
131 &self,
132 target: &MessageTarget,
133 html: &str,
134 mentions: &[OutgoingMention],
135 ) -> Result<()> {
136 self.write(
137 Method::Patch,
138 &target.url(),
139 target.write_scope(),
140 Some(message_body(html, mentions)),
141 )
142 .await
143 }
144
145 /// `user_id` is only used for chats.
146 pub async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
147 self.write(
148 Method::Post,
149 &target.soft_delete_url(user_id),
150 target.write_scope(),
151 None,
152 )
153 .await
154 }
155
156 pub async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
157 let body = json!({
158 "chatType": "oneOnOne",
159 "members": [conversation_member(my_user_id), conversation_member(user_id)],
160 });
161 self.write_for_chat(body).await
162 }
163
164 pub async fn create_group(
165 &self,
166 my_user_id: &str,
167 user_ids: &[String],
168 topic: Option<&str>,
169 ) -> Result<Chat> {
170 let members: Vec<Value> = std::iter::once(my_user_id)
171 .chain(user_ids.iter().map(String::as_str))
172 .map(conversation_member)
173 .collect();
174 let mut body = json!({"chatType": "group", "members": members});
175 if let Some(topic) = topic.filter(|topic| !topic.is_empty()) {
176 body["topic"] = json!(topic);
177 }
178 self.write_for_chat(body).await
179 }
180
181 async fn write_for_chat(&self, body: Value) -> Result<Chat> {
182 let answer = self
183 .session()
184 .request(
185 Method::Post,
186 &urls::create_chat(),
187 &Scope::graph("Chat.ReadWrite"),
188 Some(body),
189 )
190 .await?;
191 Ok(serde_json::from_value(answer.body)?)
192 }
193
194 async fn write_for_message(
195 &self,
196 method: Method,
197 url: &str,
198 scope: &'static str,
199 body: Option<Value>,
200 ) -> Result<Message> {
201 let answer = self
202 .session()
203 .request(method, url, &Scope::graph(scope), body)
204 .await?;
205 Ok(serde_json::from_value(answer.body)?)
206 }
207
208 async fn write(
209 &self,
210 method: Method,
211 url: &str,
212 scope: &'static str,
213 body: Option<Value>,
214 ) -> Result<()> {
215 self.session()
216 .request(method, url, &Scope::graph(scope), body)
217 .await?;
218 Ok(())
219 }
220}