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/user.rs⁩ - ⁨3697⁩ bytes
Raw
1 use std::collections::HashMap;
2
3 use serde::{de::DeserializeOwned, Deserialize, Serialize};
4
5 use crate::model::{
6 instance::Instance, repository::RepositorySummary, settings::Setting, user::User,
7 };
8
9 use super::MessageTarget;
10
11 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12 pub struct UserDisplayNameRequest {
13 pub user: User,
14 }
15
16 impl MessageTarget for UserDisplayNameRequest {
17 fn target(&self) -> Option<Instance> {
18 Some(self.user.instance.clone())
19 }
20 }
21
22 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
23 pub struct UserDisplayNameResponse {
24 pub display_name: Option<String>,
25 }
26
27 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28 pub struct UserDisplayImageRequest {
29 pub user: User,
30 }
31
32 impl MessageTarget for UserDisplayImageRequest {
33 fn target(&self) -> Option<Instance> {
34 Some(self.user.instance.clone())
35 }
36 }
37
38 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
39 pub struct UserDisplayImageResponse {
40 pub image_url: Option<String>,
41 }
42
43 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
44 pub struct UserBioRequest {
45 pub user: User,
46 }
47
48 impl MessageTarget for UserBioRequest {
49 fn target(&self) -> Option<Instance> {
50 Some(self.user.instance.clone())
51 }
52 }
53
54 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
55 pub struct UserBioResponse {
56 pub bio: Option<String>,
57 }
58
59 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
60 pub struct UserRepositoriesRequest {
61 pub instance: Instance,
62 pub user: User,
63 }
64
65 impl MessageTarget for UserRepositoriesRequest {
66 fn target(&self) -> Option<Instance> {
67 Some(self.instance.clone())
68 }
69 }
70
71 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
72 pub struct UserRepositoriesResponse {
73 pub repositories: Vec<RepositorySummary>,
74 }
75
76 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
77 pub struct UserSettingsRequest {
78 pub user: User,
79 }
80
81 impl MessageTarget for UserSettingsRequest {
82 fn target(&self) -> Option<Instance> {
83 Some(self.user.instance.clone())
84 }
85 }
86
87 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)]
88 pub struct UserSettingsResponse {
89 pub settings: HashMap<String, serde_json::Value>,
90 }
91
92 impl UserSettingsResponse {
93 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
94 let setting_member = self
95 .settings
96 .iter()
97 .filter(|(key, _)| key.as_str() == S::name())
98 .next()?;
99
100 serde_json::from_value(setting_member.1.clone()).ok()
101 }
102 }
103
104 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
105 pub struct UserWriteSettingsRequest {
106 pub user: User,
107 pub settings: Vec<(String, String)>,
108 }
109
110 impl MessageTarget for UserWriteSettingsRequest {
111 fn target(&self) -> Option<Instance> {
112 Some(self.user.instance.clone())
113 }
114 }
115
116 impl UserWriteSettingsRequest {
117 pub fn new(user: impl ToOwned<Owned = User>) -> Self {
118 Self {
119 user: user.to_owned(),
120 settings: Default::default(),
121 }
122 }
123
124 pub fn set<S: Setting>(&mut self, setting: S) {
125 self.settings.push((
126 S::name().to_string(),
127 serde_json::to_string(&setting).unwrap(),
128 ));
129 }
130 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
131 let setting_member = self
132 .settings
133 .iter()
134 .filter(|(key, _)| key.as_str() == S::name())
135 .next()?;
136
137 serde_json::from_str(&setting_member.1).ok()
138 }
139 }
140
141 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
142 pub struct UserWriteSettingsResponse {
143 // IDK?
144 }
145