egg_mode/account/mod.rs
1//! Functionality to alter a user's public profile.
2//!
3//! Specifically, this module contains functions which update the information
4//! that is publically visible on a user's timeline (e.g. name, location). This module does *not*
5//! modify a user's private account settings (e.g. email, password).
6
7use crate::{
8 auth,
9 common::{post, request_with_empty_response, request_with_json_response, ParamList},
10 error, links,
11 user::TwitterUser,
12 Response,
13};
14
15/// Options for updating the profile banner
16#[derive(Debug, Default)]
17pub struct ProfileBannerOption {
18 /// The width of the preferred section of the image being uploaded in pixels.
19 /// Use with height , offset_left , and offset_top to select the desired region of the image to use.
20 pub width: Option<String>,
21 /// The height of the preferred section of the image being uploaded in pixels.
22 /// Use with width , offset_left , and offset_top to select the desired region of the image to use.
23 pub height: Option<String>,
24 /// The number of pixels by which to offset the uploaded image from the left.
25 /// Use with height , width , and offset_top to select the desired region of the image to use.
26 pub offset_left: Option<String>,
27 /// The number of pixels by which to offset the uploaded image from the top.
28 /// Use with height , width , and offset_left to select the desired region of the image to use.
29 pub offset_top: Option<String>,
30}
31
32/// Options for updating the user profile
33#[derive(Debug, Default)]
34pub struct UserProfile {
35 /// Full name associated with the profile.
36 pub name: Option<String>,
37 /// URL associated with the profile. Will be prepended with http:// if not present.
38 pub url: Option<String>,
39 /// The city or country describing where the user of the account is located. The contents are not normalized or geocoded in any way.
40 pub location: Option<String>,
41 /// A description of the user owning the account.
42 pub description: Option<String>,
43 /// Sets a hex value that controls the color scheme of links used on the authenticating user's profile page on twitter.com.
44 /// This must be a valid hexadecimal value, and may be either three or six characters (ex: F00 or FF0000).
45 /// This parameter replaces the deprecated (and separate) update_profile_colors API method.
46 pub profile_link_color: Option<String>,
47}
48
49/// Updates the authenticating user's profile image.
50///
51/// This function takes the image as a slice of bytes. This slice must be a valid GIF, JPG or PNG image.
52/// Note that this method expects raw multipart data, not a URL to an image.
53///
54/// This method asynchronously processes the uploaded file before updating the user's profile image URL.
55/// You can either update your local cache the next time you request the user's information, or, at least 5 seconds after uploading the image, ask for the updated URL using GET users / show.
56pub async fn update_profile_image(
57 image: &[u8],
58 token: &auth::Token,
59) -> error::Result<Response<TwitterUser>> {
60 let params = ParamList::new().add_param("image", base64::encode(image));
61 let req = post(links::account::UPDATE_PROFILE_IMAGE, token, Some(¶ms));
62 request_with_json_response(req).await
63}
64
65/// Uploads a profile banner on behalf of the authenticating user.
66///
67/// This function takes the banner as a slice of bytes. This slice must be a valid GIF, JPG or PNG image.
68/// More information about sizing variations can be found in User Profile Images and Banners and GET users / profile_banner.
69///
70/// Profile banner images are processed asynchronously.
71/// The profile_banner_url and its variant sizes will not necessary be available directly after upload.
72pub async fn update_profile_banner(
73 banner: &[u8],
74 options: Option<ProfileBannerOption>,
75 token: &auth::Token,
76) -> error::Result<Response<()>> {
77 let params = match options {
78 Some(o) => ParamList::new()
79 .add_param("banner", base64::encode(banner))
80 .add_opt_param("width", o.width)
81 .add_opt_param("height", o.height)
82 .add_opt_param("offset_top", o.offset_top)
83 .add_opt_param("offset_left", o.offset_left),
84 None => ParamList::new().add_param("banner", base64::encode(banner)),
85 };
86
87 let req = post(links::account::UPDATE_PROFILE_BNNER, token, Some(¶ms));
88
89 request_with_empty_response(req).await
90}
91
92/// Sets some values that users are able to set under the "Account" tab of their settings page.
93/// Only the parameters specified will be updated.
94pub async fn update_profile(
95 user_profile: UserProfile,
96 token: &auth::Token,
97) -> error::Result<Response<TwitterUser>> {
98 let params = ParamList::new()
99 .add_opt_param("name", user_profile.name)
100 .add_opt_param("url", user_profile.url)
101 .add_opt_param("location", user_profile.location)
102 .add_opt_param("description", user_profile.description)
103 .add_opt_param("profile_link_color", user_profile.profile_link_color);
104
105 let req = post(links::account::UPDATE_PROFILE, token, Some(¶ms));
106
107 request_with_json_response(req).await
108}