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

ambee/giterated

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

Basic tag search and range

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨112ca96

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