From dae723e612d62c1b9150f3f522ecf1fede2764ff Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Sat, 3 Dec 2016 07:20:43 -0800 Subject: Make rest::edit_profile set new token If a user account changes their profile settings, they'll receive a new token; update the locally stored token for REST requests with this token. Additionally, the `Context::edit_profile` does not need mutability over self. Fixes #25. --- src/client/context.rs | 4 ++-- src/client/rest/mod.rs | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'src/client') diff --git a/src/client/context.rs b/src/client/context.rs index bc983bf..6968233 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -828,7 +828,7 @@ impl Context { rest::edit_nickname(guild_id.into().0, new_nickname) } - /// Edits the current user's settings. + /// Edits the current user's profile settings. /// /// Refer to `EditProfile`'s documentation for its methods. /// @@ -840,7 +840,7 @@ impl Context { /// context.edit_member(|p| /// p.username("meew zero")); /// ``` - pub fn edit_profile EditProfile>(&mut self, f: F) + pub fn edit_profile EditProfile>(&self, f: F) -> Result { let user = try!(rest::get_current_user()); diff --git a/src/client/rest/mod.rs b/src/client/rest/mod.rs index a325f15..d5e5761 100644 --- a/src/client/rest/mod.rs +++ b/src/client/rest/mod.rs @@ -690,12 +690,30 @@ pub fn edit_note(user_id: u64, map: Value) -> Result<()> { user_id)) } -/// Edits profile we're connected to. +/// Edits the current user's profile settings. +/// +/// For bot users, the password is optional. +/// +/// # User Accounts +/// +/// If a new token is received due to a password change, then the stored token +/// internally will be updated. +/// +/// **Note**: this token change may cause requests made between the actual token +/// change and when the token is internally changed to be invalid requests, as +/// the token may be outdated. +/// pub fn edit_profile(map: Value) -> Result { let body = try!(serde_json::to_string(&map)); let response = request!(Route::UsersMe, patch(body), "/users/@me"); - CurrentUser::decode(try!(serde_json::from_reader(response))) + let mut map: BTreeMap = try!(serde_json::from_reader(response)); + + if let Some(Value::String(token)) = map.remove("token") { + set_token(&token) + } + + CurrentUser::decode(Value::Object(map)) } /// Changes a role in a guild. -- cgit v1.2.3