fabaccess-bffh/bffhd/capnp/user_system.rs

49 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2024-04-23 13:55:37 +02:00
use api::user_capnp::user;
use api::users_capnp::users;
use capnp::capability::Promise;
2022-03-21 00:01:50 +01:00
use capnp_rpc::pry;
2022-06-24 13:57:47 +02:00
use tracing::Span;
2022-04-26 23:21:43 +02:00
use crate::capnp::user::User;
2022-03-15 20:00:43 +01:00
2022-03-12 17:31:53 +01:00
use crate::session::SessionHandle;
2022-04-28 20:33:46 +02:00
use crate::users::{db, UserRef};
2022-04-26 23:21:43 +02:00
2022-06-24 13:57:47 +02:00
const TARGET: &str = "bffh::api::usersystem";
2022-03-15 19:14:04 +01:00
#[derive(Clone)]
pub struct Users {
2022-06-24 13:57:47 +02:00
span: Span,
2022-03-12 17:31:53 +01:00
session: SessionHandle,
}
impl Users {
2022-03-12 17:31:53 +01:00
pub fn new(session: SessionHandle) -> Self {
2022-06-24 13:57:47 +02:00
let span = tracing::info_span!(target: TARGET, "UserSystem",);
Self { span, session }
}
}
2024-04-23 13:55:37 +02:00
impl users::Server for Users {
fn list(&mut self, _: users::ListParams<>, mut result: users::ListResults<>) -> Promise<(), capnp::Error> {
2022-06-24 13:57:47 +02:00
let _guard = self.span.enter();
2024-04-23 13:55:37 +02:00
let _span = tracing::trace_span!(target: TARGET, "list",).entered();
2022-06-24 13:57:47 +02:00
tracing::trace!("method call");
2022-03-21 00:01:50 +01:00
let userdb = self.session.users.into_inner();
2022-05-05 15:50:44 +02:00
let users = pry!(userdb
.get_all()
2022-03-21 00:01:50 +01:00
.map_err(|e| capnp::Error::failed(format!("UserDB error: {:?}", e))));
2024-04-23 13:55:37 +02:00
let mut builder = result.get().init_users(users.len() as u32);
2022-07-24 16:39:33 +02:00
for (i, (id, userdata)) in users.into_iter().enumerate() {
2024-04-23 13:55:37 +02:00
let user = User::new(self.session.clone(), UserRef::new(id));
let client: user::Client = capnp_rpc::new_client(user);
builder.reborrow().set(i as u32, client.client.hook);
}
2022-06-24 13:57:47 +02:00
tracing::trace!("method return");
Promise::ok(())
2024-04-23 13:55:37 +02:00
2022-04-28 20:33:46 +02:00
}
2022-05-05 15:50:44 +02:00
}