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

ambee/giterated

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

Fixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨73a5af5

Showing ⁨⁨8⁩ changed files⁩ with ⁨⁨85⁩ insertions⁩ and ⁨⁨18⁩ deletions⁩

Cargo.lock

View file
@@ -699,6 +699,7 @@ version = "0.1.0"
699 699 dependencies = [
700 700 "anyhow",
701 701 "async-trait",
702 "bincode",
702 703 "chrono",
703 704 "deadpool",
704 705 "futures-util",

giterated-daemon/src/connection/wrapper.rs

View file
@@ -23,7 +23,7 @@ use toml::Table;
23 23 use crate::{
24 24 authentication::AuthenticationTokenGranter,
25 25 backend::{MetadataBackend, RepositoryBackend, UserBackend},
26 database_backend::Foobackend,
26 database_backend::DatabaseBackend,
27 27 federation::connections::InstanceConnections,
28 28 keys::PublicKeyCache,
29 29 };
@@ -41,6 +41,7 @@ pub async fn connection_wrapper(
41 41 instance: impl ToOwned<Owned = Instance>,
42 42 instance_connections: Arc<Mutex<InstanceConnections>>,
43 43 config: Table,
44 backend: DatabaseBackend,
44 45 ) {
45 46 let connection_state = ConnectionState {
46 47 socket: Arc::new(Mutex::new(socket)),
@@ -82,8 +83,6 @@ pub async fn connection_wrapper(
82 83
83 84 let message: GiteratedMessage<AnyObject, AnyOperation> = message.into_message();
84 85
85 let backend = Foobackend {};
86
87 86 backend
88 87 .object_operation(message.object, message.payload)
89 88 .await

giterated-daemon/src/database_backend/mod.rs

View file
@@ -1,12 +1,15 @@
1 1 pub mod handler;
2 2
3 use std::any::type_name;
3 4 use std::{str::FromStr, sync::Arc};
4 5
5 6 use giterated_models::error::OperationError;
6 7 use giterated_models::instance::Instance;
7 use giterated_models::object::{GiteratedObject, Object, ObjectRequestError};
8 use giterated_models::object::{
9 GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse,
10 };
8 11 use giterated_models::object_backend::ObjectBackend;
9 use giterated_models::operation::GiteratedOperation;
12 use giterated_models::operation::{AnyOperation, GiteratedOperation};
10 13 use giterated_models::repository::Repository;
11 14 use giterated_models::user::User;
12 15 use std::fmt::Debug;
@@ -49,6 +52,20 @@ pub struct DatabaseBackend {
49 52 pub(self) repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>>,
50 53 }
51 54
55 impl DatabaseBackend {
56 pub fn new(
57 instance: Instance,
58 user_backend: Arc<Mutex<dyn UserBackend + Send>>,
59 repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>>,
60 ) -> Self {
61 Self {
62 our_instance: instance,
63 user_backend,
64 repository_backend,
65 }
66 }
67 }
68
52 69 impl Debug for DatabaseBackend {
53 70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 71 f.debug_struct("DatabaseBackend").finish()
@@ -121,8 +138,29 @@ impl ObjectBackend for DatabaseBackend {
121 138 )),
122 139 },
123 140 }
124 } else if serde_json::from_str::<Instance>(&object).is_ok() {
125 Err(OperationError::Unhandled)
141 } else if let Ok(instance) = Instance::from_str(&object) {
142 if instance != self.our_instance {
143 // We don't handle other objects currently
144 return Err(OperationError::Unhandled);
145 }
146
147 if let Ok(object_request) = serde_json::from_value::<ObjectRequest>(serialized.clone())
148 {
149 // No-op
150 let response = serde_json::to_string(&ObjectResponse(object_request.0)).unwrap();
151
152 info!("Target: {}", type_name::<D>());
153 info!("Meow: {}", response);
154 info!(
155 "Im just a neko! {:?}",
156 serde_json::from_str::<AnyOperation>(&response)
157 );
158
159 Ok(serde_json::from_str(&response)
160 .map_err(|e| OperationError::Internal(e.to_string()))?)
161 } else {
162 Err(OperationError::Unhandled)
163 }
126 164 } else {
127 165 Err(OperationError::Unhandled)
128 166 }
@@ -168,7 +206,12 @@ impl ObjectBackend for DatabaseBackend {
168 206 } else {
169 207 return Err(OperationError::Unhandled);
170 208 }
171 } else if Instance::from_str(object_str).is_ok() {
209 } else if let Ok(instance) = Instance::from_str(object_str) {
210 if instance != self.our_instance {
211 // We don't handle other objects currently
212 return Err(OperationError::Unhandled);
213 }
214
172 215 return Err(OperationError::Unhandled);
173 216 } else {
174 217 // Invalid object type

giterated-daemon/src/main.rs

View file
@@ -6,6 +6,7 @@ use giterated_daemon::{
6 6 git::GitBackend, settings::DatabaseSettings, user::UserAuth, RepositoryBackend, UserBackend,
7 7 },
8 8 connection::{self, wrapper::connection_wrapper},
9 database_backend::DatabaseBackend,
9 10 federation::connections::InstanceConnections,
10 11 };
11 12
@@ -81,6 +82,12 @@ async fn main() -> Result<(), Error> {
81 82
82 83 info!("Connected");
83 84
85 let database_backend = DatabaseBackend::new(
86 Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(),
87 user_backend.clone(),
88 repository_backend.clone(),
89 );
90
84 91 loop {
85 92 let stream = accept_stream(&mut listener).await;
86 93 info!("Connected");
@@ -122,6 +129,7 @@ async fn main() -> Result<(), Error> {
122 129 Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(),
123 130 instance_connections.clone(),
124 131 config.clone(),
132 database_backend.clone(),
125 133 )),
126 134 };
127 135

