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.

125 lines
3.3 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-12-14 21:58:01 -05:00
use conduwuit::{
arrayvec::ArrayVec,
matrix::{Event, PduCount},
2024-12-04 00:00:40 +00:00
utils::{
ReadyExt,
2024-12-04 00:00:40 +00:00
stream::{TryIgnore, WidebandExt},
u64_from_u8,
2024-12-04 00:00:40 +00:00
},
2024-08-08 17:18:30 +00:00
};
2024-07-18 06:37:47 +00:00
use database::Map;
2024-08-08 17:18:30 +00:00
use futures::{Stream, StreamExt};
use ruma::{EventId, RoomId, UserId, api::Direction};
2022-09-06 23:15:09 +02:00
2024-11-02 06:12:54 +00:00
use crate::{
2025-06-04 00:11:09 +01:00
Dep,
2024-11-02 06:12:54 +00:00
rooms::{
2025-06-04 00:11:09 +01:00
self,
2024-11-02 06:12:54 +00:00
short::{ShortEventId, ShortRoomId},
2025-06-04 00:11:09 +01:00
timeline::{PduId, PdusIterItem, RawPduId},
2024-11-02 06:12:54 +00:00
},
};
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-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>(
&'a self,
user_id: &'a UserId,
shortroomid: ShortRoomId,
target: ShortEventId,
from: PduCount,
dir: Direction,
2025-06-04 00:11:09 +01:00
) -> impl Stream<Item = PdusIterItem> + Send + 'a {
// Query from exact position then filter excludes it (saturating_inc could skip
// events at min/max boundaries)
let from_unsigned = from.into_unsigned();
2024-11-06 21:02:23 +00:00
let mut current = ArrayVec::<u8, 16>::new();
current.extend(target.to_be_bytes());
current.extend(from_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 {
| 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)
.ready_filter(move |count| {
if from == PduCount::min() || from == PduCount::max() {
true
} else {
let count_unsigned = count.into_unsigned();
match dir {
| Direction::Forward => count_unsigned > from_unsigned,
| Direction::Backward => count_unsigned < from_unsigned,
}
}
})
2024-12-04 00:00:40 +00:00
.wide_filter_map(move |shorteventid| async move {
let pdu_id: RawPduId = PduId { shortroomid, shorteventid }.into();
2024-11-02 06:12:54 +00:00
let mut pdu = self.services.timeline.get_pdu_from_id(&pdu_id).await.ok()?;
pdu.as_mut_pdu().set_unsigned(Some(user_id));
2024-10-31 23:39:20 +03:00
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
}
#[inline]
pub(super) fn mark_as_referenced<'a, I>(&self, room_id: &RoomId, event_ids: I)
where
I: Iterator<Item = &'a 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
}
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
}
}