feat: handle txn ids
This commit is contained in:
parent
6343eea417
commit
4954df3cc3
8 changed files with 111 additions and 9 deletions
|
@ -1,10 +1,13 @@
|
|||
use super::State;
|
||||
use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Ruma};
|
||||
use ruma::api::client::{
|
||||
error::ErrorKind,
|
||||
r0::message::{get_message_events, send_message_event},
|
||||
use crate::{pdu::PduBuilder, utils, ConduitResult, Database, Error, Ruma};
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
r0::message::{get_message_events, send_message_event},
|
||||
},
|
||||
EventId,
|
||||
};
|
||||
use std::convert::TryInto;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, put};
|
||||
|
@ -18,6 +21,29 @@ pub fn send_message_event_route(
|
|||
body: Ruma<send_message_event::IncomingRequest>,
|
||||
) -> ConduitResult<send_message_event::Response> {
|
||||
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
||||
let device_id = body.device_id.as_ref().expect("user is authenticated");
|
||||
|
||||
// Check if this is a new transaction id
|
||||
if let Some(response) = db
|
||||
.transaction_ids
|
||||
.existing_txnid(sender_id, device_id, &body.txn_id)?
|
||||
{
|
||||
// The client might have sent a txnid of the /sendToDevice endpoint
|
||||
// This txnid has no response associated with it
|
||||
if response.is_empty() {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"Tried to use txn id already used for an incompatible endpoint.",
|
||||
));
|
||||
}
|
||||
|
||||
let event_id = EventId::try_from(
|
||||
utils::string_from_bytes(&response)
|
||||
.map_err(|_| Error::bad_database("Invalid txnid bytes in database."))?,
|
||||
)
|
||||
.map_err(|_| Error::bad_database("Invalid event id in txnid data."))?;
|
||||
return Ok(send_message_event::Response { event_id }.into());
|
||||
}
|
||||
|
||||
let mut unsigned = serde_json::Map::new();
|
||||
unsigned.insert("transaction_id".to_owned(), body.txn_id.clone().into());
|
||||
|
@ -29,6 +55,7 @@ pub fn send_message_event_route(
|
|||
event_type: body.event_type.clone(),
|
||||
content: serde_json::from_str(
|
||||
body.json_body
|
||||
.as_ref()
|
||||
.ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?
|
||||
.get(),
|
||||
)
|
||||
|
@ -41,6 +68,8 @@ pub fn send_message_event_route(
|
|||
&db.account_data,
|
||||
)?;
|
||||
|
||||
db.transaction_ids
|
||||
.add_txnid(sender_id, device_id, &body.txn_id, event_id.as_bytes())?;
|
||||
Ok(send_message_event::Response { event_id }.into())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue