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

ambee/giterated

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

Add repository settings

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨f8eaf38

⁨giterated-models/src/messages/repository.rs⁩ - ⁨6115⁩ bytes
Raw
1 use std::collections::HashMap;
2
3 use serde::de::DeserializeOwned;
4 use serde::{Deserialize, Serialize};
5
6 use crate::model::repository::RepositoryVisibility;
7 use crate::model::settings::Setting;
8 use crate::model::{
9 repository::{Commit, Repository, RepositoryTreeEntry},
10 user::User,
11 };
12
13 /// A request to create a repository.
14 ///
15 /// # Authentication
16 /// - Instance Authentication
17 /// - Used to validate User token `issued_for`
18 /// - User Authentication
19 /// - Used to source owning user
20 /// - Used to authorize user token against user's instance
21 /// # Authorization
22 /// - Instance Authorization
23 /// - Used to authorize action using User token requiring a correct `issued_for` and valid issuance from user's instance
24 /// - User Authorization
25 /// - Potential User permissions checks
26 #[derive(Clone, Debug, Serialize, Deserialize)]
27 pub struct RepositoryCreateRequest {
28 pub name: String,
29 pub description: Option<String>,
30 pub visibility: RepositoryVisibility,
31 pub default_branch: String,
32 pub owner: User,
33 }
34
35 #[derive(Clone, Debug, Serialize, Deserialize)]
36 pub struct RepositoryCreateResponse;
37
38 /// A request to inspect the tree of a repository.
39 ///
40 /// # Authentication
41 /// - Instance Authentication
42 /// - Validate request against the `issued_for` public key
43 /// - Validate User token against the user's instance's public key
44 /// # Authorization
45 /// - User Authorization
46 /// - Potential User permissions checks
47 #[derive(Clone, Debug, Serialize, Deserialize)]
48 pub struct RepositoryFileInspectRequest {
49 pub path: RepositoryTreeEntry,
50 }
51
52 #[derive(Clone, Debug, Serialize, Deserialize)]
53 pub enum RepositoryFileInspectionResponse {
54 File {
55 commit_metadata: Commit,
56 },
57 Folder {
58 commit_metadata: Commit,
59 members: Vec<RepositoryTreeEntry>,
60 },
61 Invalid {
62 path: RepositoryTreeEntry,
63 },
64 }
65
66 /// A request to get a repository's information.
67 ///
68 /// # Authentication
69 /// - Instance Authentication
70 /// - Validate request against the `issued_for` public key
71 /// - Validate User token against the user's instance's public key
72 /// # Authorization
73 /// - User Authorization
74 /// - Potential User permissions checks
75 #[derive(Clone, Debug, Serialize, Deserialize)]
76 pub struct RepositoryIssuesCountRequest;
77
78 #[derive(Clone, Debug, Serialize, Deserialize)]
79 pub struct RepositoryIssuesCountResponse {
80 pub count: u64,
81 }
82
83 /// A request to get a repository's issues count.
84 ///
85 /// # Authentication
86 /// - Instance Authentication
87 /// - Validate request against the `issued_for` public key
88 /// - Validate User token against the user's instance's public key
89 /// # Authorization
90 /// - User Authorization
91 /// - Potential User permissions checks
92 #[derive(Clone, Debug, Serialize, Deserialize)]
93 pub struct RepositoryIssueLabelsRequest;
94
95 #[derive(Clone, Debug, Serialize, Deserialize)]
96 pub struct RepositoryIssueLabelsResponse {
97 pub labels: Vec<IssueLabel>,
98 }
99
100 #[derive(Clone, Debug, Serialize, Deserialize)]
101 pub struct IssueLabel {
102 pub name: String,
103 pub color: String,
104 }
105
106 /// A request to get a repository's issue labels.
107 ///
108 /// # Authentication
109 /// - Instance Authentication
110 /// - Validate request against the `issued_for` public key
111 /// - Validate User token against the user's instance's public key
112 /// # Authorization
113 /// - User Authorization
114 /// - Potential User permissions checks
115 #[derive(Clone, Debug, Serialize, Deserialize)]
116 pub struct RepositoryIssuesRequest;
117
118 #[derive(Clone, Debug, Serialize, Deserialize)]
119 pub struct RepositoryIssuesResponse {
120 pub issues: Vec<RepositoryIssue>,
121 }
122
123 /// A request to get a repository's issues.
124 ///
125 /// # Authentication
126 /// - Instance Authentication
127 /// - Validate request against the `issued_for` public key
128 /// - Validate User token against the user's instance's public key
129 /// # Authorization
130 /// - User Authorization
131 /// - Potential User permissions checks
132 #[derive(Clone, Debug, Serialize, Deserialize)]
133 pub struct RepositoryIssue {
134 pub author: User,
135 pub id: u64,
136 pub title: String,
137 pub contents: String,
138 pub labels: Vec<IssueLabel>,
139 }
140
141 #[derive(Clone, Debug, Serialize, Deserialize)]
142 pub struct RepositoryInfoRequest {
143 pub repository: Repository,
144 /// Whether to fetch extra metadata like the last commit made to file or size
145 pub extra_metadata: bool,
146 /// Rev (branch) being requested
147 pub rev: Option<String>,
148 /// Tree path being requested
149 pub path: Option<String>,
150 }
151
152
153 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
154 pub struct RepositorySettingsRequest {
155 pub repository: Repository,
156 }
157
158 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)]
159 pub struct RepositorySettingsResponse {
160 pub settings: HashMap<String, serde_json::Value>,
161 }
162
163 impl RepositorySettingsResponse {
164 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
165 let setting_member = self
166 .settings
167 .iter()
168 .filter(|(key, _)| key.as_str() == S::name())
169 .next()?;
170
171 serde_json::from_value(setting_member.1.clone()).ok()
172 }
173 }
174
175 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
176 pub struct RepositoryWriteSettingsRequest {
177 pub repository: Repository,
178 pub settings: Vec<(String, String)>,
179 }
180
181 impl RepositoryWriteSettingsRequest {
182 pub fn new(repository: impl ToOwned<Owned = Repository>) -> Self {
183 Self {
184 repository: repository.to_owned(),
185 settings: Default::default(),
186 }
187 }
188
189 pub fn set<S: Setting>(&mut self, setting: S) {
190 self.settings.push((
191 S::name().to_string(),
192 serde_json::to_string(&setting).unwrap(),
193 ));
194 }
195 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
196 let setting_member = self
197 .settings
198 .iter()
199 .filter(|(key, _)| key.as_str() == S::name())
200 .next()?;
201
202 serde_json::from_str(&setting_member.1).ok()
203 }
204 }
205
206 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
207 pub struct UserWriteSettingsResponse {
208 // IDK?
209 }
210