Add displayname and avatar_url endpoints

Add PUT and GET /_matrix/client/r0/profile/{userId}/displayname Endpoint
Add PUT and GET /_matrix/client/r0/profile/{userId}/avatar_url Endpoint
Add GET /_matrix/client/r0/profile/{userId} Endpoint

Took 2 hours 16 minutes
This commit is contained in:
Marcel 2020-04-09 18:49:27 +02:00
parent 11e75e7081
commit 062c5521f0
3 changed files with 189 additions and 1 deletions

View file

@ -26,6 +26,9 @@ use ruma_client_api::{
membership::{join_room_by_id, join_room_by_id_or_alias},
message::create_message_event,
presence::set_presence,
profile::{
get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name,
},
push::get_pushrules_all,
room::create_room,
session::{get_login_types, login},
@ -39,7 +42,12 @@ use ruma_events::{collections::only::Event, EventType};
use ruma_identifiers::{RoomId, RoomIdOrAliasId, UserId};
use ruma_wrapper::{MatrixResult, Ruma};
use serde_json::json;
use std::{collections::HashMap, convert::TryInto, path::PathBuf, time::Duration};
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
path::PathBuf,
time::Duration,
};
const GUEST_NAME_LENGTH: usize = 10;
const DEVICE_ID_LENGTH: usize = 10;
@ -282,6 +290,127 @@ fn get_global_account_data_route(
}))
}
#[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "<body>")]
fn set_displayname_route(
data: State<Data>,
body: Ruma<set_display_name::Request>,
_user_id: String,
) -> MatrixResult<set_display_name::Response> {
let user_id = body.user_id.clone().expect("user is authenticated");
if body.displayname.is_none() {
debug!("Request was missing the displayname payload.");
return MatrixResult(Err(Error {
kind: ErrorKind::MissingParam,
message: "Missing displayname".to_owned(),
status_code: http::StatusCode::BAD_REQUEST,
}));
}
data.displayname_set(&user_id, body.displayname.clone());
// TODO send a new m.room.member join event with the updated displayname
// TODO send a new m.presence event with the updated displayname
MatrixResult(Ok(set_display_name::Response))
}
#[get(
"/_matrix/client/r0/profile/<user_id_raw>/displayname",
data = "<body>"
)]
fn get_displayname_route(
data: State<Data>,
body: Ruma<get_display_name::Request>,
user_id_raw: String,
) -> MatrixResult<get_display_name::Response> {
let user_id = (*body).user_id.clone();
if let Some(displayname) = data.displayname_get(&user_id) {
return MatrixResult(Ok(get_display_name::Response {
displayname: Some(displayname),
}));
}
// Return 404 if we don't have any
debug!("Profile was not found.");
MatrixResult(Err(Error {
kind: ErrorKind::NotFound,
message: "Profile was not found".to_owned(),
status_code: http::StatusCode::NOT_FOUND,
}))
}
#[put("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "<body>")]
fn set_avatar_url_route(
data: State<Data>,
body: Ruma<set_avatar_url::Request>,
_user_id: String,
) -> MatrixResult<set_avatar_url::Response> {
let user_id = body.user_id.clone().expect("user is authenticated");
if body.avatar_url == "" {
debug!("Request was missing the avatar_url payload.");
return MatrixResult(Err(Error {
kind: ErrorKind::MissingParam,
message: "Missing avatar_url".to_owned(),
status_code: http::StatusCode::BAD_REQUEST,
}));
}
// TODO in the future when we can handle media uploads make sure that this url is our own server
// TODO also make sure this is mxc:// format
data.avatar_url_set(&user_id, body.avatar_url.clone());
// TODO send a new m.room.member join event with the updated avatar_url
// TODO send a new m.presence event with the updated avatar_url
MatrixResult(Ok(set_avatar_url::Response))
}
#[get("/_matrix/client/r0/profile/<user_id_raw>/avatar_url", data = "<body>")]
fn get_avatar_url_route(
data: State<Data>,
body: Ruma<get_avatar_url::Request>,
user_id_raw: String,
) -> MatrixResult<get_avatar_url::Response> {
let user_id = (*body).user_id.clone();
if let Some(avatar_url) = data.avatar_url_get(&user_id) {
return MatrixResult(Ok(get_avatar_url::Response {
avatar_url: Some(avatar_url),
}));
}
// Return 404 if we don't have a profile for this id
debug!("Profile was not found.");
MatrixResult(Err(Error {
kind: ErrorKind::NotFound,
message: "Profile was not found".to_owned(),
status_code: http::StatusCode::NOT_FOUND,
}))
}
#[get("/_matrix/client/r0/profile/<user_id_raw>", data = "<body>")]
fn get_profile_route(
data: State<Data>,
body: Ruma<get_profile::Request>,
user_id_raw: String,
) -> MatrixResult<get_profile::Response> {
let user_id = (*body).user_id.clone();
let avatar_url = data.avatar_url_get(&user_id);
let displayname = data.displayname_get(&user_id);
if avatar_url.is_some() && displayname.is_some() {
return MatrixResult(Ok(get_profile::Response {
avatar_url,
displayname,
}));
}
// Return 404 if we don't have a profile for this id
debug!("Profile was not found.");
MatrixResult(Err(Error {
kind: ErrorKind::NotFound,
message: "Profile was not found".to_owned(),
status_code: http::StatusCode::NOT_FOUND,
}))
}
#[put("/_matrix/client/r0/presence/<_user_id>/status", data = "<body>")]
fn set_presence_route(
body: Ruma<set_presence::Request>,
@ -634,6 +763,11 @@ fn main() {
create_filter_route,
set_global_account_data_route,
get_global_account_data_route,
set_displayname_route,
get_displayname_route,
set_avatar_url_route,
get_avatar_url_route,
get_profile_route,
set_presence_route,
get_keys_route,
upload_keys_route,