Automatically populate the target instance field from the request using MessageTarget trait
parent: tbd commit: 37da513
1 | use Error; |
2 | use |
3 | |
4 | UserBioRequest, UserDisplayImageRequest, UserDisplayNameRequest, UserRepositoriesRequest, |
5 | UserRepositoriesResponse, UserSettingsRequest, UserSettingsResponse, |
6 | UserWriteSettingsRequest, UserWriteSettingsResponse, |
7 | , |
8 | , | User
9 | ; |
10 | |
11 | use crate; |
12 | |
13 | use ConnectionState; |
14 | |
15 | pub async |
16 | message_type: &str, |
17 | message: &NetworkMessage, |
18 | state: &ConnectionState, |
19 | |
20 | match message_type |
21 | "giterated_models::messages::user::UserDisplayNameRequest" => |
22 | display_name.handle_message .await?; |
23 | |
24 | Ok |
25 | |
26 | "giterated_models::messages::user::UserDisplayImageRequest" => |
27 | display_image.handle_message .await?; |
28 | |
29 | Ok |
30 | |
31 | "giterated_models::messages::user::UserBioRequest" => |
32 | bio.handle_message .await?; |
33 | |
34 | Ok |
35 | |
36 | "giterated_models::messages::user::UserRepositoriesRequest" => |
37 | repositories.handle_message .await?; |
38 | |
39 | Ok |
40 | |
41 | "giterated_models::messages::user::UserSettingsRequest" => |
42 | user_settings.handle_message .await?; |
43 | |
44 | Ok |
45 | |
46 | "giterated_models::messages::user::UserWriteSettingsRequest" => |
47 | write_user_settings.handle_message .await?; |
48 | |
49 | Ok |
50 | |
51 | _ => Ok, |
52 | |
53 | |
54 | |
55 | async |
56 | Message | : ,
57 | State | : ,
58 | |
59 | let mut user_backend = connection_state.user_backend.lock .await; |
60 | let response = user_backend.display_name .await?; |
61 | |
62 | drop; |
63 | |
64 | connection_state.send .await?; |
65 | |
66 | Ok |
67 | |
68 | |
69 | async |
70 | Message | : ,
71 | State | : ,
72 | |
73 | let mut user_backend = connection_state.user_backend.lock .await; |
74 | let response = user_backend.display_image .await?; |
75 | |
76 | drop; |
77 | |
78 | connection_state.send .await?; |
79 | |
80 | Ok |
81 | |
82 | |
83 | async |
84 | Message | : ,
85 | State | : ,
86 | |
87 | let mut user_backend = connection_state.user_backend.lock .await; |
88 | let response = user_backend.bio .await?; |
89 | |
90 | drop; |
91 | |
92 | connection_state.send .await?; |
93 | |
94 | Ok |
95 | |
96 | |
97 | async |
98 | Message | : ,
99 | State | : ,
100 | requesting_user: , |
101 | |
102 | let requesting_user = requesting_user.map; |
103 | |
104 | let mut repository_backend = connection_state.repository_backend.lock .await; |
105 | let repositories = repository_backend |
106 | .repositories_for_user |
107 | .await; |
108 | |
109 | let repositories = match repositories |
110 | Ok => repositories, |
111 | Err => |
112 | error!; |
113 | return Ok; |
114 | |
115 | ; |
116 | drop; |
117 | |
118 | let mut user_backend = connection_state.user_backend.lock .await; |
119 | let user_exists = user_backend.exists .await; |
120 | |
121 | if repositories.is_empty && !matches! |
122 | return Err; |
123 | |
124 | |
125 | let response: UserRepositoriesResponse = UserRepositoriesResponse ; |
126 | |
127 | connection_state.send .await?; |
128 | |
129 | Ok |
130 | |
131 | |
132 | async |
133 | Message | : ,
134 | State | : ,
135 | AuthenticatedUser | : AuthenticatedUser,
136 | |
137 | if request.user != requesting_user |
138 | return Err; |
139 | |
140 | |
141 | let mut settings_backend = connection_state.settings_backend.lock .await; |
142 | let mut settings = settings_backend.user_get .await?; |
143 | drop; |
144 | |
145 | let response = UserSettingsResponse |
146 | settings: settings.drain .collect, |
147 | ; |
148 | |
149 | connection_state.send .await?; |
150 | |
151 | Ok |
152 | |
153 | |
154 | async |
155 | Message | : ,
156 | State | : ,
157 | AuthenticatedUser | : AuthenticatedUser,
158 | |
159 | if request.user != requesting_user |
160 | return Err; |
161 | |
162 | |
163 | let mut settings_backend = connection_state.settings_backend.lock .await; |
164 | settings_backend |
165 | .user_write |
166 | .await?; |
167 | drop; |
168 | |
169 | let response = UserWriteSettingsResponse ; |
170 | |
171 | connection_state.send .await?; |
172 | |
173 | Ok |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | InvalidUser, |
180 | |
181 | Other, |
182 | |
183 |