feat: user renaming
This commit is contained in:
parent
720cc0cffc
commit
4cc0a07092
5 changed files with 98 additions and 42 deletions
|
@ -38,7 +38,7 @@ use ruma_identifiers::{RoomId, UserId};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::{
|
use std::{
|
||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
convert::TryInto,
|
convert::{TryFrom, TryInto},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
};
|
};
|
||||||
|
@ -325,8 +325,7 @@ pub fn set_displayname_route(
|
||||||
if displayname == "" {
|
if displayname == "" {
|
||||||
data.displayname_remove(&user_id);
|
data.displayname_remove(&user_id);
|
||||||
} else {
|
} else {
|
||||||
data.displayname_set(&user_id, body.displayname.clone());
|
data.displayname_set(&user_id, 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
|
// TODO send a new m.presence event with the updated displayname
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -610,26 +609,34 @@ pub fn create_room_route(
|
||||||
MatrixResult(Ok(create_room::Response { room_id }))
|
MatrixResult(Ok(create_room::Response { room_id }))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/_matrix/client/r0/directory/room/<room_alias>")]
|
#[get("/_matrix/client/r0/directory/room/<_room_alias>", data = "<body>")]
|
||||||
pub fn get_alias_route(room_alias: String) -> MatrixResult<get_alias::Response> {
|
pub fn get_alias_route(
|
||||||
|
data: State<Data>,
|
||||||
|
body: Ruma<get_alias::Request>,
|
||||||
|
_room_alias: String,
|
||||||
|
) -> MatrixResult<get_alias::Response> {
|
||||||
// TODO
|
// TODO
|
||||||
let room_id = match &*room_alias {
|
let room_id = if body.room_alias.server_name() == data.hostname() {
|
||||||
"#room:localhost" => "!xclkjvdlfj:localhost",
|
match body.room_alias.alias() {
|
||||||
_ => {
|
"conduit" => "!lgOCCXQKtXOAPlAlG5:conduit.rs",
|
||||||
debug!("Room not found.");
|
_ => {
|
||||||
return MatrixResult(Err(Error {
|
debug!("Room alias not found.");
|
||||||
kind: ErrorKind::NotFound,
|
return MatrixResult(Err(Error {
|
||||||
message: "Room not found.".to_owned(),
|
kind: ErrorKind::NotFound,
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
message: "Room not found.".to_owned(),
|
||||||
}));
|
status_code: http::StatusCode::NOT_FOUND,
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
todo!("ask remote server");
|
||||||
}
|
}
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
MatrixResult(Ok(get_alias::Response {
|
MatrixResult(Ok(get_alias::Response {
|
||||||
room_id,
|
room_id,
|
||||||
servers: vec!["localhost".to_owned()],
|
servers: vec!["conduit.rs".to_owned()],
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -661,21 +668,19 @@ pub fn join_room_by_id_or_alias_route(
|
||||||
body: Ruma<join_room_by_id_or_alias::Request>,
|
body: Ruma<join_room_by_id_or_alias::Request>,
|
||||||
_room_id_or_alias: String,
|
_room_id_or_alias: String,
|
||||||
) -> MatrixResult<join_room_by_id_or_alias::Response> {
|
) -> MatrixResult<join_room_by_id_or_alias::Response> {
|
||||||
let room_id = if body.room_id_or_alias.is_room_alias_id() {
|
let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) {
|
||||||
match body.room_id_or_alias.as_ref() {
|
Ok(room_id) => room_id,
|
||||||
"#room:localhost" => "!xclkjvdlfj:localhost".try_into().unwrap(),
|
Err(room_alias) => if room_alias.server_name() == data.hostname() {
|
||||||
_ => {
|
return MatrixResult(Err(Error {
|
||||||
debug!("Room not found.");
|
kind: ErrorKind::NotFound,
|
||||||
return MatrixResult(Err(Error {
|
message: "Room alias not found.".to_owned(),
|
||||||
kind: ErrorKind::NotFound,
|
status_code: http::StatusCode::NOT_FOUND,
|
||||||
message: "Room not found.".to_owned(),
|
}));
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
} else {
|
||||||
}));
|
// Ask creator server of the room to join TODO ask someone else when not available
|
||||||
}
|
//server_server::send_request(data, destination, request)
|
||||||
|
todo!();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
todo!();
|
|
||||||
//body.room_id_or_alias.try_into().unwrap()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if data.room_join(
|
if data.room_join(
|
||||||
|
@ -758,9 +763,9 @@ pub async fn get_public_rooms_filtered_route(
|
||||||
chunk.extend_from_slice(
|
chunk.extend_from_slice(
|
||||||
&server_server::send_request(
|
&server_server::send_request(
|
||||||
&data,
|
&data,
|
||||||
"chat.privacytools.io".to_owned(),
|
"privacytools.io".to_owned(),
|
||||||
ruma_federation_api::v1::get_public_rooms::Request {
|
ruma_federation_api::v1::get_public_rooms::Request {
|
||||||
limit: None,
|
limit: Some(20_u32.into()),
|
||||||
since: None,
|
since: None,
|
||||||
include_all_networks: None,
|
include_all_networks: None,
|
||||||
third_party_instance_id: None,
|
third_party_instance_id: None,
|
||||||
|
|
37
src/data.rs
37
src/data.rs
|
@ -91,11 +91,21 @@ impl Data {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a new displayname.
|
/// Set a new displayname.
|
||||||
pub fn displayname_set(&self, user_id: &UserId, displayname: Option<String>) {
|
pub fn displayname_set(&self, user_id: &UserId, displayname: String) {
|
||||||
self.db
|
self.db
|
||||||
.userid_displayname
|
.userid_displayname
|
||||||
.insert(user_id.to_string(), &*displayname.unwrap_or_default())
|
.insert(user_id.to_string(), &*displayname)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
for room_id in self.rooms_joined(user_id) {
|
||||||
|
self.pdu_append(
|
||||||
|
room_id.clone(),
|
||||||
|
user_id.clone(),
|
||||||
|
EventType::RoomMember,
|
||||||
|
json!({"membership": "join", "displayname": displayname}),
|
||||||
|
None,
|
||||||
|
Some(user_id.to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a the displayname of a user.
|
/// Get a the displayname of a user.
|
||||||
|
@ -200,11 +210,19 @@ impl Data {
|
||||||
room_id.to_string().as_bytes().into(),
|
room_id.to_string().as_bytes().into(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut content = json!({"membership": "join"});
|
||||||
|
if let Some(displayname) = self.displayname_get(user_id) {
|
||||||
|
content
|
||||||
|
.as_object_mut()
|
||||||
|
.unwrap()
|
||||||
|
.insert("displayname".to_owned(), displayname.into());
|
||||||
|
}
|
||||||
|
|
||||||
self.pdu_append(
|
self.pdu_append(
|
||||||
room_id.clone(),
|
room_id.clone(),
|
||||||
user_id.clone(),
|
user_id.clone(),
|
||||||
EventType::RoomMember,
|
EventType::RoomMember,
|
||||||
json!({"membership": "join"}),
|
content,
|
||||||
None,
|
None,
|
||||||
Some(user_id.to_string()),
|
Some(user_id.to_string()),
|
||||||
);
|
);
|
||||||
|
@ -429,6 +447,17 @@ impl Data {
|
||||||
.unwrap_or(0_u64)
|
.unwrap_or(0_u64)
|
||||||
+ 1;
|
+ 1;
|
||||||
|
|
||||||
|
let mut unsigned = unsigned.unwrap_or_default();
|
||||||
|
// TODO: Optimize this to not load the whole room state?
|
||||||
|
if let Some(state_key) = &state_key {
|
||||||
|
if let Some(prev_pdu) = self
|
||||||
|
.room_state(&room_id)
|
||||||
|
.get(&(event_type.clone(), state_key.clone()))
|
||||||
|
{
|
||||||
|
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut pdu = PduEvent {
|
let mut pdu = PduEvent {
|
||||||
event_id: EventId::try_from("$thiswillbefilledinlater").unwrap(),
|
event_id: EventId::try_from("$thiswillbefilledinlater").unwrap(),
|
||||||
room_id: room_id.clone(),
|
room_id: room_id.clone(),
|
||||||
|
@ -442,7 +471,7 @@ impl Data {
|
||||||
depth: depth.try_into().unwrap(),
|
depth: depth.try_into().unwrap(),
|
||||||
auth_events: Vec::new(),
|
auth_events: Vec::new(),
|
||||||
redacts: None,
|
redacts: None,
|
||||||
unsigned: unsigned.unwrap_or_default(),
|
unsigned,
|
||||||
hashes: ruma_federation_api::EventHash {
|
hashes: ruma_federation_api::EventHash {
|
||||||
sha256: "aaa".to_owned(),
|
sha256: "aaa".to_owned(),
|
||||||
},
|
},
|
||||||
|
|
|
@ -76,7 +76,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
let data = Data::load_or_create("matrixtesting.koesters.xyz:14004");
|
let data = Data::load_or_create("conduit.rs");
|
||||||
//data.debug();
|
//data.debug();
|
||||||
|
|
||||||
setup_rocket(data).launch().unwrap();
|
setup_rocket(data).launch().unwrap();
|
||||||
|
|
|
@ -164,3 +164,11 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T, E> Deref for MatrixResult<T, E> {
|
||||||
|
type Target = Result<T, E>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -12,6 +12,22 @@ use std::{
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub async fn request_well_known(data: &crate::Data, destination: &str) -> Option<String> {
|
||||||
|
let body: serde_json::Value = serde_json::from_str(&data
|
||||||
|
.reqwest_client()
|
||||||
|
.get(&format!(
|
||||||
|
"https://{}/.well-known/matrix/server",
|
||||||
|
destination
|
||||||
|
))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.ok()?
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.ok()?).ok()?;
|
||||||
|
Some(body.get("m.server")?.as_str()?.to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn send_request<T: Endpoint>(
|
pub async fn send_request<T: Endpoint>(
|
||||||
data: &crate::Data,
|
data: &crate::Data,
|
||||||
destination: String,
|
destination: String,
|
||||||
|
@ -19,7 +35,8 @@ pub async fn send_request<T: Endpoint>(
|
||||||
) -> Option<T::Response> {
|
) -> Option<T::Response> {
|
||||||
let mut http_request: http::Request<_> = request.try_into().unwrap();
|
let mut http_request: http::Request<_> = request.try_into().unwrap();
|
||||||
|
|
||||||
*http_request.uri_mut() = format!("https://{}:8448{}", &destination.clone(), T::METADATA.path)
|
let actual_destination = "https://".to_owned() + &request_well_known(data, &destination).await.unwrap_or(destination.clone() + ":8448");
|
||||||
|
*http_request.uri_mut() = (actual_destination + T::METADATA.path)
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -35,7 +52,7 @@ pub async fn send_request<T: Endpoint>(
|
||||||
request_map.insert("method".to_owned(), T::METADATA.method.to_string().into());
|
request_map.insert("method".to_owned(), T::METADATA.method.to_string().into());
|
||||||
request_map.insert("uri".to_owned(), T::METADATA.path.into());
|
request_map.insert("uri".to_owned(), T::METADATA.path.into());
|
||||||
request_map.insert("origin".to_owned(), data.hostname().into());
|
request_map.insert("origin".to_owned(), data.hostname().into());
|
||||||
request_map.insert("destination".to_owned(), "privacytools.io".into());
|
request_map.insert("destination".to_owned(), destination.into());
|
||||||
|
|
||||||
let mut request_json = request_map.into();
|
let mut request_json = request_map.into();
|
||||||
ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap();
|
ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap();
|
||||||
|
@ -141,9 +158,6 @@ pub fn get_server_keys(data: State<Data>) -> Json<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/_matrix/key/v2/server/<_key_id>")]
|
#[get("/_matrix/key/v2/server/<_key_id>")]
|
||||||
pub fn get_server_keys_deprecated(
|
pub fn get_server_keys_deprecated(data: State<Data>, _key_id: String) -> Json<String> {
|
||||||
data: State<Data>,
|
|
||||||
_key_id: String,
|
|
||||||
) -> Json<String> {
|
|
||||||
get_server_keys(data)
|
get_server_keys(data)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue