1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::Error;
use misskey_api::model::{
drive::DriveFile, messaging::MessagingMessage, user::User, user_group::UserGroup,
};
use misskey_api::{endpoint, EntityRef};
use misskey_core::Client;
/// Builder for the [`build_message`][`crate::ClientExt::build_message`] method.
pub struct MessagingMessageBuilder<C> {
client: C,
request: endpoint::messaging::messages::create::Request,
}
impl<C> MessagingMessageBuilder<C> {
/// Creates a builder with the client.
pub fn new(client: C) -> Self {
let request = endpoint::messaging::messages::create::Request {
text: None,
user_id: None,
group_id: None,
file_id: None,
};
MessagingMessageBuilder { client, request }
}
/// Gets the request object for reuse.
pub fn as_request(&self) -> &endpoint::messaging::messages::create::Request {
&self.request
}
/// Sets the text content of the message.
pub fn text(&mut self, text: impl Into<String>) -> &mut Self {
self.request.text.replace(text.into());
self
}
/// Specifies the recipient user.
///
/// Note that user and group cannot be specified as the recipient at the same time.
/// Therefore, even if [`group`][`MessagingMessageBuilder::group`] is used before this method call,
/// it will be overwritten and the message is only sent to the user specified in this call.
pub fn user(&mut self, user: impl EntityRef<User>) -> &mut Self {
self.request.user_id.replace(user.entity_ref());
self.request.group_id.take();
self
}
/// Specifies the recipient user group.
///
/// Note that user and group cannot be specified as the recipient at the same time.
/// Therefore, even if [`user`][`MessagingMessageBuilder::user`] is used before this method call,
/// it will be overwritten and the message is only sent to the user group specified in this call.
pub fn group(&mut self, group: impl EntityRef<UserGroup>) -> &mut Self {
self.request.group_id.replace(group.entity_ref());
self.request.user_id.take();
self
}
/// Sets the file content of the message.
pub fn file(&mut self, file: impl EntityRef<DriveFile>) -> &mut Self {
self.request.file_id.replace(file.entity_ref());
self
}
}
impl<C: Client> MessagingMessageBuilder<C> {
/// Creates the message.
pub async fn create(&self) -> Result<MessagingMessage, Error<C::Error>> {
let message = self
.client
.request(&self.request)
.await
.map_err(Error::Client)?
.into_result()?;
Ok(message)
}
}