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

ambee/giterated

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

Improve oid_from_reference tag id fetching and pass full Repository Object to every function

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨6d7e35e

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