summary refs log tree commit diff
path: root/src/lib.rs
blob: f2436f83804f7094ec978ea4a60176989f55de4f (plain) (blame)
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
use std::{borrow::Borrow, sync::Arc};

use adw::prelude::*;
use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentParts, 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};
}

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 VISIBILITY: [&str; 2] = ["public", "private"];

#[derive(Debug)]
pub struct App {
    state: AuthState
}
#[derive(Debug)]
enum AuthState {
    LoggedOut(gtk::EntryBuffer),
    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,
    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,

    login_box: gtk::Box,
    login_label: gtk::Label,
    login_button: 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);
        #[cfg(debug_assertions)]
        window.add_css_class("devel");

        window
    }

    /// Initialize the UI and model.
    async fn init(
        _init: Self::Init,
        window: Self::Root,
        sender: AsyncComponentSender<Self>,
    ) -> AsyncComponentParts<Self> {
        let model = App {
            state: AuthState::LoggedOut(Default::default())
        };

        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.login_box.set_orientation(gtk::Orientation::Vertical);
        widgets.login_box.append(&widgets.login_label);
        widgets.login_box.append(&widgets.login_button);

        widgets.login_label.set_text("You need to authorize first.");

        widgets.login_button.set_label("Pretend to authorize");
        widgets.login_button.set_tooltip("(check MICROPUB_URI and MICROPUB_TOKEN environment variables)");
        widgets.login_button.connect_clicked(glib::clone!(
            #[strong] sender,
            move |_button| sender.input(Self::Input::Authorize)
        ));

        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(_entry_buffer) => {
                widgets.root.set_title(Some("Sign in with your website"));
                widgets.toolbar_view.set_content(None::<&gtk::Box>);
                widgets.top_bar_btn.set_visible(false);
                widgets.toolbar_view.set_content(Some(&widgets.login_box));
            },
            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 => {
                self.state = AuthState::LoggedIn {
                    post_editor: components::PostEditor::builder()
                        .launch(None)
                        .forward(_sender.clone().input_sender(), Self::Input::PostEditor),
                    micropub: std::env::var("MICROPUB_TOKEN").ok()
                        .and_then(|token| {
                            Some((token, glib::Uri::parse(
                                &std::env::var("MICROPUB_URI").ok()?,
                                glib::UriFlags::NONE
                            ).ok()?))
                        })
                        .map(|(token, uri)| crate::micropub::Client::new(
                            uri, token
                        ))
                        .unwrap(),
                    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;
                }
            },
        }
    }
}