Skip to main content

sofie_db/
remote.rs

1//! Where a split app serves the apps it left, shared by its server and their clients.
2
3use sofie_auth::Acting;
4
5pub const PRINCIPAL_HEADER: &str = "sofie-principal";
6pub const CONTRACTS_PATH: &str = "/_sofie/contracts/";
7pub const LOOKUPS_PATH: &str = "/_sofie/lookups/";
8pub const FEED_PATH: &str = "/_sofie/feed";
9
10/// Both apps compile the DTO from its contract crate, so both name it alike.
11#[must_use]
12pub fn lookup_name<D>() -> &'static str {
13    std::any::type_name::<D>()
14}
15
16tokio::task_local! {
17    static ACTING: Option<Acting>;
18}
19
20pub async fn acting_as<F: Future>(acting: Option<Acting>, work: F) -> F::Output {
21    ACTING.scope(acting, work).await
22}
23
24#[must_use]
25pub fn acting() -> Option<Acting> {
26    ACTING.try_with(Clone::clone).ok().flatten()
27}
28
29#[cfg(test)]
30mod tests {
31    use sofie_core::Uuid;
32
33    use super::*;
34
35    #[tokio::test]
36    async fn work_acts_for_the_principal_of_its_scope_only() {
37        let customer = Acting {
38            area: "customer".to_owned(),
39            principal: Uuid::now_v7().unwrap(),
40        };
41
42        let inside = acting_as(Some(customer.clone()), async { acting() }).await;
43
44        assert_eq!(inside, Some(customer));
45        assert_eq!(acting(), None);
46    }
47}