diff --git a/src/api/comment.rs b/src/api/comment.rs index 13e2ca1..5d18712 100644 --- a/src/api/comment.rs +++ b/src/api/comment.rs @@ -6,14 +6,14 @@ use lemmy_api_common::{ use crate::settings; pub fn create_comment( - post_id: i32, + post_id: PostId, content: String, - parent_id: Option, + parent_id: Option, ) -> Result { let params = CreateComment { - post_id: PostId(post_id), + post_id, content, - parent_id: parent_id.map(CommentId), + parent_id, auth: settings::get_current_account().jwt.unwrap(), ..Default::default() }; @@ -30,10 +30,10 @@ pub fn like_comment(comment_id: CommentId, score: i16) -> Result Result { +pub fn edit_comment(body: String, comment_id: CommentId) -> Result { let params = EditComment { content: Some(body), - comment_id: CommentId(comment_id), + comment_id, auth: settings::get_current_account().jwt.unwrap(), ..Default::default() }; diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index 70f4e2f..5d24b9b 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -29,8 +29,9 @@ pub struct CommentRow { pub enum CommentRowMsg { OpenPerson, DeleteComment, - OpenEditCommentDialog, + OpenEditor(bool), EditCommentRequest(EditorData), + CreateCommentRequest(EditorData), UpdateComment(CommentView), } @@ -87,14 +88,24 @@ impl FactoryComponent for CommentRow { gtk::Box { set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + #[local_ref] voting_row -> gtk::Box {}, + if settings::get_current_account().jwt.is_some() { + gtk::Button { + set_icon_name: "mail-replied", + connect_clicked => CommentRowMsg::OpenEditor(true), + } + } else { + gtk::Box {} + }, + if self.comment.creator.id.0 == settings::get_current_account().id { gtk::Button { set_icon_name: "document-edit", - connect_clicked => CommentRowMsg::OpenEditCommentDialog, - set_margin_start: 5, + connect_clicked => CommentRowMsg::OpenEditor(false), } } else { gtk::Box {} @@ -104,11 +115,10 @@ impl FactoryComponent for CommentRow { gtk::Button { set_icon_name: "edit-delete", connect_clicked => CommentRowMsg::DeleteComment, - set_margin_start: 10, } } else { gtk::Box {} - } + }, }, gtk::Separator {} @@ -133,7 +143,7 @@ impl FactoryComponent for CommentRow { sender.input_sender(), |msg| match msg { EditorOutput::EditRequest(data, _) => CommentRowMsg::EditCommentRequest(data), - _ => unreachable!(), + EditorOutput::CreateRequest(data, _) => CommentRowMsg::CreateCommentRequest(data), }, ); @@ -174,24 +184,28 @@ impl FactoryComponent for CommentRow { )); }); } - CommentRowMsg::OpenEditCommentDialog => { - let data = EditorData { - name: String::from(""), - body: self.comment.comment.content.clone(), - url: None, - id: Some(self.comment.comment.id.0), + CommentRowMsg::OpenEditor(is_new) => { + let data = if is_new { + EditorData::default() + } else { + EditorData { + name: String::from(""), + body: self.comment.comment.content.clone(), + url: None, + } }; let sender = self.comment_editor_dialog.sender(); sender.emit(DialogMsg::UpdateData(data)); - sender.emit(DialogMsg::UpdateType(EditorType::Comment, false)); + sender.emit(DialogMsg::UpdateType(EditorType::Comment, is_new)); sender.emit(DialogMsg::Show); } CommentRowMsg::UpdateComment(comment) => { self.comment = comment; } CommentRowMsg::EditCommentRequest(data) => { + let id = self.comment.comment.id; std::thread::spawn(move || { - let message = match api::comment::edit_comment(data.body, data.id.unwrap()) { + let message = match api::comment::edit_comment(data.body, id) { Ok(comment) => Some(CommentRowMsg::UpdateComment(comment.comment_view)), Err(err) => { println!("{}", err.to_string()); @@ -203,6 +217,19 @@ impl FactoryComponent for CommentRow { }; }); } + CommentRowMsg::CreateCommentRequest(data) => { + let post_id = self.comment.comment.post_id; + let parent_id = self.comment.comment.id; + std::thread::spawn(move || { + match api::comment::create_comment(post_id, data.body, Some(parent_id)) + { + Ok(_comment) => {} + Err(err) => { + println!("{}", err.to_string()); + } + }; + }); + } } } } diff --git a/src/components/post_page.rs b/src/components/post_page.rs index 9079c16..c9b7b72 100644 --- a/src/components/post_page.rs +++ b/src/components/post_page.rs @@ -303,7 +303,7 @@ impl SimpleComponent for PostPage { self.comments.guard().push_front(comment); } PostInput::CreateCommentRequest(post) => { - let id = self.info.post_view.post.id.0; + let id = self.info.post_view.post.id; std::thread::spawn(move || { let message = match api::comment::create_comment(id, post.body, None) { Ok(comment) => Some(PostInput::CreatedComment(comment.comment_view)), @@ -340,8 +340,7 @@ impl SimpleComponent for PostPage { .body .clone() .unwrap_or(String::from("")), - url: reqwest::Url::parse(&url).ok(), - id: None, + url: reqwest::Url::parse(&url).ok() }; let sender = self.create_comment_dialog.sender(); sender.emit(DialogMsg::UpdateData(data)); diff --git a/src/dialogs/editor.rs b/src/dialogs/editor.rs index a9012e7..986413b 100644 --- a/src/dialogs/editor.rs +++ b/src/dialogs/editor.rs @@ -10,8 +10,7 @@ use crate::api; pub struct EditorData { pub name: String, pub body: String, - pub url: Option, - pub id: Option, + pub url: Option } pub struct EditorDialog { @@ -21,8 +20,6 @@ pub struct EditorDialog { name_buffer: gtk::EntryBuffer, url_buffer: gtk::EntryBuffer, body_buffer: gtk::TextBuffer, - // Optional field to temporarily store the post or comment id - id: Option, window: gtk::Window, } @@ -172,7 +169,6 @@ impl SimpleComponent for EditorDialog { name_buffer, url_buffer, body_buffer, - id: None, window, }; let widgets = view_output!(); @@ -194,12 +190,7 @@ impl SimpleComponent for EditorDialog { let (start, end) = &self.body_buffer.bounds(); let body = self.body_buffer.text(start, end, true).to_string(); let url = reqwest::Url::parse(&url).ok(); - let post = EditorData { - name, - body, - url, - id: self.id, - }; + let post = EditorData { name, body, url }; let message = match self.is_new { true => EditorOutput::CreateRequest(post, self.type_), false => EditorOutput::EditRequest(post, self.type_),