mastodon_api\methods/tags.rs
1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::{FeaturedTag, Tag};
4
5/// Handler for tag-related API endpoints (followed and featured tags).
6pub struct TagsHandler<'a> {
7 client: &'a MastodonClient,
8}
9
10impl<'a> TagsHandler<'a> {
11 /// Creates a new `TagsHandler` for the given client.
12 pub fn new(client: &'a MastodonClient) -> Self {
13 Self { client }
14 }
15
16 /// Fetches all tags that the authenticated user is following.
17 ///
18 /// Returns:
19 /// - `Result<Vec<Tag>>`: The followed tags.
20 ///
21 /// Corresponds to `GET /api/v1/followed_tags`.
22 pub async fn list_followed(&self) -> Result<Vec<Tag>> {
23 let url = format!("{}/api/v1/followed_tags", self.client.base_url());
24 let req = self.client.http_client().get(&url);
25 self.client.send(req).await
26 }
27
28 /// Follows a tag.
29 ///
30 /// Parameters:
31 /// - `name`: The name of the tag to follow.
32 ///
33 /// Returns:
34 /// - `Result<Tag>`: The followed tag.
35 ///
36 /// Corresponds to `POST /api/v1/tags/:name/follow`.
37 pub async fn follow(&self, name: &str) -> Result<Tag> {
38 let url = format!("{}/api/v1/tags/{}/follow", self.client.base_url(), name);
39 let req = self.client.http_client().post(&url);
40 self.client.send(req).await
41 }
42
43 /// Unfollows a tag.
44 ///
45 /// Parameters:
46 /// - `name`: The name of the tag to unfollow.
47 ///
48 /// Returns:
49 /// - `Result<Tag>`: The unfollowed tag.
50 ///
51 /// Corresponds to `POST /api/v1/tags/{name}/unfollow`.
52 pub async fn unfollow(&self, name: &str) -> Result<Tag> {
53 let url = format!("{}/api/v1/tags/{}/unfollow", self.client.base_url(), name);
54 let req = self.client.http_client().post(&url);
55 self.client.send(req).await
56 }
57
58 /// Fetches all tags featured on the authenticated user's profile.
59 ///
60 /// Returns:
61 /// - `Result<Vec<FeaturedTag>>`: The featured tags.
62 ///
63 /// Corresponds to `GET /api/v1/featured_tags`.
64 pub async fn list_featured(&self) -> Result<Vec<FeaturedTag>> {
65 let url = format!("{}/api/v1/featured_tags", self.client.base_url());
66 let req = self.client.http_client().get(&url);
67 self.client.send(req).await
68 }
69
70 /// Features a tag on the authenticated user's profile.
71 ///
72 /// Parameters:
73 /// - `name`: The name of the tag to feature.
74 ///
75 /// Returns:
76 /// - `Result<FeaturedTag>`: The featured tag.
77 ///
78 /// Corresponds to `POST /api/v1/featured_tags`.
79 pub async fn feature(&self, name: &str) -> Result<FeaturedTag> {
80 let url = format!("{}/api/v1/featured_tags", self.client.base_url());
81 let req = self.client.http_client().post(&url).form(&[("name", name)]);
82 self.client.send(req).await
83 }
84
85 /// Unfeatures a tag from the profile.
86 ///
87 /// Parameters:
88 /// - `id`: The ID of the featured tag.
89 ///
90 /// Returns:
91 /// - `Result<()>`: Success if the tag was unfeatured.
92 ///
93 /// Corresponds to `DELETE /api/v1/featured_tags/:id`.
94 pub async fn unfeature(&self, id: &str) -> Result<()> {
95 let url = format!("{}/api/v1/featured_tags/{}", self.client.base_url(), id);
96 let req = self.client.http_client().delete(&url);
97 self.client.send(req).await
98 }
99
100 /// Fetches suggestions for tags to feature.
101 ///
102 /// Returns:
103 /// - `Result<Vec<Tag>>`: The suggested tags.
104 ///
105 /// Corresponds to `GET /api/v1/featured_tags/suggestions`.
106 pub async fn featured_suggestions(&self) -> Result<Vec<Tag>> {
107 let url = format!(
108 "{}/api/v1/featured_tags/suggestions",
109 self.client.base_url()
110 );
111 let req = self.client.http_client().get(&url);
112 self.client.send(req).await
113 }
114}