misskey_util/builder/
admin.rs

1use crate::Error;
2
3#[cfg(feature = "12-62-0")]
4use misskey_api::model::clip::Clip;
5#[cfg(feature = "12-9-0")]
6use misskey_api::model::emoji::Emoji;
7use misskey_api::model::{
8    announcement::Announcement,
9    log::{Log, LogLevel},
10    user::User,
11};
12use misskey_api::{endpoint, EntityRef};
13use misskey_core::Client;
14use url::Url;
15
16/// Builder for the [`server_logs`][`crate::ClientExt::server_logs`] method.
17pub struct ServerLogListBuilder<C> {
18    client: C,
19    request: endpoint::admin::logs::Request,
20}
21
22impl<C> ServerLogListBuilder<C> {
23    /// Creates a builder with the client.
24    pub fn new(client: C) -> Self {
25        let request = endpoint::admin::logs::Request::default();
26        ServerLogListBuilder { client, request }
27    }
28
29    /// Gets the request object for reuse.
30    pub fn as_request(&self) -> &endpoint::admin::logs::Request {
31        &self.request
32    }
33
34    /// Limits the number of logs to be listed.
35    pub fn take(&mut self, length: u8) -> &mut Self {
36        self.request.limit.replace(length);
37        self
38    }
39
40    /// Limits the level of logs to be listed to the specified one.
41    pub fn level(&mut self, level: LogLevel) -> &mut Self {
42        self.request.level.replace(level);
43        self
44    }
45
46    /// Limits the listed logs to errors.
47    ///
48    /// This is equivalent to `.level(LogLevel::Error)`.
49    pub fn error(&mut self) -> &mut Self {
50        self.level(LogLevel::Error)
51    }
52
53    /// Limits the listed logs to warnings.
54    ///
55    /// This is equivalent to `.level(LogLevel::Warning)`.
56    pub fn warning(&mut self) -> &mut Self {
57        self.level(LogLevel::Warning)
58    }
59
60    /// Limits the listed logs to informations.
61    ///
62    /// This is equivalent to `.level(LogLevel::Info)`.
63    pub fn info(&mut self) -> &mut Self {
64        self.level(LogLevel::Info)
65    }
66
67    /// Limits the listed logs to successes.
68    ///
69    /// This is equivalent to `.level(LogLevel::Success)`.
70    pub fn success(&mut self) -> &mut Self {
71        self.level(LogLevel::Success)
72    }
73
74    /// Limits the listed logs to debug logs.
75    ///
76    /// This is equivalent to `.level(LogLevel::Debug)`.
77    pub fn debug(&mut self) -> &mut Self {
78        self.level(LogLevel::Debug)
79    }
80
81    /// Adds a domain name to be included in the listed logs.
82    ///
83    /// You can add more domains to be included with subsequent calls to this method.
84    pub fn with_domain(&mut self, domain: impl AsRef<str>) -> &mut Self {
85        if let Some(domains) = self.request.domain.as_mut() {
86            domains.push(' ');
87            domains.push_str(domain.as_ref());
88        } else {
89            self.request.domain.replace(domain.as_ref().to_owned());
90        }
91        self
92    }
93
94    /// Adds a domain name to be excluded from the listed logs.
95    ///
96    /// You can add more domains to be excluded with subsequent calls to this method.
97    pub fn without_domain(&mut self, domain: impl AsRef<str>) -> &mut Self {
98        if let Some(domains) = self.request.domain.as_mut() {
99            domains.push_str(" -");
100            domains.push_str(domain.as_ref());
101        } else {
102            self.request.domain.replace(format!("-{}", domain.as_ref()));
103        }
104        self
105    }
106}
107
108impl<C: Client> ServerLogListBuilder<C> {
109    /// Lists the logs.
110    pub async fn list(&self) -> Result<Vec<Log>, Error<C::Error>> {
111        let logs = self
112            .client
113            .request(&self.request)
114            .await
115            .map_err(Error::Client)?
116            .into_result()?;
117        Ok(logs)
118    }
119}
120
121/// Builder for the [`update_meta`][`crate::ClientExt::update_meta`] method.
122pub struct MetaUpdateBuilder<C> {
123    client: C,
124    request: endpoint::admin::update_meta::Request,
125}
126
127impl<C> MetaUpdateBuilder<C> {
128    /// Creates a builder with the client.
129    pub fn new(client: C) -> Self {
130        let request = endpoint::admin::update_meta::Request::default();
131        MetaUpdateBuilder { client, request }
132    }
133
134    /// Gets the request object for reuse.
135    pub fn as_request(&self) -> &endpoint::admin::update_meta::Request {
136        &self.request
137    }
138
139    update_builder_bool_field! {
140        /// Sets whether the instance has registration enabled.
141        pub disable_registration;
142        /// Sets whether the instance has local timeline enabled.
143        pub disable_local_timeline;
144        /// Sets whether the instance has global timeline enabled.
145        pub disable_global_timeline;
146        /// Sets whether the instance uses ★ as fallback if the reaction emoji is unknown.
147        pub use_star_for_reaction_fallback;
148    }
149
150    update_builder_string_collection_field! {
151        /// Sets the pinned users of the instance.
152        pub pinned_users;
153        /// Sets the hashtags that the instance will ignore for statistics, etc.
154        pub hidden_tags;
155        /// Sets the hosts to be blocked by the instance.
156        pub blocked_hosts;
157        /// Sets the pinned pages of the instance.
158        #[cfg(feature = "12-58-0")]
159        #[cfg_attr(docsrs, doc(cfg(feature = "12-58-0")))]
160        pub pinned_pages;
161    }
162
163    update_builder_option_field! {
164        #[doc_name = "pinned clip"]
165        #[cfg(feature = "12-62-0")]
166        #[cfg_attr(docsrs, doc(cfg(feature = "12-62-0")))]
167        pub pinned_clip : impl EntityRef<Clip> { pinned_clip_id =  pinned_clip.entity_ref() };
168    }
169
170    update_builder_string_option_field! {
171        #[doc_name = "URL of the mascot image for the instance"]
172        pub mascot_image_url;
173        #[doc_name = "URL of the banner image for the instance"]
174        pub bannar_url;
175        #[doc_name = "URL of the error image for the instance"]
176        pub error_image_url;
177        #[doc_name = "URL of the icon for the instance"]
178        pub icon_url;
179        #[doc_name = "name of the instance"]
180        pub name;
181        #[doc_name = "description of the instance"]
182        pub description;
183        #[doc_name = "URL of the background image for the instance"]
184        #[cfg(feature = "12-60-0")]
185        #[cfg_attr(docsrs, doc(cfg(feature = "12-60-0")))]
186        pub background_image_url;
187        #[doc_name = "URL of the logo image for the instance"]
188        #[cfg(feature = "12-60-0")]
189        #[cfg_attr(docsrs, doc(cfg(feature = "12-60-0")))]
190        pub logo_image_url;
191    }
192
193    /// Sets the maximum number of characters for posts in the instance.
194    pub fn max_note_text_length(&mut self, max_note_text_length: u64) -> &mut Self {
195        self.request
196            .max_note_text_length
197            .replace(max_note_text_length);
198        self
199    }
200
201    /// Sets the drive capacity per local user in megabytes.
202    pub fn local_drive_capacity(&mut self, mb: u64) -> &mut Self {
203        self.request.local_drive_capacity_mb.replace(mb);
204        self
205    }
206
207    /// Sets the drive capacity per remote user in megabytes.
208    pub fn remote_drive_capacity(&mut self, mb: u64) -> &mut Self {
209        self.request.remote_drive_capacity_mb.replace(mb);
210        self
211    }
212
213    update_builder_bool_field! {
214        /// Sets whether or not the instance would cache remote files.
215        pub cache_remote_files;
216        /// Sets whether or not the instance would proxy remote files that are not available
217        /// locally.
218        pub proxy_remote_files;
219    }
220
221    update_builder_bool_field! {
222        /// Sets whether or not the instance enables hCaptcha.
223        #[cfg(feature = "12-37-0")]
224        #[cfg_attr(docsrs, doc(cfg(feature = "12-37-0")))]
225        pub enable_hcaptcha;
226
227        /// Sets whether or not the instance enables reCAPTCHA.
228        pub enable_recaptcha;
229    }
230
231    update_builder_string_option_field! {
232        #[doc_name = "hCaptcha site key"]
233        #[cfg(feature = "12-37-0")]
234        #[cfg_attr(docsrs, doc(cfg(feature = "12-37-0")))]
235        pub hcaptcha_site_key;
236
237        #[doc_name = "hCaptcha secret key"]
238        #[cfg(feature = "12-37-0")]
239        #[cfg_attr(docsrs, doc(cfg(feature = "12-37-0")))]
240        pub hcaptcha_secret_key;
241
242        #[doc_name = "reCAPTCHA site key"]
243        pub recaptcha_site_key;
244        #[doc_name = "reCAPTCHA secret key"]
245        pub recaptcha_secret_key;
246    }
247
248    update_builder_option_field! {
249        #[doc_name = "proxy account for the instance"]
250        pub proxy_account: impl EntityRef<User> { proxy_account_id =  proxy_account.entity_ref() };
251    }
252
253    update_builder_string_option_field! {
254        #[doc_name = "name of the instance maintainer"]
255        pub maintainer_name;
256        #[doc_name = "email of the instance maintainer"]
257        pub maintainer_email;
258    }
259
260    update_builder_string_collection_field! {
261        /// Sets the target language of the instance.
262        pub languages { langs };
263    }
264
265    update_builder_option_field! {
266        #[doc_name = "summaly proxy URL"]
267        pub summaly_proxy: Url { summaly_proxy };
268    }
269
270    update_builder_bool_field! {
271        /// Sets whether or not to enable the Twitter integration.
272        pub enable_twitter_integration;
273    }
274    update_builder_string_option_field! {
275        #[doc_name = "Twitter consumer key"]
276        pub twitter_consumer_key;
277        #[doc_name = "Twitter consumer secret"]
278        pub twitter_consumer_secret;
279    }
280
281    update_builder_bool_field! {
282        /// Sets whether or not to enable the GitHub integration.
283        pub enable_github_integration;
284    }
285    update_builder_string_option_field! {
286        #[doc_name = "GitHub client ID"]
287        pub github_client_id;
288        #[doc_name = "GitHub client secret"]
289        pub github_client_secret;
290    }
291
292    update_builder_bool_field! {
293        /// Sets whether or not to enable the Discord integration.
294        pub enable_discord_integration;
295    }
296    update_builder_string_option_field! {
297        #[doc_name = "Discord client ID"]
298        pub discord_client_id;
299        #[doc_name = "Discord client secret"]
300        pub discord_client_secret;
301    }
302
303    update_builder_bool_field! {
304        /// Sets whether or not to enable email delivery.
305        pub enable_email;
306        /// Sets whether or not the SMTP server uses SSL.
307        pub smtp_secure;
308    }
309    update_builder_string_option_field! {
310        #[doc_name = "email address to be used for email delivery"]
311        pub email_address { email };
312        #[doc_name = "host of the SMTP server"]
313        pub smtp_host;
314        #[doc_name = "username of the SMTP server"]
315        pub smtp_user;
316        #[doc_name = "password of the SMTP server"]
317        pub smtp_pass;
318    }
319    update_builder_option_field! {
320        #[doc_name = "port number of the SMTP server"]
321        pub smtp_port: u16 { smtp_port };
322    }
323
324    update_builder_bool_field! {
325        /// Sets whether or not to enable the service worker.
326        pub enable_service_worker;
327    }
328    update_builder_string_option_field! {
329        #[doc_name = "public key for the service worker's VAPID key pair"]
330        pub service_worker_public_key { sw_public_key };
331        #[doc_name = "private key for the service worker's VAPID key pair"]
332        pub service_worker_private_key { sw_private_key };
333    }
334
335    update_builder_string_option_field! {
336        #[doc_name = "URL for the Terms of Service"]
337        pub tos_url;
338    }
339
340    /// Sets the repository URL.
341    pub fn repository_url(&mut self, url: Url) -> &mut Self {
342        self.request.repository_url.replace(url);
343        self
344    }
345
346    update_builder_string_option_field! {
347        #[doc_name = "URL for the feedback"]
348        pub feedback_url;
349    }
350
351    update_builder_bool_field! {
352        /// Sets whether or not to use extenal object storage.
353        pub use_object_storage;
354        /// Sets whether or not the extenal object storage uses SSL.
355        pub object_storage_use_ssl;
356        /// Sets whether or not the extenal object storage uses the proxy.
357        #[cfg(feature = "12-31-0")]
358        #[cfg_attr(docsrs, doc(cfg(feature = "12-31-0")))]
359        pub object_storage_use_proxy;
360        /// Sets whether or not to set `'public-read'` when uploading to the extenal object
361        /// storage.
362        #[cfg(feature = "12-47-0")]
363        #[cfg_attr(docsrs, doc(cfg(feature = "12-47-0")))]
364        pub object_storage_set_public_read;
365    }
366    update_builder_option_field! {
367        #[doc_name = "base URL of the extenal object storage"]
368        pub object_storage_base_url: Url { object_storage_base_url };
369        #[doc_name = "port number of the extenal object storage"]
370        pub object_storage_port: u16 { object_storage_port };
371    }
372    update_builder_string_option_field! {
373        #[doc_name = "bucket name for the extenal object storage"]
374        pub object_storage_bucket;
375        #[doc_name = "prefix for the extenal object storage"]
376        pub object_storage_prefix;
377        #[doc_name = "endpoint for the extenal object storage"]
378        pub object_storage_endpoint;
379        #[doc_name = "region for the extenal object storage"]
380        pub object_storage_region;
381        #[doc_name = "access key for the extenal object storage"]
382        pub object_storage_access_key;
383        #[doc_name = "secret key for the extenal object storage"]
384        pub object_storage_secret_key;
385    }
386}
387
388impl<C: Client> MetaUpdateBuilder<C> {
389    /// Updates the instance information.
390    pub async fn update(&self) -> Result<(), Error<C::Error>> {
391        self.client
392            .request(&self.request)
393            .await
394            .map_err(Error::Client)?
395            .into_result()?;
396        Ok(())
397    }
398}
399
400/// Builder for the [`update_announcement`][`crate::ClientExt::update_announcement`] method.
401pub struct AnnouncementUpdateBuilder<C> {
402    client: C,
403    request: endpoint::admin::announcements::update::Request,
404}
405
406impl<C> AnnouncementUpdateBuilder<C> {
407    /// Creates a builder with the client and the announcement you are going to update.
408    pub fn new(client: C, announcement: Announcement) -> Self {
409        let Announcement {
410            id,
411            title,
412            text,
413            image_url,
414            ..
415        } = announcement;
416        let request = endpoint::admin::announcements::update::Request {
417            id,
418            title,
419            text,
420            image_url,
421        };
422        AnnouncementUpdateBuilder { client, request }
423    }
424
425    /// Gets the request object for reuse.
426    pub fn as_request(&self) -> &endpoint::admin::announcements::update::Request {
427        &self.request
428    }
429
430    /// Sets the title of the announcement.
431    pub fn title(&mut self, title: impl Into<String>) -> &mut Self {
432        self.request.title = title.into();
433        self
434    }
435
436    /// Sets the body text of the announcement.
437    pub fn text(&mut self, text: impl Into<String>) -> &mut Self {
438        self.request.text = text.into();
439        self
440    }
441
442    /// Sets the image for the announcement.
443    pub fn set_image(&mut self, image_url: Url) -> &mut Self {
444        self.request.image_url.replace(image_url);
445        self
446    }
447
448    /// Deletes the image of the announcement.
449    pub fn delete_image(&mut self) -> &mut Self {
450        self.request.image_url.take();
451        self
452    }
453}
454
455impl<C: Client> AnnouncementUpdateBuilder<C> {
456    /// Updates the announcement.
457    pub async fn update(&self) -> Result<(), Error<C::Error>> {
458        self.client
459            .request(&self.request)
460            .await
461            .map_err(Error::Client)?
462            .into_result()?;
463        Ok(())
464    }
465}
466
467/// Builder for the [`update_emoji`][`crate::ClientExt::update_emoji`] method.
468pub struct EmojiUpdateBuilder<C> {
469    client: C,
470    request: endpoint::admin::emoji::update::Request,
471}
472
473impl<C> EmojiUpdateBuilder<C> {
474    /// Creates a builder with the client and the emoji you are going to update.
475    #[cfg(feature = "12-9-0")]
476    #[cfg_attr(docsrs, doc(cfg(feature = "12-9-0")))]
477    pub fn new(client: C, emoji: Emoji) -> Self {
478        let Emoji {
479            id,
480            name,
481            category,
482            aliases,
483            ..
484        } = emoji;
485        let request = endpoint::admin::emoji::update::Request {
486            id,
487            name,
488            category,
489            aliases,
490        };
491        EmojiUpdateBuilder { client, request }
492    }
493
494    /// Gets the request object for reuse.
495    pub fn as_request(&self) -> &endpoint::admin::emoji::update::Request {
496        &self.request
497    }
498
499    /// Sets the name of the custom emoji.
500    pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
501        self.request.name = name.into();
502        self
503    }
504
505    /// Sets the category of the custom emoji.
506    pub fn set_category(&mut self, category: impl Into<String>) -> &mut Self {
507        self.request.category.replace(category.into());
508        self
509    }
510
511    /// Deletes the category of the custom emoji.
512    pub fn delete_category(&mut self) -> &mut Self {
513        self.request.category.take();
514        self
515    }
516
517    /// Sets the aliases of the custom emoji.
518    pub fn aliases(&mut self, aliases: impl IntoIterator<Item = impl Into<String>>) -> &mut Self {
519        self.request.aliases = aliases.into_iter().map(Into::into).collect();
520        self
521    }
522
523    /// Adds the given aliases to the custom emoji.
524    pub fn add_aliases(
525        &mut self,
526        aliases: impl IntoIterator<Item = impl Into<String>>,
527    ) -> &mut Self {
528        self.request
529            .aliases
530            .extend(aliases.into_iter().map(Into::into));
531        self
532    }
533
534    /// Adds the given alias to the custom emoji.
535    pub fn add_alias(&mut self, alias: impl Into<String>) -> &mut Self {
536        self.request.aliases.push(alias.into());
537        self
538    }
539}
540
541impl<C: Client> EmojiUpdateBuilder<C> {
542    /// Updates the custom emoji.
543    pub async fn update(&self) -> Result<(), Error<C::Error>> {
544        self.client
545            .request(&self.request)
546            .await
547            .map_err(Error::Client)?
548            .into_result()?;
549        Ok(())
550    }
551}