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

ambee/giterated

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

Unified stack `GetValue` implementation

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨325f5af

⁨giterated-daemon/src/database_backend/mod.rs⁩ - ⁨4371⁩ 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::StackOperationState;
14 use giterated_stack::SubstackBuilder;
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_commit_by_id,
23 repository_diff, repository_diff_patch, repository_file_from_id, repository_file_from_path,
24 repository_get_branches, repository_get_default_branch, repository_get_latest_commit,
25 repository_get_statistics, repository_get_value_description, repository_get_value_visibility,
26 repository_info, repository_last_commit_of_file, user_get_repositories, user_get_value_bio,
27 user_get_value_display_name,
28 };
29
30 #[derive(Clone, Debug)]
31 pub struct Foobackend {}
32
33 #[async_trait::async_trait(?Send)]
34 impl ObjectBackend<StackOperationState> for Foobackend {
35 async fn object_operation<O: GiteratedObject + Debug, D: GiteratedOperation<O> + Debug>(
36 &self,
37 _object: O,
38 _operation: &str,
39 _payload: D,
40 _operation_state: &StackOperationState,
41 ) -> Result<D::Success, OperationError<D::Failure>> {
42 // We don't handle operations with this backend
43 Err(OperationError::Unhandled)
44 }
45
46 async fn get_object<O: GiteratedObject + Debug>(
47 &self,
48 _object_str: &str,
49 _operation_state: &StackOperationState,
50 ) -> Result<Object<StackOperationState, O, Self>, OperationError<ObjectRequestError>> {
51 Err(OperationError::Unhandled)
52 }
53 }
54
55 /// A backend implementation which attempts to resolve data from the instance's database.
56 #[derive(Clone)]
57 #[allow(unused)]
58 pub struct DatabaseBackend {
59 pub(self) our_instance: Instance,
60 pub(self) user_backend: Arc<Mutex<dyn UserBackend + Send>>,
61 pub(self) repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>>,
62 }
63
64 impl DatabaseBackend {
65 pub fn new(
66 instance: Instance,
67 user_backend: Arc<Mutex<dyn UserBackend + Send>>,
68 repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>>,
69 ) -> Self {
70 Self {
71 our_instance: instance,
72 user_backend,
73 repository_backend,
74 }
75 }
76
77 pub fn into_substack(self) -> SubstackBuilder<Self> {
78 let mut builder = SubstackBuilder::<Self>::new(self);
79
80 builder
81 .object::<Repository>()
82 .object::<User>()
83 .object::<Instance>();
84
85 builder
86 .setting::<DisplayName>()
87 .setting::<Bio>()
88 .setting::<Description>()
89 .setting::<Visibility>()
90 .setting::<DefaultBranch>();
91
92 builder
93 .value(user_get_value_bio)
94 .value(user_get_value_display_name)
95 .value(repository_get_value_description)
96 .value(repository_get_value_visibility)
97 .value(repository_get_default_branch)
98 .value(repository_get_latest_commit);
99
100 builder
101 .operation(user_get_repositories)
102 .operation(instance_registration_request)
103 .operation(instance_authentication_request)
104 .operation(instance_create_repository_request)
105 .operation(repository_info)
106 .operation(repository_get_statistics)
107 .operation(repository_file_from_id)
108 .operation(repository_file_from_path)
109 .operation(repository_last_commit_of_file)
110 .operation(repository_commit_by_id)
111 .operation(repository_diff)
112 .operation(repository_diff_patch)
113 .operation(repository_commit_before)
114 .operation(repository_get_branches);
115
116 builder
117 }
118 }
119
120 impl Debug for DatabaseBackend {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 f.debug_struct("DatabaseBackend").finish()
123 }
124 }
125