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

ambee/giterated

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

add discovery stub

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨cb91a68

Showing ⁨⁨7⁩ changed files⁩ with ⁨⁨104⁩ insertions⁩ and ⁨⁨2⁩ deletions⁩

src/backend/discovery.rs

View file
@@ -0,0 +1,18 @@
1 use anyhow::Error;
2
3 use crate::{messages::discovery::DiscoveryMessage, model::repository::Repository};
4
5 use super::DiscoveryBackend;
6
7 pub struct GiteratedDiscoveryProtocol {}
8
9 #[async_trait::async_trait]
10 impl DiscoveryBackend for GiteratedDiscoveryProtocol {
11 async fn try_handle(&mut self, request: DiscoveryMessage) -> Result<bool, Error> {
12 todo!()
13 }
14
15 async fn search(&mut self, search: &str) -> Result<Vec<Repository>, Error> {
16 todo!()
17 }
18 }

src/backend/mod.rs

View file
@@ -1,3 +1,4 @@
1 pub mod discovery;
1 2 pub mod git;
2 3 pub mod github;
3 4 pub mod user;
@@ -11,6 +12,7 @@ use crate::{
11 12 AuthenticationTokenRequest, AuthenticationTokenResponse, RegisterAccountRequest,
12 13 RegisterAccountResponse,
13 14 },
15 discovery::DiscoveryMessage,
14 16 repository::{
15 17 CreateRepositoryRequest, CreateRepositoryResponse, RepositoryFileInspectRequest,
16 18 RepositoryFileInspectionResponse, RepositoryInfoRequest, RepositoryIssueLabelsRequest,
@@ -23,7 +25,7 @@ use crate::{
23 25 },
24 26 ValidatedUserAuthenticated,
25 27 },
26 model::repository::RepositoryView,
28 model::repository::{Repository, RepositoryView},
27 29 };
28 30
29 31 #[async_trait]
@@ -84,3 +86,9 @@ pub trait UserBackend: AuthBackend {
84 86
85 87 async fn bio(&mut self, request: UserBioRequest) -> Result<UserBioResponse, Error>;
86 88 }
89
90 #[async_trait::async_trait]
91 pub trait DiscoveryBackend {
92 async fn try_handle(&mut self, request: DiscoveryMessage) -> Result<bool, Error>;
93 async fn search(&mut self, search: &str) -> Result<Vec<Repository>, Error>;
94 }

src/messages/discovery.rs

View file
@@ -0,0 +1,35 @@
1 use chrono::{DateTime, Utc};
2 use serde::{Deserialize, Serialize};
3
4 use crate::model::discovery::DiscoveryItem;
5
6 use super::InstanceAuthenticated;
7
8 #[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)]
9 pub struct DiscoveryMessage {
10 pub message: InstanceAuthenticated<DiscoveryMessageKind>,
11 }
12
13 #[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)]
14 pub enum DiscoveryMessageKind {
15 Offer(DiscoveryOffer),
16 Request(DiscoveryRequest),
17 Discoveries(Discoveries),
18 }
19
20 #[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)]
21 pub struct DiscoveryOffer {
22 pub earliest: DateTime<Utc>,
23 pub hashes: Vec<u128>,
24 }
25
26 #[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)]
27 pub struct DiscoveryRequest {
28 pub since: DateTime<Utc>,
29 pub hashes: Vec<u128>,
30 }
31
32 #[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)]
33 pub struct Discoveries {
34 pub discoveries: Vec<DiscoveryItem>,
35 }

src/messages/mod.rs

View file
@@ -19,6 +19,7 @@ use crate::{
19 19 use self::{authentication::AuthenticationMessage, repository::RepositoryMessage};
20 20
21 21 pub mod authentication;
22 pub mod discovery;
22 23 pub mod issues;
23 24 pub mod repository;
24 25 pub mod user;
@@ -39,6 +40,30 @@ pub struct InstanceAuthenticated<T: Serialize> {
39 40 signature: Vec<u8>,
40 41 }
41 42
43 impl<T> PartialEq for InstanceAuthenticated<T>
44 where
45 T: PartialEq + Serialize,
46 {
47 fn eq(&self, other: &Self) -> bool {
48 self.message == other.message
49 && self.instance == other.instance
50 && self.signature == other.signature
51 }
52 }
53
54 impl<T> Eq for InstanceAuthenticated<T> where T: Eq + Serialize {}
55
56 impl<T> std::hash::Hash for InstanceAuthenticated<T>
57 where
58 T: std::hash::Hash + Serialize,
59 {
60 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
61 self.message.hash(state);
62 self.instance.hash(state);
63 self.signature.hash(state);
64 }
65 }
66
42 67 impl<T> Clone for InstanceAuthenticated<T>
43 68 where
44 69 T: Clone + Serialize,

src/model/discovery.rs

View file
@@ -0,0 +1,15 @@
1 use serde::{Deserialize, Serialize};
2
3 use crate::model::{instance::Instance, repository::Repository};
4
5 #[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)]
6 pub enum DiscoveryItem {
7 Instance {
8 instance: Instance,
9 signature: Vec<u8>,
10 },
11 Repository {
12 repository: Repository,
13 signature: Vec<u8>,
14 },
15 }

src/model/mod.rs

View file
@@ -3,6 +3,7 @@
3 3 //! All network data model types that are not directly associated with
4 4 //! individual requests or responses.
5 5
6 pub mod discovery;
6 7 pub mod instance;
7 8 pub mod repository;
8 9 pub mod user;

src/model/repository.rs

View file
@@ -27,7 +27,7 @@ use super::{instance::Instance, user::User};
27 27 /// // This is correct
28 28 /// assert_eq!(Repository::from_str("barson:giterated.dev/[email protected]").unwrap(), repository);
29 29 /// ```
30 #[derive(Hash, Clone, Serialize, Deserialize)]
30 #[derive(Hash, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
31 31 pub struct Repository {
32 32 pub owner: User,
33 33 pub name: String,