hyaenidae/src/admin/pagination.rs

105 lines
3.2 KiB
Rust

use crate::pagination::SearchPagination;
use hyaenidae_profiles::store::Server;
use std::collections::HashMap;
use uuid::Uuid;
pub(super) struct ServerPager<'b> {
pub(super) self_id: Uuid,
pub(super) store: &'b hyaenidae_profiles::store::Store,
pub(super) servers: &'b mut HashMap<Uuid, Server>,
}
pub(super) struct FederatedPager<'a>(pub(super) ServerPager<'a>);
pub(super) struct InboundPager<'a>(pub(super) ServerPager<'a>);
pub(super) struct OutboundPager<'a>(pub(super) ServerPager<'a>);
pub(super) struct BlockedPager<'a>(pub(super) ServerPager<'a>);
pub(super) struct KnownPager<'a>(pub(super) ServerPager<'a>);
impl<'b> SearchPagination for FederatedPager<'b> {
fn from_term<'a>(&'a mut self, _: &'a str) -> Box<dyn DoubleEndedIterator<Item = Uuid> + 'a> {
Box::new(
self.0
.store
.view
.server_follows
.forward_iter(self.0.self_id)
.chain(
self.0
.store
.view
.server_follows
.backward_iter(self.0.self_id),
)
.filter_map(move |server_id| self.0.filter_server(server_id))
.rev(),
)
}
}
impl<'b> SearchPagination for InboundPager<'b> {
fn from_term<'a>(&'a mut self, _: &'a str) -> Box<dyn DoubleEndedIterator<Item = Uuid> + 'a> {
Box::new(
self.0
.store
.view
.server_follow_requests
.forward_iter(self.0.self_id)
.filter_map(move |server_id| self.0.filter_server(server_id))
.rev(),
)
}
}
impl<'b> SearchPagination for OutboundPager<'b> {
fn from_term<'a>(&'a mut self, _: &'a str) -> Box<dyn DoubleEndedIterator<Item = Uuid> + 'a> {
Box::new(
self.0
.store
.view
.server_follow_requests
.backward_iter(self.0.self_id)
.filter_map(move |server_id| self.0.filter_server(server_id))
.rev(),
)
}
}
impl<'b> SearchPagination for BlockedPager<'b> {
fn from_term<'a>(&'a mut self, _: &'a str) -> Box<dyn DoubleEndedIterator<Item = Uuid> + 'a> {
Box::new(
self.0
.store
.view
.server_blocks
.backward_iter(self.0.self_id)
.filter_map(move |server_id| self.0.filter_server(server_id))
.rev(),
)
}
}
impl<'b> SearchPagination for KnownPager<'b> {
fn from_term<'a>(&'a mut self, _: &'a str) -> Box<dyn DoubleEndedIterator<Item = Uuid> + 'a> {
Box::new(
self.0
.store
.servers
.known()
.filter_map(move |server_id| self.0.filter_server(server_id))
.rev(),
)
}
}
impl<'b> ServerPager<'b> {
fn filter_server(&mut self, server_id: Uuid) -> Option<Uuid> {
if !self.servers.contains_key(&server_id) {
let server = self.store.servers.by_id(server_id).ok()??;
self.servers.insert(server.id(), server);
Some(server_id)
} else {
None
}
}
}