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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// 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 common::*;
use super::*;

use std::collections::HashMap;

use auth;
use cursor::{CursorIter, UserCursor, ListCursor};
use error::Error::TwitterError;
use links;
use user::{UserID, TwitterUser};
use 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<'a, T: Into<UserID<'a>>>(user: T, token: &auth::Token, handle: &Handle)
    -> CursorIter<'a, ListCursor>
{
    let mut params = HashMap::new();
    add_name_param(&mut params, &user.into());
    CursorIter::new(links::lists::MEMBERSHIPS, token, handle, 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 fn list<'id, T: Into<UserID<'id>>>(user: T,
                                       owned_first: bool,
                                       token: &auth::Token,
                                       handle: &Handle)
    -> FutureResponse<Vec<List>>
{
    let mut params = HashMap::new();
    add_name_param(&mut params, &user.into());
    add_param(&mut params, "reverse", owned_first.to_string());

    let req = auth::get(links::lists::LIST, token, Some(&params));

    make_parsed_future(handle, req)
}

///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<'a, T: Into<UserID<'a>>>(user: T, token: &auth::Token, handle: &Handle)
    -> CursorIter<'a, ListCursor>
{
    let mut params = HashMap::new();
    add_name_param(&mut params, &user.into());
    CursorIter::new(links::lists::SUBSCRIPTIONS, token, handle, 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<'a, T: Into<UserID<'a>>>(user: T, token: &auth::Token, handle: &Handle)
    -> CursorIter<'a, ListCursor>
{
    let mut params = HashMap::new();
    add_name_param(&mut params, &user.into());
    CursorIter::new(links::lists::OWNERSHIPS, token, handle, Some(params), Some(20))
}

///Look up information for a single list.
pub fn show(list: ListID, token: &auth::Token, handle: &Handle) -> FutureResponse<List> {
    let mut params = HashMap::new();

    add_list_param(&mut params, &list);

    let req = auth::get(links::lists::SHOW, token, Some(&params));

    make_parsed_future(handle, req)
}

///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<'a>, token: &auth::Token, handle: &Handle)
    -> CursorIter<'a, UserCursor>
{
    let mut params = HashMap::new();

    add_list_param(&mut params, &list);

    CursorIter::new(links::lists::MEMBERS, token, handle, 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<'a>, token: &auth::Token, handle: &Handle)
    -> CursorIter<'a, UserCursor>
{
    let mut params = HashMap::new();

    add_list_param(&mut params, &list);

    CursorIter::new(links::lists::SUBSCRIBERS, token, handle, Some(params), Some(20))
}

///Check whether the given user is subscribed to the given list.
pub fn is_subscribed<'id, T: Into<UserID<'id>>>(user: T,
                                                list: ListID,
                                                token: &auth::Token,
                                                handle: &Handle) ->
    FutureResponse<bool>
{
    let mut params = HashMap::new();

    add_list_param(&mut params, &list);
    add_name_param(&mut params, &user.into());

    let req = auth::get(links::lists::IS_SUBSCRIBER, token, Some(&params));

    fn parse_resp(full_resp: String, headers: &Headers) -> Result<Response<bool>, error::Error> {
        let out: WebResponse<TwitterUser> = make_response(full_resp, headers);

        match out {
            Ok(user) => Ok(Response::map(user, |_| true)),
            Err(TwitterError(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::map(rate_headers(headers), |_| false))
                } else {
                    Err(TwitterError(terrs))
                }
            },
            Err(err) => Err(err),
        }
    }

    make_future(handle, req, parse_resp)
}

///Check whether the given user has been added to the given list.
pub fn is_member<'id, T: Into<UserID<'id>>>(user: T,
                                            list: ListID,
                                            token: &auth::Token,
                                            handle: &Handle) ->
    FutureResponse<bool>
{
    let mut params = HashMap::new();

    add_list_param(&mut params, &list);
    add_name_param(&mut params, &user.into());

    let req = auth::get(links::lists::IS_MEMBER, token, Some(&params));

    fn parse_resp(full_resp: String, headers: &Headers) -> Result<Response<bool>, error::Error> {
        let out: WebResponse<TwitterUser> = make_response(full_resp, headers);

        match out {
            Ok(user) => Ok(Response::map(user, |_| true)),
            Err(TwitterError(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::map(rate_headers(headers), |_| false))
                } else {
                    Err(TwitterError(terrs))
                }
            },
            Err(err) => Err(err),
        }
    }

    make_future(handle, req, parse_resp)
}

///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<'a>(list: ListID<'a>, with_rts: bool, token: &auth::Token, handle: &Handle)
    -> tweet::Timeline<'a>
{
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);
    add_param(&mut params, "include_rts", with_rts.to_string());

    tweet::Timeline::new(links::lists::STATUSES, Some(params), token, handle)
}

///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 fn add_member<'id, T: Into<UserID<'id>>>(list: ListID,
                                             user: T,
                                             token: &auth::Token,
                                             handle: &Handle)
    -> FutureResponse<List>
{
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);
    add_name_param(&mut params, &user.into());

    let req = auth::post(links::lists::ADD, token, Some(&params));

    make_parsed_future(handle, req)
}

///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 fn add_member_list<'id, T, I>(members: I,
                                  list: ListID,
                                  token: &auth::Token,
                                  handle: &Handle)
    -> FutureResponse<List>
    where T: Into<UserID<'id>>, I: IntoIterator<Item=T>
{
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);

    let (id_param, name_param) = multiple_names_param(members);
    if !id_param.is_empty() {
        add_param(&mut params, "user_id", id_param);
    }
    if !name_param.is_empty() {
        add_param(&mut params, "screen_name", name_param);
    }

    let req = auth::post(links::lists::ADD_LIST, token, Some(&params));

    make_parsed_future(handle, req)
}

