1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
use std::{borrow::Borrow, sync::Arc};
use adw::prelude::*;
use libsecret::prelude::{RetrievableExtManual, RetrievableExt};
use relm4::{gtk, loading_widgets::LoadingWidgets, prelude::{AsyncComponent, AsyncComponentController, AsyncComponentParts, AsyncController, ComponentController, Controller}, AsyncComponentSender, Component, RelmWidgetExt};
pub mod components {
pub(crate) mod smart_summary;
pub(crate) use smart_summary::{
SmartSummaryButton, Output as SmartSummaryOutput, Input as SmartSummaryInput
};
pub(crate) mod post_editor;
pub(crate) use post_editor::{
PostEditor, Input as PostEditorInput
};
pub(crate) mod tag_pill;
pub(crate) use tag_pill::{TagPill, TagPillDelete};
pub mod signin;
pub use signin::{SignIn, Output as SignInOutput};
}
use components::post_editor::Post;
pub mod secrets;
pub mod micropub;
pub mod util;
pub const APPLICATION_ID: &str = "xyz.vikanezrimaya.kittybox.Bowl";
pub const CLIENT_ID_STR: &str = "https://kittybox.fireburn.ru/bowl/";
pub const VISIBILITY: [&str; 2] = ["public", "private"];
#[derive(Debug)]
pub struct App {
state: AuthState
}
#[derive(Debug)]
enum AuthState {
LoggedOut(AsyncController<components::SignIn>),
LoggedIn {
submit_busy_guard: Option<gtk::gio::ApplicationBusyGuard>,
post_editor: Controller<components::PostEditor<micropub::Error>>,
micropub: micropub::Client
}
}
#[derive(Debug)]
#[doc(hidden)]
pub enum Input {
SubmitButtonPressed,
Authorize(Box<components::SignInOutput>),
PostEditor(Option<Post>)
}
#[derive(Default, Debug)]
pub struct AppRootWidgets {
root: adw::ApplicationWindow,
toolbar_view: adw::ToolbarView,
top_bar: adw::HeaderBar,
top_bar_btn: gtk::Button,
}
//#[relm4::component(pub async)]
impl AsyncComponent for App {
/// The type of the messages that this component can receive.
type Input = Input;
/// The type of the messages that this component can send.
type Output = ();
/// The type of data with which this component will be initialized.
type Init = ();
/// The type of the command outputs that this component can receive.
type CommandOutput = ();
type Widgets = AppRootWidgets;
type Root = adw::ApplicationWindow;
fn init_root() -> Self::Root {
let window = Self::Root::default();
window.set_size_request(360, 294);
window.set_default_size(360, 640);
#[cfg(debug_assertions)]
window.add_css_class("devel");
window
}
fn init_loading_widgets(_root: Self::Root) -> Option<relm4::loading_widgets::LoadingWidgets> {
let root = gtk::Box::default();
let spinner = gtk::Spinner::builder()
.spinning(true)
.halign(gtk::Align::Center)
.valign(gtk::Align::Center)
.build();
root.append(&spinner);
Some(LoadingWidgets::new(root, spinner))
}
/// Initialize the UI and model.
async fn init(
_init: Self::Init,
window: Self::Root,
sender: AsyncComponentSender<Self>,
) -> AsyncComponentParts<Self> {
let schema = crate::secrets::get_schema();
let state = match libsecret::password_search_future(Some(&schema), {
let mut attrs = std::collections::HashMap::default();
attrs.insert(crate::secrets::TOKEN_KIND, crate::secrets::ACCESS_TOKEN);
attrs
}, libsecret::SearchFlags::ALL).await {
Ok(mut retrievables) => {
if retrievables.is_empty() {
AuthState::LoggedOut(
components::SignIn::builder()
.launch(glib::Uri::parse(
CLIENT_ID_STR, glib::UriFlags::NONE
).unwrap())
.forward(sender.input_sender(), |o| Self::Input::Authorize(Box::new(o)))
)
} else {
retrievables.sort_by_key(|s| s.created());
let retrievable = retrievables.last().unwrap();
let attrs = retrievable.attributes();
let micropub_uri = attrs
.get(crate::secrets::MICROPUB)
.and_then(|v| glib::Uri::parse(v, glib::UriFlags::NONE).ok())
.unwrap();
AuthState::LoggedIn {
post_editor: components::PostEditor::builder()
.launch(None)
.forward(sender.clone().input_sender(), Self::Input::PostEditor),
micropub: crate::micropub::Client::new(
micropub_uri,
retrievable.retrieve_secret_future().await.unwrap().unwrap().text().unwrap().to_string()
),
submit_busy_guard: None
}
}
},
Err(err) => {
log::warn!("Error retrieving secrets: {}", err);
AuthState::LoggedOut(
components::SignIn::builder()
.launch(glib::Uri::parse(
CLIENT_ID_STR, glib::UriFlags::NONE
).unwrap())
.forward(sender.input_sender(), |o| Self::Input::Authorize(Box::new(o)))
)
},
};
let model = App {
state,
};
let mut widgets = Self::Widgets {
root: window,
..Self::Widgets::default()
};
widgets.toolbar_view.add_top_bar(&widgets.top_bar);
widgets.top_bar.pack_end(&widgets.top_bar_btn);
widgets.top_bar_btn.set_icon_name("document-send-symbolic");
widgets.top_bar_btn.set_tooltip("Send post");
widgets.top_bar_btn.connect_clicked(glib::clone!(
#[strong] sender,
move |_button| sender.input(Self::Input::SubmitButtonPressed)
));
widgets.root.set_content(Some(&widgets.toolbar_view));
// Separate component choosing logic from initialization. We
// already have all the parts here, might as well use them.
model.update_view(&mut widgets, sender);
AsyncComponentParts { model, widgets }
}
fn update_view(&self, widgets: &mut Self::Widgets, _sender: AsyncComponentSender<Self>) {
// Bind the child component, if any, here.
match &self.state {
AuthState::LoggedOut(signin) => {
widgets.root.set_title(Some("Sign in with your website"));
widgets.toolbar_view.set_content(Some(signin.widget()));
widgets.top_bar_btn.set_visible(false);
},
AuthState::LoggedIn {
post_editor,
submit_busy_guard,
..
} => {
widgets.root.set_title(Some("Create post"));
widgets.toolbar_view.set_content(Some(post_editor.widget()));
widgets.top_bar_btn.set_sensitive(submit_busy_guard.is_none());
widgets.top_bar_btn.set_visible(true);
}
}
}
async fn update(
&mut self,
message: Self::Input,
_sender: AsyncComponentSender<Self>,
_root: &Self::Root
) {
match message {
Input::Authorize(data) => {
let schema = crate::secrets::get_schema();
let mut attributes = std::collections::HashMap::new();
let _me = data.me.to_string();
let _micropub = data.micropub.to_string();
attributes.insert(secrets::ME, _me.as_str());
attributes.insert(secrets::TOKEN_KIND, secrets::ACCESS_TOKEN);
attributes.insert(secrets::MICROPUB, _micropub.as_str());
let exp = data.expires_in
.as_ref()
.map(std::time::Duration::as_secs)
.as_ref()
.map(u64::to_string);
if let Some(expires_in) = exp.as_deref() {
attributes.insert(secrets::EXPIRES_IN, expires_in);
}
match libsecret::password_store_future(
Some(&schema),
attributes.clone(),
Some(libsecret::COLLECTION_DEFAULT),
data.me.to_str().as_str(),
&data.access_token
).await {
Ok(()) => {},
Err(err) => log::error!("Failed to store access token to the secret store: {}", err),
}
if let Some(refresh_token) = data.refresh_token.as_deref() {
attributes.insert(secrets::TOKEN_KIND, secrets::REFRESH_TOKEN);
attributes.remove(secrets::EXPIRES_IN);
match libsecret::password_store_future(
Some(&schema),
attributes,
Some(libsecret::COLLECTION_DEFAULT),
data.me.to_str().as_str(),
refresh_token
).await {
Ok(()) => {},
Err(err) => log::error!("Failed to store refresh token to the secret store: {}", err),
}
}
self.state = AuthState::LoggedIn {
post_editor: components::PostEditor::builder()
.launch(None)
.forward(_sender.clone().input_sender(), Self::Input::PostEditor),
micropub: crate::micropub::Client::new(
data.micropub.clone(), data.access_token.clone()
),
submit_busy_guard: None
};
},
Input::SubmitButtonPressed => {
if let AuthState::LoggedIn {
ref mut submit_busy_guard,
ref post_editor,
..
} = &mut self.state {
*submit_busy_guard = Some(relm4::main_adw_application().mark_busy());
post_editor.sender().emit(components::PostEditorInput::Submit);
};
},
Input::PostEditor(None) => {
if let AuthState::LoggedIn {
ref mut submit_busy_guard,
..
} = &mut self.state {
*submit_busy_guard = None;
}
}
Input::PostEditor(Some(post)) => {
if let AuthState::LoggedIn {
ref mut submit_busy_guard,
ref post_editor,
ref micropub,
} = &mut self.state {
let mf2 = post.into();
log::debug!("Submitting post: {:#}", serde_json::to_string(&mf2).unwrap());
match micropub.send_post(mf2).await {
Ok(uri) => {
post_editor.sender()
.emit(components::PostEditorInput::SubmitDone(uri));
},
Err(err) => {
log::warn!("Error sending post: {}", err);
post_editor.sender()
.emit(components::PostEditorInput::SubmitError(err));
}
}
*submit_busy_guard = None;
}
},
}
}
}
|