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 #[serde(skip, default = "init_stable_state")]
18 pub stable: StableState,
19
20 pub heap: HeapState,
22
23 #[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>, 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 serde::{Deserialize, Serialize};
73
74 #[derive(CandidType, Deserialize)]
75 pub struct Config {
76 pub storage: StorageConfig,
77 pub db: Option<DbConfig>,
78 pub authentication: Option<AuthenticationConfig>,
79 pub automation: Option<AutomationConfig>,
80 }
81
82 #[derive(CandidType, Serialize, Deserialize, Clone)]
83 pub struct DeleteProposalAssets {
84 pub proposal_ids: Vec<ProposalId>,
85 }
86
87 #[derive(CandidType, Serialize, Deserialize)]
88 pub enum AuthenticationArgs {
89 OpenId(OpenIdPrepareDelegationArgs),
90 }
91
92 pub type AuthenticationResult = Result<Authentication, AuthenticationError>;
93
94 #[derive(CandidType, Serialize, Deserialize)]
95 pub struct Authentication {
96 pub delegation: PreparedDelegation,
97 pub doc: Doc,
98 }
99
100 #[derive(CandidType, Serialize, Deserialize)]
101 pub enum AuthenticationError {
102 PrepareDelegation(PrepareDelegationError),
103 RegisterUser(String),
104 }
105
106 #[derive(CandidType, Serialize, Deserialize)]
107 pub enum GetDelegationArgs {
108 OpenId(OpenIdGetDelegationArgs),
109 }
110
111 #[derive(CandidType, Serialize, Deserialize)]
115 pub enum AuthenticateResultResponse {
116 Ok(Authentication),
117 Err(AuthenticationError),
118 }
119
120 #[derive(CandidType, Serialize, Deserialize)]
121 pub enum GetDelegationResultResponse {
122 Ok(SignedDelegation),
123 Err(GetDelegationError),
124 }
125
126 #[derive(CandidType, Serialize, Deserialize)]
127 pub enum AuthenticateAutomationResultResponse {
128 Ok(PreparedAutomation),
129 Err(AuthenticationAutomationError),
130 }
131}
132
133pub mod store {
134 use junobuild_auth::state::types::config::AuthenticationConfig;
135 use junobuild_collections::types::core::CollectionKey;
136 use junobuild_collections::types::rules::Rule;
137 use junobuild_shared::types::state::{Controllers, UserId};
138
139 pub struct StoreContext<'a> {
140 pub caller: UserId,
141 pub controllers: &'a Controllers,
142 pub collection: &'a CollectionKey,
143 }
144
145 pub struct AssertContext<'a> {
146 pub rule: &'a Rule,
147 pub auth_config: &'a Option<AuthenticationConfig>,
148 }
149}
150
151pub mod hooks {
152 use crate::db::types::state::{DocAssertDelete, DocAssertSet, DocContext, DocUpsert};
153 use crate::Doc;
154 use candid::{CandidType, Deserialize};
155 use junobuild_shared::types::state::UserId;
156 use junobuild_storage::types::store::{Asset, AssetAssertUpload};
157
158 #[derive(CandidType, Deserialize, Clone)]
177 pub struct HookContext<T> {
178 pub caller: UserId,
179 pub data: T,
180 }
181
182 pub type OnSetDocContext = HookContext<DocContext<DocUpsert>>;
184
185 pub type OnSetManyDocsContext = HookContext<Vec<DocContext<DocUpsert>>>;
187
188 pub type OnDeleteDocContext = HookContext<DocContext<Option<Doc>>>;
190
191 pub type OnDeleteManyDocsContext = HookContext<Vec<DocContext<Option<Doc>>>>;
193
194 pub type OnDeleteFilteredDocsContext = HookContext<Vec<DocContext<Option<Doc>>>>;
196
197 pub type OnUploadAssetContext = HookContext<Asset>;
199
200 pub type OnDeleteAssetContext = HookContext<Option<Asset>>;
202
203 pub type OnDeleteManyAssetsContext = HookContext<Vec<Option<Asset>>>;
205
206 pub type OnDeleteFilteredAssetsContext = HookContext<Vec<Option<Asset>>>;
208
209 pub type AssertSetDocContext = HookContext<DocContext<DocAssertSet>>;
211
212 pub type AssertDeleteDocContext = HookContext<DocContext<DocAssertDelete>>;
214
215 pub type AssertUploadAssetContext = HookContext<AssetAssertUpload>;
217
218 pub type AssertDeleteAssetContext = HookContext<Asset>;
220}