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:
parent
11e75e7081
commit
062c5521f0
3 changed files with 189 additions and 1 deletions
34
src/data.rs
34
src/data.rs
|
@ -60,6 +60,40 @@ impl Data {
|
|||
.map(|bytes| utils::string_from_bytes(&bytes))
|
||||
}
|
||||
|
||||
/// Set a new displayname.
|
||||
pub fn displayname_set(&self, user_id: &UserId, displayname: Option<String>) {
|
||||
self.db
|
||||
.profile_displayname
|
||||
.insert(user_id.to_string(), &*displayname.unwrap_or_default())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Get a the displayname of a user.
|
||||
pub fn displayname_get(&self, user_id: &UserId) -> Option<String> {
|
||||
self.db
|
||||
.profile_displayname
|
||||
.get(user_id.to_string())
|
||||
.unwrap()
|
||||
.map(|bytes| utils::string_from_bytes(&bytes))
|
||||
}
|
||||
|
||||
/// Set a new avatar_url.
|
||||
pub fn avatar_url_set(&self, user_id: &UserId, avatar_url: String) {
|
||||
self.db
|
||||
.profile_avatar_url
|
||||
.insert(user_id.to_string(), &*avatar_url)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Get a the avatar_url of a user.
|
||||
pub fn avatar_url_get(&self, user_id: &UserId) -> Option<String> {
|
||||
self.db
|
||||
.profile_avatar_url
|
||||
.get(user_id.to_string())
|
||||
.unwrap()
|
||||
.map(|bytes| utils::string_from_bytes(&bytes))
|
||||
}
|
||||
|
||||
/// Add a new device to a user.
|
||||
pub fn device_add(&self, user_id: &UserId, device_id: &str) {
|
||||
if self
|
||||
|
|
|
@ -52,6 +52,8 @@ impl MultiValue {
|
|||
pub struct Database {
|
||||
pub userid_password: sled::Tree,
|
||||
pub userid_deviceids: MultiValue,
|
||||
pub profile_displayname: sled::Tree,
|
||||
pub profile_avatar_url: sled::Tree,
|
||||
pub deviceid_token: sled::Tree,
|
||||
pub token_userid: sled::Tree,
|
||||
pub pduid_pdus: sled::Tree,
|
||||
|
@ -75,6 +77,8 @@ impl Database {
|
|||
Self {
|
||||
userid_password: db.open_tree("userid_password").unwrap(),
|
||||
userid_deviceids: MultiValue(db.open_tree("userid_deviceids").unwrap()),
|
||||
profile_displayname: db.open_tree("profile_displayname").unwrap(),
|
||||
profile_avatar_url: db.open_tree("profile_avatar_url").unwrap(),
|
||||
deviceid_token: db.open_tree("deviceid_token").unwrap(),
|
||||
token_userid: db.open_tree("token_userid").unwrap(),
|
||||
pduid_pdus: db.open_tree("pduid_pdus").unwrap(),
|
||||
|
@ -103,6 +107,22 @@ impl Database {
|
|||
String::from_utf8_lossy(&v),
|
||||
);
|
||||
}
|
||||
println!("# AccountData -> Displayname:");
|
||||
for (k, v) in self.profile_displayname.iter().map(|r| r.unwrap()) {
|
||||
println!(
|
||||
"{:?} -> {:?}",
|
||||
String::from_utf8_lossy(&k),
|
||||
String::from_utf8_lossy(&v),
|
||||
);
|
||||
}
|
||||
println!("# AccountData -> AvatarURL:");
|
||||
for (k, v) in self.profile_avatar_url.iter().map(|r| r.unwrap()) {
|
||||
println!(
|
||||
"{:?} -> {:?}",
|
||||
String::from_utf8_lossy(&k),
|
||||
String::from_utf8_lossy(&v),
|
||||
);
|
||||
}
|
||||
println!("\n# DeviceId -> Token:");
|
||||
for (k, v) in self.deviceid_token.iter().map(|r| r.unwrap()) {
|
||||
println!(
|
||||
|
|
136
src/main.rs
136
src/main.rs
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue