BlueGit

ict rusty-teams

Files

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

writes.rs

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