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

ambee/giterated

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

Automatically populate the target instance field from the request using MessageTarget trait

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨37da513

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