2022-08-07 19:42:22 +02:00
|
|
|
mod data;
|
2022-10-05 18:36:12 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-08-07 19:42:22 +02:00
|
|
|
pub use data::Data;
|
2021-06-08 18:10:00 +02:00
|
|
|
|
2022-09-07 13:25:51 +02:00
|
|
|
use ruma::{UserId, DeviceId, TransactionId};
|
|
|
|
use crate::Result;
|
2020-08-25 13:24:38 +02:00
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
pub struct Service {
|
2022-10-05 18:36:12 +02:00
|
|
|
db: Arc<dyn Data>,
|
2020-08-25 13:24:38 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
impl Service {
|
2020-08-25 13:24:38 +02:00
|
|
|
pub fn add_txnid(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
2020-12-08 10:33:44 +01:00
|
|
|
device_id: Option<&DeviceId>,
|
2022-01-17 14:35:38 +01:00
|
|
|
txn_id: &TransactionId,
|
2020-08-25 13:24:38 +02:00
|
|
|
data: &[u8],
|
|
|
|
) -> Result<()> {
|
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
|
|
|
key.push(0xff);
|
2020-12-08 10:33:44 +01:00
|
|
|
key.extend_from_slice(device_id.map(|d| d.as_bytes()).unwrap_or_default());
|
2020-08-25 13:24:38 +02:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(txn_id.as_bytes());
|
|
|
|
|
2021-06-08 18:10:00 +02:00
|
|
|
self.userdevicetxnid_response.insert(&key, data)?;
|
2020-08-25 13:24:38 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn existing_txnid(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
2020-12-08 10:33:44 +01:00
|
|
|
device_id: Option<&DeviceId>,
|
2022-01-17 14:35:38 +01:00
|
|
|
txn_id: &TransactionId,
|
2021-06-08 18:10:00 +02:00
|
|
|
) -> Result<Option<Vec<u8>>> {
|
2020-08-25 13:24:38 +02:00
|
|
|
let mut key = user_id.as_bytes().to_vec();
|
|
|
|
key.push(0xff);
|
2020-12-08 10:33:44 +01:00
|
|
|
key.extend_from_slice(device_id.map(|d| d.as_bytes()).unwrap_or_default());
|
2020-08-25 13:24:38 +02:00
|
|
|
key.push(0xff);
|
|
|
|
key.extend_from_slice(txn_id.as_bytes());
|
|
|
|
|
|
|
|
// If there's no entry, this is a new transaction
|
2021-06-17 20:34:14 +02:00
|
|
|
self.userdevicetxnid_response.get(&key)
|
2020-08-25 13:24:38 +02:00
|
|
|
}
|
|
|
|
}
|