From 900928be7166bd13aba6ee40e3dc3f3ab83e5caf Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 11 Jul 2023 09:49:44 +0200 Subject: [PATCH] support for saving/bookmarking comments --- src/api/comment.rs | 18 ++++++++++++++++-- src/components/comment_row.rs | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/api/comment.rs b/src/api/comment.rs index 5d18712..276990a 100644 --- a/src/api/comment.rs +++ b/src/api/comment.rs @@ -1,5 +1,7 @@ use lemmy_api_common::{ - comment::{CommentResponse, CreateComment, CreateCommentLike, DeleteComment, EditComment}, + comment::{ + CommentResponse, CreateComment, CreateCommentLike, DeleteComment, EditComment, SaveComment, + }, lemmy_db_schema::newtypes::{CommentId, PostId}, }; @@ -30,7 +32,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, @@ -48,3 +53,12 @@ pub fn delete_comment(comment_id: CommentId) -> Result Result { + let params = SaveComment { + auth: settings::get_current_account().jwt.unwrap(), + comment_id, + save, + }; + super::put("/comment/save", ¶ms) +} diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index 4d19394..a68e197 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -28,6 +28,7 @@ pub struct CommentRow { pub enum CommentRowMsg { OpenPerson, DeleteComment, + ToggleSaved, OpenEditor(bool), EditCommentRequest(EditorData), CreateCommentRequest(EditorData), @@ -97,6 +98,15 @@ impl FactoryComponent for CommentRow { set_visible: settings::get_current_account().jwt.is_some(), }, + gtk::ToggleButton { + set_icon_name: "bookmark-new", + set_margin_start: 5, + connect_clicked => CommentRowMsg::ToggleSaved, + set_visible: settings::get_current_account().jwt.is_some(), + #[watch] + set_active: self.comment.saved, + }, + gtk::Button { set_icon_name: "document-edit", connect_clicked => CommentRowMsg::OpenEditor(false), @@ -214,6 +224,18 @@ impl FactoryComponent for CommentRow { }; }); } + CommentRowMsg::ToggleSaved => { + let comment_id = self.comment.comment.id; + let new_state = !self.comment.saved; + std::thread::spawn(move || { + match api::comment::save_comment(comment_id, new_state) { + Ok(comment) => { + sender.input(CommentRowMsg::UpdateComment(comment.comment_view)) + } + Err(err) => println!("{}", err), + } + }); + } } } }