2021-06-08 16:10:00 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-08-25 11:24:38 +00:00
|
|
|
use crate::Result;
|
|
|
|
use ruma::{DeviceId, UserId};
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
use super::abstraction::Tree;
|
|
|
|
|
2020-08-25 11:24:38 +00:00
|
|
|
pub struct TransactionIds {
|
2021-06-08 16:10:00 +00:00
|
|
|
pub(super) userdevicetxnid_response: Arc<dyn Tree>, // Response can be empty (/sendToDevice) or the event id (/send)
|
2020-08-25 11:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TransactionIds {
|
|
|
|
pub fn add_txnid(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
2020-12-08 09:33:44 +00:00
|
|
|
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);
|
2020-12-08 09:33:44 +00:00
|
|
|
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());
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
self.userdevicetxnid_response.insert(&key, data)?;
|
2020-08-25 11:24:38 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn existing_txnid(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
2020-12-08 09:33:44 +00:00
|
|
|
device_id: Option<&DeviceId>,
|
2020-08-25 11:24:38 +00:00
|
|
|
txn_id: &str,
|
2021-06-08 16:10:00 +00:00
|
|
|
) -> Result<Option<Vec<u8>>> {
|
2020-08-25 11:24:38 +00:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
|
|
|
key.push(0xff);
|
2020-12-08 09:33:44 +00:00
|
|
|
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
|
2021-06-08 16:10:00 +00:00
|
|
|
Ok(self.userdevicetxnid_response.get(&key)?)
|
2020-08-25 11:24:38 +00:00
|
|
|
}
|
|
|
|
}
|