Skip to main content

junobuild_satellite/
types.rs

1pub mod state {
2    use crate::assets::storage::types::state::{AssetsStable, ContentChunksStable};
3    use crate::db::types::state::{DbHeapState, DbRuntimeState, DbStable};
4    use crate::memory::internal::init_stable_state;
5    use candid::CandidType;
6    use junobuild_auth::state::types::state::AuthenticationHeapState;
7    use junobuild_cdn::proposals::ProposalsStable;
8    use junobuild_cdn::storage::{ProposalAssetsStable, ProposalContentChunksStable};
9    use junobuild_shared::types::state::AccessKeys;
10    use junobuild_storage::types::state::StorageHeapState;
11    use rand::rngs::StdRng;
12    use serde::{Deserialize, Serialize};
13
14    #[derive(Serialize, Deserialize)]
15    pub struct State {
16        // Direct stable state: State that is uses stable memory directly as its store. No need for pre/post upgrade hooks.
17        #[serde(skip, default = "init_stable_state")]
18        pub stable: StableState,
19
20        // Indirect stable state: State that lives on the heap, but is saved into stable memory on upgrades.
21        pub heap: HeapState,
22
23        // Unstable state: State that resides only on the heap, that’s lost after an upgrade.
24        #[serde(skip, default)]
25        pub runtime: RuntimeState,
26    }
27
28    pub struct StableState {
29        pub db: DbStable,
30        pub assets: AssetsStable,
31        pub content_chunks: ContentChunksStable,
32        pub proposals_assets: ProposalAssetsStable,
33        pub proposals_content_chunks: ProposalContentChunksStable,
34        pub proposals: ProposalsStable,
35    }
36
37    #[derive(Default, CandidType, Serialize, Deserialize, Clone)]
38    pub struct HeapState {
39        pub controllers: AccessKeys,
40        pub db: DbHeapState,
41        pub storage: StorageHeapState,
42        pub authentication: Option<AuthenticationHeapState>,
43    }
44
45    #[derive(Default, Clone)]
46    pub struct RuntimeState {
47        pub rng: Option<StdRng>, // rng = Random Number Generator
48        pub db: DbRuntimeState,
49    }
50
51    #[derive(CandidType, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
52    pub enum CollectionType {
53        Db,
54        Storage,
55    }
56}
57
58pub mod interface {
59    use crate::automation::types::AuthenticationAutomationError;
60    use crate::db::types::config::DbConfig;
61    use crate::Doc;
62    use candid::CandidType;
63    use junobuild_auth::automation::types::PreparedAutomation;
64    use junobuild_auth::delegation::types::{
65        GetDelegationError, OpenIdGetDelegationArgs, OpenIdPrepareDelegationArgs,
66        PrepareDelegationError, PreparedDelegation, SignedDelegation,
67    };
68    use junobuild_auth::state::types::automation::AutomationConfig;
69    use junobuild_auth::state::types::config::AuthenticationConfig;
70    use junobuild_cdn::proposals::ProposalId;
71    use junobuild_storage::types::config::StorageConfig;
72    use junobuild_storage::types::interface::{CertifyAssetsCursor, CertifyAssetsStrategy};
73    use serde::{Deserialize, Serialize};
74
75    #[derive(CandidType, Deserialize)]
76    pub struct Config {
77        pub storage: StorageConfig,
78        pub db: Option<DbConfig>,
79        pub authentication: Option<AuthenticationConfig>,
80        pub automation: Option<AutomationConfig>,
81    }
82
83    #[derive(CandidType, Serialize, Deserialize, Clone)]
84    pub struct DeleteProposalAssets {
85        pub proposal_ids: Vec<ProposalId>,
86    }
87
88    #[derive(CandidType, Serialize, Deserialize)]
89    pub enum AuthenticationArgs {
90        OpenId(OpenIdPrepareDelegationArgs),
91    }
92
93    pub type AuthenticationResult = Result<Authentication, AuthenticationError>;
94
95    #[derive(CandidType, Serialize, Deserialize)]
96    pub struct Authentication {
97        pub delegation: PreparedDelegation,
98        pub doc: Doc,
99    }
100
101    #[derive(CandidType, Serialize, Deserialize)]
102    pub enum AuthenticationError {
103        PrepareDelegation(PrepareDelegationError),
104        RegisterUser(String),
105    }
106
107    #[derive(CandidType, Serialize, Deserialize)]
108    pub enum GetDelegationArgs {
109        OpenId(OpenIdGetDelegationArgs),
110    }
111
112    // We need custom types for Result to avoid
113    // clashes with didc when developers
114    // include_satellite and use Result as well.
115    #[derive(CandidType, Serialize, Deserialize)]
116    pub enum AuthenticateResultResponse {
117        Ok(Authentication),
118        Err(AuthenticationError),
119    }
120
121    #[derive(CandidType, Serialize, Deserialize)]
122    pub enum GetDelegationResultResponse {
123        Ok(SignedDelegation),
124        Err(GetDelegationError),
125    }
126
127    #[derive(CandidType, Serialize, Deserialize)]
128    pub enum AuthenticateAutomationResultResponse {
129        Ok(PreparedAutomation),
130        Err(AuthenticationAutomationError),
131    }
132
133    #[derive(CandidType, Serialize, Deserialize)]
134    pub struct CertifyAssetsArgs {
135        pub cursor: CertifyAssetsCursor,
136        pub chunk_size: Option<u32>,
137        pub strategy: CertifyAssetsStrategy,
138    }
139
140    #[derive(CandidType, Serialize, Deserialize)]
141    pub struct CertifyAssetsResult {
142        pub next_cursor: Option<CertifyAssetsCursor>,
143    }
144}
145
146pub mod store {
147    use junobuild_auth::state::types::config::AuthenticationConfig;
148    use junobuild_collections::types::core::CollectionKey;
149    use junobuild_collections::types::rules::Rule;
150    use junobuild_shared::types::state::{AccessKeys, UserId};
151
152    pub struct StoreContext<'a> {
153        pub caller: UserId,
154        pub controllers: &'a AccessKeys,
155        pub collection: &'a CollectionKey,
156    }
157
158    pub struct AssertContext<'a> {
159        pub rule: &'a Rule,
160        pub auth_config: &'a Option<AuthenticationConfig>,
161    }
162}
163
164pub mod hooks {
165    use crate::db::types::state::{DocAssertDelete, DocAssertSet, DocContext, DocUpsert};
166    use crate::Doc;
167    use candid::{CandidType, Deserialize};
168    use junobuild_shared::types::state::UserId;
169    use junobuild_storage::types::store::{Asset, AssetAssertUpload};
170
171    /// A generic context struct used in Juno satellite hooks.
172    ///
173    /// The `HookContext` struct contains information about the caller and associated data.
174    ///
175    /// # Fields
176    /// - `caller`: A `UserId` representing the caller of the hook.
177    /// - `data`: A generic type `T` representing the associated data for the hook.
178    ///
179    /// This context struct is used in various satellite hooks to provide information about the caller
180    /// and the specific data related to the hook.
181    ///
182    /// Example usage:
183    /// ```rust
184    /// #[on_set_doc(collections = ["demo"])]
185    /// async fn on_set_doc(context: OnSetDocContext) -> Result<(), String> {
186    ///     // Your hook logic here
187    /// }
188    /// ```
189    #[derive(CandidType, Deserialize, Clone)]
190    pub struct HookContext<T> {
191        pub caller: UserId,
192        pub data: T,
193    }
194
195    /// A type alias for the context used in the `on_set_doc` satellite hook.
196    pub type OnSetDocContext = HookContext<DocContext<DocUpsert>>;
197
198    /// A type alias for the context used in the `on_set_many_docs` satellite hook.
199    pub type OnSetManyDocsContext = HookContext<Vec<DocContext<DocUpsert>>>;
200
201    /// A type alias for the context used in the `on_delete_doc` satellite hook.
202    pub type OnDeleteDocContext = HookContext<DocContext<Option<Doc>>>;
203
204    /// A type alias for the context used in the `on_delete_many_docs` satellite hook.
205    pub type OnDeleteManyDocsContext = HookContext<Vec<DocContext<Option<Doc>>>>;
206
207    /// A type alias for the context used in the `on_delete_filtered_docs` satellite hook.
208    pub type OnDeleteFilteredDocsContext = HookContext<Vec<DocContext<Option<Doc>>>>;
209
210    /// A type alias for the context used in the `on_upload_asset` satellite hook.
211    pub type OnUploadAssetContext = HookContext<Asset>;
212
213    /// A type alias for the context used in the `on_delete_asset` satellite hook.
214    pub type OnDeleteAssetContext = HookContext<Option<Asset>>;
215
216    /// A type alias for the context used in the `on_delete_many_assets` satellite hook.
217    pub type OnDeleteManyAssetsContext = HookContext<Vec<Option<Asset>>>;
218
219    /// A type alias for the context used in the `on_delete_filtered_assets` satellite hook.
220    pub type OnDeleteFilteredAssetsContext = HookContext<Vec<Option<Asset>>>;
221
222    /// A type alias for the context used in the `assert_set_doc` satellite hook.
223    pub type AssertSetDocContext = HookContext<DocContext<DocAssertSet>>;
224
225    /// A type alias for the context used in the `assert_delete_doc` satellite hook.
226    pub type AssertDeleteDocContext = HookContext<DocContext<DocAssertDelete>>;
227
228    /// A type alias for the context used in the `assert_upload_asset` satellite hook.
229    pub type AssertUploadAssetContext = HookContext<AssetAssertUpload>;
230
231    /// A type alias for the context used in the `assert_delete_asset` satellite hook.
232    pub type AssertDeleteAssetContext = HookContext<Asset>;
233}