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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use serde::Serialize;
use serde_json::json;

use crate::{
    auth::{AuthFlow, Verifier},
    client::Body,
    error::Result,
    model::{
        artist::{Artist, PagedArtists},
        user::{TimeRange, UserItem, UserItemType},
        CursorPage, Page,
    },
    query_list, Nil,
};

use super::{Builder, Endpoint, Limit, PrivateEndpoint};

impl Endpoint for UserTopItemsEndpoint {}
impl Endpoint for FollowPlaylistBuilder {}
impl Endpoint for FollowedArtistsBuilder {}
impl Endpoint for FollowUserOrArtistEndpoint {}

#[derive(Clone, Debug, Default, Serialize)]
pub struct UserTopItemsEndpoint {
    #[serde(skip)]
    pub(crate) r#type: UserItemType,
    pub(crate) time_range: Option<TimeRange>,
    pub(crate) limit: Option<Limit>,
    pub(crate) offset: Option<u32>,
}

impl<F: AuthFlow, V: Verifier> Builder<'_, F, V, UserTopItemsEndpoint> {
    /// The time frame of the computed affinities.
    pub fn time_range(mut self, time_range: TimeRange) -> Self {
        self.endpoint.time_range = Some(time_range);
        self
    }

    #[doc = include_str!("../docs/limit.md")]
    pub fn limit(mut self, limit: u32) -> Self {
        self.endpoint.limit = Some(Limit::new(limit));
        self
    }

    #[doc = include_str!("../docs/offset.md")]
    pub fn offset(mut self, offset: u32) -> Self {
        self.endpoint.offset = Some(offset);
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self) -> Result<Page<UserItem>> {
        self.spotify
            .get(format!("/me/top/{}", self.endpoint.r#type), self.endpoint)
            .await
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct FollowPlaylistBuilder {
    #[serde(skip)]
    pub(crate) id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) public: Option<bool>,
}

impl<F: AuthFlow, V: Verifier> Builder<'_, F, V, FollowPlaylistBuilder> {
    /// If set to `true`, the playlist will be included in the user's
    /// public playlists. Defaults to `true`.
    pub fn public(mut self, public: bool) -> Self {
        self.endpoint.public = Some(public);
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn send(self) -> Result<Nil> {
        self.spotify
            .put(
                format!("/playlists/{}/followers", self.endpoint.id),
                self.endpoint.json(),
            )
            .await
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct FollowedArtistsBuilder {
    pub(crate) r#type: String,
    pub(crate) after: Option<String>,
    pub(crate) limit: Option<Limit>,
}

impl<F: AuthFlow, V: Verifier> Builder<'_, F, V, FollowedArtistsBuilder> {
    /// The last artist ID retrieved from the previous request.
    pub fn after(mut self, artist_id: impl Into<String>) -> Self {
        self.endpoint.after = Some(artist_id.into());
        self
    }

    #[doc = include_str!("../docs/limit.md")]
    pub fn limit(mut self, limit: u32) -> Self {
        self.endpoint.limit = Some(Limit::new(limit));
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self) -> Result<CursorPage<Artist>> {
        self.spotify
            .get("/me/following".to_owned(), self.endpoint)
            .await
            .map(|a: PagedArtists| a.artists)
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct FollowUserOrArtistEndpoint {
    pub(crate) r#type: String,
    #[serde(skip)]
    pub(crate) ids: Vec<String>,
}

impl<F: AuthFlow, V: Verifier> Builder<'_, F, V, FollowUserOrArtistEndpoint> {
    #[doc = include_str!("../docs/send.md")]
    pub async fn follow(self) -> Result<Nil> {
        self.spotify
            .put(
                format!("/me/following?type={}", self.endpoint.r#type),
                Body::Json(json!({ "ids": self.endpoint.ids })),
            )
            .await
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn unfollow(self) -> Result<Nil> {
        self.spotify
            .delete(
                format!("/me/following?type={}", self.endpoint.r#type),
                Body::Json(json!({ "ids": self.endpoint.ids })),
            )
            .await
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn check(self) -> Result<Vec<bool>> {
        self.spotify
            .get(
                "/me/following/contains".to_owned(),
                [
                    ("type", self.endpoint.r#type),
                    ("ids", query_list(&self.endpoint.ids)),
                ],
            )
            .await
    }
}