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

ambee/giterated

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

Create `NetworkedSubstack`.

# giterated-protocol - Create `NetworkedSubstack` which will handle all networked operations giterated needs - Add support for `NetworkedSubstack` for both the daemon and client - Pipe everything through but leave APIs temp # `giterated-daemon` - Remove a bunch of random old code, dead code, and files that aren't needed. - Moved all connection handling to `client.rs`, simplified connection logic with new types

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨202bb12

⁨giterated-stack/src/lib.rs⁩ - ⁨16769⁩ bytes
Raw
1 mod handler;
2 pub use handler::*;
3 mod meta;
4 pub use meta::*;
5 pub mod provider;
6 mod stack;
7 pub use stack::*;
8 mod substack;
9 use serde::{de::DeserializeOwned, Deserialize, Serialize};
10 pub use stack::*;
11 pub use substack::*;
12 pub mod state;
13 pub mod update;
14
15 // Temp pub use to figure out what's important
16 pub mod models {
17 pub use anyhow::Error;
18 pub use giterated_models::error::{IntoInternalError, OperationError};
19 pub use giterated_models::object::GiteratedObject;
20 pub use giterated_models::operation::GiteratedOperation;
21 }
22
23 use std::{any::Any, convert::Infallible, ops::Deref, sync::Arc};
24
25 use core::fmt::Debug;
26 use giterated_models::{
27 error::{ExtractorError, UnauthorizedError},
28 instance::{
29 AuthenticationTokenRequest, Instance, RegisterAccountRequest, RepositoryCreateRequest,
30 },
31 object::GiteratedObject,
32 object_backend::ObjectBackend,
33 operation::GiteratedOperation,
34 repository::{AccessList, Repository},
35 settings::{GetSetting, SetSetting, Setting},
36 user::User,
37 value::{GetValue, GiteratedObjectValue},
38 };
39
40 #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
41 pub struct ObjectOperationPair<'a> {
42 pub object_name: &'a str,
43 pub operation_name: &'a str,
44 }
45
46 impl ObjectOperationPair<'static> {
47 #[allow(unused)]
48 pub fn from_types<O: GiteratedObject, D: GiteratedOperation<O>>() -> Self {
49 Self {
50 object_name: O::object_name(),
51 operation_name: D::operation_name(),
52 }
53 }
54 }
55
56 #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
57 pub struct ObjectValuePair<'a> {
58 pub object_kind: &'a str,
59 pub value_kind: &'a str,
60 }
61
62 impl ObjectValuePair<'static> {
63 pub fn from_types<O: GiteratedObject, V: GiteratedObjectValue<Object = O>>() -> Self {
64 Self {
65 object_kind: O::object_name(),
66 value_kind: V::value_name(),
67 }
68 }
69 }
70
71 #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
72 pub struct ObjectSettingPair<'a> {
73 pub object_kind: &'a str,
74 pub setting_name: &'a str,
75 }
76
77 impl ObjectSettingPair<'static> {
78 pub fn from_types<O: GiteratedObject, S: Setting>() -> Self {
79 Self {
80 object_kind: O::object_name(),
81 setting_name: S::name(),
82 }
83 }
84 }
85
86 #[async_trait::async_trait(?Send)]
87 pub trait FromOperationState<O: GiteratedObject, D: GiteratedOperation<O>>: Sized + Clone {
88 type Error: Into<anyhow::Error>;
89
90 async fn from_state(
91 object: &O,
92 operation: &D,
93 state: &StackOperationState,
94 ) -> Result<Self, ExtractorError<Self::Error>>;
95 }
96
97 #[async_trait::async_trait(?Send)]
98 impl<O: GiteratedObject, D: GiteratedOperation<O>> FromOperationState<O, D>
99 for StackOperationState
100 {
101 type Error = Infallible;
102
103 async fn from_state(
104 _object: &O,
105 _operation: &D,
106 state: &StackOperationState,
107 ) -> Result<StackOperationState, ExtractorError<Infallible>> {
108 Ok(state.clone())
109 }
110 }
111
112 #[derive(Debug, thiserror::Error, Serialize, Deserialize)]
113 #[error("missing value")]
114 pub struct MissingValue;
115
116 #[async_trait::async_trait(?Send)]
117 impl<O: GiteratedObject, D: GiteratedOperation<O> + Send + Sync> FromOperationState<O, D>
118 for AuthenticatedUser
119 {
120 type Error = MissingValue;
121
122 async fn from_state(
123 _object: &O,
124 _operation: &D,
125 state: &StackOperationState,
126 ) -> Result<AuthenticatedUser, ExtractorError<MissingValue>> {
127 state.user.clone().ok_or(ExtractorError(MissingValue))
128 }
129 }
130
131 #[async_trait::async_trait(?Send)]
132 impl<O: GiteratedObject, D: GiteratedOperation<O> + Send + Sync> FromOperationState<O, D>
133 for AuthenticatedInstance
134 {
135 type Error = MissingValue;
136
137 async fn from_state(
138 _object: &O,
139 _operation: &D,
140 state: &StackOperationState,
141 ) -> Result<AuthenticatedInstance, ExtractorError<MissingValue>> {
142 state.instance.clone().ok_or(ExtractorError(MissingValue))
143 }
144 }
145
146 #[async_trait::async_trait(?Send)]
147 impl<
148 T: FromOperationState<O, D> + Send + Sync,
149 O: GiteratedObject + Sync,
150 D: GiteratedOperation<O> + Send + Sync,
151 > FromOperationState<O, D> for Option<T>
152 {
153 type Error = Infallible;
154
155 async fn from_state(
156 object: &O,
157 operation: &D,
158 state: &StackOperationState,
159 ) -> Result<Option<T>, ExtractorError<Infallible>> {
160 Ok(T::from_state(object, operation, state).await.ok())
161 }
162 }
163
164 #[derive(Clone)]
165 pub struct AuthorizedUser(AuthenticatedUser);
166
167 #[derive(Clone)]
168 pub struct AuthorizedInstance(AuthenticatedInstance);
169
170 #[async_trait::async_trait(?Send)]
171 pub trait AuthorizedOperation<O: GiteratedObject>: GiteratedOperation<O> {
172 type Error: Into<anyhow::Error>;
173
174 async fn authorize(
175 &self,
176 authorize_for: &O,
177 state: &StackOperationState,
178 ) -> Result<bool, ExtractorError<Self::Error>>;
179 }
180
181 #[async_trait::async_trait(?Send)]
182 impl<O: GiteratedObject + Send + Sync + Debug + 'static> AuthorizedOperation<O> for GetValue {
183 type Error = anyhow::Error;
184
185 async fn authorize(
186 &self,
187 authorize_for: &O,
188 operation_state: &StackOperationState,
189 ) -> Result<bool, ExtractorError<anyhow::Error>> {
190 Ok(operation_state
191 .runtime
192 .get_object::<O>(&authorize_for.to_string(), operation_state)
193 .await
194 .is_ok())
195 }
196 }
197
198 #[async_trait::async_trait(?Send)]
199 impl AuthorizedOperation<User> for SetSetting {
200 type Error = MissingValue;
201
202 async fn authorize(
203 &self,
204 authorize_for: &User,
205 operation_state: &StackOperationState,
206 ) -> Result<bool, ExtractorError<MissingValue>> {
207 let authenticated_user = operation_state.user.as_ref().ok_or(MissingValue)?;
208
209 Ok(authorize_for == authenticated_user.deref())
210 }
211 }
212
213 #[async_trait::async_trait(?Send)]
214 impl AuthorizedOperation<User> for GetSetting {
215 type Error = MissingValue;
216
217 async fn authorize(
218 &self,
219 authorize_for: &User,
220 operation_state: &StackOperationState,
221 ) -> Result<bool, ExtractorError<MissingValue>> {
222 let authenticated_user = operation_state.user.as_ref().ok_or(MissingValue)?;
223
224 Ok(authorize_for == authenticated_user.deref())
225 }
226 }
227
228 #[async_trait::async_trait(?Send)]
229 impl AuthorizedOperation<Repository> for SetSetting {
230 type Error = anyhow::Error;
231
232 async fn authorize(
233 &self,
234 authorize_for: &Repository,
235 operation_state: &StackOperationState,
236 ) -> Result<bool, ExtractorError<anyhow::Error>> {
237 let authenticated_user = operation_state
238 .user
239 .as_ref()
240 .ok_or_else(|| anyhow::Error::from(MissingValue))?;
241
242 let mut object = operation_state
243 .runtime
244 .get_object::<Repository>(&authorize_for.to_string(), operation_state)
245 .await
246 .map_err(anyhow::Error::from)?;
247
248 let access_list = object
249 .get_setting::<AccessList>(operation_state)
250 .await
251 .map_err(anyhow::Error::from)?;
252
253 if access_list
254 .0
255 .iter()
256 .any(|user| user == authenticated_user.deref())
257 {
258 Ok(true)
259 } else {
260 Ok(false)
261 }
262 }
263 }
264
265 #[async_trait::async_trait(?Send)]
266 impl AuthorizedOperation<Repository> for GetSetting {
267 type Error = anyhow::Error;
268
269 async fn authorize(
270 &self,
271 authorize_for: &Repository,
272 operation_state: &StackOperationState,
273 ) -> Result<bool, ExtractorError<anyhow::Error>> {
274 let authenticated_user = operation_state
275 .user
276 .as_ref()
277 .ok_or_else(|| anyhow::Error::from(MissingValue))?;
278
279 let mut object = operation_state
280 .runtime
281 .get_object::<Repository>(&authorize_for.to_string(), operation_state)
282 .await
283 .map_err(anyhow::Error::from)?;
284
285 let access_list = object
286 .get_setting::<AccessList>(operation_state)
287 .await
288 .map_err(anyhow::Error::from)?;
289
290 if access_list
291 .0
292 .iter()
293 .any(|user| user == authenticated_user.deref())
294 {
295 Ok(true)
296 } else {
297 Ok(false)
298 }
299 }
300 }
301
302 #[async_trait::async_trait(?Send)]
303 impl AuthorizedOperation<Instance> for RegisterAccountRequest {
304 type Error = Infallible;
305
306 async fn authorize(
307 &self,
308 authorize_for: &Instance,
309 state: &StackOperationState,
310 ) -> Result<bool, ExtractorError<Infallible>> {
311 if state.our_instance == *authorize_for {
312 Ok(true)
313 } else {
314 Ok(false)
315 }
316 }
317 }
318
319 #[async_trait::async_trait(?Send)]
320 impl AuthorizedOperation<Instance> for AuthenticationTokenRequest {
321 type Error = Infallible;
322
323 async fn authorize(
324 &self,
325 authorize_for: &Instance,
326 state: &StackOperationState,
327 ) -> Result<bool, ExtractorError<Infallible>> {
328 if state.our_instance == *authorize_for {
329 Ok(true)
330 } else {
331 Ok(false)
332 }
333 }
334 }
335
336 #[async_trait::async_trait(?Send)]
337 impl AuthorizedOperation<Instance> for RepositoryCreateRequest {
338 type Error = Infallible;
339
340 async fn authorize(
341 &self,
342 authorize_for: &Instance,
343 state: &StackOperationState,
344 ) -> Result<bool, ExtractorError<Infallible>> {
345 if state.our_instance == *authorize_for {
346 Ok(true)
347 } else {
348 Ok(false)
349 }
350 }
351 }
352
353 #[async_trait::async_trait(?Send)]
354 impl<A: AuthorizedOperation<User> + Send + Sync> FromOperationState<User, A> for AuthorizedUser {
355 type Error = UnauthorizedError;
356
357 async fn from_state(
358 object: &User,
359 operation: &A,
360 state: &StackOperationState,
361 ) -> Result<AuthorizedUser, ExtractorError<UnauthorizedError>> {
362 // TODO
363 let authenticated = AuthenticatedUser::from_state(object, operation, state)
364 .await
365 .map_err(|_| ExtractorError(UnauthorizedError))?;
366
367 match operation.authorize(object, state).await {
368 Ok(authorized) => {
369 assert!(authorized);
370 }
371 Err(_err) => return Err(ExtractorError(UnauthorizedError)),
372 };
373
374 Ok(AuthorizedUser(authenticated))
375 }
376 }
377
378 #[async_trait::async_trait(?Send)]
379 impl<A: AuthorizedOperation<Instance> + Send + Sync> FromOperationState<Instance, A>
380 for AuthorizedInstance
381 {
382 type Error = UnauthorizedError;
383
384 async fn from_state(
385 object: &Instance,
386 operation: &A,
387 state: &StackOperationState,
388 ) -> Result<AuthorizedInstance, ExtractorError<UnauthorizedError>> {
389 //TODO
390 let authenticated = AuthenticatedInstance::from_state(object, operation, state)
391 .await
392 .map_err(|_| ExtractorError(UnauthorizedError))?;
393
394 match operation.authorize(object, state).await {
395 Ok(authorized) => {
396 assert!(authorized);
397 }
398 Err(_err) => return Err(ExtractorError(UnauthorizedError)),
399 };
400
401 Ok(AuthorizedInstance(authenticated))
402 }
403 }
404
405 #[derive(Clone)]
406 pub struct StackOperationState {
407 pub our_instance: Instance,
408 pub runtime: Arc<GiteratedStack>,
409 pub instance: Option<AuthenticatedInstance>,
410 pub user: Option<AuthenticatedUser>,
411 }
412
413 #[derive(Clone, Debug)]
414 pub struct AuthenticatedInstance(Instance);
415
416 impl AuthenticatedInstance {
417 pub fn new(instance: Instance) -> Self {
418 AuthenticatedInstance(instance)
419 }
420 }
421
422 impl Deref for AuthenticatedInstance {
423 type Target = Instance;
424
425 fn deref(&self) -> &Self::Target {
426 &self.0
427 }
428 }
429
430 #[derive(Clone, Debug)]
431 pub struct AuthenticatedUser(User);
432
433 impl AuthenticatedUser {
434 pub fn new(user: User) -> Self {
435 AuthenticatedUser(user)
436 }
437 }
438
439 impl Deref for AuthenticatedUser {
440 type Target = User;
441
442 fn deref(&self) -> &Self::Target {
443 &self.0
444 }
445 }
446
447 #[derive(Clone, Debug, Serialize, Deserialize)]
448 #[serde(transparent)]
449 #[serde(bound(deserialize = "S: DeserializeOwned"))]
450 pub struct SettingUpdate<S: Setting + Serialize + DeserializeOwned>(pub S);
451
452 impl<O, S> GiteratedOperation<O> for SettingUpdate<S>
453 where
454 O: GiteratedObject,
455 S: Setting + Serialize + DeserializeOwned,
456 {
457 type Success = ();
458
459 type Failure = ();
460 }
461
462 #[derive(Clone)]
463 pub struct AnyObject {
464 inner: Arc<dyn Any + Send + Sync>,
465 kind: &'static str,
466 }
467
468 impl AnyObject {
469 pub fn new<O: GiteratedObject + 'static>(object: O) -> Self {
470 Self {
471 inner: Arc::new(object) as _,
472 kind: O::object_name(),
473 }
474 }
475
476 pub fn new_raw(_object: Arc<dyn Any + Send + Sync>, _kind: &'static str) -> Self {
477 todo!()
478 }
479
480 pub fn kind(&self) -> &'static str {
481 self.kind
482 }
483 }
484
485 impl Deref for AnyObject {
486 type Target = dyn Any + Send + Sync;
487
488 fn deref(&self) -> &Self::Target {
489 self.inner.as_ref()
490 }
491 }
492
493 #[derive(Clone)]
494 pub struct AnyOperation {
495 inner: Arc<dyn Any + Send + Sync>,
496 kind: ObjectOperationPair<'static>,
497 }
498
499 impl AnyOperation {
500 pub fn new<O: GiteratedObject, D: GiteratedOperation<O> + 'static>(operation: D) -> Self {
501 Self {
502 inner: Arc::new(operation) as _,
503 kind: ObjectOperationPair::from_types::<O, D>(),
504 }
505 }
506
507 pub fn new_raw(
508 _operation: Arc<dyn Any + Send + Sync>,
509 _kind: ObjectOperationPair<'static>,
510 ) -> Self {
511 todo!()
512 }
513
514 pub fn kind(&self) -> ObjectOperationPair<'static> {
515 self.kind
516 }
517 }
518
519 impl Deref for AnyOperation {
520 type Target = dyn Any + Send + Sync;
521
522 fn deref(&self) -> &Self::Target {
523 self.inner.as_ref()
524 }
525 }
526
527 #[derive(Clone)]
528 pub struct AnyValue {
529 inner: Arc<dyn Any + Send + Sync>,
530 kind: ObjectValuePair<'static>,
531 }
532
533 impl AnyValue {
534 pub fn new<O: GiteratedObject, V: GiteratedObjectValue<Object = O> + 'static>(
535 value: V,
536 ) -> Self {
537 Self {
538 inner: Arc::new(value) as _,
539 kind: ObjectValuePair::from_types::<O, V>(),
540 }
541 }
542
543 pub fn new_raw(_value: Arc<dyn Any + Send + Sync>, _kind: ObjectValuePair<'static>) -> Self {
544 todo!()
545 }
546
547 pub fn kind(&self) -> ObjectValuePair<'static> {
548 self.kind
549 }
550 }
551
552 impl Deref for AnyValue {
553 type Target = dyn Any + Send + Sync;
554
555 fn deref(&self) -> &Self::Target {
556 self.inner.as_ref()
557 }
558 }
559
560 #[derive(Clone)]
561 pub struct AnySetting {
562 inner: Arc<dyn Any + Send + Sync>,
563 kind: ObjectSettingPair<'static>,
564 }
565
566 impl AnySetting {
567 pub fn new<O: GiteratedObject, S: Setting + 'static>(setting: S) -> Self {
568 Self {
569 inner: Arc::new(setting) as _,
570 kind: ObjectSettingPair::from_types::<O, S>(),
571 }
572 }
573
574 pub fn new_raw(
575 _setting: Arc<dyn Any + Send + Sync>,
576 _kind: ObjectSettingPair<'static>,
577 ) -> Self {
578 todo!()
579 }
580
581 pub fn kind(&self) -> ObjectSettingPair<'static> {
582 self.kind
583 }
584 }
585
586 impl Deref for AnySetting {
587 type Target = dyn Any + Send + Sync;
588
589 fn deref(&self) -> &Self::Target {
590 self.inner.as_ref()
591 }
592 }
593
594 #[derive(Clone)]
595 pub struct AnySuccess(pub Arc<dyn Any + Send>);
596
597 #[derive(Clone)]
598 pub struct AnyFailure(pub Arc<dyn Any + Send>);
599
600 /// Should be renamed.
601 ///
602 /// Allows accepting object types that are either GiteratedObject types or
603 /// AnyObject.
604 pub trait MaybeDynamicObject: Clone {
605 fn from_any(object: &AnyObject) -> Self;
606
607 fn object_name() -> &'static str;
608 }
609
610 impl<O: GiteratedObject> MaybeDynamicObject for O {
611 fn from_any(_object: &AnyObject) -> Self {
612 todo!()
613 }
614
615 fn object_name() -> &'static str {
616 <O as GiteratedObject>::object_name()
617 }
618 }
619
620 impl MaybeDynamicObject for AnyObject {
621 fn from_any(object: &AnyObject) -> Self {
622 object.clone()
623 }
624
625 fn object_name() -> &'static str {
626 "any"
627 }
628 }
629
630 pub trait MaybeDynamicValue {
631 fn from_any(value: &AnyValue) -> Self;
632 fn into_any(self) -> AnyValue;
633 fn meta() -> Option<ValueMeta>;
634
635 fn value_name() -> &'static str;
636 }
637
638 impl<V: GiteratedObjectValue> MaybeDynamicValue for V {
639 fn from_any(_object: &AnyValue) -> Self {
640 todo!()
641 }
642
643 fn value_name() -> &'static str {
644 todo!()
645 }
646
647 fn into_any(self) -> AnyValue {
648 todo!()
649 }
650
651 fn meta() -> Option<ValueMeta> {
652 todo!()
653 }
654 }
655
656 impl MaybeDynamicValue for AnyValue {
657 fn value_name() -> &'static str {
658 "any"
659 }
660
661 fn from_any(value: &AnyValue) -> Self {
662 value.clone()
663 }
664
665 fn into_any(self) -> AnyValue {
666 self
667 }
668
669 fn meta() -> Option<ValueMeta> {
670 todo!()
671 }
672 }
673