BlueGit

ict rusty-teams

Files

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

writes.rs

writes.rs 3.7 KiB Download Raw
1use serde_json::{Value, json};
2use session::{Method, Scope};
3
4use crate::client::Graph;
5use crate::error::Result;
6use crate::models::Message;
7use crate::outgoing::{MessageExtras, OutgoingMention, message_body};
8use crate::target::MessageTarget;
9use crate::urls;
10
11impl Graph {
12 pub async fn send_channel_message(
13 &self,
14 team_id: &str,
15 channel_id: &str,
16 html: &str,
17 subject: Option<&str>,
18 mentions: &[OutgoingMention],
19 extras: &MessageExtras,
20 ) -> Result<Message> {
21 let mut body = message_body(html, mentions, extras);
22 if let Some(subject) = subject.filter(|subject| !subject.is_empty()) {
23 body["subject"] = json!(subject);
24 }
25 self.write_for_message(
26 Method::Post,
27 &urls::channel_post(team_id, channel_id),
28 "ChannelMessage.Send",
29 Some(body),
30 )
31 .await
32 }
33
34 pub async fn reply_to_channel_message(
35 &self,
36 team_id: &str,
37 channel_id: &str,
38 message_id: &str,
39 html: &str,
40 mentions: &[OutgoingMention],
41 extras: &MessageExtras,
42 ) -> Result<Message> {
43 self.write_for_message(
44 Method::Post,
45 &urls::channel_reply_post(team_id, channel_id, message_id),
46 "ChannelMessage.Send",
47 Some(message_body(html, mentions, extras)),
48 )
49 .await
50 }
51
52 pub async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
53 self.write(
54 Method::Post,
55 &target.reaction_url("setReaction"),
56 target.react_scope(),
57 Some(json!({"reactionType": reaction_type})),
58 )
59 .await
60 }
61
62 pub async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
63 self.write(
64 Method::Post,
65 &target.reaction_url("unsetReaction"),
66 target.react_scope(),
67 Some(json!({"reactionType": reaction_type})),
68 )
69 .await
70 }
71
72 pub async fn edit_message(
73 &self,
74 target: &MessageTarget,
75 html: &str,
76 mentions: &[OutgoingMention],
77 extras: &MessageExtras,
78 ) -> Result<()> {
79 self.write(
80 Method::Patch,
81 &target.url(),
82 target.write_scope(),
83 Some(message_body(html, mentions, extras)),
84 )
85 .await
86 }
87
88 /// `user_id` is only used for chats.
89 pub async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
90 self.write(
91 Method::Post,
92 &target.soft_delete_url(user_id),
93 target.write_scope(),
94 None,
95 )
96 .await
97 }
98
99 /// `user_id` is only used for chats.
100 pub async fn undo_soft_delete_message(
101 &self,
102 user_id: &str,
103 target: &MessageTarget,
104 ) -> Result<()> {
105 self.write(
106 Method::Post,
107 &target.undo_soft_delete_url(user_id),
108 target.write_scope(),
109 None,
110 )
111 .await
112 }
113
114 async fn write_for_message(
115 &self,
116 method: Method,
117 url: &str,
118 scope: &'static str,
119 body: Option<Value>,
120 ) -> Result<Message> {
121 let answer = self
122 .session()
123 .request(method, url, &Scope::graph(scope), body)
124 .await?;
125 Ok(serde_json::from_value(answer.body)?)
126 }
127
128 async fn write(
129 &self,
130 method: Method,
131 url: &str,
132 scope: &'static str,
133 body: Option<Value>,
134 ) -> Result<()> {
135 self.session()
136 .request(method, url, &Scope::graph(scope), body)
137 .await?;
138 Ok(())
139 }
140}