summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs71
1 files changed, 65 insertions, 6 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 5ee80c2..f5cb19e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,6 +5,8 @@ use gtk::GridLayoutChild;
 use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentParts}, AsyncComponentSender, RelmWidgetExt};
 
 mod widgets;
+pub mod micropub;
+pub mod util;
 pub const APPLICATION_ID: &str = "xyz.vikanezrimaya.kittybox.Bowl";
 
 #[tracker::track]
@@ -13,6 +15,7 @@ pub struct PostComposerModel {
     /// Busy guard for generating the summary using an LLM.
     /// Makes the summary field read-only and blocks the Smart Summary button.
     #[no_eq] ai_summary_busy_guard: Option<gtk::gio::ApplicationBusyGuard>,
+    #[no_eq] submit_busy_guard: Option<gtk::gio::ApplicationBusyGuard>,
 
     #[do_not_track] name_buffer: gtk::EntryBuffer,
     #[do_not_track] summary_buffer: gtk::EntryBuffer,
@@ -20,6 +23,8 @@ pub struct PostComposerModel {
 
     #[do_not_track] wide_layout: gtk::GridLayout,
     #[do_not_track] narrow_layout: gtk::BoxLayout,
+
+    #[do_not_track] micropub: Arc<micropub::Client>,
 }
 
 impl PostComposerModel {
@@ -59,7 +64,7 @@ impl AsyncComponent for PostComposerModel {
     /// 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 = ();
+    type Init = micropub::Client;
     /// The type of the command outputs that this component can receive.
     type CommandOutput = PostComposerCommandOutput;
  
@@ -74,6 +79,8 @@ impl AsyncComponent for PostComposerModel {
                         send_button = gtk::Button {
                             set_label: "Post",
                             connect_clicked => Self::Input::Submit,
+                            #[track = "model.changed(Self::submit_busy_guard())"]
+                            set_sensitive: model.submit_busy_guard.is_none(),
                         },
 
                         bar = adw::HeaderBar::new() {
@@ -106,6 +113,8 @@ impl AsyncComponent for PostComposerModel {
                         gtk::Entry {
                             set_hexpand: true,
                             set_buffer: &model.name_buffer,
+                            #[track = "model.changed(Self::submit_busy_guard())"]
+                            set_sensitive: model.submit_busy_guard.is_none(),
                         },
 
                         #[name = "summary_label"]
@@ -125,15 +134,15 @@ impl AsyncComponent for PostComposerModel {
                             gtk::Entry {
                                 set_hexpand: true,
                                 set_buffer: &model.summary_buffer,
-                                #[track = "model.changed(Self::ai_summary_busy_guard())"]
-                                set_sensitive: model.ai_summary_busy_guard.is_none(),
+                                #[track = "model.changed(Self::ai_summary_busy_guard() | Self::submit_busy_guard())"]
+                                set_sensitive: model.ai_summary_busy_guard.is_none() && model.submit_busy_guard.is_none(),
                             },
                             #[name = "ai_summary_button"]
                             gtk::Button {
                                 connect_clicked => Self::Input::AiGenSummaryBegin,
 
-                                #[track = "model.changed(Self::ai_summary_busy_guard())"]
-                                set_sensitive: model.ai_summary_busy_guard.is_none(),
+                                #[track = "model.changed(Self::ai_summary_busy_guard() | Self::submit_busy_guard())"]
+                                set_sensitive: model.ai_summary_busy_guard.is_none() && model.submit_busy_guard.is_none(),
                                 set_tooltip: "Smart Summary\nAsk a language model to summarize the content of your post in a single sentence.",
 
                                 gtk::Stack {
@@ -191,6 +200,9 @@ impl AsyncComponent for PostComposerModel {
                             set_right_margin: 8,
                             set_top_margin: 8,
                             set_bottom_margin: 8,
+
+                            #[track = "model.changed(Self::submit_busy_guard())"]
+                            set_sensitive: model.submit_busy_guard.is_none(),
                         },
                     },
 
@@ -221,6 +233,7 @@ impl AsyncComponent for PostComposerModel {
     ) -> AsyncComponentParts<Self> {
         let model = PostComposerModel {
             ai_summary_busy_guard: None,
+            submit_busy_guard: None,
 
             name_buffer: gtk::EntryBuffer::default(),
             summary_buffer: gtk::EntryBuffer::default(),
@@ -229,6 +242,8 @@ impl AsyncComponent for PostComposerModel {
             wide_layout: gtk::GridLayout::new(),
             narrow_layout: gtk::BoxLayout::new(gtk::Orientation::Vertical),
 
+            micropub: Arc::new(init),
+
             tracker: Default::default()
         };
         let widgets = view_output!();        
@@ -293,7 +308,51 @@ impl AsyncComponent for PostComposerModel {
                 self.summary_buffer.insert_text(self.summary_buffer.length(), text);
             },
             PostComposerInput::Submit => {
-                log::warn!("Submitting posts is not yet implemented.");
+                self.set_submit_busy_guard(
+                    Some(relm4::main_adw_application().mark_busy())
+                );
+                // Update view to lock the interface up
+                self.update_view(widgets, sender.clone());
+                self.reset();
+
+                use microformats::types::{Item, Class, KnownClass, PropertyValue};
+                let mut mf2 = Item::new(vec![Class::Known(KnownClass::Entry)]);
+                if self.name_buffer.length() > 0 {
+                    let proplist = mf2.properties.entry("name".to_owned()).or_default();
+                    proplist.push(PropertyValue::Plain(self.name_buffer.text().into()));
+                }
+                if self.summary_buffer.length() > 0 {
+                    let proplist = mf2.properties.entry("summary".to_owned()).or_default();
+                    proplist.push(PropertyValue::Plain(self.summary_buffer.text().into()));
+                }
+
+                // TODO: tags
+
+                {
+                    let proplist = mf2.properties.entry("content".to_owned()).or_default();
+                    proplist.push(PropertyValue::Plain(self.content_buffer.text(
+                        &self.content_buffer.start_iter(),
+                        &self.content_buffer.end_iter(),
+                        false
+                    ).into()));
+                }
+
+                log::warn!("sending post: {:?}", &mf2);
+                match self.micropub.send_post(mf2).await {
+                    Ok(location) => {
+                        self.name_buffer.set_text("");
+                        self.summary_buffer.set_text("");
+                        // TODO: tags
+                        self.content_buffer.set_text("");
+                        // TODO: display toast!
+                        log::warn!("post submitted: {}", location);
+                    },
+                    Err(err) => {
+                        // TODO: display error dialog
+                        log::warn!("error sending the post: {}", err);
+                    }
+                }
+                self.set_submit_busy_guard(None);
             },
         }