ng_net/
server_broker.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
12//! Trait for ServerBroker
13
14use std::path::PathBuf;
15use std::sync::Arc;
16
17use async_std::sync::Mutex;
18
19use ng_repo::block_storage::BlockStorage;
20use ng_repo::errors::*;
21use ng_repo::types::*;
22
23use crate::app_protocol::{AppRequest, AppSessionStart, AppSessionStartResponse, AppSessionStop};
24use crate::broker::ClientPeerId;
25use crate::connection::NoiseFSM;
26use crate::types::*;
27use crate::utils::Receiver;
28
29#[async_trait::async_trait]
30pub trait IServerBroker: Send + Sync {
31    async fn remove_rendezvous(&self, rendezvous: &SymKey);
32    async fn put_wallet_export(&self, rendezvous: SymKey, export: ExportedWallet);
33    async fn get_wallet_export(&self, rendezvous: SymKey) -> Result<ExportedWallet, ServerError>;
34    async fn put_wallet_at_rendezvous(
35        &self,
36        rendezvous: SymKey,
37        export: ExportedWallet,
38    ) -> Result<(), ServerError>;
39    async fn wait_for_wallet_at_rendezvous(
40        &self,
41        rendezvous: SymKey,
42    ) -> Receiver<Result<ExportedWallet, ServerError>>;
43    fn get_path_users(&self) -> PathBuf;
44    fn get_block_storage(&self) -> Arc<std::sync::RwLock<dyn BlockStorage + Send + Sync>>;
45    fn put_block(&self, overlay_id: &OverlayId, block: Block) -> Result<(), ServerError>;
46    fn has_block(&self, overlay_id: &OverlayId, block_id: &BlockId) -> Result<(), ServerError>;
47    fn get_block(&self, overlay_id: &OverlayId, block_id: &BlockId) -> Result<Block, ServerError>;
48    async fn create_user(&self, broker_id: &DirectPeerId) -> Result<UserId, ProtocolError>;
49    fn get_user(&self, user_id: PubKey) -> Result<bool, ProtocolError>;
50    fn has_no_user(&self) -> Result<bool, ProtocolError>;
51    fn get_user_credentials(&self, user_id: &PubKey) -> Result<Credentials, ProtocolError>;
52    fn add_user_credentials(
53        &self,
54        user_id: &PubKey,
55        credentials: &Credentials,
56    ) -> Result<(), ProtocolError>;
57    fn add_user(&self, user_id: PubKey, is_admin: bool) -> Result<(), ProtocolError>;
58    fn del_user(&self, user_id: PubKey) -> Result<(), ProtocolError>;
59    fn list_users(&self, admins: bool) -> Result<Vec<PubKey>, ProtocolError>;
60    fn list_invitations(
61        &self,
62        admin: bool,
63        unique: bool,
64        multi: bool,
65    ) -> Result<Vec<(InvitationCode, u32, Option<String>)>, ProtocolError>;
66    fn add_invitation(
67        &self,
68        invite_code: &InvitationCode,
69        expiry: u32,
70        memo: &Option<String>,
71    ) -> Result<(), ProtocolError>;
72    fn get_invitation_type(&self, invite: [u8; 32]) -> Result<u8, ProtocolError>;
73    fn remove_invitation(&self, invite: [u8; 32]) -> Result<(), ProtocolError>;
74    fn take_master_key(&mut self) -> Result<SymKey, ProtocolError>;
75    async fn app_process_request(
76        &self,
77        req: AppRequest,
78        request_id: i64,
79        fsm: &Mutex<NoiseFSM>,
80    ) -> Result<(), ServerError>;
81
82    async fn app_session_start(
83        &self,
84        req: AppSessionStart,
85        remote_peer_id: DirectPeerId,
86        local_peer_id: DirectPeerId,
87    ) -> Result<AppSessionStartResponse, ServerError>;
88    async fn app_session_stop(
89        &self,
90        req: AppSessionStop,
91        remote_peer_id: &DirectPeerId,
92    ) -> Result<EmptyAppResponse, ServerError>;
93
94    fn next_seq_for_peer(&self, peer: &PeerId, seq: u64) -> Result<(), ServerError>;
95
96    fn get_repo_pin_status(
97        &self,
98        overlay: &OverlayId,
99        repo: &RepoHash,
100        user_id: &UserId,
101    ) -> Result<RepoPinStatus, ServerError>;
102
103    async fn pin_repo_write(
104        &self,
105        overlay: &OverlayAccess,
106        repo: &RepoHash,
107        user_id: &UserId,
108        ro_topics: &Vec<TopicId>,
109        rw_topics: &Vec<PublisherAdvert>,
110        overlay_root_topic: &Option<TopicId>,
111        expose_outer: bool,
112        peer: &ClientPeerId,
113    ) -> Result<RepoOpened, ServerError>;
114
115    async fn pin_repo_read(
116        &self,
117        overlay: &OverlayId,
118        repo: &RepoHash,
119        user_id: &UserId,
120        ro_topics: &Vec<TopicId>,
121        peer: &ClientPeerId,
122    ) -> Result<RepoOpened, ServerError>;
123
124    async fn topic_sub(
125        &self,
126        overlay: &OverlayId,
127        repo: &RepoHash,
128        topic: &TopicId,
129        user_id: &UserId,
130        publisher: Option<&PublisherAdvert>,
131        peer: &ClientPeerId,
132    ) -> Result<TopicSubRes, ServerError>;
133
134    fn get_commit(&self, overlay: &OverlayId, id: &ObjectId) -> Result<Vec<Block>, ServerError>;
135
136    async fn dispatch_event(
137        &self,
138        overlay: &OverlayId,
139        event: Event,
140        user_id: &UserId,
141        remote_peer: &PubKey,
142    ) -> Result<Vec<ClientPeerId>, ServerError>;
143
144    async fn remove_all_subscriptions_of_client(&self, client: &ClientPeerId);
145
146    fn topic_sync_req(
147        &self,
148        overlay: &OverlayId,
149        topic: &TopicId,
150        known_heads: &Vec<ObjectId>,
151        target_heads: &Vec<ObjectId>,
152        known_commits: &Option<BloomFilter>,
153    ) -> Result<Vec<TopicSyncRes>, ServerError>;
154}