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

ambee/giterated

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

Finish unified stack refactor.

Adds support for operation state, which will be used to pass authentication information around. Added generic backend that uses a channel to communicate with a typed backend.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨d15581c

⁨giterated-models/src/repository/operations.rs⁩ - ⁨7804⁩ 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 the difference between two repository trees.
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 RepositoryDiffRequest {
64 pub old_id: String,
65 pub new_id: String,
66 }
67
68 impl GiteratedOperation<Repository> for RepositoryDiffRequest {
69 type Success = RepositoryDiff;
70 type Failure = RepositoryError;
71 }
72
73 /// A request to get the commit before the one with the passed id
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 RepositoryCommitBeforeRequest(pub String);
84
85 impl GiteratedOperation<Repository> for RepositoryCommitBeforeRequest {
86 type Success = Commit;
87 type Failure = RepositoryError;
88 }
89
90 #[derive(Clone, Debug, Serialize, Deserialize)]
91 pub struct RepositoryIssuesCountRequest;
92
93 impl GiteratedOperation<Repository> for RepositoryIssuesCountRequest {
94 type Success = u64;
95 type Failure = RepositoryError;
96 }
97
98 /// A request to get a repository's issues count.
99 ///
100 /// # Authentication
101 /// - Instance Authentication
102 /// - Validate request against the `issued_for` public key
103 /// - Validate User token against the user's instance's public key
104 /// # Authorization
105 /// - User Authorization
106 /// - Potential User permissions checks
107 #[derive(Clone, Debug, Serialize, Deserialize)]
108 pub struct RepositoryIssueLabelsRequest;
109
110 impl GiteratedOperation<Repository> for RepositoryIssueLabelsRequest {
111 type Success = Vec<IssueLabel>;
112 type Failure = RepositoryError;
113 }
114
115 /// A request to get a repository's issue labels.
116 ///
117 /// # Authentication
118 /// - Instance Authentication
119 /// - Validate request against the `issued_for` public key
120 /// - Validate User token against the user's instance's public key
121 /// # Authorization
122 /// - User Authorization
123 /// - Potential User permissions checks
124 #[derive(Clone, Debug, Serialize, Deserialize)]
125 pub struct RepositoryIssuesRequest;
126
127 impl GiteratedOperation<Repository> for RepositoryIssuesRequest {
128 type Success = Vec<RepositoryIssue>;
129 type Failure = RepositoryError;
130 }
131
132 /// A request to inspect the tree of a repository.
133 ///
134 /// # Authentication
135 /// - Instance Authentication
136 /// - Validate request against the `issued_for` public key
137 /// - Validate User token against the user's instance's public key
138 /// # Authorization
139 /// - User Authorization
140 /// - Potential User permissions checks
141 #[derive(Clone, Debug, Serialize, Deserialize)]
142 pub struct RepositoryFileInspectRequest {
143 /// Whether to get extra metadata for every entry (file mode, size and last commit made to it).
144 pub extra_metadata: bool,
145 /// Revision of the repository to get (branch, commit id).
146 pub rev: Option<String>,
147 /// If not given a path, it'll default to the base.
148 pub path: Option<String>,
149 }
150
151 impl GiteratedOperation<Repository> for RepositoryFileInspectRequest {
152 type Success = Vec<RepositoryTreeEntry>;
153 type Failure = RepositoryError;
154 }
155
156 impl<S: Clone + Send + Sync, B: ObjectBackend<S> + std::fmt::Debug> Object<'_, S, Repository, B> {
157 pub async fn info(
158 &mut self,
159 extra_metadata: bool,
160 rev: Option<String>,
161 path: Option<String>,
162 operation_state: &S,
163 ) -> Result<RepositoryView, OperationError<RepositoryError>> {
164 self.request::<RepositoryInfoRequest>(
165 RepositoryInfoRequest {
166 extra_metadata,
167 rev,
168 path,
169 },
170 operation_state,
171 )
172 .await
173 }
174
175 pub async fn file_from_id(
176 &mut self,
177 id: String,
178 operation_state: &S,
179 ) -> Result<RepositoryFile, OperationError<RepositoryError>> {
180 self.request::<RepositoryFileFromIdRequest>(
181 RepositoryFileFromIdRequest(id),
182 operation_state,
183 )
184 .await
185 }
186
187 pub async fn diff(
188 &mut self,
189 old_id: String,
190 new_id: String,
191 operation_state: &S,
192 ) -> Result<RepositoryDiff, OperationError<RepositoryError>> {
193 self.request::<RepositoryDiffRequest>(
194 RepositoryDiffRequest { old_id, new_id },
195 operation_state,
196 )
197 .await
198 }
199
200 pub async fn commit_before(
201 &mut self,
202 id: String,
203 operation_state: &S,
204 ) -> Result<Commit, OperationError<RepositoryError>> {
205 self.request::<RepositoryCommitBeforeRequest>(
206 RepositoryCommitBeforeRequest(id),
207 operation_state,
208 )
209 .await
210 }
211
212 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {
213 // self.request::<RepositoryIssuesCountRequest>(RepositoryIssuesCountRequest)
214 // .await
215 // }
216
217 pub async fn issue_labels(
218 &mut self,
219 operation_state: &S,
220 ) -> Result<Vec<IssueLabel>, OperationError<RepositoryError>> {
221 self.request::<RepositoryIssueLabelsRequest>(RepositoryIssueLabelsRequest, operation_state)
222 .await
223 }
224
225 pub async fn issues(
226 &mut self,
227 operation_state: &S,
228 ) -> Result<Vec<RepositoryIssue>, OperationError<RepositoryError>> {
229 self.request::<RepositoryIssuesRequest>(RepositoryIssuesRequest, operation_state)
230 .await
231 }
232
233 pub async fn inspect_files(
234 &mut self,
235 extra_metadata: bool,
236 rev: Option<&str>,
237 path: Option<&str>,
238 operation_state: &S,
239 ) -> Result<Vec<RepositoryTreeEntry>, OperationError<RepositoryError>> {
240 self.request::<RepositoryFileInspectRequest>(
241 RepositoryFileInspectRequest {
242 extra_metadata,
243 rev: rev.map(|r| r.to_string()),
244 path: path.map(|p| p.to_string()),
245 },
246 operation_state,
247 )
248 .await
249 }
250 }
251