misskey_util/builder/
channel.rs

1use crate::Error;
2
3use misskey_api::model::{channel::Channel, drive::DriveFile};
4use misskey_api::{endpoint, EntityRef};
5use misskey_core::Client;
6
7/// Builder for the [`build_channel`][`crate::ClientExt::build_channel`] method.
8pub struct ChannelBuilder<C> {
9    client: C,
10    request: endpoint::channels::create::Request,
11}
12
13impl<C> ChannelBuilder<C> {
14    /// Creates a builder with the client.
15    pub fn new(client: C) -> Self {
16        let request = endpoint::channels::create::Request {
17            name: String::default(),
18            description: None,
19            banner_id: None,
20        };
21        ChannelBuilder { client, request }
22    }
23
24    /// Gets the request object for reuse.
25    pub fn as_request(&self) -> &endpoint::channels::create::Request {
26        &self.request
27    }
28
29    /// Sets the name of the channel.
30    pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
31        self.request.name = name.into();
32        self
33    }
34
35    /// Sets the description of the channel.
36    pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
37        self.request.description.replace(description.into());
38        self
39    }
40
41    /// Sets the banner image of the channel.
42    pub fn banner(&mut self, file: impl EntityRef<DriveFile>) -> &mut Self {
43        self.request.banner_id.replace(file.entity_ref());
44        self
45    }
46}
47
48impl<C: Client> ChannelBuilder<C> {
49    /// Creates the channel.
50    pub async fn create(&self) -> Result<Channel, Error<C::Error>> {
51        let channel = self
52            .client
53            .request(&self.request)
54            .await
55            .map_err(Error::Client)?
56            .into_result()?;
57        Ok(channel)
58    }
59}
60
61/// Builder for the [`update_channel`][`crate::ClientExt::update_channel`] method.
62pub struct ChannelUpdateBuilder<C> {
63    client: C,
64    request: endpoint::channels::update::Request,
65}
66
67impl<C> ChannelUpdateBuilder<C> {
68    /// Creates a builder with the client and the channel you are going to update.
69    pub fn new(client: C, channel: impl EntityRef<Channel>) -> Self {
70        let channel_id = channel.entity_ref();
71        let request = endpoint::channels::update::Request {
72            channel_id,
73            name: None,
74            description: None,
75            banner_id: None,
76        };
77        ChannelUpdateBuilder { client, request }
78    }
79
80    /// Gets the request object for reuse.
81    pub fn as_request(&self) -> &endpoint::channels::update::Request {
82        &self.request
83    }
84
85    /// Sets the name of the channel.
86    pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
87        self.request.name.replace(name.into());
88        self
89    }
90
91    update_builder_string_option_field! {
92        pub description;
93    }
94
95    update_builder_option_field! {
96        #[doc_name = "banner image"]
97        pub banner: impl EntityRef<DriveFile> { banner_id = banner.entity_ref() };
98    }
99}
100
101impl<C: Client> ChannelUpdateBuilder<C> {
102    /// Updates the channel.
103    pub async fn update(&self) -> Result<Channel, Error<C::Error>> {
104        let channel = self
105            .client
106            .request(&self.request)
107            .await
108            .map_err(Error::Client)?
109            .into_result()?;
110        Ok(channel)
111    }
112}