conduit/src/database/transaction_ids.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

2020-08-25 11:24:38 +00:00
use crate::Result;
use ruma::{DeviceId, UserId};
use sled::IVec;
2020-11-09 11:21:04 +00:00
#[derive(Clone)]
2020-08-25 11:24:38 +00:00
pub struct TransactionIds {
pub(super) userdevicetxnid_response: sled::Tree, // Response can be empty (/sendToDevice) or the event id (/send)
}
impl TransactionIds {
pub fn add_txnid(
&self,
user_id: &UserId,
device_id: Option<&DeviceId>,
2020-08-25 11:24:38 +00:00
txn_id: &str,
data: &[u8],
) -> Result<()> {
let mut key = user_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.map(|d| d.as_bytes()).unwrap_or_default());
2020-08-25 11:24:38 +00:00
key.push(0xff);
key.extend_from_slice(txn_id.as_bytes());
self.userdevicetxnid_response.insert(key, data)?;
Ok(())
}
pub fn existing_txnid(
&self,
user_id: &UserId,
device_id: Option<&DeviceId>,
2020-08-25 11:24:38 +00:00
txn_id: &str,
) -> Result<Option<IVec>> {
let mut key = user_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.map(|d| d.as_bytes()).unwrap_or_default());
2020-08-25 11:24:38 +00:00
key.push(0xff);
key.extend_from_slice(txn_id.as_bytes());
// If there's no entry, this is a new transaction
Ok(self.userdevicetxnid_response.get(key)?)
}
}