///Removes the given user from the given list.
pub fn remove_member<'id, T: Into<UserID<'id>>>(list: ListID,
                                                user: T,
                                                token: &auth::Token,
                                                handle: &Handle)
    -> FutureResponse<List>
{
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);
    add_name_param(&mut params, &user.into());

    let req = auth::post(links::lists::REMOVE_MEMBER, token, Some(&params));

    make_parsed_future(handle, req)
}

///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 fn remove_member_list<'a, T, I>(members: I, list: ListID, token: &auth::Token, handle: &Handle)
    -> FutureResponse<List>
    where T: Into<UserID<'a>>, I: IntoIterator<Item=T>
{
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);

    let (id_param, name_param) = multiple_names_param(members);
    if !id_param.is_empty() {
        add_param(&mut params, "user_id", id_param);
    }
    if !name_param.is_empty() {
        add_param(&mut params, "screen_name", name_param);
    }

    let req = auth::post(links::lists::REMOVE_LIST, token, Some(&params));

    make_parsed_future(handle, req)
}

///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 fn create(name: &str, public: bool, desc: Option<&str>, token: &auth::Token, handle: &Handle)
    -> FutureResponse<List>
{
    let mut params = HashMap::new();
    add_param(&mut params, "name", name);
    if public {
        add_param(&mut params, "mode", "public");
    } else {
        add_param(&mut params, "mode", "private");
    }
    if let Some(desc) = desc {
        add_param(&mut params, "description", desc);
    }

    let req = auth::post(links::lists::CREATE, token, Some(&params));

    make_parsed_future(handle, req)
}

///Deletes the given list.
///
///The authenticated user must have created the list.
pub fn delete(list: ListID, token: &auth::Token, handle: &Handle) -> FutureResponse<List> {
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);

    let req = auth::post(links::lists::DELETE, token, Some(&params));

    make_parsed_future(handle, req)
}

///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 fn subscribe(list: ListID, token: &auth::Token, handle: &Handle) -> FutureResponse<List> {
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);

    let req = auth::post(links::lists::SUBSCRIBE, token, Some(&params));

    make_parsed_future(handle, req)
}

///Unsubscribes the authenticated user from the given list.
pub fn unsubscribe(list: ListID, token: &auth::Token, handle: &Handle) -> FutureResponse<List> {
    let mut params = HashMap::new();
    add_list_param(&mut params, &list);

    let req = auth::post(links::lists::UNSUBSCRIBE, token, Some(&params));

    make_parsed_future(handle, req)
}

///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<'a>(list: ListID<'a>) -> ListUpdate<'a> {
    ListUpdate {
        list: list,
        name: None,
        public: None,
        desc: None,
    }
}