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

ambee/giterated

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

Singular tag request

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨d239950

⁨giterated-daemon/src/database_backend/handler.rs⁩ - ⁨16781⁩ bytes
Raw
1 use giterated_models::{
2 authenticated::UserAuthenticationToken,
3 error::{InstanceError, IntoInternalError, OperationError, RepositoryError, UserError},
4 instance::{
5 AuthenticationTokenRequest, Instance, RegisterAccountRequest, RepositoryCreateRequest,
6 },
7 object_backend::ObjectBackend,
8 repository::{
9 Commit, DefaultBranch, Description, LatestCommit, Repository, RepositoryBranch,
10 RepositoryBranchRequest, RepositoryBranchesRequest, RepositoryCommitBeforeRequest,
11 RepositoryCommitFromIdRequest, RepositoryDiff, RepositoryDiffPatchRequest,
12 RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest,
13 RepositoryFileFromPathRequest, RepositoryFileInspectRequest, RepositoryInfoRequest,
14 RepositoryLastCommitOfFileRequest, RepositoryStatistics, RepositoryStatisticsRequest,
15 RepositorySummary, RepositoryTag, RepositoryTagRequest, RepositoryTagsRequest,
16 RepositoryView, Visibility,
17 },
18 user::{User, UserRepositoriesRequest},
19 };
20 use giterated_stack::{AuthenticatedUser, GiteratedStack, OperationState, StackOperationState};
21
22 use super::DatabaseBackend;
23
24 pub async fn user_get_repositories(
25 object: User,
26 _operation: UserRepositoriesRequest,
27 state: DatabaseBackend,
28 OperationState(_operation_state): OperationState<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 OperationState(operation_state): OperationState<StackOperationState>,
61 backend: 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 &mut object,
74 OperationState(operation_state.clone()),
75 &RepositoryFileInspectRequest {
76 extra_metadata: operation.extra_metadata,
77 path: operation.path.clone(),
78 rev: operation.rev.clone(),
79 },
80 )
81 .await
82 .as_internal_error()?;
83
84 let statistics = repository_backend
85 .repository_get_statistics(
86 &requester,
87 &mut object,
88 OperationState(operation_state.clone()),
89 &RepositoryStatisticsRequest {
90 rev: operation.rev.clone(),
91 },
92 )
93 .await
94 .as_internal_error()?;
95 drop(repository_backend);
96
97 let info = RepositoryView {
98 name: object.object().name.clone(),
99 owner: object.object().owner.clone(),
100 description: object.get::<Description>(&operation_state).await.ok(),
101 visibility: object
102 .get::<Visibility>(&operation_state)
103 .await
104 .as_internal_error()?,
105 default_branch: object
106 .get::<DefaultBranch>(&operation_state)
107 .await
108 .as_internal_error()?,
109 // 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.
110 latest_commit: object.get::<LatestCommit>(&operation_state).await.ok(),
111 stats: statistics,
112 tree_rev: operation.rev.clone(),
113 tree,
114 };
115
116 Ok(info)
117 }
118
119 pub async fn repository_get_statistics(
120 object: Repository,
121 operation: RepositoryStatisticsRequest,
122 state: DatabaseBackend,
123 OperationState(operation_state): OperationState<StackOperationState>,
124 backend: GiteratedStack,
125 requester: Option<AuthenticatedUser>,
126 ) -> Result<RepositoryStatistics, OperationError<RepositoryError>> {
127 let mut object = backend
128 .get_object::<Repository>(&object.to_string(), &operation_state)
129 .await
130 .unwrap();
131
132 let mut repository_backend = state.repository_backend.lock().await;
133 let statistics = repository_backend
134 .repository_get_statistics(
135 &requester,
136 &mut object,
137 OperationState(operation_state),
138 &RepositoryStatisticsRequest {
139 rev: operation.rev.clone(),
140 },
141 )
142 .await
143 .as_internal_error()?;
144 drop(repository_backend);
145
146 Ok(statistics)
147 }
148
149 pub async fn repository_get_branches(
150 object: Repository,
151 operation: RepositoryBranchesRequest,
152 state: DatabaseBackend,
153 OperationState(operation_state): OperationState<StackOperationState>,
154 backend: GiteratedStack,
155 requester: Option<AuthenticatedUser>,
156 ) -> Result<(Vec<RepositoryBranch>, usize), OperationError<RepositoryError>> {
157 let mut object = backend
158 .get_object::<Repository>(&object.to_string(), &operation_state)
159 .await
160 .unwrap();
161
162 let mut repository_backend = state.repository_backend.lock().await;
163 let branches = repository_backend
164 .repository_get_branches(
165 &requester,
166 &mut object,
167 OperationState(operation_state),
168 &operation,
169 )
170 .await
171 .as_internal_error()?;
172 drop(repository_backend);
173
174 Ok(branches)
175 }
176
177 pub async fn repository_get_branch(
178 object: Repository,
179 operation: RepositoryBranchRequest,
180 state: DatabaseBackend,
181 OperationState(operation_state): OperationState<StackOperationState>,
182 backend: GiteratedStack,
183 requester: Option<AuthenticatedUser>,
184 ) -> Result<RepositoryBranch, OperationError<RepositoryError>> {
185 let mut object = backend
186 .get_object::<Repository>(&object.to_string(), &operation_state)
187 .await
188 .unwrap();
189
190 let mut repository_backend = state.repository_backend.lock().await;
191 let branch = repository_backend
192 .repository_get_branch(
193 &requester,
194 &mut object,
195 OperationState(operation_state),
196 &operation,
197 )
198 .await
199 .as_internal_error()?;
200 drop(repository_backend);
201
202 Ok(branch)
203 }
204
205 pub async fn repository_get_tags(
206 object: Repository,
207 operation: RepositoryTagsRequest,
208 state: DatabaseBackend,
209 OperationState(operation_state): OperationState<StackOperationState>,
210 backend: GiteratedStack,
211 requester: Option<AuthenticatedUser>,
212 ) -> Result<(Vec<RepositoryTag>, usize), OperationError<RepositoryError>> {
213 let mut object = backend
214 .get_object::<Repository>(&object.to_string(), &operation_state)
215 .await
216 .unwrap();
217
218 let mut repository_backend = state.repository_backend.lock().await;
219 let branches = repository_backend
220 .repository_get_tags(
221 &requester,
222 &mut object,
223 OperationState(operation_state),
224 &operation,
225 )
226 .await
227 .as_internal_error()?;
228 drop(repository_backend);
229
230 Ok(branches)
231 }
232
233 pub async fn repository_get_tag(
234 object: Repository,
235 operation: RepositoryTagRequest,
236 state: DatabaseBackend,
237 OperationState(operation_state): OperationState<StackOperationState>,
238 backend: GiteratedStack,
239 requester: Option<AuthenticatedUser>,
240 ) -> Result<RepositoryTag, OperationError<RepositoryError>> {
241 let mut object = backend
242 .get_object::<Repository>(&object.to_string(), &operation_state)
243 .await
244 .unwrap();
245
246 let mut repository_backend = state.repository_backend.lock().await;
247 let branches = repository_backend
248 .repository_get_tag(
249 &requester,
250 &mut object,
251 OperationState(operation_state),
252 &operation,
253 )
254 .await
255 .as_internal_error()?;
256 drop(repository_backend);
257
258 Ok(branches)
259 }
260
261 pub async fn repository_file_from_id(
262 object: Repository,
263 operation: RepositoryFileFromIdRequest,
264 state: DatabaseBackend,
265 OperationState(operation_state): OperationState<StackOperationState>,
266 backend: GiteratedStack,
267 requester: Option<AuthenticatedUser>,
268 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
269 let mut object = backend
270 .get_object::<Repository>(&object.to_string(), &operation_state)
271 .await
272 .unwrap();
273
274 let mut repository_backend = state.repository_backend.lock().await;
275 let file = repository_backend
276 .repository_file_from_id(
277 &requester,
278 &mut object,
279 OperationState(operation_state),
280 &RepositoryFileFromIdRequest(operation.0.clone()),
281 )
282 .await
283 .as_internal_error()?;
284 drop(repository_backend);
285
286 Ok(file)
287 }
288
289 pub async fn repository_file_from_path(
290 object: Repository,
291 operation: RepositoryFileFromPathRequest,
292 state: DatabaseBackend,
293 OperationState(operation_state): OperationState<StackOperationState>,
294 backend: GiteratedStack,
295 requester: Option<AuthenticatedUser>,
296 ) -> Result<(RepositoryFile, String), OperationError<RepositoryError>> {
297 let mut object = backend
298 .get_object::<Repository>(&object.to_string(), &operation_state)
299 .await
300 .unwrap();
301
302 let mut repository_backend = state.repository_backend.lock().await;
303 let file = repository_backend
304 .repository_file_from_path(
305 &requester,
306 &mut object,
307 OperationState(operation_state),
308 &RepositoryFileFromPathRequest {
309 rev: operation.rev.clone(),
310 path: operation.path.clone(),
311 },
312 )
313 .await
314 .as_internal_error()?;
315 drop(repository_backend);
316
317 Ok(file)
318 }
319
320 pub async fn repository_last_commit_of_file(
321 object: Repository,
322 operation: RepositoryLastCommitOfFileRequest,
323 state: DatabaseBackend,
324 OperationState(operation_state): OperationState<StackOperationState>,
325 backend: GiteratedStack,
326 requester: Option<AuthenticatedUser>,
327 ) -> Result<Commit, OperationError<RepositoryError>> {
328 let mut object = backend
329 .get_object::<Repository>(&object.to_string(), &operation_state)
330 .await
331 .unwrap();
332
333 let mut repository_backend = state.repository_backend.lock().await;
334 let commit = repository_backend
335 .repository_last_commit_of_file(
336 &requester,
337 &mut object,
338 OperationState(operation_state),
339 &RepositoryLastCommitOfFileRequest {
340 start_commit: operation.start_commit.clone(),
341 path: operation.path.clone(),
342 },
343 )
344 .await
345 .as_internal_error()?;
346 drop(repository_backend);
347
348 Ok(commit)
349 }
350
351 pub async fn repository_commit_by_id(
352 object: Repository,
353 operation: RepositoryCommitFromIdRequest,
354 state: DatabaseBackend,
355 OperationState(operation_state): OperationState<StackOperationState>,
356 backend: GiteratedStack,
357 requester: Option<AuthenticatedUser>,
358 ) -> Result<Commit, OperationError<RepositoryError>> {
359 let mut object = backend
360 .get_object::<Repository>(&object.to_string(), &operation_state)
361 .await
362 .unwrap();
363
364 let mut repository_backend = state.repository_backend.lock().await;
365 let commit = repository_backend
366 .repository_commit_from_id(
367 &requester,
368 &mut object,
369 OperationState(operation_state),
370 &RepositoryCommitFromIdRequest(operation.0.clone()),
371 )
372 .await
373 .as_internal_error()?;
374 drop(repository_backend);
375
376 Ok(commit)
377 }
378
379 pub async fn repository_diff(
380 object: Repository,
381 operation: RepositoryDiffRequest,
382 state: DatabaseBackend,
383 OperationState(operation_state): OperationState<StackOperationState>,
384 backend: GiteratedStack,
385 requester: Option<AuthenticatedUser>,
386 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
387 let mut object = backend
388 .get_object::<Repository>(&object.to_string(), &operation_state)
389 .await
390 .unwrap();
391
392 let mut repository_backend = state.repository_backend.lock().await;
393 let diff = repository_backend
394 .repository_diff(
395 &requester,
396 &mut object,
397 OperationState(operation_state),
398 &operation,
399 )
400 .await
401 .as_internal_error()?;
402 drop(repository_backend);
403
404 Ok(diff)
405 }
406
407 pub async fn repository_diff_patch(
408 object: Repository,
409 operation: RepositoryDiffPatchRequest,
410 state: DatabaseBackend,
411 OperationState(operation_state): OperationState<StackOperationState>,
412 backend: GiteratedStack,
413 requester: Option<AuthenticatedUser>,
414 ) -> Result<String, OperationError<RepositoryError>> {
415 let mut object = backend
416 .get_object::<Repository>(&object.to_string(), &operation_state)
417 .await
418 .unwrap();
419
420 let mut repository_backend = state.repository_backend.lock().await;
421 let patch = repository_backend
422 .repository_diff_patch(
423 &requester,
424 &mut object,
425 OperationState(operation_state),
426 &operation,
427 )
428 .await
429 .as_internal_error()?;
430 drop(repository_backend);
431
432 Ok(patch)
433 }
434
435 pub async fn repository_commit_before(
436 object: Repository,
437 operation: RepositoryCommitBeforeRequest,
438 state: DatabaseBackend,
439 OperationState(operation_state): OperationState<StackOperationState>,
440 backend: GiteratedStack,
441 requester: Option<AuthenticatedUser>,
442 ) -> Result<Commit, OperationError<RepositoryError>> {
443 let mut object = backend
444 .get_object::<Repository>(&object.to_string(), &operation_state)
445 .await
446 .unwrap();
447
448 let mut repository_backend = state.repository_backend.lock().await;
449 let file = repository_backend
450 .repository_commit_before(
451 &requester,
452 &mut object,
453 OperationState(operation_state),
454 &operation,
455 )
456 .await
457 .as_internal_error()?;
458 drop(repository_backend);
459
460 Ok(file)
461 }
462
463 pub async fn instance_authentication_request(
464 object: Instance,
465 operation: AuthenticationTokenRequest,
466 state: DatabaseBackend,
467 OperationState(_operation_state): OperationState<StackOperationState>,
468 // Authorizes the request for SAME-INSTANCE
469 // _authorized_instance: AuthorizedInstance,
470 ) -> Result<UserAuthenticationToken, OperationError<InstanceError>> {
471 let mut backend = state.user_backend.lock().await;
472
473 backend
474 .login(&object, operation.clone())
475 .await
476 .as_internal_error()
477 }
478
479 pub async fn instance_registration_request(
480 _object: Instance,
481 operation: RegisterAccountRequest,
482 state: DatabaseBackend,
483 OperationState(_operation_state): OperationState<StackOperationState>, // Authorizes the request for SAME-INSTANCE
484 // _authorized_instance: AuthorizedInstance,
485 ) -> Result<UserAuthenticationToken, OperationError<InstanceError>> {
486 let mut backend = state.user_backend.lock().await;
487
488 backend
489 .register(operation.clone())
490 .await
491 .as_internal_error()
492 }
493
494 pub async fn instance_create_repository_request(
495 _object: Instance,
496 operation: RepositoryCreateRequest,
497 state: DatabaseBackend,
498 OperationState(_operation_state): OperationState<StackOperationState>,
499 requester: AuthenticatedUser,
500 // Authorizes the request for SAME-INSTANCE
501 // _authorized_instance: AuthorizedInstance,
502 ) -> Result<Repository, OperationError<InstanceError>> {
503 let mut backend = state.repository_backend.lock().await;
504
505 backend
506 .create_repository(&requester, &operation)
507 .await
508 .as_internal_error()
509 }
510
511 pub async fn repository_latest_commit(
512 _repository: Repository,
513 _state: DatabaseBackend,
514 OperationState(_operation_state): OperationState<StackOperationState>,
515 ) -> Result<LatestCommit, OperationError<RepositoryError>> {
516 Ok(LatestCommit(None))
517 }
518