make pdu batch tokens zeroith-indexed

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-11-06 22:21:51 +00:00
parent f36757027e
commit e507c31306
9 changed files with 67 additions and 52 deletions

View file

@ -62,7 +62,7 @@ impl Data {
{
hash_map::Entry::Occupied(o) => Ok(*o.get()),
hash_map::Entry::Vacant(v) => Ok(self
.pdus_until(sender_user, room_id, PduCount::max())
.pdus_rev(sender_user, room_id, PduCount::max())
.await?
.next()
.await
@ -201,10 +201,10 @@ impl Data {
/// Returns an iterator over all events and their tokens in a room that
/// happened before the event with id `until` in reverse-chronological
/// order.
pub(super) async fn pdus_until<'a>(
pub(super) async fn pdus_rev<'a>(
&'a self, user_id: &'a UserId, room_id: &'a RoomId, until: PduCount,
) -> Result<impl Stream<Item = PdusIterItem> + Send + 'a> {
let current = self.count_to_id(room_id, until, true).await?;
let current = self.count_to_id(room_id, until).await?;
let prefix = current.shortroomid();
let stream = self
.pduid_pdu
@ -216,10 +216,10 @@ impl Data {
Ok(stream)
}
pub(super) async fn pdus_after<'a>(
pub(super) async fn pdus<'a>(
&'a self, user_id: &'a UserId, room_id: &'a RoomId, from: PduCount,
) -> Result<impl Stream<Item = PdusIterItem> + Send + 'a> {
let current = self.count_to_id(room_id, from, false).await?;
let current = self.count_to_id(room_id, from).await?;
let prefix = current.shortroomid();
let stream = self
.pduid_pdu
@ -266,7 +266,7 @@ impl Data {
}
}
async fn count_to_id(&self, room_id: &RoomId, count: PduCount, subtract: bool) -> Result<RawPduId> {
async fn count_to_id(&self, room_id: &RoomId, shorteventid: PduCount) -> Result<RawPduId> {
let shortroomid: ShortRoomId = self
.services
.short
@ -277,11 +277,7 @@ impl Data {
// +1 so we don't send the base event
let pdu_id = PduId {
shortroomid,
shorteventid: if subtract {
count.checked_sub(1)?
} else {
count.checked_add(1)?
},
shorteventid,
};
Ok(pdu_id.into())

View file

@ -177,7 +177,7 @@ impl Service {
#[tracing::instrument(skip(self), level = "debug")]
pub async fn latest_pdu_in_room(&self, room_id: &RoomId) -> Result<Arc<PduEvent>> {
self.pdus_until(user_id!("@placeholder:conduwuit.placeholder"), room_id, PduCount::max())
self.pdus_rev(user_id!("@placeholder:conduwuit.placeholder"), room_id, PduCount::max())
.await?
.next()
.await
@ -976,26 +976,23 @@ impl Service {
pub async fn all_pdus<'a>(
&'a self, user_id: &'a UserId, room_id: &'a RoomId,
) -> Result<impl Stream<Item = PdusIterItem> + Send + 'a> {
self.pdus_after(user_id, room_id, PduCount::min()).await
self.pdus(user_id, room_id, PduCount::min()).await
}
/// Returns an iterator over all events and their tokens in a room that
/// happened before the event with id `until` in reverse-chronological
/// order.
/// Reverse iteration starting at from.
#[tracing::instrument(skip(self), level = "debug")]
pub async fn pdus_until<'a>(
pub async fn pdus_rev<'a>(
&'a self, user_id: &'a UserId, room_id: &'a RoomId, until: PduCount,
) -> Result<impl Stream<Item = PdusIterItem> + Send + 'a> {
self.db.pdus_until(user_id, room_id, until).await
self.db.pdus_rev(user_id, room_id, until).await
}
/// Returns an iterator over all events and their token in a room that
/// happened after the event with id `from` in chronological order.
/// Forward iteration starting at from.
#[tracing::instrument(skip(self), level = "debug")]
pub async fn pdus_after<'a>(
pub async fn pdus<'a>(
&'a self, user_id: &'a UserId, room_id: &'a RoomId, from: PduCount,
) -> Result<impl Stream<Item = PdusIterItem> + Send + 'a> {
self.db.pdus_after(user_id, room_id, from).await
self.db.pdus(user_id, room_id, from).await
}
/// Replace a PDU with the redacted form.