|
|
|
@ -14,6 +14,7 @@ use ruma_client_api::{ |
|
|
|
alias::{create_alias, delete_alias, get_alias}, |
|
|
|
alias::{create_alias, delete_alias, get_alias}, |
|
|
|
capabilities::get_capabilities, |
|
|
|
capabilities::get_capabilities, |
|
|
|
config::{get_global_account_data, set_global_account_data}, |
|
|
|
config::{get_global_account_data, set_global_account_data}, |
|
|
|
|
|
|
|
context::get_context, |
|
|
|
device::{self, delete_device, delete_devices, get_device, get_devices, update_device}, |
|
|
|
device::{self, delete_device, delete_devices, get_device, get_devices, update_device}, |
|
|
|
directory::{ |
|
|
|
directory::{ |
|
|
|
self, get_public_rooms, get_public_rooms_filtered, get_room_visibility, |
|
|
|
self, get_public_rooms, get_public_rooms_filtered, get_room_visibility, |
|
|
|
@ -200,7 +201,7 @@ pub fn register_route( |
|
|
|
content: ruma_events::push_rules::PushRulesEventContent { |
|
|
|
content: ruma_events::push_rules::PushRulesEventContent { |
|
|
|
global: ruma_events::push_rules::Ruleset { |
|
|
|
global: ruma_events::push_rules::Ruleset { |
|
|
|
content: vec![], |
|
|
|
content: vec![], |
|
|
|
override_rules: vec![ruma_events::push_rules::ConditionalPushRule { |
|
|
|
override_: vec![ruma_events::push_rules::ConditionalPushRule { |
|
|
|
actions: vec![ruma_events::push_rules::Action::DontNotify], |
|
|
|
actions: vec![ruma_events::push_rules::Action::DontNotify], |
|
|
|
default: true, |
|
|
|
default: true, |
|
|
|
enabled: false, |
|
|
|
enabled: false, |
|
|
|
@ -219,12 +220,10 @@ pub fn register_route( |
|
|
|
default: true, |
|
|
|
default: true, |
|
|
|
enabled: true, |
|
|
|
enabled: true, |
|
|
|
rule_id: ".m.rule.message".to_owned(), |
|
|
|
rule_id: ".m.rule.message".to_owned(), |
|
|
|
conditions: vec![ruma_events::push_rules::PushCondition::EventMatch( |
|
|
|
conditions: vec![ruma_events::push_rules::PushCondition::EventMatch { |
|
|
|
ruma_events::push_rules::EventMatchCondition { |
|
|
|
key: "type".to_owned(), |
|
|
|
key: "type".to_owned(), |
|
|
|
pattern: "m.room.message".to_owned(), |
|
|
|
pattern: "m.room.message".to_owned(), |
|
|
|
}], |
|
|
|
}, |
|
|
|
|
|
|
|
)], |
|
|
|
|
|
|
|
}], |
|
|
|
}], |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
@ -348,11 +347,11 @@ pub fn logout_route( |
|
|
|
pub fn get_capabilities_route() -> MatrixResult<get_capabilities::Response> { |
|
|
|
pub fn get_capabilities_route() -> MatrixResult<get_capabilities::Response> { |
|
|
|
let mut available = BTreeMap::new(); |
|
|
|
let mut available = BTreeMap::new(); |
|
|
|
available.insert( |
|
|
|
available.insert( |
|
|
|
"5".to_owned(), |
|
|
|
RoomVersionId::version_5(), |
|
|
|
get_capabilities::RoomVersionStability::Stable, |
|
|
|
get_capabilities::RoomVersionStability::Stable, |
|
|
|
); |
|
|
|
); |
|
|
|
available.insert( |
|
|
|
available.insert( |
|
|
|
"6".to_owned(), |
|
|
|
RoomVersionId::version_6(), |
|
|
|
get_capabilities::RoomVersionStability::Stable, |
|
|
|
get_capabilities::RoomVersionStability::Stable, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
@ -374,7 +373,6 @@ pub fn get_pushrules_all_route( |
|
|
|
body: Ruma<get_pushrules_all::Request>, |
|
|
|
body: Ruma<get_pushrules_all::Request>, |
|
|
|
) -> MatrixResult<get_pushrules_all::Response> { |
|
|
|
) -> MatrixResult<get_pushrules_all::Response> { |
|
|
|
let user_id = body.user_id.as_ref().expect("user is authenticated"); |
|
|
|
let user_id = body.user_id.as_ref().expect("user is authenticated"); |
|
|
|
warn!("TODO: get_pushrules_all_route"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(EduEvent::PushRules(pushrules)) = db |
|
|
|
if let Some(EduEvent::PushRules(pushrules)) = db |
|
|
|
.account_data |
|
|
|
.account_data |
|
|
|
@ -383,7 +381,7 @@ pub fn get_pushrules_all_route( |
|
|
|
.map(|edu| edu.deserialize().expect("PushRules event in db is valid")) |
|
|
|
.map(|edu| edu.deserialize().expect("PushRules event in db is valid")) |
|
|
|
{ |
|
|
|
{ |
|
|
|
MatrixResult(Ok(get_pushrules_all::Response { |
|
|
|
MatrixResult(Ok(get_pushrules_all::Response { |
|
|
|
global: BTreeMap::new(), |
|
|
|
global: pushrules.content.global |
|
|
|
})) |
|
|
|
})) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
MatrixResult(Err(Error { |
|
|
|
MatrixResult(Err(Error { |
|
|
|
@ -2092,9 +2090,15 @@ pub fn sync_route( |
|
|
|
.unwrap() |
|
|
|
.unwrap() |
|
|
|
> since |
|
|
|
> since |
|
|
|
{ |
|
|
|
{ |
|
|
|
edus.push(serde_json::from_str(&serde_json::to_string( |
|
|
|
edus.push( |
|
|
|
&EduEvent::Typing(db.rooms.edus.roomactives_all(&room_id).unwrap()), |
|
|
|
serde_json::from_str( |
|
|
|
).unwrap()).unwrap()); |
|
|
|
&serde_json::to_string(&EduEvent::Typing( |
|
|
|
|
|
|
|
db.rooms.edus.roomactives_all(&room_id).unwrap(), |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
.unwrap(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.unwrap(), |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
joined_rooms.insert( |
|
|
|
joined_rooms.insert( |
|
|
|
@ -2170,9 +2174,15 @@ pub fn sync_route( |
|
|
|
.unwrap() |
|
|
|
.unwrap() |
|
|
|
> since |
|
|
|
> since |
|
|
|
{ |
|
|
|
{ |
|
|
|
edus.push(serde_json::from_str(&serde_json::to_string( |
|
|
|
edus.push( |
|
|
|
&EduEvent::Typing(db.rooms.edus.roomactives_all(&room_id).unwrap()), |
|
|
|
serde_json::from_str( |
|
|
|
).unwrap()).unwrap()); |
|
|
|
&serde_json::to_string(&EduEvent::Typing( |
|
|
|
|
|
|
|
db.rooms.edus.roomactives_all(&room_id).unwrap(), |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
.unwrap(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.unwrap(), |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
left_rooms.insert( |
|
|
|
left_rooms.insert( |
|
|
|
@ -2271,12 +2281,16 @@ pub fn sync_route( |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[get("/_matrix/client/r0/rooms/<_room_id>/messages", data = "<body>")] |
|
|
|
#[get(
|
|
|
|
pub fn get_message_events_route( |
|
|
|
"/_matrix/client/r0/rooms/<_room_id>/context/<_event_id>", |
|
|
|
|
|
|
|
data = "<body>" |
|
|
|
|
|
|
|
)] |
|
|
|
|
|
|
|
pub fn get_context_route( |
|
|
|
db: State<'_, Database>, |
|
|
|
db: State<'_, Database>, |
|
|
|
body: Ruma<get_message_events::Request>, |
|
|
|
body: Ruma<get_context::Request>, |
|
|
|
_room_id: String, |
|
|
|
_room_id: String, |
|
|
|
) -> MatrixResult<get_message_events::Response> { |
|
|
|
_event_id: String, |
|
|
|
|
|
|
|
) -> MatrixResult<get_context::Response> { |
|
|
|
let user_id = body.user_id.as_ref().expect("user is authenticated"); |
|
|
|
let user_id = body.user_id.as_ref().expect("user is authenticated"); |
|
|
|
|
|
|
|
|
|
|
|
if !db.rooms.is_joined(user_id, &body.room_id).unwrap() { |
|
|
|
if !db.rooms.is_joined(user_id, &body.room_id).unwrap() { |
|
|
|
@ -2287,41 +2301,160 @@ pub fn get_message_events_route( |
|
|
|
})); |
|
|
|
})); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if let get_message_events::Direction::Forward = body.dir { |
|
|
|
if let Some(base_event) = db.rooms.get_pdu(&body.event_id).unwrap() { |
|
|
|
todo!(); |
|
|
|
let base_event = base_event |
|
|
|
} |
|
|
|
.to_room_event(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let base_token = db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.get_pdu_count(&body.event_id) |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.expect("event exists, so count should exist too"); |
|
|
|
|
|
|
|
|
|
|
|
if let Ok(from) = body.from.clone().parse() { |
|
|
|
let events_before = db |
|
|
|
let pdus = db |
|
|
|
|
|
|
|
.rooms |
|
|
|
.rooms |
|
|
|
.pdus_until(&body.room_id, from) |
|
|
|
.pdus_until(&body.room_id, base_token) |
|
|
|
.take(body.limit.map(|l| l.try_into().unwrap()).unwrap_or(10_u32) as usize) |
|
|
|
.take(u32::try_from(body.limit).unwrap() as usize / 2) |
|
|
|
.map(|r| r.unwrap()) |
|
|
|
.map(|r| r.unwrap()) |
|
|
|
.collect::<Vec<_>>(); |
|
|
|
.collect::<Vec<_>>(); |
|
|
|
let prev_batch = pdus |
|
|
|
|
|
|
|
|
|
|
|
let start_token = events_before |
|
|
|
.last() |
|
|
|
.last() |
|
|
|
.and_then(|e| db.rooms.get_pdu_count(&e.event_id).unwrap()) |
|
|
|
.and_then(|e| db.rooms.get_pdu_count(&e.event_id).unwrap()) |
|
|
|
.map(|c| c.to_string()); |
|
|
|
.map(|c| c.to_string()); |
|
|
|
let room_events = pdus |
|
|
|
|
|
|
|
|
|
|
|
let events_before = events_before |
|
|
|
.into_iter() |
|
|
|
.into_iter() |
|
|
|
.map(|pdu| pdu.to_room_event()) |
|
|
|
.map(|pdu| pdu.to_room_event()) |
|
|
|
.collect::<Vec<_>>(); |
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(get_message_events::Response { |
|
|
|
let events_after = db |
|
|
|
start: Some(body.from.clone()), |
|
|
|
.rooms |
|
|
|
end: prev_batch, |
|
|
|
.pdus_after(&body.room_id, base_token) |
|
|
|
chunk: room_events, |
|
|
|
.take(u32::try_from(body.limit).unwrap() as usize / 2) |
|
|
|
state: Vec::new(), |
|
|
|
.map(|r| r.unwrap()) |
|
|
|
})) |
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let end_token = events_after |
|
|
|
|
|
|
|
.last() |
|
|
|
|
|
|
|
.and_then(|e| db.rooms.get_pdu_count(&e.event_id).unwrap()) |
|
|
|
|
|
|
|
.map(|c| c.to_string()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let events_after = events_after |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.map(|pdu| pdu.to_room_event()) |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(get_context::Response { |
|
|
|
|
|
|
|
start: start_token, |
|
|
|
|
|
|
|
end: end_token, |
|
|
|
|
|
|
|
events_before, |
|
|
|
|
|
|
|
event: Some(base_event), |
|
|
|
|
|
|
|
events_after, |
|
|
|
|
|
|
|
state: db // TODO: State at event
|
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.room_state(&body.room_id) |
|
|
|
|
|
|
|
.unwrap() |
|
|
|
|
|
|
|
.values() |
|
|
|
|
|
|
|
.map(|pdu| pdu.to_state_event()) |
|
|
|
|
|
|
|
.collect(), |
|
|
|
|
|
|
|
})) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
MatrixResult(Err(Error { |
|
|
|
MatrixResult(Err(Error { |
|
|
|
kind: ErrorKind::Unknown, |
|
|
|
kind: ErrorKind::Unknown, |
|
|
|
message: "Invalid from.".to_owned(), |
|
|
|
message: "Invalid base event.".to_owned(), |
|
|
|
status_code: http::StatusCode::BAD_REQUEST, |
|
|
|
status_code: http::StatusCode::BAD_REQUEST, |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/_matrix/client/r0/rooms/<_room_id>/messages", data = "<body>")] |
|
|
|
|
|
|
|
pub fn get_message_events_route( |
|
|
|
|
|
|
|
db: State<'_, Database>, |
|
|
|
|
|
|
|
body: Ruma<get_message_events::Request>, |
|
|
|
|
|
|
|
_room_id: String, |
|
|
|
|
|
|
|
) -> MatrixResult<get_message_events::Response> { |
|
|
|
|
|
|
|
let user_id = body.user_id.as_ref().expect("user is authenticated"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !db.rooms.is_joined(user_id, &body.room_id).unwrap() { |
|
|
|
|
|
|
|
return MatrixResult(Err(Error { |
|
|
|
|
|
|
|
kind: ErrorKind::Forbidden, |
|
|
|
|
|
|
|
message: "You don't have permission to view this room.".to_owned(), |
|
|
|
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST, |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match body.dir { |
|
|
|
|
|
|
|
get_message_events::Direction::Forward => { |
|
|
|
|
|
|
|
if let Ok(from) = body.from.clone().parse() { |
|
|
|
|
|
|
|
let events_after = db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.pdus_after(&body.room_id, from) |
|
|
|
|
|
|
|
.take(body.limit.map(|l| l.try_into().unwrap()).unwrap_or(10_u32) as usize) |
|
|
|
|
|
|
|
.map(|r| r.unwrap()) |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let end_token = events_after |
|
|
|
|
|
|
|
.last() |
|
|
|
|
|
|
|
.and_then(|e| db.rooms.get_pdu_count(&e.event_id).unwrap()) |
|
|
|
|
|
|
|
.map(|c| c.to_string()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let events_after = events_after |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.map(|pdu| pdu.to_room_event()) |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(get_message_events::Response { |
|
|
|
|
|
|
|
start: Some(body.from.clone()), |
|
|
|
|
|
|
|
end: end_token, |
|
|
|
|
|
|
|
chunk: events_after, |
|
|
|
|
|
|
|
state: Vec::new(), |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
MatrixResult(Err(Error { |
|
|
|
|
|
|
|
kind: ErrorKind::Unknown, |
|
|
|
|
|
|
|
message: "Invalid from.".to_owned(), |
|
|
|
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST, |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
get_message_events::Direction::Backward => { |
|
|
|
|
|
|
|
if let Ok(from) = body.from.clone().parse() { |
|
|
|
|
|
|
|
let events_before = db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.pdus_until(&body.room_id, from) |
|
|
|
|
|
|
|
.take(body.limit.map(|l| l.try_into().unwrap()).unwrap_or(10_u32) as usize) |
|
|
|
|
|
|
|
.map(|r| r.unwrap()) |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let start_token = events_before |
|
|
|
|
|
|
|
.last() |
|
|
|
|
|
|
|
.and_then(|e| db.rooms.get_pdu_count(&e.event_id).unwrap()) |
|
|
|
|
|
|
|
.map(|c| c.to_string()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let events_before = events_before |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.map(|pdu| pdu.to_room_event()) |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(get_message_events::Response { |
|
|
|
|
|
|
|
start: Some(body.from.clone()), |
|
|
|
|
|
|
|
end: start_token, |
|
|
|
|
|
|
|
chunk: events_before, |
|
|
|
|
|
|
|
state: Vec::new(), |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
MatrixResult(Err(Error { |
|
|
|
|
|
|
|
kind: ErrorKind::Unknown, |
|
|
|
|
|
|
|
message: "Invalid from.".to_owned(), |
|
|
|
|
|
|
|
status_code: http::StatusCode::BAD_REQUEST, |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[get("/_matrix/client/r0/voip/turnServer")] |
|
|
|
#[get("/_matrix/client/r0/voip/turnServer")] |
|
|
|
pub fn turn_server_route() -> MatrixResult<create_message_event::Response> { |
|
|
|
pub fn turn_server_route() -> MatrixResult<create_message_event::Response> { |
|
|
|
MatrixResult(Err(Error { |
|
|
|
MatrixResult(Err(Error { |
|
|
|
|