Files
continuwuity/src/service/rooms/pdu_metadata/data.rs
T

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

105 lines
2.9 KiB
Rust
Raw Normal View History

2024-06-16 00:10:53 +00:00
use std::{mem::size_of, sync::Arc};
2022-09-06 23:15:09 +02:00
2024-11-06 21:02:23 +00:00
use arrayvec::ArrayVec;
2024-08-08 17:18:30 +00:00
use conduit::{
result::LogErr,
2024-11-02 06:12:54 +00:00
utils::{stream::TryIgnore, u64_from_u8, ReadyExt},
2024-08-08 17:18:30 +00:00
PduCount, PduEvent,
};
2024-07-18 06:37:47 +00:00
use database::Map;
2024-08-08 17:18:30 +00:00
use futures::{Stream, StreamExt};
2024-10-31 23:39:20 +03:00
use ruma::{api::Direction, EventId, RoomId, UserId};
2022-09-06 23:15:09 +02:00
2024-11-02 06:12:54 +00:00
use crate::{
rooms,
rooms::{
short::{ShortEventId, ShortRoomId},
timeline::{PduId, RawPduId},
},
Dep,
};
2024-03-05 19:48:54 -05:00
2024-05-27 03:17:20 +00:00
pub(super) struct Data {
2024-06-28 22:51:39 +00:00
tofrom_relation: Arc<Map>,
referencedevents: Arc<Map>,
softfailedeventids: Arc<Map>,
2024-07-18 06:37:47 +00:00
services: Services,
}
struct Services {
timeline: Dep<rooms::timeline::Service>,
}
2024-05-26 21:29:19 +00:00
2024-08-08 17:18:30 +00:00
pub(super) type PdusIterItem = (PduCount, PduEvent);
2024-06-28 23:05:45 +00:00
2024-05-27 03:17:20 +00:00
impl Data {
2024-07-18 06:37:47 +00:00
pub(super) fn new(args: &crate::Args<'_>) -> Self {
let db = &args.db;
2024-05-27 03:17:20 +00:00
Self {
2024-06-28 22:51:39 +00:00
tofrom_relation: db["tofrom_relation"].clone(),
referencedevents: db["referencedevents"].clone(),
softfailedeventids: db["softfailedeventids"].clone(),
2024-07-18 06:37:47 +00:00
services: Services {
timeline: args.depend::<rooms::timeline::Service>("rooms::timeline"),
},
2024-05-27 03:17:20 +00:00
}
}
2024-08-08 17:18:30 +00:00
pub(super) fn add_relation(&self, from: u64, to: u64) {
2024-10-07 17:54:27 +00:00
const BUFSIZE: usize = size_of::<u64>() * 2;
let key: &[u64] = &[to, from];
self.tofrom_relation.aput_raw::<BUFSIZE, _, _>(key, []);
2024-05-26 21:29:19 +00:00
}
2024-10-31 23:39:20 +03:00
pub(super) fn get_relations<'a>(
2024-11-02 06:12:54 +00:00
&'a self, user_id: &'a UserId, shortroomid: ShortRoomId, target: ShortEventId, from: PduCount, dir: Direction,
2024-10-31 23:39:20 +03:00
) -> impl Stream<Item = PdusIterItem> + Send + '_ {
2024-11-06 21:02:23 +00:00
let mut current = ArrayVec::<u8, 16>::new();
current.extend(target.to_be_bytes());
2024-11-11 05:00:29 +00:00
current.extend(from.saturating_inc(dir).into_unsigned().to_be_bytes());
2024-11-06 21:02:23 +00:00
let current = current.as_slice();
2024-10-31 23:39:20 +03:00
match dir {
2024-11-06 21:02:23 +00:00
Direction::Forward => self.tofrom_relation.raw_keys_from(current).boxed(),
Direction::Backward => self.tofrom_relation.rev_raw_keys_from(current).boxed(),
2024-10-31 23:39:20 +03:00
}
.ignore_err()
2024-11-02 06:12:54 +00:00
.ready_take_while(move |key| key.starts_with(&target.to_be_bytes()))
.map(|to_from| u64_from_u8(&to_from[8..16]))
.map(PduCount::from_unsigned)
.filter_map(move |shorteventid| async move {
let pdu_id: RawPduId = PduId {
shortroomid,
shorteventid,
}
.into();
let mut pdu = self.services.timeline.get_pdu_from_id(&pdu_id).await.ok()?;
2024-10-31 23:39:20 +03:00
if pdu.sender != user_id {
pdu.remove_transaction_id().log_err().ok();
}
2024-11-02 06:12:54 +00:00
Some((shorteventid, pdu))
2024-10-31 23:39:20 +03:00
})
2024-05-26 21:29:19 +00:00
}
2024-08-08 17:18:30 +00:00
pub(super) fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) {
2024-05-26 21:29:19 +00:00
for prev in event_ids {
2024-10-07 17:54:27 +00:00
let key = (room_id, prev);
self.referencedevents.put_raw(key, []);
2024-05-26 21:29:19 +00:00
}
}
2024-08-08 17:18:30 +00:00
pub(super) async fn is_event_referenced(&self, room_id: &RoomId, event_id: &EventId) -> bool {
let key = (room_id, event_id);
self.referencedevents.qry(&key).await.is_ok()
2024-05-26 21:29:19 +00:00
}
2024-10-07 17:54:27 +00:00
pub(super) fn mark_event_soft_failed(&self, event_id: &EventId) { self.softfailedeventids.insert(event_id, []); }
2024-05-26 21:29:19 +00:00
2024-08-08 17:18:30 +00:00
pub(super) async fn is_event_soft_failed(&self, event_id: &EventId) -> bool {
2024-10-09 03:37:13 +00:00
self.softfailedeventids.get(event_id).await.is_ok()
2024-05-26 21:29:19 +00:00
}
}