diff --git a/src/backend/discovery.rs b/src/backend/discovery.rs new file mode 100644 index 0000000..4a395f8 --- /dev/null +++ b/src/backend/discovery.rs @@ -0,0 +1,18 @@ +use anyhow::Error; + +use crate::{messages::discovery::DiscoveryMessage, model::repository::Repository}; + +use super::DiscoveryBackend; + +pub struct GiteratedDiscoveryProtocol {} + +#[async_trait::async_trait] +impl DiscoveryBackend for GiteratedDiscoveryProtocol { + async fn try_handle(&mut self, request: DiscoveryMessage) -> Result { + todo!() + } + + async fn search(&mut self, search: &str) -> Result, Error> { + todo!() + } +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 0e13756..0d71899 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,3 +1,4 @@ +pub mod discovery; pub mod git; pub mod github; pub mod user; @@ -11,6 +12,7 @@ use crate::{ AuthenticationTokenRequest, AuthenticationTokenResponse, RegisterAccountRequest, RegisterAccountResponse, }, + discovery::DiscoveryMessage, repository::{ CreateRepositoryRequest, CreateRepositoryResponse, RepositoryFileInspectRequest, RepositoryFileInspectionResponse, RepositoryInfoRequest, RepositoryIssueLabelsRequest, @@ -23,7 +25,7 @@ use crate::{ }, ValidatedUserAuthenticated, }, - model::repository::RepositoryView, + model::repository::{Repository, RepositoryView}, }; #[async_trait] @@ -84,3 +86,9 @@ pub trait UserBackend: AuthBackend { async fn bio(&mut self, request: UserBioRequest) -> Result; } + +#[async_trait::async_trait] +pub trait DiscoveryBackend { + async fn try_handle(&mut self, request: DiscoveryMessage) -> Result; + async fn search(&mut self, search: &str) -> Result, Error>; +} diff --git a/src/messages/discovery.rs b/src/messages/discovery.rs new file mode 100644 index 0000000..a94df28 --- /dev/null +++ b/src/messages/discovery.rs @@ -0,0 +1,35 @@ +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; + +use crate::model::discovery::DiscoveryItem; + +use super::InstanceAuthenticated; + +#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub struct DiscoveryMessage { + pub message: InstanceAuthenticated, +} + +#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub enum DiscoveryMessageKind { + Offer(DiscoveryOffer), + Request(DiscoveryRequest), + Discoveries(Discoveries), +} + +#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub struct DiscoveryOffer { + pub earliest: DateTime, + pub hashes: Vec, +} + +#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub struct DiscoveryRequest { + pub since: DateTime, + pub hashes: Vec, +} + +#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub struct Discoveries { + pub discoveries: Vec, +} diff --git a/src/messages/mod.rs b/src/messages/mod.rs index a4032cc..673a2d9 100644 --- a/src/messages/mod.rs +++ b/src/messages/mod.rs @@ -19,6 +19,7 @@ use crate::{ use self::{authentication::AuthenticationMessage, repository::RepositoryMessage}; pub mod authentication; +pub mod discovery; pub mod issues; pub mod repository; pub mod user; @@ -39,6 +40,30 @@ pub struct InstanceAuthenticated { signature: Vec, } +impl PartialEq for InstanceAuthenticated +where + T: PartialEq + Serialize, +{ + fn eq(&self, other: &Self) -> bool { + self.message == other.message + && self.instance == other.instance + && self.signature == other.signature + } +} + +impl Eq for InstanceAuthenticated where T: Eq + Serialize {} + +impl std::hash::Hash for InstanceAuthenticated +where + T: std::hash::Hash + Serialize, +{ + fn hash(&self, state: &mut H) { + self.message.hash(state); + self.instance.hash(state); + self.signature.hash(state); + } +} + impl Clone for InstanceAuthenticated where T: Clone + Serialize, diff --git a/src/model/discovery.rs b/src/model/discovery.rs new file mode 100644 index 0000000..b29e034 --- /dev/null +++ b/src/model/discovery.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +use crate::model::{instance::Instance, repository::Repository}; + +#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub enum DiscoveryItem { + Instance { + instance: Instance, + signature: Vec, + }, + Repository { + repository: Repository, + signature: Vec, + }, +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 9c3fa48..a4d3269 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -3,6 +3,7 @@ //! All network data model types that are not directly associated with //! individual requests or responses. +pub mod discovery; pub mod instance; pub mod repository; pub mod user; diff --git a/src/model/repository.rs b/src/model/repository.rs index 4b9e66e..a68dea3 100644 --- a/src/model/repository.rs +++ b/src/model/repository.rs @@ -27,7 +27,7 @@ use super::{instance::Instance, user::User}; /// // This is correct /// assert_eq!(Repository::from_str("barson:giterated.dev/foo@giterated.dev").unwrap(), repository); /// ``` -#[derive(Hash, Clone, Serialize, Deserialize)] +#[derive(Hash, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Repository { pub owner: User, pub name: String,