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

ambee/giterated

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

Simple branch staleness implementation

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨efc3f02

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