ng_net/actors/admin/
list_invitations.rs

1/*
2 * Copyright (c) 2022-2025 Niko Bonnieure, Par le Peuple, NextGraph.org developers
3 * All rights reserved.
4 * Licensed under the Apache License, Version 2.0
5 * <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
6 * or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
7 * at your option. All files in the project carrying such
8 * notice may not be copied, modified, or distributed except
9 * according to those terms.
10*/
11
12use std::sync::Arc;
13
14use async_std::sync::Mutex;
15use serde::{Deserialize, Serialize};
16
17use ng_repo::errors::*;
18#[allow(unused_imports)]
19use ng_repo::log::*;
20
21use super::super::StartProtocol;
22
23use crate::broker::BROKER;
24use crate::connection::NoiseFSM;
25use crate::types::*;
26use crate::{actor::*, types::ProtocolMessage};
27
28/// List invitations registered on this broker
29#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
30pub struct ListInvitationsV0 {
31    /// should list only the admin invitations.
32    pub admin: bool,
33    /// should list only the unique invitations.
34    pub unique: bool,
35    /// should list only the multi invitations.
36    pub multi: bool,
37}
38
39/// List invitations registered on this broker
40#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
41pub enum ListInvitations {
42    V0(ListInvitationsV0),
43}
44
45impl ListInvitations {
46    pub fn admin(&self) -> bool {
47        match self {
48            Self::V0(o) => o.admin,
49        }
50    }
51    pub fn unique(&self) -> bool {
52        match self {
53            Self::V0(o) => o.unique,
54        }
55    }
56    pub fn multi(&self) -> bool {
57        match self {
58            Self::V0(o) => o.multi,
59        }
60    }
61    pub fn get_actor(&self) -> Box<dyn EActor> {
62        Actor::<ListInvitations, AdminResponse>::new_responder(0)
63    }
64}
65
66impl TryFrom<ProtocolMessage> for ListInvitations {
67    type Error = ProtocolError;
68    fn try_from(msg: ProtocolMessage) -> Result<Self, Self::Error> {
69        if let ProtocolMessage::Start(StartProtocol::Admin(AdminRequest::V0(AdminRequestV0 {
70            content: AdminRequestContentV0::ListInvitations(a),
71            ..
72        }))) = msg
73        {
74            Ok(a)
75        } else {
76            //log_debug!("INVALID {:?}", msg);
77            Err(ProtocolError::InvalidValue)
78        }
79    }
80}
81
82impl From<ListInvitations> for ProtocolMessage {
83    fn from(_msg: ListInvitations) -> ProtocolMessage {
84        unimplemented!();
85    }
86}
87
88impl From<ListInvitations> for AdminRequestContentV0 {
89    fn from(msg: ListInvitations) -> AdminRequestContentV0 {
90        AdminRequestContentV0::ListInvitations(msg)
91    }
92}
93
94impl Actor<'_, ListInvitations, AdminResponse> {}
95
96#[async_trait::async_trait]
97impl EActor for Actor<'_, ListInvitations, AdminResponse> {
98    async fn respond(
99        &mut self,
100        msg: ProtocolMessage,
101        fsm: Arc<Mutex<NoiseFSM>>,
102    ) -> Result<(), ProtocolError> {
103        let req = ListInvitations::try_from(msg)?;
104        let sb = { BROKER.read().await.get_server_broker()? };
105        let res = {
106            sb.read()
107                .await
108                .list_invitations(req.admin(), req.unique(), req.multi())
109        };
110        let response: AdminResponseV0 = res.into();
111        fsm.lock().await.send(response.into()).await?;
112        Ok(())
113    }
114}
115
116impl From<Result<Vec<(InvitationCode, u32, Option<String>)>, ProtocolError>> for AdminResponseV0 {
117    fn from(
118        res: Result<Vec<(InvitationCode, u32, Option<String>)>, ProtocolError>,
119    ) -> AdminResponseV0 {
120        match res {
121            Err(e) => AdminResponseV0 {
122                id: 0,
123                result: e.into(),
124                content: AdminResponseContentV0::EmptyResponse,
125                padding: vec![],
126            },
127            Ok(vec) => AdminResponseV0 {
128                id: 0,
129                result: 0,
130                content: AdminResponseContentV0::Invitations(vec),
131                padding: vec![],
132            },
133        }
134    }
135}