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

ambee/giterated

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

Re-expose Operation State in generics.

This is the worst code I have ever written. I hate the way this changes everything. ugh.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨90db3e2

⁨giterated-daemon/src/database_backend/handler.rs⁩ - ⁨13466⁩ 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
174 requester: Option<AuthenticatedUser>,
175 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
176 let object = backend
177 .get_object::<Repository>(&object.to_string(), &operation_state)
178 .await
179 .unwrap();
180
181 let mut repository_backend = state.repository_backend.lock().await;
182 let file = repository_backend
183 .repository_file_from_id(
184 &requester,
185 object.object(),
186 &RepositoryFileFromIdRequest(operation.0.clone()),
187 )
188 .await
189 .as_internal_error()?;
190 drop(repository_backend);
191
192 Ok(file)
193 }
194
195 pub async fn repository_file_from_path(
196 object: Repository,
197 operation: RepositoryFileFromPathRequest,
198 state: DatabaseBackend,
199 OperationState(operation_state): OperationState<StackOperationState>,
200 backend: GiteratedStack,
201 requester: Option<AuthenticatedUser>,
202 ) -> Result<(RepositoryFile, String), OperationError<RepositoryError>> {
203 let object = backend
204 .get_object::<Repository>(&object.to_string(), &operation_state)
205 .await
206 .unwrap();
207
208 let mut repository_backend = state.repository_backend.lock().await;
209 let file = repository_backend
210 .repository_file_from_path(
211 &requester,
212 object.object(),
213 &RepositoryFileFromPathRequest {
214 rev: operation.rev.clone(),
215 path: operation.path.clone(),
216 },
217 )
218 .await
219 .as_internal_error()?;
220 drop(repository_backend);
221
222 Ok(file)
223 }
224
225 pub async fn repository_last_commit_of_file(
226 object: Repository,
227 operation: RepositoryLastCommitOfFileRequest,
228 state: DatabaseBackend,
229 OperationState(operation_state): OperationState<StackOperationState>,
230 backend: GiteratedStack,
231 requester: Option<AuthenticatedUser>,
232 ) -> Result<Commit, OperationError<RepositoryError>> {
233 let object = backend
234 .get_object::<Repository>(&object.to_string(), &operation_state)
235 .await
236 .unwrap();
237
238 let mut repository_backend = state.repository_backend.lock().await;
239 let commit = repository_backend
240 .repository_last_commit_of_file(
241 &requester,
242 object.object(),
243 &RepositoryLastCommitOfFileRequest {
244 start_commit: operation.start_commit.clone(),
245 path: operation.path.clone(),
246 },
247 )
248 .await
249 .as_internal_error()?;
250 drop(repository_backend);
251
252 Ok(commit)
253 }
254
255 pub async fn repository_commit_by_id(
256 object: Repository,
257 operation: RepositoryCommitFromIdRequest,
258 state: DatabaseBackend,
259 OperationState(operation_state): OperationState<StackOperationState>,
260 backend: GiteratedStack,
261 requester: Option<AuthenticatedUser>,
262 ) -> Result<Commit, OperationError<RepositoryError>> {
263 let object = backend
264 .get_object::<Repository>(&object.to_string(), &operation_state)
265 .await
266 .unwrap();
267
268 let mut repository_backend = state.repository_backend.lock().await;
269 let commit = repository_backend
270 .repository_commit_from_id(
271 &requester,
272 object.object(),
273 &RepositoryCommitFromIdRequest(operation.0.clone()),
274 )
275 .await
276 .as_internal_error()?;
277 drop(repository_backend);
278
279 Ok(commit)
280 }
281
282 pub async fn repository_diff(
283 object: Repository,
284 operation: RepositoryDiffRequest,
285 state: DatabaseBackend,
286 OperationState(operation_state): OperationState<StackOperationState>,
287 backend: GiteratedStack,
288 requester: Option<AuthenticatedUser>,
289 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
290 let object = backend
291 .get_object::<Repository>(&object.to_string(), &operation_state)
292 .await
293 .unwrap();
294
295 let mut repository_backend = state.repository_backend.lock().await;
296 let diff = repository_backend
297 .repository_diff(&requester, object.object(), &operation)
298 .await
299 .as_internal_error()?;
300 drop(repository_backend);
301
302 Ok(diff)
303 }
304
305 pub async fn repository_diff_patch(
306 object: Repository,
307 operation: RepositoryDiffPatchRequest,
308 state: DatabaseBackend,
309 OperationState(operation_state): OperationState<StackOperationState>,
310 backend: GiteratedStack,
311 requester: Option<AuthenticatedUser>,
312 ) -> Result<String, OperationError<RepositoryError>> {
313 let object = backend
314 .get_object::<Repository>(&object.to_string(), &operation_state)
315 .await
316 .unwrap();
317
318 let mut repository_backend = state.repository_backend.lock().await;
319 let patch = repository_backend
320 .repository_diff_patch(&requester, object.object(), &operation)
321 .await
322 .as_internal_error()?;
323 drop(repository_backend);
324
325 Ok(patch)
326 }
327
328 pub async fn repository_commit_before(
329 object: Repository,
330 operation: RepositoryCommitBeforeRequest,
331 state: DatabaseBackend,
332 OperationState(operation_state): OperationState<StackOperationState>,
333 backend: GiteratedStack,
334 requester: Option<AuthenticatedUser>,
335 ) -> Result<Commit, OperationError<RepositoryError>> {
336 let object = backend
337 .get_object::<Repository>(&object.to_string(), &operation_state)
338 .await
339 .unwrap();
340
341 let mut repository_backend = state.repository_backend.lock().await;
342 let file = repository_backend
343 .repository_commit_before(&requester, object.object(), &operation)
344 .await
345 .as_internal_error()?;
346 drop(repository_backend);
347
348 Ok(file)
349 }
350
351 pub async fn instance_authentication_request(
352 object: Instance,
353 operation: AuthenticationTokenRequest,
354 state: DatabaseBackend,
355 OperationState(_operation_state): OperationState<StackOperationState>,
356 // Authorizes the request for SAME-INSTANCE
357 // _authorized_instance: AuthorizedInstance,
358 ) -> Result<UserAuthenticationToken, OperationError<InstanceError>> {
359 let mut backend = state.user_backend.lock().await;
360
361 backend
362 .login(&object, operation.clone())
363 .await
364 .as_internal_error()
365 }
366
367 pub async fn instance_registration_request(
368 _object: Instance,
369 operation: RegisterAccountRequest,
370 state: DatabaseBackend,
371 OperationState(_operation_state): OperationState<StackOperationState>, // Authorizes the request for SAME-INSTANCE
372 // _authorized_instance: AuthorizedInstance,
373 ) -> Result<UserAuthenticationToken, OperationError<InstanceError>> {
374 let mut backend = state.user_backend.lock().await;
375
376 backend
377 .register(operation.clone())
378 .await
379 .as_internal_error()
380 }
381
382 pub async fn instance_create_repository_request(
383 _object: Instance,
384 operation: RepositoryCreateRequest,
385 state: DatabaseBackend,
386 OperationState(_operation_state): OperationState<StackOperationState>,
387 requester: AuthenticatedUser,
388 // Authorizes the request for SAME-INSTANCE
389 // _authorized_instance: AuthorizedInstance,
390 ) -> Result<Repository, OperationError<InstanceError>> {
391 let mut backend = state.repository_backend.lock().await;
392
393 backend
394 .create_repository(&requester, &operation)
395 .await
396 .as_internal_error()
397 }
398
399 pub async fn repository_latest_commit(
400 _repository: Repository,
401 _state: DatabaseBackend,
402 OperationState(_operation_state): OperationState<StackOperationState>,
403 ) -> Result<LatestCommit, OperationError<RepositoryError>> {
404 Ok(LatestCommit(None))
405 }
406