Skip to main content

tuwunel_service/transaction_ids/
mod.rs

1use std::sync::Arc;
2
3use ruma::{DeviceId, TransactionId, UserId};
4use tuwunel_core::{Result, implement};
5use tuwunel_database::{Handle, Map};
6
7pub struct Service {
8	db: Data,
9}
10
11struct Data {
12	userdevicetxnid_response: Arc<Map>,
13}
14
15impl crate::Service for Service {
16	fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
17		Ok(Arc::new(Self {
18			db: Data {
19				userdevicetxnid_response: args.db["userdevicetxnid_response"].clone(),
20			},
21		}))
22	}
23
24	fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
25}
26
27#[implement(Service)]
28pub fn add_txnid(
29	&self,
30	user_id: &UserId,
31	device_id: Option<&DeviceId>,
32	txn_id: &TransactionId,
33	data: &[u8],
34) {
35	let mut key = user_id.as_bytes().to_vec();
36	key.push(0xFF);
37	key.extend_from_slice(
38		device_id
39			.map(DeviceId::as_bytes)
40			.unwrap_or_default(),
41	);
42	key.push(0xFF);
43	key.extend_from_slice(txn_id.as_bytes());
44
45	self.db
46		.userdevicetxnid_response
47		.insert(&key, data);
48}
49
50// If there's no entry, this is a new transaction
51#[implement(Service)]
52pub async fn existing_txnid(
53	&self,
54	user_id: &UserId,
55	device_id: Option<&DeviceId>,
56	txn_id: &TransactionId,
57) -> Result<Handle<'_>> {
58	let key = (user_id, device_id, txn_id);
59	self.db.userdevicetxnid_response.qry(&key).await
60}