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

ambee/giterated

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

Fix networked setting request

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨4b5330d

⁨giterated-daemon/src/database_backend/mod.rs⁩ - ⁨4513⁩ 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_setting, repository_get_statistics, repository_get_value_description,
26 repository_get_value_visibility, repository_info, repository_last_commit_of_file,
27 user_get_repositories, user_get_setting, user_get_value_bio, 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_settings(repository_get_setting)
83 .object::<User>()
84 .object_settings(user_get_setting)
85 .object::<Instance>();
86
87 builder
88 .setting::<DisplayName>()
89 .setting::<Bio>()
90 .setting::<Description>()
91 .setting::<Visibility>()
92 .setting::<DefaultBranch>();
93
94 builder
95 .value(user_get_value_bio)
96 .value(user_get_value_display_name)
97 .value(repository_get_value_description)
98 .value(repository_get_value_visibility)
99 .value(repository_get_default_branch)
100 .value(repository_get_latest_commit);
101
102 builder
103 .operation(user_get_repositories)
104 .operation(instance_registration_request)
105 .operation(instance_authentication_request)
106 .operation(instance_create_repository_request)
107 .operation(repository_info)
108 .operation(repository_get_statistics)
109 .operation(repository_file_from_id)
110 .operation(repository_file_from_path)
111 .operation(repository_last_commit_of_file)
112 .operation(repository_commit_by_id)
113 .operation(repository_diff)
114 .operation(repository_diff_patch)
115 .operation(repository_commit_before)
116 .operation(repository_get_branches);
117
118 builder
119 }
120 }
121
122 impl Debug for DatabaseBackend {
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 f.debug_struct("DatabaseBackend").finish()
125 }
126 }
127