tuwunel_service/transaction_ids/
mod.rs1use std::sync::Arc;
9
10use ruma::{DeviceId, EventId, RoomId, TransactionId, UserId};
11use tuwunel_core::{Result, implement};
12use tuwunel_database::{Handle, Map, Txn};
13
14pub struct Service {
21 db: Data,
22}
23
24#[derive(Clone, Copy)]
28pub struct RoomTxn<'a> {
29 pub user_id: &'a UserId,
31
32 pub device_id: Option<&'a DeviceId>,
34
35 pub txn_id: &'a TransactionId,
37
38 pub room_id: &'a RoomId,
40}
41
42#[derive(Clone, Copy)]
47pub enum RoomScope<'a> {
48 Send(&'a str),
50
51 Redact(&'a EventId),
53
54 Delayed(&'a str),
56}
57
58struct Data {
59 userdevicetxnid_response: Arc<Map>,
60}
61
62type Key<'a> = (&'a UserId, Option<&'a DeviceId>, &'a TransactionId);
63type RoomKey<'a> = (
64 &'a UserId,
65 Option<&'a DeviceId>,
66 &'a TransactionId,
67 &'static str,
68 &'a RoomId,
69 &'a str,
70);
71
72const ROOM_SEND_TAG: &str = "room-send";
73const ROOM_REDACT_TAG: &str = "room-redact";
74const ROOM_DELAYED_TAG: &str = "room-delayed";
75
76impl crate::Service for Service {
77 fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
78 Ok(Arc::new(Self {
79 db: Data {
80 userdevicetxnid_response: args.db["userdevicetxnid_response"].clone(),
81 },
82 }))
83 }
84
85 fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
86}
87
88#[implement(Service)]
89pub fn add_txnid(
95 &self,
96 user_id: &UserId,
97 device_id: Option<&DeviceId>,
98 txn_id: &TransactionId,
99 data: &[u8],
100) {
101 let key = txnid_key(user_id, device_id, txn_id);
102
103 self.db
104 .userdevicetxnid_response
105 .put_raw(key, data);
106}
107
108#[implement(Service)]
109pub async fn existing_txnid(
115 &self,
116 user_id: &UserId,
117 device_id: Option<&DeviceId>,
118 txn_id: &TransactionId,
119) -> Result<Handle<'_>> {
120 let key = txnid_key(user_id, device_id, txn_id);
121
122 self.db.userdevicetxnid_response.qry(&key).await
123}
124
125#[implement(Service)]
126pub fn add_room_txnid(&self, txn: RoomTxn<'_>, scope: RoomScope<'_>, data: &[u8]) {
132 let key = room_txnid_key(txn, scope);
133
134 self.db
135 .userdevicetxnid_response
136 .put_raw(key, data);
137}
138
139#[implement(Service)]
140pub fn add_room_txnid_batch(
145 &self,
146 batch: &mut Txn,
147 txn: RoomTxn<'_>,
148 scope: RoomScope<'_>,
149 data: &[u8],
150) {
151 batch.put_raw(&self.db.userdevicetxnid_response, room_txnid_key(txn, scope), data);
152}
153
154#[implement(Service)]
155pub async fn existing_room_txnid(
161 &self,
162 txn: RoomTxn<'_>,
163 scope: RoomScope<'_>,
164) -> Result<Handle<'_>> {
165 let key = room_txnid_key(txn, scope);
166
167 self.db.userdevicetxnid_response.qry(&key).await
168}
169
170fn txnid_key<'a>(
171 user_id: &'a UserId,
172 device_id: Option<&'a DeviceId>,
173 txn_id: &'a TransactionId,
174) -> Key<'a> {
175 (user_id, device_id, txn_id)
176}
177
178fn room_txnid_key<'a>(txn: RoomTxn<'a>, scope: RoomScope<'a>) -> RoomKey<'a> {
179 let (tag, scope) = match scope {
180 | RoomScope::Send(event_type) => (ROOM_SEND_TAG, event_type),
181 | RoomScope::Redact(event_id) => (ROOM_REDACT_TAG, event_id.as_str()),
182 | RoomScope::Delayed(event_type) => (ROOM_DELAYED_TAG, event_type),
183 };
184
185 (txn.user_id, txn.device_id, txn.txn_id, tag, txn.room_id, scope)
186}
187
188#[cfg(test)]
189mod tests;