Files
continuwuity/src/service/transaction_ids/data.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.2 KiB
Rust
Raw Normal View History

2024-05-27 03:17:20 +00:00
use std::sync::Arc;
2024-03-05 19:48:54 -05:00
2024-05-27 03:17:20 +00:00
use conduit::Result;
2024-06-28 22:51:39 +00:00
use database::{Database, Map};
2024-05-27 03:17:20 +00:00
use ruma::{DeviceId, TransactionId, UserId};
2024-05-27 03:17:20 +00:00
pub struct Data {
2024-06-28 22:51:39 +00:00
userdevicetxnid_response: Arc<Map>,
}
2024-05-26 21:29:19 +00:00
2024-05-27 03:17:20 +00:00
impl Data {
2024-06-28 22:51:39 +00:00
pub(super) fn new(db: &Arc<Database>) -> Self {
2024-05-27 03:17:20 +00:00
Self {
2024-06-28 22:51:39 +00:00
userdevicetxnid_response: db["userdevicetxnid_response"].clone(),
2024-05-27 03:17:20 +00:00
}
}
pub(super) fn add_txnid(
2024-05-26 21:29:19 +00:00
&self, user_id: &UserId, device_id: Option<&DeviceId>, txn_id: &TransactionId, data: &[u8],
) -> Result<()> {
let mut key = user_id.as_bytes().to_vec();
key.push(0xFF);
key.extend_from_slice(device_id.map(DeviceId::as_bytes).unwrap_or_default());
key.push(0xFF);
key.extend_from_slice(txn_id.as_bytes());
self.userdevicetxnid_response.insert(&key, data)?;
Ok(())
}
2024-05-27 03:17:20 +00:00
pub(super) fn existing_txnid(
2024-05-26 21:29:19 +00:00
&self, user_id: &UserId, device_id: Option<&DeviceId>, txn_id: &TransactionId,
2024-07-02 05:56:10 +00:00
) -> Result<Option<database::Handle<'_>>> {
2024-05-26 21:29:19 +00:00
let mut key = user_id.as_bytes().to_vec();
key.push(0xFF);
key.extend_from_slice(device_id.map(DeviceId::as_bytes).unwrap_or_default());
key.push(0xFF);
key.extend_from_slice(txn_id.as_bytes());
// If there's no entry, this is a new transaction
self.userdevicetxnid_response.get(&key)
}
}