use std::collections::HashMap; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use crate::model::instance::Instance; use crate::model::repository::RepositoryVisibility; use crate::model::settings::Setting; use crate::model::{ repository::{Commit, Repository, RepositoryTreeEntry}, user::User, }; use super::MessageTarget; /// A request to create a repository. /// /// # Authentication /// - Instance Authentication /// - Used to validate User token `issued_for` /// - User Authentication /// - Used to source owning user /// - Used to authorize user token against user's instance /// # Authorization /// - Instance Authorization /// - Used to authorize action using User token requiring a correct `issued_for` and valid issuance from user's instance /// - User Authorization /// - Potential User permissions checks #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryCreateRequest { pub instance: Option, pub name: String, pub description: Option, pub visibility: RepositoryVisibility, pub default_branch: String, pub owner: User, } impl MessageTarget for RepositoryCreateRequest { fn target(&self) -> Option { self.instance.clone() } } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryCreateResponse; /// A request to inspect the tree of a repository. /// /// # Authentication /// - Instance Authentication /// - Validate request against the `issued_for` public key /// - Validate User token against the user's instance's public key /// # Authorization /// - User Authorization /// - Potential User permissions checks #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryFileInspectRequest { pub path: RepositoryTreeEntry, } impl MessageTarget for RepositoryFileInspectRequest { fn target(&self) -> Option { None } } #[derive(Clone, Debug, Serialize, Deserialize)] pub enum RepositoryFileInspectionResponse { File { commit_metadata: Commit, }, Folder { commit_metadata: Commit, members: Vec, }, Invalid { path: RepositoryTreeEntry, }, } /// A request to get a repository's information. /// /// # Authentication /// - Instance Authentication /// - Validate request against the `issued_for` public key /// - Validate User token against the user's instance's public key /// # Authorization /// - User Authorization /// - Potential User permissions checks #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryIssuesCountRequest; impl MessageTarget for RepositoryIssuesCountRequest {} #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryIssuesCountResponse { pub count: u64, } /// A request to get a repository's issues count. /// /// # Authentication /// - Instance Authentication /// - Validate request against the `issued_for` public key /// - Validate User token against the user's instance's public key /// # Authorization /// - User Authorization /// - Potential User permissions checks #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryIssueLabelsRequest; impl MessageTarget for RepositoryIssueLabelsRequest {} #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryIssueLabelsResponse { pub labels: Vec, } /// A request to get a repository's issue labels. /// /// # Authentication /// - Instance Authentication /// - Validate request against the `issued_for` public key /// - Validate User token against the user's instance's public key /// # Authorization /// - User Authorization /// - Potential User permissions checks #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryIssuesRequest; impl MessageTarget for RepositoryIssuesRequest {} #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryIssuesResponse { pub issues: Vec, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryInfoRequest { pub repository: Repository, /// Whether to fetch extra metadata like the last commit made to file or size pub extra_metadata: bool, /// Rev (branch) being requested pub rev: Option, /// Tree path being requested pub path: Option, } impl MessageTarget for RepositoryInfoRequest { fn target(&self) -> Option { Some(self.repository.instance.clone()) } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct RepositorySettingsRequest { pub repository: Repository, } impl MessageTarget for RepositorySettingsRequest { fn target(&self) -> Option { Some(self.repository.instance.clone()) } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)] pub struct RepositorySettingsResponse { pub settings: HashMap, } impl RepositorySettingsResponse { pub fn try_get(&self) -> Option { let setting_member = self .settings .iter() .filter(|(key, _)| key.as_str() == S::name()) .next()?; serde_json::from_value(setting_member.1.clone()).ok() } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct RepositoryWriteSettingsRequest { pub repository: Repository, pub settings: Vec<(String, String)>, } impl MessageTarget for RepositoryWriteSettingsRequest { fn target(&self) -> Option { Some(self.repository.instance.clone()) } } impl RepositoryWriteSettingsRequest { pub fn new(repository: impl ToOwned) -> Self { Self { repository: repository.to_owned(), settings: Default::default(), } } pub fn set(&mut self, setting: S) { self.settings.push(( S::name().to_string(), serde_json::to_string(&setting).unwrap(), )); } pub fn try_get(&self) -> Option { let setting_member = self .settings .iter() .filter(|(key, _)| key.as_str() == S::name()) .next()?; serde_json::from_str(&setting_member.1).ok() } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserWriteSettingsResponse { // IDK? }