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

ambee/giterated

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

Giterated stack changeover, refactor still incomplete

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8d40dfe

⁨giterated-daemon/src/database_backend/handler.rs⁩ - ⁨8946⁩ bytes
Raw
1 use std::error::Error;
2
3 use futures_util::{future::BoxFuture, FutureExt};
4 use giterated_models::{
5 error::{GetValueError, OperationError, RepositoryError, UserError},
6 object_backend::ObjectBackend,
7 repository::{
8 Commit, DefaultBranch, Description, LatestCommit, Repository,
9 RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryFile,
10 RepositoryFileFromIdRequest, RepositoryFileInspectRequest, RepositoryInfoRequest,
11 RepositorySummary, RepositoryView, Visibility,
12 },
13 settings::{AnySetting, GetSetting, GetSettingError, SetSetting, SetSettingError},
14 user::{User, UserRepositoriesRequest},
15 value::{AnyValue, GetValue},
16 };
17
18 use super::DatabaseBackend;
19
20 pub fn user_get_repositories(
21 object: &User,
22 _operation: UserRepositoriesRequest,
23 state: DatabaseBackend,
24 ) -> BoxFuture<'static, Result<Vec<RepositorySummary>, OperationError<UserError>>> {
25 let object = object.clone();
26
27 async move {
28 let mut user_backend = state.user_backend.lock().await;
29 let repositories = user_backend
30 .repositories_for_user(None, &object)
31 .await
32 .map_err(|e| OperationError::Internal(e.to_string()))?;
33
34 Ok(repositories)
35 }
36 .boxed()
37 }
38
39 pub fn user_get_value(
40 object: &User,
41 operation: GetValue<AnyValue<User>>,
42 state: DatabaseBackend,
43 ) -> BoxFuture<'static, Result<AnyValue<User>, OperationError<GetValueError>>> {
44 let object = object.clone();
45
46 async move {
47 let mut user_backend = state.user_backend.lock().await;
48 let value = user_backend
49 .get_value(&object, &operation.value_name)
50 .await
51 .map_err(|e| OperationError::Internal(e.to_string()))?;
52
53 Ok(value)
54 }
55 .boxed()
56 }
57
58 pub fn user_get_setting(
59 object: &User,
60 operation: GetSetting<AnySetting>,
61 state: DatabaseBackend,
62 ) -> BoxFuture<'static, Result<AnySetting, OperationError<GetSettingError>>> {
63 let object = object.clone();
64
65 async move {
66 let mut user_backend = state.user_backend.lock().await;
67 let value = user_backend
68 .get_setting(&object, &operation.setting_name)
69 .await
70 .map_err(|e| OperationError::Internal(e.to_string()))?;
71
72 Ok(value)
73 }
74 .boxed()
75 }
76
77 pub fn user_set_setting(
78 object: &User,
79 operation: SetSetting<AnySetting>,
80 state: DatabaseBackend,
81 ) -> BoxFuture<'static, Result<(), OperationError<SetSettingError>>> {
82 let object = object.clone();
83
84 async move {
85 let mut user_backend = state.user_backend.lock().await;
86 let value = user_backend
87 .write_setting(&object, &operation.setting_name, &operation.value.0)
88 .await
89 .map_err(|e| OperationError::Internal(e.to_string()))?;
90
91 Ok(value)
92 }
93 .boxed()
94 }
95
96 pub fn repository_info(
97 object: &Repository,
98 operation: RepositoryInfoRequest,
99 state: DatabaseBackend,
100 ) -> BoxFuture<'static, Result<RepositoryView, OperationError<RepositoryError>>> {
101 let object = object.clone();
102 let backend = state.into_backend();
103
104 async move {
105 let mut object = backend
106 .get_object::<Repository>(&object.to_string())
107 .await
108 .unwrap();
109
110 let mut repository_backend = state.repository_backend.lock().await;
111 let tree = repository_backend
112 .repository_file_inspect(
113 None,
114 object.object(),
115 &RepositoryFileInspectRequest {
116 extra_metadata: operation.extra_metadata,
117 path: operation.path,
118 rev: operation.rev.clone(),
119 },
120 )
121 .await
122 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
123 drop(repository_backend);
124
125 let info = RepositoryView {
126 name: object.object().name.clone(),
127 owner: object.object().owner.clone(),
128 description: object.get::<Description>().await.ok(),
129 visibility: object
130 .get::<Visibility>()
131 .await
132 .map_err(|e| OperationError::Internal(format!("{:?}: {}", e.source(), e)))?,
133 default_branch: object
134 .get::<DefaultBranch>()
135 .await
136 .map_err(|e| OperationError::Internal(format!("{:?}: {}", e.source(), e)))?,
137 // 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.
138 latest_commit: object.get::<LatestCommit>().await.ok(),
139 tree_rev: operation.rev,
140 tree,
141 };
142
143 Ok(info)
144 }
145 .boxed()
146 }
147
148 pub fn repository_file_from_id(
149 object: &Repository,
150 operation: RepositoryFileFromIdRequest,
151 state: DatabaseBackend,
152 ) -> BoxFuture<'static, Result<RepositoryFile, OperationError<RepositoryError>>> {
153 let object = object.clone();
154 let backend = state.into_backend();
155
156 async move {
157 let object = backend
158 .get_object::<Repository>(&object.to_string())
159 .await
160 .unwrap();
161
162 let mut repository_backend = state.repository_backend.lock().await;
163 let file = repository_backend
164 .repository_file_from_id(
165 None,
166 object.object(),
167 &RepositoryFileFromIdRequest(operation.0),
168 )
169 .await
170 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
171 drop(repository_backend);
172
173 Ok(file)
174 }
175 .boxed()
176 }
177
178 pub fn repository_diff(
179 object: &Repository,
180 operation: RepositoryDiffRequest,
181 state: DatabaseBackend,
182 ) -> BoxFuture<'static, Result<RepositoryDiff, OperationError<RepositoryError>>> {
183 let object = object.clone();
184 let backend = state.into_backend();
185
186 async move {
187 let object = backend
188 .get_object::<Repository>(&object.to_string())
189 .await
190 .unwrap();
191
192 let mut repository_backend = state.repository_backend.lock().await;
193 let file = repository_backend
194 .repository_diff(None, object.object(), &operation)
195 .await
196 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
197 drop(repository_backend);
198
199 Ok(file)
200 }
201 .boxed()
202 }
203
204 pub fn repository_commit_before(
205 object: &Repository,
206 operation: RepositoryCommitBeforeRequest,
207 state: DatabaseBackend,
208 ) -> BoxFuture<'static, Result<Commit, OperationError<RepositoryError>>> {
209 let object = object.clone();
210 let backend = state.into_backend();
211
212 async move {
213 let object = backend
214 .get_object::<Repository>(&object.to_string())
215 .await
216 .unwrap();
217
218 let mut repository_backend = state.repository_backend.lock().await;
219 let file = repository_backend
220 .repository_commit_before(None, object.object(), &operation)
221 .await
222 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
223 drop(repository_backend);
224
225 Ok(file)
226 }
227 .boxed()
228 }
229
230 pub fn repository_get_value(
231 object: &Repository,
232 operation: GetValue<AnyValue<Repository>>,
233 state: DatabaseBackend,
234 ) -> BoxFuture<'static, Result<AnyValue<Repository>, OperationError<GetValueError>>> {
235 let object = object.clone();
236
237 async move {
238 let mut repository_backend = state.repository_backend.lock().await;
239 let value = repository_backend
240 .get_value(&object, &operation.value_name)
241 .await
242 .map_err(|e| {
243 OperationError::Internal(format!("error getting value: {}", e.to_string()))
244 })?;
245
246 Ok(value)
247 }
248 .boxed()
249 }
250
251 pub fn repository_get_setting(
252 object: &Repository,
253 operation: GetSetting<AnySetting>,
254 state: DatabaseBackend,
255 ) -> BoxFuture<'static, Result<AnySetting, OperationError<GetSettingError>>> {
256 let object = object.clone();
257
258 async move {
259 let mut repository_backend = state.repository_backend.lock().await;
260 let value = repository_backend
261 .get_setting(&object, &operation.setting_name)
262 .await
263 .map_err(|e| OperationError::Internal(e.to_string()))?;
264
265 Ok(value)
266 }
267 .boxed()
268 }
269
270 pub fn repository_set_setting(
271 object: &Repository,
272 operation: SetSetting<AnySetting>,
273 state: DatabaseBackend,
274 ) -> BoxFuture<'static, Result<(), OperationError<SetSettingError>>> {
275 let object = object.clone();
276
277 async move {
278 let mut repository_backend = state.repository_backend.lock().await;
279 let value = repository_backend
280 .write_setting(&object, &operation.setting_name, &operation.value.0)
281 .await
282 .map_err(|e| OperationError::Internal(e.to_string()))?;
283
284 Ok(value)
285 }
286 .boxed()
287 }
288