JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Fix internal call values

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8e8ab90

⁨giterated-daemon/src/database_backend/handler.rs⁩ - ⁨13009⁩ bytes
Raw
1 use std::sync::Arc;
2
3 use giterated_models::{
4 authenticated::UserAuthenticationToken,
5 error::{InstanceError, IntoInternalError, OperationError, RepositoryError, UserError},
6 instance::{
7 AuthenticationTokenRequest, Instance, RegisterAccountRequest, RepositoryCreateRequest,
8 },
9 object_backend::ObjectBackend,
10 repository::{
11 Commit, DefaultBranch, Description, LatestCommit, Repository, RepositoryBranch,
12 RepositoryBranchesRequest, RepositoryCommitBeforeRequest, RepositoryCommitFromIdRequest,
13 RepositoryDiff, RepositoryDiffPatchRequest, RepositoryDiffRequest, RepositoryFile,
14 RepositoryFileFromIdRequest, RepositoryFileFromPathRequest, RepositoryFileInspectRequest,
15 RepositoryInfoRequest, RepositoryLastCommitOfFileRequest, RepositoryStatistics,
16 RepositoryStatisticsRequest, RepositorySummary, RepositoryView, Visibility,
17 },
18 user::{User, UserRepositoriesRequest},
19 };
20 use giterated_stack::{AuthenticatedUser, GiteratedStack, StackOperationState};
21
22 use super::DatabaseBackend;
23
24 pub async fn user_get_repositories(
25 object: User,
26 _operation: UserRepositoriesRequest,
27 state: DatabaseBackend,
28 _operation_state: StackOperationState,
29 requester: Option<AuthenticatedUser>,
30 ) -> Result<Vec<RepositorySummary>, OperationError<UserError>> {
31 let object = object.clone();
32
33 let mut user_backend = state.user_backend.lock().await;
34 let repositories_response = user_backend
35 .repositories_for_user(&requester, &object)
36 .await
37 .as_internal_error()?;
38 drop(user_backend);
39 let mut repositories_backend = state.repository_backend.lock().await;
40
41 let mut repositories = vec![];
42
43 for repository in repositories_response {
44 if repositories_backend
45 .exists(&requester, &repository.repository)
46 .await
47 .as_internal_error()?
48 {
49 repositories.push(repository);
50 }
51 }
52
53 Ok(repositories)
54 }
55
56 pub async fn repository_info(
57 object: Repository,
58 operation: RepositoryInfoRequest,
59 state: DatabaseBackend,
60 operation_state: StackOperationState,
61 backend: Arc<GiteratedStack>,
62 requester: Option<AuthenticatedUser>,
63 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
64 info!("Called");
65 let mut object = backend
66 .get_object::<Repository>(&object.to_string(), &operation_state)
67 .await
68 .unwrap();
69 let mut repository_backend = state.repository_backend.lock().await;
70 let tree = repository_backend
71 .repository_file_inspect(
72 &requester,
73 object.object(),
74 &RepositoryFileInspectRequest {
75 extra_metadata: operation.extra_metadata,
76 path: operation.path.clone(),
77 rev: operation.rev.clone(),
78 },
79 )
80 .await
81 .as_internal_error()?;
82
83 let statistics = repository_backend
84 .repository_get_statistics(
85 &requester,
86 object.object(),
87 &RepositoryStatisticsRequest {
88 rev: operation.rev.clone(),
89 },
90 )
91 .await
92 .as_internal_error()?;
93 drop(repository_backend);
94
95 let info = RepositoryView {
96 name: object.object().name.clone(),
97 owner: object.object().owner.clone(),
98 description: object.get::<Description>(&operation_state).await.ok(),
99 visibility: object
100 .get::<Visibility>(&operation_state)
101 .await
102 .as_internal_error()?,
103 default_branch: object
104 .get::<DefaultBranch>(&operation_state)
105 .await
106 .as_internal_error()?,
107 // TODO: Can't be a simple get function, this needs to be returned alongside the tree as this differs depending on the rev and path.
108 latest_commit: object.get::<LatestCommit>(&operation_state).await.ok(),
109 stats: statistics,
110 tree_rev: operation.rev.clone(),
111 tree,
112 };
113
114 Ok(info)
115 }
116
117 pub async fn repository_get_statistics(
118 object: Repository,
119 operation: RepositoryStatisticsRequest,
120 state: DatabaseBackend,
121 operation_state: StackOperationState,
122 backend: Arc<GiteratedStack>,
123 requester: Option<AuthenticatedUser>,
124 ) -> Result<RepositoryStatistics, OperationError<RepositoryError>> {
125 let object = backend
126 .get_object::<Repository>(&object.to_string(), &operation_state)
127 .await
128 .unwrap();
129
130 let mut repository_backend = state.repository_backend.lock().await;
131 let statistics = repository_backend
132 .repository_get_statistics(
133 &requester,
134 object.object(),
135 &RepositoryStatisticsRequest {
136 rev: operation.rev.clone(),
137 },
138 )
139 .await
140 .as_internal_error()?;
141 drop(repository_backend);
142
143 Ok(statistics)
144 }
145
146 pub async fn repository_get_branches(
147 object: Repository,
148 operation: RepositoryBranchesRequest,
149 state: DatabaseBackend,
150 operation_state: StackOperationState,
151 backend: Arc<GiteratedStack>,
152 requester: Option<AuthenticatedUser>,
153 ) -> Result<Vec<RepositoryBranch>, OperationError<RepositoryError>> {
154 let object = backend
155 .get_object::<Repository>(&object.to_string(), &operation_state)
156 .await
157 .unwrap();
158
159 let mut repository_backend = state.repository_backend.lock().await;
160 let branches = repository_backend
161 .repository_get_branches(&requester, object.object(), &operation)
162 .await
163 .as_internal_error()?;
164 drop(repository_backend);
165
166 Ok(branches)
167 }
168
169 pub async fn repository_file_from_id(
170 object: Repository,
171 operation: RepositoryFileFromIdRequest,
172 state: DatabaseBackend,
173 operation_state: StackOperationState,
174 backend: Arc<GiteratedStack>,
175
176 requester: Option<AuthenticatedUser>,
177 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
178 let object = backend
179 .get_object::<Repository>(&object.to_string(), &operation_state)
180 .await
181 .unwrap();
182
183 let mut repository_backend = state.repository_backend.lock().await;
184 let file = repository_backend
185 .repository_file_from_id(
186 &requester,
187 object.object(),
188 &RepositoryFileFromIdRequest(operation.0.clone()),
189 )
190 .await
191 .as_internal_error()?;
192 drop(repository_backend);
193
194 Ok(file)
195 }
196
197 pub async fn repository_file_from_path(
198 object: Repository,
199 operation: RepositoryFileFromPathRequest,
200 state: DatabaseBackend,
201 operation_state: StackOperationState,
202 backend: Arc<GiteratedStack>,
203 requester: Option<AuthenticatedUser>,
204 ) -> Result<(RepositoryFile, String), OperationError<RepositoryError>> {
205 let object = backend
206 .get_object::<Repository>(&object.to_string(), &operation_state)
207 .await
208 .unwrap();
209
210 let mut repository_backend = state.repository_backend.lock().await;
211 let file = repository_backend
212 .repository_file_from_path(
213 &requester,
214 object.object(),
215 &RepositoryFileFromPathRequest {
216 rev: operation.rev.clone(),
217 path: operation.path.clone(),
218 },
219 )
220 .await
221 .as_internal_error()?;
222 drop(repository_backend);
223
224 Ok(file)
225 }
226
227 pub async fn repository_last_commit_of_file(
228 object: Repository,
229 operation: RepositoryLastCommitOfFileRequest,
230 state: DatabaseBackend,
231 operation_state: StackOperationState,
232 backend: Arc<GiteratedStack>,
233 requester: Option<AuthenticatedUser>,
234 ) -> Result<Commit, OperationError<RepositoryError>> {
235 let object = backend
236 .get_object::<Repository>(&object.to_string(), &operation_state)
237 .await
238 .unwrap();
239
240 let mut repository_backend = state.repository_backend.lock().await;
241 let commit = repository_backend
242 .repository_last_commit_of_file(
243 &requester,
244 object.object(),
245 &RepositoryLastCommitOfFileRequest {
246 start_commit: operation.start_commit.clone(),
247 path: operation.path.clone(),
248 },
249 )
250 .await
251 .as_internal_error()?;
252 drop(repository_backend);
253
254 Ok(commit)
255 }
256
257 pub async fn repository_commit_by_id(
258 object: Repository,
259 operation: RepositoryCommitFromIdRequest,
260 state: DatabaseBackend,
261 operation_state: StackOperationState,
262 backend: Arc<GiteratedStack>,
263 requester: Option<AuthenticatedUser>,
264 ) -> Result<Commit, OperationError<RepositoryError>> {
265 let object = backend
266 .get_object::<Repository>(&object.to_string(), &operation_state)
267 .await
268 .unwrap();
269
270 let mut repository_backend = state.repository_backend.lock().await;
271 let commit = repository_backend
272 .repository_commit_from_id(
273 &requester,
274 object.object(),
275 &RepositoryCommitFromIdRequest(operation.0.clone()),
276 )
277 .await
278 .as_internal_error()?;
279 drop(repository_backend);
280
281 Ok(commit)
282 }
283
284 pub async fn repository_diff(
285 object: Repository,
286 operation: RepositoryDiffRequest,
287 state: DatabaseBackend,
288 operation_state: StackOperationState,
289 backend: Arc<GiteratedStack>,
290 requester: Option<AuthenticatedUser>,
291 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
292 let object = backend
293 .get_object::<Repository>(&object.to_string(), &operation_state)
294 .await
295 .unwrap();
296
297 let mut repository_backend = state.repository_backend.lock().await;
298 let diff = repository_backend
299 .repository_diff(&requester, object.object(), &operation)
300 .await
301 .as_internal_error()?;
302 drop(repository_backend);
303
304 Ok(diff)
305 }
306
307 pub async fn repository_diff_patch(
308 object: Repository,
309 operation: RepositoryDiffPatchRequest,
310 state: DatabaseBackend,
311 operation_state: StackOperationState,
312 backend: Arc<GiteratedStack>,
313 requester: Option<AuthenticatedUser>,
314 ) -> Result<String, OperationError<RepositoryError>> {
315 let object = backend
316 .get_object::<Repository>(&object.to_string(), &operation_state)
317 .await
318 .unwrap();
319
320 let mut repository_backend = state.repository_backend.lock().await;
321 let patch = repository_backend
322 .repository_diff_patch(&requester, object.object(), &operation)
323 .await
324 .as_internal_error()?;
325 drop(repository_backend);
326
327 Ok(patch)
328 }
329
330 pub async fn repository_commit_before(
331 object: Repository,
332 operation: RepositoryCommitBeforeRequest,
333 state: DatabaseBackend,
334 operation_state: StackOperationState,
335 backend: Arc<GiteratedStack>,
336 requester: Option<AuthenticatedUser>,
337 ) -> Result<Commit, OperationError<RepositoryError>> {
338 let object = backend
339 .get_object::<Repository>(&object.to_string(), &operation_state)
340 .await
341 .unwrap();
342
343 let mut repository_backend = state.repository_backend.lock().await;
344 let file = repository_backend
345 .repository_commit_before(&requester, object.object(), &operation)
346 .await
347 .as_internal_error()?;
348 drop(repository_backend);
349
350 Ok(file)
351 }
352
353 pub async fn instance_authentication_request(
354 object: Instance,
355 operation: AuthenticationTokenRequest,
356 state: DatabaseBackend,
357 _operation_state: StackOperationState,
358 // Authorizes the request for SAME-INSTANCE
359 // _authorized_instance: AuthorizedInstance,
360 ) -> Result<UserAuthenticationToken, OperationError<InstanceError>> {
361 let mut backend = state.user_backend.lock().await;
362
363 backend
364 .login(&object, operation.clone())
365 .await
366 .as_internal_error()
367 }
368
369 pub async fn instance_registration_request(
370 _object: Instance,
371 operation: RegisterAccountRequest,
372 state: DatabaseBackend,
373 _operation_state: StackOperationState, // Authorizes the request for SAME-INSTANCE
374 // _authorized_instance: AuthorizedInstance,
375 ) -> Result<UserAuthenticationToken, OperationError<InstanceError>> {
376 let mut backend = state.user_backend.lock().await;
377
378 backend
379 .register(operation.clone())
380 .await
381 .as_internal_error()
382 }
383
384 pub async fn instance_create_repository_request(
385 _object: Instance,
386 operation: RepositoryCreateRequest,
387 state: DatabaseBackend,
388 _operation_state: StackOperationState,
389 requester: AuthenticatedUser,
390 // Authorizes the request for SAME-INSTANCE
391 // _authorized_instance: AuthorizedInstance,
392 ) -> Result<Repository, OperationError<InstanceError>> {
393 let mut backend = state.repository_backend.lock().await;
394
395 backend
396 .create_repository(&requester, &operation)
397 .await
398 .as_internal_error()
399 }
400
401 pub async fn repository_latest_commit(
402 _repository: Repository,
403 _state: DatabaseBackend,
404 _operation_state: StackOperationState,
405 ) -> Result<LatestCommit, OperationError<RepositoryError>> {
406 Ok(LatestCommit(None))
407 }
408