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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use std::convert::TryFrom; use super::*; use crate::common::RateLimit; use crate::cursor::{CursorIter, ListCursor, UserCursor}; use crate::error::{Error::TwitterError, Result}; use crate::user::{TwitterUser, UserID}; use crate::{auth, links, tweet}; ///Look up the lists the given user has been added to. /// ///This function returns a `Stream` over the lists returned by Twitter. This method defaults to ///reeturning 20 lists in a single network call; the maximum is 1000. pub fn memberships<T: Into<UserID>>(user: T, token: &auth::Token) -> CursorIter<ListCursor> { let params = ParamList::new().add_name_param(user.into()); CursorIter::new(links::lists::MEMBERSHIPS, token, Some(params), Some(20)) } ///Return up to 100 lists the given user is subscribed to, including those the user made ///themselves. /// ///This function can be used to get a snapshot of a user's lists, but if they've created or ///subscribed to a lot of lists, then the limitations of this function can get in the way. ///If the `owned_first` parameter is `true`, Twitter will load the lists the given user created, ///then the ones they've subscribed to, stopping when it reaches 100 lists. If it's `false`, then ///the lists are loaded in the opposite order. /// ///If the user has more than 100 lists total like this, you'll need to call `ownerships` and ///`subscriptions` separately to be able to properly load everything. pub async fn list<'id, T: Into<UserID>>( user: T, owned_first: bool, token: &auth::Token, ) -> Result<Response<Vec<List>>> { let params = ParamList::new() .add_name_param(user.into()) .add_param("reverse", owned_first.to_string()); let req = auth::get(links::lists::LIST, token, Some(¶ms)); request_with_json_response(req).await } ///Look up the lists the given user is subscribed to, but not ones the user made themselves. /// ///This function returns a `Stream` over the lists returned by Twitter. This method defaults to ///reeturning 20 lists in a single network call; the maximum is 1000. pub fn subscriptions<T: Into<UserID>>(user: T, token: &auth::Token) -> CursorIter<ListCursor> { let params = ParamList::new().add_name_param(user.into()); CursorIter::new(links::lists::SUBSCRIPTIONS, token, Some(params), Some(20)) } ///Look up the lists created by the given user. /// ///This function returns a `Stream` over the lists returned by Twitter. This method defaults to ///reeturning 20 lists in a single network call; the maximum is 1000. pub fn ownerships<T: Into<UserID>>(user: T, token: &auth::Token) -> CursorIter<ListCursor> { let params = ParamList::new().add_name_param(user.into()); CursorIter::new(links::lists::OWNERSHIPS, token, Some(params), Some(20)) } ///Look up information for a single list. pub async fn show(list: ListID, token: &auth::Token) -> Result<Response<List>> { let params = ParamList::new().add_list_param(list); let req = auth::get(links::lists::SHOW, token, Some(¶ms)); request_with_json_response(req).await } ///Look up the users that have been added to the given list. /// ///This function returns a `Stream` over the users returned by Twitter. This method defaults to ///reeturning 20 users in a single network call; the maximum is 5000. pub fn members<'a>(list: ListID, token: &auth::Token) -> CursorIter<UserCursor> { let params = ParamList::new().add_list_param(list); CursorIter::new(links::lists::MEMBERS, token, Some(params), Some(20)) } ///Look up the users that have subscribed to the given list. /// ///This function returns a `Stream` over the users returned by Twitter. This method defaults to ///reeturning 20 users in a single network call; the maximum is 5000. pub fn subscribers<'a>(list: ListID, token: &auth::Token) -> CursorIter<UserCursor> { let params = ParamList::new().add_list_param(list); CursorIter::new(links::lists::SUBSCRIBERS, token, Some(params), Some(20)) } ///Check whether the given user is subscribed to the given list. pub async fn is_subscribed<'id, T: Into<UserID>>( user: T, list: ListID, token: &auth::Token, ) -> Result<Response<bool>> { let params = ParamList::new() .add_list_param(list) .add_name_param(user.into()); let req = auth::get(links::lists::IS_SUBSCRIBER, token, Some(¶ms)); let out = request_with_json_response::<TwitterUser>(req).await; match out { Ok(user) => Ok(Response::map(user, |_| true)), Err(TwitterError(headers, terrs)) => { if terrs.errors.iter().any(|e| e.code == 109) { // here's a fun conundrum: since "is not in this list" is returned as an error code, // the rate limit info that would otherwise be part of the response isn't there. the // rate_headers method was factored out specifically for this location, since it's // still there, just accompanying an error response instead of a user. Ok(Response::new(RateLimit::try_from(&headers)?, false)) } else { Err(TwitterError(headers, terrs)) } } Err(err) => Err(err), } } ///Check whether the given user has been added to the given list. pub async fn is_member<'id, T: Into<UserID>>( user: T, list: ListID, token: &auth::Token, ) -> Result<Response<bool>> { let params = ParamList::new() .add_list_param(list) .add_name_param(user.into()); let req = auth::get(links::lists::IS_MEMBER, token, Some(¶ms)); let out = request_with_json_response::<TwitterUser>(req).await; match out { Ok(resp) => Ok(Response::map(resp, |_| true)), Err(TwitterError(headers, errors)) => { if errors.errors.iter().any(|e| e.code == 109) { // here's a fun conundrum: since "is not in this list" is returned as an error code, // the rate limit info that would otherwise be part of the response isn't there. the // rate_headers method was factored out specifically for this location, since it's // still there, just accompanying an error response instead of a user. Ok(Response::new(RateLimit::try_from(&headers)?, false)) } else { Err(TwitterError(headers, errors)) } } Err(err) => Err(err), } } ///Begin navigating the collection of tweets made by the users added to the given list. /// ///The interface for loading statuses from a list is exactly the same as loading from a personal ///timeline. see the [`Timeline`] docs for details. /// ///[`Timeline`]: ../tweet/struct.Timeline.html pub fn statuses(list: ListID, with_rts: bool, token: &auth::Token) -> tweet::Timeline { let params = ParamList::new() .add_list_param(list) .add_param("include_rts", with_rts.to_string()); tweet::Timeline::new(links::lists::STATUSES, Some(params), token) } ///Adds the given user to the given list. /// ///Note that lists cannot have more than 5000 members. /// ///Upon success, the future returned by this function yields the freshly-modified list. pub async fn add_member<'id, T: Into<UserID>>( list: ListID, user: T, token: &auth::Token, ) -> Result<Response<List>> { let params = ParamList::new() .add_list_param(list) .add_name_param(user.into()); let req = auth::post(links::lists::ADD, token, Some(¶ms)); request_with_json_response(req).await } ///Adds a set of users to the given list. /// ///The `members` param can be used the same way as the `accts` param in [`user::lookup`]. See that ///method's documentation for details. /// ///[`user::lookup`]: ../user/fn.lookup.html /// ///Note that you cannot add more than 100 members to a list at a time, and that lists in general ///cannot have more than 5000 members. /// ///When using this method, take care not to add and remove many members in rapid succession; there ///are no guarantees that the result of a `add_member_list` or `remove_member_list` will be ///immediately available for a corresponding removal or addition, respectively. pub async fn add_member_list<'id, T, I>( members: I, list: ListID, token: &auth::Token, ) -> Result<Response<List>> where T: Into<UserID>, I: IntoIterator<Item = T>, { let (id_param, name_param) = multiple_names_param(members); let params = ParamList::new() .add_list_param(list) .add_opt_param( "user_id", if !id_param.is_empty() { Some(id_param) } else { None }, ) .add_opt_param( "screen_name", if !name_param.is_empty() { Some(name_param) } else { None }, ); let req = auth::post(links::lists::ADD_LIST, token, Some(¶ms)); request_with_json_response(req).await } ///Removes the given user from the given list. pub async fn remove_member<'id, T: Into<UserID>>( list: ListID, user: T, token: &auth::Token, ) -> Result<Response<List>> { let params = ParamList::new() .add_list_param(list) .add_name_param(user.into()); let req = auth::post(links::lists::REMOVE_MEMBER, token, Some(¶ms)); request_with_json_response(req).await } ///Removes a set of users from the given list. /// ///The `members` param can be used the same way as the `accts` param in [`user::lookup`]. See that ///method's documentation for details. /// ///[`user::lookup`]: ../user/fn.lookup.html /// ///This method is limited to removing 100 members at a time. /// ///When using this method, take care not to add and remove many members in rapid succession; there ///are no guarantees that the result of a `add_member_list` or `remove_member_list` will be ///immediately available for a corresponding removal or addition, respectively. pub async fn remove_member_list<T, I>( members: I, list: ListID, token: &auth::Token, ) -> Result<Response<List>> where T: Into<UserID>, I: IntoIterator<Item = T>, { let (id_param, name_param) = multiple_names_param(members); let params = ParamList::new() .add_list_param(list) .add_opt_param( "user_id", if !id_param.is_empty() { Some(id_param) } else { None }, ) .add_opt_param( "screen_name", if !name_param.is_empty() { Some(name_param) } else { None }, ); let req = auth::post(links::lists::REMOVE_LIST, token, Some(¶ms)); request_with_json_response(req).await } ///Creates a list, with the given name, visibility, and description. /// ///The new list is owned by the authenticated user, and its slug can be created with their handle ///and the name given to `name`. Twitter places an upper limit on 1000 lists owned by a single ///account. pub async fn create( name: String, public: bool, desc: Option<String>, token: &auth::Token, ) -> Result<Response<List>> { let params = ParamList::new() .add_param("name", name) .add_param("mode", if public { "public" } else { "private" }) .add_opt_param("description", desc); let req = auth::post(links::lists::CREATE, token, Some(¶ms)); request_with_json_response(req).await } ///Deletes the given list. /// ///The authenticated user must have created the list. pub async fn delete(list: ListID, token: &auth::Token) -> Result<Response<List>> { let params = ParamList::new().add_list_param(list); let req = auth::post(links::lists::DELETE, token, Some(¶ms)); request_with_json_response(req).await } ///Subscribes the authenticated user to the given list. /// ///Subscribing to a list is a way to make it available in the "Lists" section of a user's profile ///without having to create it themselves. pub async fn subscribe(list: ListID, token: &auth::Token) -> Result<Response<List>> { let params = ParamList::new().add_list_param(list); let req = auth::post(links::lists::SUBSCRIBE, token, Some(¶ms)); request_with_json_response(req).await } ///Unsubscribes the authenticated user from the given list. pub async fn unsubscribe(list: ListID, token: &auth::Token) -> Result<Response<List>> { let params = ParamList::new().add_list_param(list); let req = auth::post(links::lists::UNSUBSCRIBE, token, Some(¶ms)); request_with_json_response(req).await } ///Begins updating a list's metadata. /// ///This method is exposed using a builder struct. See the [`ListUpdate`] docs for details. /// ///[`ListUpdate`]: struct.ListUpdate.html pub fn update(list: ListID) -> ListUpdate { ListUpdate { list: list, name: None, public: None, desc: None, } }