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

ambee/giterated

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

Add authentication back into the operation states

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨97a26fd

⁨giterated-models/src/repository/operations.rs⁩ - ⁨9807⁩ bytes
Raw
1 use serde::{Deserialize, Serialize};
2
3 use crate::{
4 error::{OperationError, RepositoryError},
5 object::Object,
6 object_backend::ObjectBackend,
7 operation::GiteratedOperation,
8 };
9
10 use super::{
11 Commit, IssueLabel, Repository, RepositoryDiff, RepositoryFile, RepositoryIssue,
12 RepositoryTreeEntry, RepositoryView,
13 };
14
15 /// A request to get a repository's information.
16 ///
17 /// # Authentication
18 /// - Instance Authentication
19 /// - Validate request against the `issued_for` public key
20 /// - Validate User token against the user's instance's public key
21 /// # Authorization
22 /// - User Authorization
23 /// - Potential User permissions checks
24 #[derive(Clone, Debug, Serialize, Deserialize)]
25 pub struct RepositoryInfoRequest {
26 pub extra_metadata: bool,
27 pub rev: Option<String>,
28 pub path: Option<String>,
29 }
30
31 impl GiteratedOperation<Repository> for RepositoryInfoRequest {
32 type Success = RepositoryView;
33 type Failure = RepositoryError;
34 }
35
36 /// A request to get a file from a repository using the given id.
37 ///
38 /// # Authentication
39 /// - Instance Authentication
40 /// - Validate request against the `issued_for` public key
41 /// - Validate User token against the user's instance's public key
42 /// # Authorization
43 /// - User Authorization
44 /// - Potential User permissions checks
45 #[derive(Clone, Debug, Serialize, Deserialize)]
46 pub struct RepositoryFileFromIdRequest(pub String);
47
48 impl GiteratedOperation<Repository> for RepositoryFileFromIdRequest {
49 type Success = RepositoryFile;
50 type Failure = RepositoryError;
51 }
52
53 /// A request to get a file from a repository using the given relative path.
54 ///
55 /// # Authentication
56 /// - Instance Authentication
57 /// - Validate request against the `issued_for` public key
58 /// - Validate User token against the user's instance's public key
59 /// # Authorization
60 /// - User Authorization
61 /// - Potential User permissions checks
62 #[derive(Clone, Debug, Serialize, Deserialize)]
63 pub struct RepositoryFileFromPathRequest {
64 pub rev: Option<String>,
65 pub path: String,
66 }
67
68 impl GiteratedOperation<Repository> for RepositoryFileFromPathRequest {
69 type Success = RepositoryFile;
70 type Failure = RepositoryError;
71 }
72
73 /// A request to get the difference between two repository trees.
74 ///
75 /// # Authentication
76 /// - Instance Authentication
77 /// - Validate request against the `issued_for` public key
78 /// - Validate User token against the user's instance's public key
79 /// # Authorization
80 /// - User Authorization
81 /// - Potential User permissions checks
82 #[derive(Clone, Debug, Serialize, Deserialize)]
83 pub struct RepositoryDiffRequest {
84 pub old_id: String,
85 pub new_id: String,
86 }
87
88 impl GiteratedOperation<Repository> for RepositoryDiffRequest {
89 type Success = RepositoryDiff;
90 type Failure = RepositoryError;
91 }
92
93 /// A request to get the difference between two repository trees as a unified git patch.
94 ///
95 /// # Authentication
96 /// - Instance Authentication
97 /// - Validate request against the `issued_for` public key
98 /// - Validate User token against the user's instance's public key
99 /// # Authorization
100 /// - User Authorization
101 /// - Potential User permissions checks
102 #[derive(Clone, Debug, Serialize, Deserialize)]
103 pub struct RepositoryDiffPatchRequest {
104 pub old_id: String,
105 pub new_id: String,
106 }
107
108 impl GiteratedOperation<Repository> for RepositoryDiffPatchRequest {
109 type Success = String;
110 type Failure = RepositoryError;
111 }
112
113 /// A request to get the commit before the one with the passed id
114 ///
115 /// # Authentication
116 /// - Instance Authentication
117 /// - Validate request against the `issued_for` public key
118 /// - Validate User token against the user's instance's public key
119 /// # Authorization
120 /// - User Authorization
121 /// - Potential User permissions checks
122 #[derive(Clone, Debug, Serialize, Deserialize)]
123 pub struct RepositoryCommitBeforeRequest(pub String);
124
125 impl GiteratedOperation<Repository> for RepositoryCommitBeforeRequest {
126 type Success = Commit;
127 type Failure = RepositoryError;
128 }
129
130 #[derive(Clone, Debug, Serialize, Deserialize)]
131 pub struct RepositoryIssuesCountRequest;
132
133 impl GiteratedOperation<Repository> for RepositoryIssuesCountRequest {
134 type Success = u64;
135 type Failure = RepositoryError;
136 }
137
138 /// A request to get a repository's issues count.
139 ///
140 /// # Authentication
141 /// - Instance Authentication
142 /// - Validate request against the `issued_for` public key
143 /// - Validate User token against the user's instance's public key
144 /// # Authorization
145 /// - User Authorization
146 /// - Potential User permissions checks
147 #[derive(Clone, Debug, Serialize, Deserialize)]
148 pub struct RepositoryIssueLabelsRequest;
149
150 impl GiteratedOperation<Repository> for RepositoryIssueLabelsRequest {
151 type Success = Vec<IssueLabel>;
152 type Failure = RepositoryError;
153 }
154
155 /// A request to get a repository's issue labels.
156 ///
157 /// # Authentication
158 /// - Instance Authentication
159 /// - Validate request against the `issued_for` public key
160 /// - Validate User token against the user's instance's public key
161 /// # Authorization
162 /// - User Authorization
163 /// - Potential User permissions checks
164 #[derive(Clone, Debug, Serialize, Deserialize)]
165 pub struct RepositoryIssuesRequest;
166
167 impl GiteratedOperation<Repository> for RepositoryIssuesRequest {
168 type Success = Vec<RepositoryIssue>;
169 type Failure = RepositoryError;
170 }
171
172 /// A request to inspect the tree of a repository.
173 ///
174 /// # Authentication
175 /// - Instance Authentication
176 /// - Validate request against the `issued_for` public key
177 /// - Validate User token against the user's instance's public key
178 /// # Authorization
179 /// - User Authorization
180 /// - Potential User permissions checks
181 #[derive(Clone, Debug, Serialize, Deserialize)]
182 pub struct RepositoryFileInspectRequest {
183 /// Whether to get extra metadata for every entry (file mode, size and last commit made to it).
184 pub extra_metadata: bool,
185 /// Revision of the repository to get (branch, commit id).
186 pub rev: Option<String>,
187 /// If not given a path, it'll default to the base.
188 pub path: Option<String>,
189 }
190
191 impl GiteratedOperation<Repository> for RepositoryFileInspectRequest {
192 type Success = Vec<RepositoryTreeEntry>;
193 type Failure = RepositoryError;
194 }
195
196 impl<S: Clone + Send + Sync, B: ObjectBackend<S> + std::fmt::Debug> Object<'_, S, Repository, B> {
197 pub async fn info(
198 &mut self,
199 extra_metadata: bool,
200 rev: Option<String>,
201 path: Option<String>,
202 operation_state: &S,
203 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
204 self.request::<RepositoryInfoRequest>(
205 RepositoryInfoRequest {
206 extra_metadata,
207 rev,
208 path,
209 },
210 operation_state,
211 )
212 .await
213 }
214
215 pub async fn file_from_id(
216 &mut self,
217 id: String,
218 operation_state: &S,
219 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
220 self.request::<RepositoryFileFromIdRequest>(
221 RepositoryFileFromIdRequest(id),
222 operation_state,
223 )
224 .await
225 }
226
227 pub async fn file_from_path(
228 &mut self,
229 rev: Option<String>,
230 path: String,
231 operation_state: &S,
232 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
233 self.request::<RepositoryFileFromPathRequest>(
234 RepositoryFileFromPathRequest { rev, path },
235 operation_state,
236 )
237 .await
238 }
239
240 pub async fn diff(
241 &mut self,
242 old_id: String,
243 new_id: String,
244 operation_state: &S,
245 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
246 self.request::<RepositoryDiffRequest>(
247 RepositoryDiffRequest { old_id, new_id },
248 operation_state,
249 )
250 .await
251 }
252
253 pub async fn diff_patch(
254 &mut self,
255 old_id: String,
256 new_id: String,
257 operation_state: &S,
258 ) -> Result<String, OperationError<RepositoryError>> {
259 self.request::<RepositoryDiffPatchRequest>(
260 RepositoryDiffPatchRequest { old_id, new_id },
261 operation_state,
262 )
263 .await
264 }
265
266 pub async fn commit_before(
267 &mut self,
268 id: String,
269 operation_state: &S,
270 ) -> Result<Commit, OperationError<RepositoryError>> {
271 self.request::<RepositoryCommitBeforeRequest>(
272 RepositoryCommitBeforeRequest(id),
273 operation_state,
274 )
275 .await
276 }
277
278 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {
279 // self.request::<RepositoryIssuesCountRequest>(RepositoryIssuesCountRequest)
280 // .await
281 // }
282
283 pub async fn issue_labels(
284 &mut self,
285 operation_state: &S,
286 ) -> Result<Vec<IssueLabel>, OperationError<RepositoryError>> {
287 self.request::<RepositoryIssueLabelsRequest>(RepositoryIssueLabelsRequest, operation_state)
288 .await
289 }
290
291 pub async fn issues(
292 &mut self,
293 operation_state: &S,
294 ) -> Result<Vec<RepositoryIssue>, OperationError<RepositoryError>> {
295 self.request::<RepositoryIssuesRequest>(RepositoryIssuesRequest, operation_state)
296 .await
297 }
298
299 pub async fn inspect_files(
300 &mut self,
301 extra_metadata: bool,
302 rev: Option<&str>,
303 path: Option<&str>,
304 operation_state: &S,
305 ) -> Result<Vec<RepositoryTreeEntry>, OperationError<RepositoryError>> {
306 self.request::<RepositoryFileInspectRequest>(
307 RepositoryFileInspectRequest {
308 extra_metadata,
309 rev: rev.map(|r| r.to_string()),
310 path: path.map(|p| p.to_string()),
311 },
312 operation_state,
313 )
314 .await
315 }
316 }
317