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

ambee/giterated

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

Add repository setings

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨3b20354

⁨giterated-daemon/src/database_backend/handler.rs⁩ - ⁨11734⁩ bytes
Raw
1 use std::error::Error;
2
3 use futures_util::{future::BoxFuture, FutureExt};
4 use giterated_models::{
5 authenticated::UserAuthenticationToken,
6 error::{GetValueError, InstanceError, OperationError, RepositoryError, UserError},
7 instance::{
8 AuthenticationTokenRequest, Instance, RegisterAccountRequest, RepositoryCreateRequest,
9 },
10 object_backend::ObjectBackend,
11 repository::{
12 Commit, DefaultBranch, Description, LatestCommit, Repository,
13 RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffPatchRequest,
14 RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest,
15 RepositoryFileInspectRequest, RepositoryInfoRequest, RepositorySummary, RepositoryView,
16 Visibility,
17 },
18 settings::{AnySetting, GetSetting, GetSettingError, SetSetting, SetSettingError},
19 user::{User, UserRepositoriesRequest},
20 value::{AnyValue, GetValue},
21 };
22 use giterated_stack::{BackendWrapper, StackOperationState};
23
24 use super::DatabaseBackend;
25
26 pub fn user_get_repositories(
27 object: &User,
28 _operation: UserRepositoriesRequest,
29 state: DatabaseBackend,
30 ) -> BoxFuture<'static, Result<Vec<RepositorySummary>, OperationError<UserError>>> {
31 let object = object.clone();
32
33 async move {
34 let mut user_backend = state.user_backend.lock().await;
35 let repositories = user_backend
36 .repositories_for_user(None, &object)
37 .await
38 .map_err(|e| OperationError::Internal(e.to_string()))?;
39
40 Ok(repositories)
41 }
42 .boxed()
43 }
44
45 pub fn user_get_value(
46 object: &User,
47 operation: GetValue<AnyValue<User>>,
48 state: DatabaseBackend,
49 ) -> BoxFuture<'static, Result<AnyValue<User>, OperationError<GetValueError>>> {
50 let object = object.clone();
51
52 async move {
53 let mut user_backend = state.user_backend.lock().await;
54 let value = user_backend
55 .get_value(&object, &operation.value_name)
56 .await
57 .map_err(|e| OperationError::Internal(e.to_string()))?;
58
59 Ok(value)
60 }
61 .boxed()
62 }
63
64 pub fn user_get_setting(
65 object: &User,
66 operation: GetSetting<AnySetting>,
67 state: DatabaseBackend,
68 ) -> BoxFuture<'static, Result<AnySetting, OperationError<GetSettingError>>> {
69 let object = object.clone();
70
71 async move {
72 let mut user_backend = state.user_backend.lock().await;
73 let value = user_backend
74 .get_setting(&object, &operation.setting_name)
75 .await
76 .map_err(|e| OperationError::Internal(e.to_string()))?;
77
78 Ok(value)
79 }
80 .boxed()
81 }
82
83 pub fn user_set_setting(
84 object: &User,
85 operation: SetSetting<AnySetting>,
86 state: DatabaseBackend,
87 ) -> BoxFuture<'static, Result<(), OperationError<SetSettingError>>> {
88 let object = object.clone();
89
90 async move {
91 let mut user_backend = state.user_backend.lock().await;
92 let value = user_backend
93 .write_setting(&object, &operation.setting_name, &operation.value.0)
94 .await
95 .map_err(|e| OperationError::Internal(e.to_string()))?;
96
97 Ok(value)
98 }
99 .boxed()
100 }
101
102 pub fn repository_info(
103 object: &Repository,
104 operation: RepositoryInfoRequest,
105 state: DatabaseBackend,
106 operation_state: StackOperationState,
107 backend: BackendWrapper,
108 ) -> BoxFuture<'static, Result<RepositoryView, OperationError<RepositoryError>>> {
109 let object = object.clone();
110
111 async move {
112 let mut object = backend
113 .get_object::<Repository>(&object.to_string(), &operation_state)
114 .await
115 .unwrap();
116
117 let mut repository_backend = state.repository_backend.lock().await;
118 let tree = repository_backend
119 .repository_file_inspect(
120 None,
121 object.object(),
122 &RepositoryFileInspectRequest {
123 extra_metadata: operation.extra_metadata,
124 path: operation.path,
125 rev: operation.rev.clone(),
126 },
127 )
128 .await
129 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
130 drop(repository_backend);
131
132 let info = RepositoryView {
133 name: object.object().name.clone(),
134 owner: object.object().owner.clone(),
135 description: object.get::<Description>(&operation_state).await.ok(),
136 visibility: object
137 .get::<Visibility>(&operation_state)
138 .await
139 .map_err(|e| OperationError::Internal(format!("{:?}: {}", e.source(), e)))?,
140 default_branch: object
141 .get::<DefaultBranch>(&operation_state)
142 .await
143 .map_err(|e| OperationError::Internal(format!("{:?}: {}", e.source(), e)))?,
144 // 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.
145 latest_commit: object.get::<LatestCommit>(&operation_state).await.ok(),
146 tree_rev: operation.rev,
147 tree,
148 };
149
150 Ok(info)
151 }
152 .boxed()
153 }
154
155 pub fn repository_file_from_id(
156 object: &Repository,
157 operation: RepositoryFileFromIdRequest,
158 state: DatabaseBackend,
159 operation_state: StackOperationState,
160 backend: BackendWrapper,
161 ) -> BoxFuture<'static, Result<RepositoryFile, OperationError<RepositoryError>>> {
162 let object = object.clone();
163
164 async move {
165 let object = backend
166 .get_object::<Repository>(&object.to_string(), &operation_state)
167 .await
168 .unwrap();
169
170 let mut repository_backend = state.repository_backend.lock().await;
171 let file = repository_backend
172 .repository_file_from_id(
173 None,
174 object.object(),
175 &RepositoryFileFromIdRequest(operation.0),
176 )
177 .await
178 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
179 drop(repository_backend);
180
181 Ok(file)
182 }
183 .boxed()
184 }
185
186 pub fn repository_diff(
187 object: &Repository,
188 operation: RepositoryDiffRequest,
189 state: DatabaseBackend,
190 operation_state: StackOperationState,
191 backend: BackendWrapper,
192 ) -> BoxFuture<'static, Result<RepositoryDiff, OperationError<RepositoryError>>> {
193 let object = object.clone();
194
195 async move {
196 let object = backend
197 .get_object::<Repository>(&object.to_string(), &operation_state)
198 .await
199 .unwrap();
200
201 let mut repository_backend = state.repository_backend.lock().await;
202 let diff = repository_backend
203 .repository_diff(None, object.object(), &operation)
204 .await
205 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
206 drop(repository_backend);
207
208 Ok(diff)
209 }
210 .boxed()
211 }
212
213 pub fn repository_diff_patch(
214 object: &Repository,
215 operation: RepositoryDiffPatchRequest,
216 state: DatabaseBackend,
217 operation_state: StackOperationState,
218 backend: BackendWrapper,
219 ) -> BoxFuture<'static, Result<String, OperationError<RepositoryError>>> {
220 let object = object.clone();
221
222 async move {
223 let object = backend
224 .get_object::<Repository>(&object.to_string(), &operation_state)
225 .await
226 .unwrap();
227
228 let mut repository_backend = state.repository_backend.lock().await;
229 let patch = repository_backend
230 .repository_diff_patch(None, object.object(), &operation)
231 .await
232 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
233 drop(repository_backend);
234
235 Ok(patch)
236 }
237 .boxed()
238 }
239
240 pub fn repository_commit_before(
241 object: &Repository,
242 operation: RepositoryCommitBeforeRequest,
243 state: DatabaseBackend,
244 operation_state: StackOperationState,
245 backend: BackendWrapper,
246 ) -> BoxFuture<'static, Result<Commit, OperationError<RepositoryError>>> {
247 let object = object.clone();
248
249 async move {
250 let object = backend
251 .get_object::<Repository>(&object.to_string(), &operation_state)
252 .await
253 .unwrap();
254
255 let mut repository_backend = state.repository_backend.lock().await;
256 let file = repository_backend
257 .repository_commit_before(None, object.object(), &operation)
258 .await
259 .map_err(|err| OperationError::Internal(format!("{:?}", err)))?;
260 drop(repository_backend);
261
262 Ok(file)
263 }
264 .boxed()
265 }
266
267 pub fn repository_get_value(
268 object: &Repository,
269 operation: GetValue<AnyValue<Repository>>,
270 state: DatabaseBackend,
271 ) -> BoxFuture<'static, Result<AnyValue<Repository>, OperationError<GetValueError>>> {
272 let object = object.clone();
273
274 async move {
275 let mut repository_backend = state.repository_backend.lock().await;
276 let value = repository_backend
277 .get_value(&object, &operation.value_name)
278 .await
279 .map_err(|e| {
280 OperationError::Internal(format!("error getting value: {}", e.to_string()))
281 })?;
282
283 Ok(value)
284 }
285 .boxed()
286 }
287
288 pub fn repository_get_setting(
289 object: &Repository,
290 operation: GetSetting<AnySetting>,
291 state: DatabaseBackend,
292 ) -> BoxFuture<'static, Result<AnySetting, OperationError<GetSettingError>>> {
293 let object = object.clone();
294
295 async move {
296 let mut repository_backend = state.repository_backend.lock().await;
297 let value = repository_backend
298 .get_setting(&object, &operation.setting_name)
299 .await
300 .map_err(|e| OperationError::Internal(e.to_string()))?;
301
302 Ok(value)
303 }
304 .boxed()
305 }
306
307 pub fn repository_set_setting(
308 object: &Repository,
309 operation: SetSetting<AnySetting>,
310 state: DatabaseBackend,
311 ) -> BoxFuture<'static, Result<(), OperationError<SetSettingError>>> {
312 let object = object.clone();
313
314 async move {
315 let mut repository_backend = state.repository_backend.lock().await;
316 let value = repository_backend
317 .write_setting(&object, &operation.setting_name, &operation.value.0)
318 .await
319 .map_err(|e| OperationError::Internal(e.to_string()))?;
320
321 Ok(value)
322 }
323 .boxed()
324 }
325
326 pub fn instance_authentication_request(
327 object: &Instance,
328 operation: AuthenticationTokenRequest,
329 state: DatabaseBackend,
330 ) -> BoxFuture<'static, Result<UserAuthenticationToken, OperationError<InstanceError>>> {
331 let object = object.clone();
332 async move {
333 let mut backend = state.user_backend.lock().await;
334
335 backend
336 .login(&object, operation)
337 .await
338 .map_err(|e| OperationError::Internal(e.to_string()))
339 }
340 .boxed()
341 }
342
343 pub fn instance_registration_request(
344 _object: &Instance,
345 operation: RegisterAccountRequest,
346 state: DatabaseBackend,
347 ) -> BoxFuture<'static, Result<UserAuthenticationToken, OperationError<InstanceError>>> {
348 async move {
349 let mut backend = state.user_backend.lock().await;
350
351 backend
352 .register(operation)
353 .await
354 .map_err(|e| OperationError::Internal(e.to_string()))
355 }
356 .boxed()
357 }
358
359 pub fn instance_create_repository_request(
360 _object: &Instance,
361 operation: RepositoryCreateRequest,
362 state: DatabaseBackend,
363 ) -> BoxFuture<'static, Result<Repository, OperationError<InstanceError>>> {
364 async move {
365 let mut backend = state.repository_backend.lock().await;
366
367 backend
368 .create_repository(&operation.owner, &operation)
369 .await
370 .map_err(|e| OperationError::Internal(e.to_string()))
371 }
372 .boxed()
373 }
374