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::Controllers;
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: Controllers,
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::db::types::config::DbConfig;
60    use crate::Doc;
61    use candid::CandidType;
62    use junobuild_auth::delegation::types::{
63        GetDelegationError, OpenIdGetDelegationArgs, OpenIdPrepareDelegationArgs,
64        PrepareDelegationError, PreparedDelegation, SignedDelegation,
65    };
66    use junobuild_auth::state::types::config::AuthenticationConfig;
67    use junobuild_cdn::proposals::ProposalId;
68    use junobuild_storage::types::config::StorageConfig;
69    use serde::{Deserialize, Serialize};
70
71    #[derive(CandidType, Deserialize)]
72    pub struct Config {
73        pub storage: StorageConfig,
74        pub db: Option<DbConfig>,
75        pub authentication: Option<AuthenticationConfig>,
76    }
77
78    #[derive(CandidType, Serialize, Deserialize, Clone)]
79    pub struct DeleteProposalAssets {
80        pub proposal_ids: Vec<ProposalId>,
81    }
82
83    #[derive(CandidType, Serialize, Deserialize)]
84    pub enum AuthenticationArgs {
85        OpenId(OpenIdPrepareDelegationArgs),
86    }
87
88    pub type AuthenticationResult = Result<Authentication, AuthenticationError>;
89
90    #[derive(CandidType, Serialize, Deserialize)]
91    pub struct Authentication {
92        pub delegation: PreparedDelegation,
93        pub doc: Doc,
94    }
95
96    #[derive(CandidType, Serialize, Deserialize)]
97    pub enum AuthenticationError {
98        PrepareDelegation(PrepareDelegationError),
99        RegisterUser(String),
100    }
101
102    #[derive(CandidType, Serialize, Deserialize)]
103    pub enum GetDelegationArgs {
104        OpenId(OpenIdGetDelegationArgs),
105    }
106
107    // We need custom types for Result to avoid
108    // clashes with didc when developers
109    // include_satellite and use Result as well.
110    #[derive(CandidType, Serialize, Deserialize)]
111    pub enum AuthenticateResultResponse {
112        Ok(Authentication),
113        Err(AuthenticationError),
114    }
115
116    #[derive(CandidType, Serialize, Deserialize)]
117    pub enum GetDelegationResultResponse {
118        Ok(SignedDelegation),
119        Err(GetDelegationError),
120    }
121}
122
123pub mod store {
124    use junobuild_auth::state::types::config::AuthenticationConfig;
125    use junobuild_collections::types::core::CollectionKey;
126    use junobuild_collections::types::rules::Rule;
127    use junobuild_shared::types::state::{Controllers, UserId};
128
129    pub struct StoreContext<'a> {
130        pub caller: UserId,
131        pub controllers: &'a Controllers,
132        pub collection: &'a CollectionKey,
133    }
134
135    pub struct AssertContext<'a> {
136        pub rule: &'a Rule,
137        pub auth_config: &'a Option<AuthenticationConfig>,
138    }
139}
140
141pub mod hooks {
142    use crate::db::types::state::{DocAssertDelete, DocAssertSet, DocContext, DocUpsert};
143    use crate::Doc;
144    use candid::{CandidType, Deserialize};
145    use junobuild_shared::types::state::UserId;
146    use junobuild_storage::types::store::{Asset, AssetAssertUpload};
147
148    /// A generic context struct used in Juno satellite hooks.
149    ///
150    /// The `HookContext` struct contains information about the caller and associated data.
151    ///
152    /// # Fields
153    /// - `caller`: A `UserId` representing the caller of the hook.
154    /// - `data`: A generic type `T` representing the associated data for the hook.
155    ///
156    /// This context struct is used in various satellite hooks to provide information about the caller
157    /// and the specific data related to the hook.
158    ///
159    /// Example usage:
160    /// ```rust
161    /// #[on_set_doc(collections = ["demo"])]
162    /// async fn on_set_doc(context: OnSetDocContext) -> Result<(), String> {
163    ///     // Your hook logic here
164    /// }
165    /// ```
166    #[derive(CandidType, Deserialize, Clone)]
167    pub struct HookContext<T> {
168        pub caller: UserId,
169        pub data: T,
170    }
171
172    /// A type alias for the context used in the `on_set_doc` satellite hook.
173    pub type OnSetDocContext = HookContext<DocContext<DocUpsert>>;
174
175    /// A type alias for the context used in the `on_set_many_docs` satellite hook.
176    pub type OnSetManyDocsContext = HookContext<Vec<DocContext<DocUpsert>>>;
177
178    /// A type alias for the context used in the `on_delete_doc` satellite hook.
179    pub type OnDeleteDocContext = HookContext<DocContext<Option<Doc>>>;
180
181    /// A type alias for the context used in the `on_delete_many_docs` satellite hook.
182    pub type OnDeleteManyDocsContext = HookContext<Vec<DocContext<Option<Doc>>>>;
183
184    /// A type alias for the context used in the `on_delete_filtered_docs` satellite hook.
185    pub type OnDeleteFilteredDocsContext = HookContext<Vec<DocContext<Option<Doc>>>>;
186
187    /// A type alias for the context used in the `on_upload_asset` satellite hook.
188    pub type OnUploadAssetContext = HookContext<Asset>;
189
190    /// A type alias for the context used in the `on_delete_asset` satellite hook.
191    pub type OnDeleteAssetContext = HookContext<Option<Asset>>;
192
193    /// A type alias for the context used in the `on_delete_many_assets` satellite hook.
194    pub type OnDeleteManyAssetsContext = HookContext<Vec<Option<Asset>>>;
195
196    /// A type alias for the context used in the `on_delete_filtered_assets` satellite hook.
197    pub type OnDeleteFilteredAssetsContext = HookContext<Vec<Option<Asset>>>;
198
199    /// A type alias for the context used in the `assert_set_doc` satellite hook.
200    pub type AssertSetDocContext = HookContext<DocContext<DocAssertSet>>;
201
202    /// A type alias for the context used in the `assert_delete_doc` satellite hook.
203    pub type AssertDeleteDocContext = HookContext<DocContext<DocAssertDelete>>;
204
205    /// A type alias for the context used in the `assert_upload_asset` satellite hook.
206    pub type AssertUploadAssetContext = HookContext<AssetAssertUpload>;
207
208    /// A type alias for the context used in the `assert_delete_asset` satellite hook.
209    pub type AssertDeleteAssetContext = HookContext<Asset>;
210}