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

ambee/giterated

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

Unified stack refactor clean up

Clean up obsolete code and some warnings

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨356f714

⁨giterated-daemon/src/database_backend/mod.rs⁩ - ⁨3819⁩ bytes
Raw
1 pub mod handler;
2 pub mod updates;
3
4 use std::sync::Arc;
5
6 use giterated_models::error::OperationError;
7 use giterated_models::instance::Instance;
8 use giterated_models::object::{GiteratedObject, Object, ObjectRequestError};
9 use giterated_models::object_backend::ObjectBackend;
10 use giterated_models::operation::GiteratedOperation;
11 use giterated_models::repository::{DefaultBranch, Description, Repository, Visibility};
12 use giterated_models::user::{Bio, DisplayName, User};
13 use giterated_stack::runtime::RuntimeBuilder;
14 use giterated_stack::StackOperationState;
15 use std::fmt::Debug;
16 use tokio::sync::Mutex;
17
18 use crate::backend::{RepositoryBackend, UserBackend};
19
20 use self::handler::{
21 instance_authentication_request, instance_create_repository_request,
22 instance_registration_request, repository_commit_before, repository_diff,
23 repository_diff_patch, repository_file_from_id, repository_file_from_path,
24 repository_get_value_description, repository_get_value_visibility, repository_info,
25 user_get_value_bio, user_get_value_display_name,
26 };
27
28 #[derive(Clone, Debug)]
29 pub struct Foobackend {}
30
31 #[async_trait::async_trait]
32 impl ObjectBackend<StackOperationState> for Foobackend {
33 async fn object_operation<O: GiteratedObject + Debug, D: GiteratedOperation<O> + Debug>(
34 &self,
35 _object: O,
36 _operation: &str,
37 _payload: D,
38 _operation_state: &StackOperationState,
39 ) -> Result<D::Success, OperationError<D::Failure>> {
40 // We don't handle operations with this backend
41 Err(OperationError::Unhandled)
42 }
43
44 async fn get_object<O: GiteratedObject + Debug>(
45 &self,
46 _object_str: &str,
47 _operation_state: &StackOperationState,
48 ) -> Result<Object<StackOperationState, O, Self>, OperationError<ObjectRequestError>> {
49 Err(OperationError::Unhandled)
50 }
51 }
52
53 /// A backend implementation which attempts to resolve data from the instance's database.
54 #[derive(Clone)]
55 #[allow(unused)]
56 pub struct DatabaseBackend {
57 pub(self) our_instance: Instance,
58 pub(self) user_backend: Arc<Mutex<dyn UserBackend + Send>>,
59 pub(self) repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>>,
60 }
61
62 impl DatabaseBackend {
63 pub fn new(
64 instance: Instance,
65 user_backend: Arc<Mutex<dyn UserBackend + Send>>,
66 repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>>,
67 ) -> Self {
68 Self {
69 our_instance: instance,
70 user_backend,
71 repository_backend,
72 }
73 }
74
75 pub fn into_backend(&self) -> RuntimeBuilder<Self> {
76 let mut builder = RuntimeBuilder::<Self>::new();
77
78 builder
79 .object::<Instance>()
80 .object::<Repository>()
81 .object::<User>();
82
83 builder
84 .setting::<DisplayName>()
85 .setting::<Bio>()
86 .setting::<Description>()
87 .setting::<Visibility>()
88 .setting::<DefaultBranch>();
89
90 builder
91 .value(user_get_value_display_name)
92 .value(user_get_value_bio)
93 .value(repository_get_value_description)
94 .value(repository_get_value_visibility);
95
96 builder
97 .operation(repository_info)
98 .operation(repository_file_from_id)
99 .operation(repository_file_from_path)
100 .operation(repository_diff)
101 .operation(repository_diff_patch)
102 .operation(repository_commit_before)
103 .operation(instance_registration_request)
104 .operation(instance_authentication_request)
105 .operation(instance_create_repository_request);
106
107 builder
108 }
109 }
110
111 impl Debug for DatabaseBackend {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 f.debug_struct("DatabaseBackend").finish()
114 }
115 }
116