/* * Copyright (c) 2022-2024 Niko Bonnieure, Par le Peuple, NextGraph.org developers * All rights reserved. * Licensed under the Apache License, Version 2.0 * * or the MIT license , * at your option. All files in the project carrying such * notice may not be copied, modified, or distributed except * according to those terms. */ use crate::broker::{ServerConfig, BROKER}; use crate::connection::NoiseFSM; use crate::types::*; use crate::{actor::*, types::ProtocolMessage}; use async_std::sync::Mutex; use ng_repo::errors::*; use ng_repo::log::*; use ng_repo::types::PubKey; use serde::{Deserialize, Serialize}; use std::sync::Arc; use super::StartProtocol; /// Add invitation #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AddInvitationV0 { pub invite_code: InvitationCode, pub expiry: u32, pub memo: Option, pub tos_url: bool, } /// Add invitation #[derive(Clone, Debug, Serialize, Deserialize)] pub enum AddInvitation { V0(AddInvitationV0), } impl AddInvitation { pub fn code(&self) -> &InvitationCode { match self { AddInvitation::V0(o) => &o.invite_code, } } pub fn expiry(&self) -> u32 { match self { AddInvitation::V0(o) => o.expiry, } } pub fn memo(&self) -> &Option { match self { AddInvitation::V0(o) => &o.memo, } } pub fn tos_url(&self) -> bool { match self { AddInvitation::V0(o) => o.tos_url, } } pub fn get_actor(&self) -> Box { Actor::::new_responder(0) } } impl TryFrom for AddInvitation { type Error = ProtocolError; fn try_from(msg: ProtocolMessage) -> Result { if let ProtocolMessage::Start(StartProtocol::Admin(AdminRequest::V0(AdminRequestV0 { content: AdminRequestContentV0::AddInvitation(a), .. }))) = msg { Ok(a) } else { log_debug!("INVALID {:?}", msg); Err(ProtocolError::InvalidValue) } } } impl From for ProtocolMessage { fn from(msg: AddInvitation) -> ProtocolMessage { unimplemented!(); } } impl From for AdminRequestContentV0 { fn from(msg: AddInvitation) -> AdminRequestContentV0 { AdminRequestContentV0::AddInvitation(msg) } } impl Actor<'_, AddInvitation, AdminResponse> {} #[async_trait::async_trait] impl EActor for Actor<'_, AddInvitation, AdminResponse> { async fn respond( &mut self, msg: ProtocolMessage, fsm: Arc>, ) -> Result<(), ProtocolError> { let req = AddInvitation::try_from(msg)?; let broker = BROKER.read().await; broker .get_server_broker()? .add_invitation(req.code(), req.expiry(), req.memo())?; let invitation = crate::types::Invitation::V0(InvitationV0::new( broker.get_bootstrap()?.clone(), Some(req.code().get_symkey()), None, if req.tos_url() { broker.get_registration_url().map(|s| s.clone()) } else { None }, )); let response: AdminResponseV0 = invitation.into(); fsm.lock().await.send(response.into()).await?; Ok(()) } } impl From for AdminResponseV0 { fn from(res: Invitation) -> AdminResponseV0 { AdminResponseV0 { id: 0, result: 0, content: AdminResponseContentV0::Invitation(res), padding: vec![], } } }