@ -1,4 +1,5 @@
#![ feature(proc_macro_hygiene, decl_macro) ]
mod data ;
mod database ;
mod pdu ;
@ -297,19 +298,29 @@ fn set_displayname_route(
_user_id : String ,
) -> MatrixResult < set_display_name ::Response > {
let user_id = body . user_id . clone ( ) . expect ( "user is authenticated" ) ;
// Send error on None and accept Some("") as valid username
// Synapse returns a parsing error but the spec doesn't require this
if body . displayname . is_none ( ) {
debug ! ( "Request was missing the displayname payload." ) ;
return MatrixResult ( Err ( Error {
kind : ErrorKind ::MissingParam ,
message : "Missing displayname ". to_owned ( ) ,
message : "Missing displayname . ". to_owned ( ) ,
status_code : http ::StatusCode ::BAD_REQUEST ,
} ) ) ;
}
if let Some ( displayname ) = body . displayname {
if displayname = = "" {
data . displayname_remove ( & user_id ) ;
} else {
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 ) )
}
@ -323,20 +334,26 @@ fn get_displayname_route(
user_id_raw : String ,
) -> MatrixResult < get_display_name ::Response > {
let user_id = ( * body ) . user_id . clone ( ) ;
if ! data . user_exists ( & user_id ) {
// 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 ,
} ) )
}
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 ,
MatrixResult ( Ok ( get_display_name ::Response {
displayname : None ,
} ) )
}
#[ put( " /_matrix/client/r0/profile/<_user_id>/avatar_url " , data = " <body> " ) ]
fn set_avatar_url_route (
data : State < Data > ,
@ -344,22 +361,29 @@ fn set_avatar_url_route(
_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." ) ;
if ! body . avatar_url . starts_with ( "mxc://" ) {
debug ! ( "Request contains an invalid avatar_url." ) ;
return MatrixResult ( Err ( Error {
kind : ErrorKind ::Missing Param,
kind : ErrorKind ::Invalid Param,
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
// TODO also make sure this is valid mxc:// format (not only starting with it)
data . avatar_url_set ( & user_id , body . avatar_url . clone ( ) ) ;
if body . avatar_url = = "" {
data . avatar_url_remove ( & user_id ) ;
} else {
data . avatar_url_set ( & user_id , body . displayname . 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 ) )
}
@ -370,12 +394,7 @@ fn get_avatar_url_route(
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 ) ,
} ) ) ;
}
if ! data . user_exists ( & user_id ) {
// Return 404 if we don't have a profile for this id
debug ! ( "Profile was not found." ) ;
MatrixResult ( Err ( Error {
@ -384,6 +403,16 @@ fn get_avatar_url_route(
status_code : http ::StatusCode ::NOT_FOUND ,
} ) )
}
if let Some ( avatar_url ) = data . avatar_url_get ( & user_id ) {
return MatrixResult ( Ok ( get_avatar_url ::Response {
avatar_url : Some ( avatar_url ) ,
} ) ) ;
}
MatrixResult ( Ok ( get_avatar_url ::Response {
avatar_url : None ,
} ) )
}
#[ get( " /_matrix/client/r0/profile/<user_id_raw> " , data = " <body> " ) ]
fn get_profile_route (
@ -395,7 +424,7 @@ fn get_profile_route(
let avatar_url = data . avatar_url_get ( & user_id ) ;
let displayname = data . displayname_get ( & user_id ) ;
if avatar_url . is_some ( ) && displayname . is_some ( ) {
if avatar_url . is_some ( ) || displayname . is_some ( ) {
return MatrixResult ( Ok ( get_profile ::Response {
avatar_url ,
displayname ,