mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
refactor: Fix errors in api/client/search.rs
This commit is contained in:
+26
-37
@@ -8,13 +8,17 @@ use conduwuit::{
|
|||||||
utils::{IterStream, stream::ReadyExt},
|
utils::{IterStream, stream::ReadyExt},
|
||||||
};
|
};
|
||||||
use conduwuit_service::{Services, rooms::search::RoomQuery};
|
use conduwuit_service::{Services, rooms::search::RoomQuery};
|
||||||
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt, future::OptionFuture};
|
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
OwnedRoomId, RoomId, UInt, UserId,
|
OwnedRoomId, RoomId, UInt, UserId,
|
||||||
api::client::search::search_events::{
|
api::client::search::search_events::{
|
||||||
self,
|
self,
|
||||||
v3::{Criteria, EventContextResult, ResultCategories, ResultRoomEvents, SearchResult},
|
v3::{
|
||||||
|
Criteria, EventContextResult, ResultCategories, ResultGroupMapsByGroupingKey,
|
||||||
|
ResultRoomEvents, SearchResult,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
assign,
|
||||||
events::AnyStateEvent,
|
events::AnyStateEvent,
|
||||||
serde::Raw,
|
serde::Raw,
|
||||||
};
|
};
|
||||||
@@ -41,20 +45,15 @@ pub(crate) async fn search_events_route(
|
|||||||
) -> Result<Response> {
|
) -> Result<Response> {
|
||||||
let sender_user = body.sender_user();
|
let sender_user = body.sender_user();
|
||||||
let next_batch = body.next_batch.as_deref();
|
let next_batch = body.next_batch.as_deref();
|
||||||
let room_events_result: OptionFuture<_> = body
|
|
||||||
.search_categories
|
|
||||||
.room_events
|
|
||||||
.as_ref()
|
|
||||||
.map(|criteria| category_room_events(&services, sender_user, next_batch, criteria))
|
|
||||||
.into();
|
|
||||||
|
|
||||||
Ok(Response {
|
let mut result_categories = ResultCategories::new();
|
||||||
search_categories: ResultCategories {
|
|
||||||
room_events: Box::pin(room_events_result)
|
if let Some(criteria) = &body.search_categories.room_events {
|
||||||
.await
|
result_categories.room_events =
|
||||||
.unwrap_or_else(|| Ok(ResultRoomEvents::default()))?,
|
category_room_events(&services, sender_user, next_batch, criteria).await?
|
||||||
},
|
}
|
||||||
})
|
|
||||||
|
Ok(Response::new(result_categories))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::map_unwrap_or)]
|
#[allow(clippy::map_unwrap_or)]
|
||||||
@@ -85,14 +84,7 @@ async fn category_room_events(
|
|||||||
.map(IntoIterator::into_iter)
|
.map(IntoIterator::into_iter)
|
||||||
.map(IterStream::stream)
|
.map(IterStream::stream)
|
||||||
.map(StreamExt::boxed)
|
.map(StreamExt::boxed)
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| services.rooms.state_cache.rooms_joined(sender_user).boxed());
|
||||||
services
|
|
||||||
.rooms
|
|
||||||
.state_cache
|
|
||||||
.rooms_joined(sender_user)
|
|
||||||
.map(ToOwned::to_owned)
|
|
||||||
.boxed()
|
|
||||||
});
|
|
||||||
|
|
||||||
let results: Vec<_> = rooms
|
let results: Vec<_> = rooms
|
||||||
.filter_map(|room_id| async move {
|
.filter_map(|room_id| async move {
|
||||||
@@ -129,7 +121,8 @@ async fn category_room_events(
|
|||||||
let total: UInt = results
|
let total: UInt = results
|
||||||
.iter()
|
.iter()
|
||||||
.fold(0, |a: usize, (_, count, _)| a.saturating_add(*count))
|
.fold(0, |a: usize, (_, count, _)| a.saturating_add(*count))
|
||||||
.try_into()?;
|
.try_into()
|
||||||
|
.expect("total results should fit into a UInt");
|
||||||
|
|
||||||
let state: RoomStates = results
|
let state: RoomStates = results
|
||||||
.iter()
|
.iter()
|
||||||
@@ -161,16 +154,12 @@ async fn category_room_events(
|
|||||||
pdu
|
pdu
|
||||||
})
|
})
|
||||||
.map(Event::into_format)
|
.map(Event::into_format)
|
||||||
.map(|result| SearchResult {
|
.map(|result| {
|
||||||
rank: None,
|
assign!(SearchResult::new(), {
|
||||||
result: Some(result),
|
rank: None,
|
||||||
context: EventContextResult {
|
result: Some(result),
|
||||||
profile_info: BTreeMap::new(), //TODO
|
context: EventContextResult::default() // TODO
|
||||||
events_after: Vec::new(), //TODO
|
})
|
||||||
events_before: Vec::new(), //TODO
|
|
||||||
start: None, //TODO
|
|
||||||
end: None, //TODO
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
.await;
|
.await;
|
||||||
@@ -186,14 +175,14 @@ async fn category_room_events(
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.map(ToString::to_string);
|
.map(ToString::to_string);
|
||||||
|
|
||||||
Ok(ResultRoomEvents {
|
Ok(assign!(ResultRoomEvents::new(), {
|
||||||
count: Some(total),
|
count: Some(total),
|
||||||
next_batch,
|
next_batch,
|
||||||
results,
|
results,
|
||||||
state,
|
state,
|
||||||
highlights,
|
highlights,
|
||||||
groups: BTreeMap::new(), // TODO
|
groups: ResultGroupMapsByGroupingKey::default(), // TODO
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn procure_room_state(services: &Services, room_id: &RoomId) -> Result<RoomState> {
|
async fn procure_room_state(services: &Services, room_id: &RoomId) -> Result<RoomState> {
|
||||||
|
|||||||
Reference in New Issue
Block a user