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