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

ambee/giterated

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

Fix settings serialization

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨a70c63a

⁨giterated-models/src/messages/user.rs⁩ - ⁨2781⁩ bytes
Raw
1 use std::collections::HashMap;
2
3 use serde::{de::DeserializeOwned, Deserialize, Serialize};
4
5 use crate::model::{repository::RepositorySummary, settings::Setting, user::User};
6
7 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
8 pub struct UserDisplayNameRequest {
9 pub user: User,
10 }
11
12 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13 pub struct UserDisplayNameResponse {
14 pub display_name: Option<String>,
15 }
16
17 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
18 pub struct UserDisplayImageRequest {
19 pub user: User,
20 }
21
22 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23 pub struct UserDisplayImageResponse {
24 pub image_url: Option<String>,
25 }
26
27 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28 pub struct UserBioRequest {
29 pub user: User,
30 }
31
32 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
33 pub struct UserBioResponse {
34 pub bio: Option<String>,
35 }
36
37 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
38 pub struct UserRepositoriesRequest {
39 pub user: User,
40 }
41
42 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
43 pub struct UserRepositoriesResponse {
44 pub repositories: Vec<RepositorySummary>,
45 }
46
47 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
48 pub struct UserSettingsRequest {
49 pub user: User,
50 }
51
52 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)]
53 pub struct UserSettingsResponse {
54 pub settings: HashMap<String, serde_json::Value>,
55 }
56
57 impl UserSettingsResponse {
58 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
59 let setting_member = self
60 .settings
61 .iter()
62 .filter(|(key, _)| key.as_str() == S::name())
63 .next()?;
64
65 serde_json::from_value(setting_member.1.clone()).ok()
66 }
67 }
68
69 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
70 pub struct UserWriteSettingsRequest {
71 pub user: User,
72 pub settings: Vec<(String, String)>,
73 }
74
75 impl UserWriteSettingsRequest {
76 pub fn new(user: impl ToOwned<Owned = User>) -> Self {
77 Self {
78 user: user.to_owned(),
79 settings: Default::default(),
80 }
81 }
82
83 pub fn set<S: Setting>(&mut self, setting: S) {
84 self.settings.push((
85 S::name().to_string(),
86 serde_json::to_string(&setting).unwrap(),
87 ));
88 }
89 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
90 let setting_member = self
91 .settings
92 .iter()
93 .filter(|(key, _)| key.as_str() == S::name())
94 .next()?;
95
96 serde_json::from_str(&setting_member.1).ok()
97 }
98 }
99
100 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
101 pub struct UserWriteSettingsResponse {
102 // IDK?
103 }
104