mirror of
https://gitlab.com/famedly/conduit.git
synced 2024-11-17 12:50:50 +01:00
Start work on message events
This commit is contained in:
parent
533260edd8
commit
b508b4d1e7
3 changed files with 25 additions and 27 deletions
|
@ -1,7 +1,8 @@
|
|||
use crate::utils;
|
||||
use directories::ProjectDirs;
|
||||
use ruma_events::collections::all::RoomEvent;
|
||||
use ruma_identifiers::UserId;
|
||||
use log::debug;
|
||||
use ruma_events::collections::all::Event;
|
||||
use ruma_identifiers::{EventId, RoomId, UserId};
|
||||
use std::convert::TryInto;
|
||||
|
||||
const USERID_PASSWORD: &str = "userid_password";
|
||||
|
@ -126,7 +127,8 @@ impl Data {
|
|||
}
|
||||
|
||||
/// Create a new room event.
|
||||
pub fn room_event_add(&self, _room_event: &RoomEvent) {
|
||||
pub fn event_add(&self, event: &Event, room_id: &RoomId, event_id: &EventId) {
|
||||
debug!("{}", serde_json::to_string(event).unwrap());
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
|
31
src/main.rs
31
src/main.rs
|
@ -14,7 +14,8 @@ use ruma_client_api::{
|
|||
},
|
||||
unversioned::get_supported_versions,
|
||||
};
|
||||
use ruma_events::{room::message::MessageEvent, EventResult};
|
||||
use ruma_events::collections::all::Event;
|
||||
use ruma_events::room::message::MessageEvent;
|
||||
use ruma_identifiers::{EventId, UserId};
|
||||
use ruma_wrapper::{MatrixResult, Ruma};
|
||||
use serde_json::map::Map;
|
||||
|
@ -213,31 +214,19 @@ fn create_message_event_route(
|
|||
_txn_id: String,
|
||||
body: Ruma<create_message_event::Request>,
|
||||
) -> MatrixResult<create_message_event::Response> {
|
||||
// Check if content is valid
|
||||
let content = match body.data.clone() {
|
||||
EventResult::Ok(content) => content,
|
||||
EventResult::Err(_) => {
|
||||
debug!("No content.");
|
||||
return MatrixResult(Err(Error {
|
||||
kind: ErrorKind::NotFound,
|
||||
message: "No content.".to_owned(),
|
||||
status_code: http::StatusCode::BAD_REQUEST,
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
// Generate event id
|
||||
let event_id = EventId::try_from("$TODOrandomeventid:localhost").unwrap();
|
||||
|
||||
data.room_event_add(
|
||||
&MessageEvent {
|
||||
content,
|
||||
data.event_add(
|
||||
&Event::RoomMessage(MessageEvent {
|
||||
content: body.data.clone().into_result().unwrap(),
|
||||
event_id: event_id.clone(),
|
||||
origin_server_ts: utils::millis_since_unix_epoch(),
|
||||
room_id: Some(body.room_id.clone()),
|
||||
sender: body.user_id.expect("user is authenticated"),
|
||||
sender: body.user_id.clone().expect("user is authenticated"),
|
||||
unsigned: Map::default(),
|
||||
}
|
||||
.into(),
|
||||
}),
|
||||
&body.room_id,
|
||||
&event_id,
|
||||
);
|
||||
|
||||
MatrixResult(Ok(create_message_event::Response { event_id }))
|
||||
|
|
|
@ -26,6 +26,7 @@ const MESSAGE_LIMIT: u64 = 65535;
|
|||
pub struct Ruma<T: Outgoing> {
|
||||
body: T::Incoming,
|
||||
pub user_id: Option<UserId>,
|
||||
pub json_body: serde_json::Value,
|
||||
}
|
||||
|
||||
impl<T: Endpoint> FromDataSimple for Ruma<T>
|
||||
|
@ -77,11 +78,17 @@ where
|
|||
let mut body = Vec::new();
|
||||
handle.read_to_end(&mut body).unwrap();
|
||||
|
||||
let http_request = http_request.body(body).unwrap();
|
||||
|
||||
let http_request = http_request.body(body.clone()).unwrap();
|
||||
log::info!("{:?}", http_request);
|
||||
|
||||
match T::Incoming::try_from(http_request) {
|
||||
Ok(t) => Success(Ruma { body: t, user_id }),
|
||||
Ok(t) => Success(Ruma {
|
||||
body: t,
|
||||
user_id,
|
||||
// TODO: Can we avoid parsing it again?
|
||||
json_body: serde_json::from_slice(&body)
|
||||
.expect("Ruma already parsed it successfuly"),
|
||||
}),
|
||||
Err(e) => {
|
||||
log::error!("{:?}", e);
|
||||
Failure((Status::InternalServerError, ()))
|
||||
|
|
Loading…
Reference in a new issue