support for saving/bookmarking comments

This commit is contained in:
Bnyro 2023-07-11 09:49:44 +02:00
parent 572b1992b1
commit 900928be71
2 changed files with 38 additions and 2 deletions

View File

@ -1,5 +1,7 @@
use lemmy_api_common::{ use lemmy_api_common::{
comment::{CommentResponse, CreateComment, CreateCommentLike, DeleteComment, EditComment}, comment::{
CommentResponse, CreateComment, CreateCommentLike, DeleteComment, EditComment, SaveComment,
},
lemmy_db_schema::newtypes::{CommentId, PostId}, lemmy_db_schema::newtypes::{CommentId, PostId},
}; };
@ -30,7 +32,10 @@ pub fn like_comment(comment_id: CommentId, score: i16) -> Result<CommentResponse
super::post("/comment/like", &params) super::post("/comment/like", &params)
} }
pub fn edit_comment(body: String, comment_id: CommentId) -> Result<CommentResponse, reqwest::Error> { pub fn edit_comment(
body: String,
comment_id: CommentId,
) -> Result<CommentResponse, reqwest::Error> {
let params = EditComment { let params = EditComment {
content: Some(body), content: Some(body),
comment_id, comment_id,
@ -48,3 +53,12 @@ pub fn delete_comment(comment_id: CommentId) -> Result<CommentResponse, reqwest:
}; };
super::post("/comment/delete", &params) super::post("/comment/delete", &params)
} }
pub fn save_comment(comment_id: CommentId, save: bool) -> Result<CommentResponse, reqwest::Error> {
let params = SaveComment {
auth: settings::get_current_account().jwt.unwrap(),
comment_id,
save,
};
super::put("/comment/save", &params)
}

View File

@ -28,6 +28,7 @@ pub struct CommentRow {
pub enum CommentRowMsg { pub enum CommentRowMsg {
OpenPerson, OpenPerson,
DeleteComment, DeleteComment,
ToggleSaved,
OpenEditor(bool), OpenEditor(bool),
EditCommentRequest(EditorData), EditCommentRequest(EditorData),
CreateCommentRequest(EditorData), CreateCommentRequest(EditorData),
@ -97,6 +98,15 @@ impl FactoryComponent for CommentRow {
set_visible: settings::get_current_account().jwt.is_some(), 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 { gtk::Button {
set_icon_name: "document-edit", set_icon_name: "document-edit",
connect_clicked => CommentRowMsg::OpenEditor(false), 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),
}
});
}
} }
} }
} }