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

ambee/giterated

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

Begin new protocol refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨26651b1

⁨giterated-models/src/messages/repository.rs⁩ - ⁨6468⁩ 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 path: RepositoryTreeEntry,
60 }
61
62 impl MessageTarget for RepositoryFileInspectRequest {
63 fn target(&self) -> Option<Instance> {
64 None
65 }
66 }
67
68 #[derive(Clone, Debug, Serialize, Deserialize)]
69 pub enum RepositoryFileInspectionResponse {
70 File {
71 commit_metadata: Commit,
72 },
73 Folder {
74 commit_metadata: Commit,
75 members: Vec<RepositoryTreeEntry>,
76 },
77 Invalid {
78 path: RepositoryTreeEntry,
79 },
80 }
81
82 /// A request to get a repository's information.
83 ///
84 /// # Authentication
85 /// - Instance Authentication
86 /// - Validate request against the `issued_for` public key
87 /// - Validate User token against the user's instance's public key
88 /// # Authorization
89 /// - User Authorization
90 /// - Potential User permissions checks
91 #[derive(Clone, Debug, Serialize, Deserialize)]
92 pub struct RepositoryIssuesCountRequest;
93
94 impl MessageTarget for RepositoryIssuesCountRequest {}
95
96 #[derive(Clone, Debug, Serialize, Deserialize)]
97 pub struct RepositoryIssuesCountResponse {
98 pub count: u64,
99 }
100
101 /// A request to get a repository's issues count.
102 ///
103 /// # Authentication
104 /// - Instance Authentication
105 /// - Validate request against the `issued_for` public key
106 /// - Validate User token against the user's instance's public key
107 /// # Authorization
108 /// - User Authorization
109 /// - Potential User permissions checks
110 #[derive(Clone, Debug, Serialize, Deserialize)]
111 pub struct RepositoryIssueLabelsRequest;
112
113 impl MessageTarget for RepositoryIssueLabelsRequest {}
114
115 #[derive(Clone, Debug, Serialize, Deserialize)]
116 pub struct RepositoryIssueLabelsResponse {
117 pub labels: Vec<IssueLabel>,
118 }
119
120
121 /// A request to get a repository's issue labels.
122 ///
123 /// # Authentication
124 /// - Instance Authentication
125 /// - Validate request against the `issued_for` public key
126 /// - Validate User token against the user's instance's public key
127 /// # Authorization
128 /// - User Authorization
129 /// - Potential User permissions checks
130 #[derive(Clone, Debug, Serialize, Deserialize)]
131 pub struct RepositoryIssuesRequest;
132
133 impl MessageTarget for RepositoryIssuesRequest {}
134
135 #[derive(Clone, Debug, Serialize, Deserialize)]
136 pub struct RepositoryIssuesResponse {
137 pub issues: Vec<RepositoryIssue>,
138 }
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 impl MessageTarget for RepositoryInfoRequest {
153 fn target(&self) -> Option<Instance> {
154 Some(self.repository.instance.clone())
155 }
156 }
157
158 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
159 pub struct RepositorySettingsRequest {
160 pub repository: Repository,
161 }
162
163 impl MessageTarget for RepositorySettingsRequest {
164 fn target(&self) -> Option<Instance> {
165 Some(self.repository.instance.clone())
166 }
167 }
168
169 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)]
170 pub struct RepositorySettingsResponse {
171 pub settings: HashMap<String, serde_json::Value>,
172 }
173
174 impl RepositorySettingsResponse {
175 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
176 let setting_member = self
177 .settings
178 .iter()
179 .filter(|(key, _)| key.as_str() == S::name())
180 .next()?;
181
182 serde_json::from_value(setting_member.1.clone()).ok()
183 }
184 }
185
186 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
187 pub struct RepositoryWriteSettingsRequest {
188 pub repository: Repository,
189 pub settings: Vec<(String, String)>,
190 }
191
192 impl MessageTarget for RepositoryWriteSettingsRequest {
193 fn target(&self) -> Option<Instance> {
194 Some(self.repository.instance.clone())
195 }
196 }
197
198 impl RepositoryWriteSettingsRequest {
199 pub fn new(repository: impl ToOwned<Owned = Repository>) -> Self {
200 Self {
201 repository: repository.to_owned(),
202 settings: Default::default(),
203 }
204 }
205
206 pub fn set<S: Setting>(&mut self, setting: S) {
207 self.settings.push((
208 S::name().to_string(),
209 serde_json::to_string(&setting).unwrap(),
210 ));
211 }
212 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
213 let setting_member = self
214 .settings
215 .iter()
216 .filter(|(key, _)| key.as_str() == S::name())
217 .next()?;
218
219 serde_json::from_str(&setting_member.1).ok()
220 }
221 }
222
223 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
224 pub struct UserWriteSettingsResponse {
225 // IDK?
226 }
227