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

ambee/giterated

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

Add CommitBodyType option

erremilia - ⁨2⁩ years ago

parent: tbd commit: ⁨6671085

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