giterated-models/src/authenticated.rs

View file
@@ -42,10 +42,13 @@ pub struct AuthenticatedPayload {
42 42
43 43 impl AuthenticatedPayload {
44 44 pub fn into_message(self) -> GiteratedMessage<AnyObject, AnyOperation> {
45 info!("Into message: {:#?}", self);
46 let payload = serde_json::from_slice::<Value>(&self.payload).unwrap();
47 info!("Message payload: {}", payload);
45 48 GiteratedMessage {
46 49 object: AnyObject(self.object),
47 50 operation: self.operation,
48 payload: AnyOperation(serde_json::from_slice::<Value>(&self.payload).unwrap()),
51 payload: AnyOperation(payload),
49 52 }
50 53 }
51 54 }
@@ -103,7 +106,7 @@ impl<O: GiteratedObject, D: GiteratedOperation<O>> Authenticated<O, D> {
103 106 }
104 107
105 108 pub fn into_payload(mut self) -> AuthenticatedPayload {
106 let payload = bincode::serialize(&self.message.payload).unwrap();
109 let payload = serde_json::to_vec(&self.message.payload).unwrap();
107 110
108 111 AuthenticatedPayload {
109 112 object: self.message.object.to_string(),

giterated-models/src/object/operations.rs

View file
@@ -10,7 +10,7 @@ use super::GiteratedObject;
10 10 pub struct ObjectRequest(pub String);
11 11
12 12 #[derive(Serialize, Deserialize)]
13 pub struct ObjectResponse(pub Vec<u8>);
13 pub struct ObjectResponse(pub String);
14 14
15 15 impl GiteratedOperation<Instance> for ObjectRequest {
16 16 type Success = ObjectResponse;

giterated-models/src/repository/mod.rs

View file
@@ -78,11 +78,19 @@ impl FromStr for Repository {
78 78
79 79 fn from_str(s: &str) -> Result<Self, Self::Err> {
80 80 let mut by_ampersand = s.split('@');
81 let mut path_split = by_ampersand.next().unwrap().split('/');
81 let mut path_split = by_ampersand
82 .next()
83 .ok_or_else(|| RepositoryParseError)?
84 .split('/');
82 85
83 let instance = Instance::from_str(by_ampersand.next().unwrap()).unwrap();
84 let owner = User::from_str(path_split.next().unwrap()).unwrap();
85 let name = path_split.next().unwrap().to_string();
86 let instance = Instance::from_str(by_ampersand.next().ok_or_else(|| RepositoryParseError)?)
87 .map_err(|_| RepositoryParseError)?;
88 let owner = User::from_str(path_split.next().ok_or_else(|| RepositoryParseError)?)
89 .map_err(|_| RepositoryParseError)?;
90 let name = path_split
91 .next()
92 .ok_or_else(|| RepositoryParseError)?
93 .to_string();
86 94
87 95 Ok(Self {
88 96 instance,
@@ -93,7 +101,8 @@ impl FromStr for Repository {
93 101 }
94 102
95 103 #[derive(Debug, thiserror::Error)]
96 pub enum RepositoryParseError {}
104 #[error("no parse!")]
105 pub struct RepositoryParseError;
97 106
98 107 /// Visibility of the repository to the general eye
99 108 #[derive(PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Clone, sqlx::Type)]

giterated-models/src/user/mod.rs

View file
@@ -72,8 +72,12 @@ impl FromStr for User {
72 72 }
73 73
74 74 let mut colon_split = s.split(':');
75 let username = colon_split.next().unwrap().to_string();
76 let instance = Instance::from_str(colon_split.next().unwrap()).unwrap();
75 let username = colon_split
76 .next()
77 .ok_or_else(|| UserParseError)?
78 .to_string();
79 let instance = Instance::from_str(colon_split.next().ok_or_else(|| UserParseError)?)
80 .map_err(|_| UserParseError)?;
77 81
78 82 Ok(Self { username, instance })
79 83 }