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

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

55 lines
1.2 KiB
Rust
Raw Normal View History

2024-05-09 15:59:08 -07:00
use std::sync::Arc;
2024-12-14 21:58:01 -05:00
use conduwuit::{implement, Result};
2024-08-08 17:18:30 +00:00
use database::{Handle, Map};
2022-10-05 20:34:31 +02:00
use ruma::{DeviceId, TransactionId, UserId};
2021-06-08 18:10:00 +02:00
2024-05-09 15:59:08 -07:00
pub struct Service {
2024-08-08 17:18:30 +00:00
db: Data,
}
struct Data {
userdevicetxnid_response: Arc<Map>,
2020-08-25 13:24:38 +02:00
}
2024-07-04 03:26:19 +00:00
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
2024-08-08 17:18:30 +00:00
db: Data {
userdevicetxnid_response: args.db["userdevicetxnid_response"].clone(),
},
2024-07-04 03:26:19 +00:00
}))
2024-05-27 03:17:20 +00:00
}
2024-07-04 03:26:19 +00:00
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
2024-08-08 17:18:30 +00:00
#[implement(Service)]
pub fn add_txnid(
&self,
user_id: &UserId,
device_id: Option<&DeviceId>,
txn_id: &TransactionId,
data: &[u8],
) {
2024-08-08 17:18:30 +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());
2020-08-25 13:24:38 +02:00
2024-08-08 17:18:30 +00:00
self.db.userdevicetxnid_response.insert(&key, data);
}
// If there's no entry, this is a new transaction
#[implement(Service)]
pub async fn existing_txnid(
&self,
user_id: &UserId,
device_id: Option<&DeviceId>,
txn_id: &TransactionId,
2024-08-08 17:18:30 +00:00
) -> Result<Handle<'_>> {
let key = (user_id, device_id, txn_id);
self.db.userdevicetxnid_response.qry(&key).await
2020-08-25 13:24:38 +02:00
}