Support for replying to comments

This commit is contained in:
Bnyro 2023-07-01 17:19:28 +02:00
parent 19fa49ecf7
commit 8fe42f3838
4 changed files with 51 additions and 34 deletions

View File

@ -6,14 +6,14 @@ use lemmy_api_common::{
use crate::settings; use crate::settings;
pub fn create_comment( pub fn create_comment(
post_id: i32, post_id: PostId,
content: String, content: String,
parent_id: Option<i32>, parent_id: Option<CommentId>,
) -> Result<CommentResponse, reqwest::Error> { ) -> Result<CommentResponse, reqwest::Error> {
let params = CreateComment { let params = CreateComment {
post_id: PostId(post_id), post_id,
content, content,
parent_id: parent_id.map(CommentId), parent_id,
auth: settings::get_current_account().jwt.unwrap(), auth: settings::get_current_account().jwt.unwrap(),
..Default::default() ..Default::default()
}; };
@ -30,10 +30,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: i32) -> 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: CommentId(comment_id), comment_id,
auth: settings::get_current_account().jwt.unwrap(), auth: settings::get_current_account().jwt.unwrap(),
..Default::default() ..Default::default()
}; };

View File

@ -29,8 +29,9 @@ pub struct CommentRow {
pub enum CommentRowMsg { pub enum CommentRowMsg {
OpenPerson, OpenPerson,
DeleteComment, DeleteComment,
OpenEditCommentDialog, OpenEditor(bool),
EditCommentRequest(EditorData), EditCommentRequest(EditorData),
CreateCommentRequest(EditorData),
UpdateComment(CommentView), UpdateComment(CommentView),
} }
@ -87,14 +88,24 @@ impl FactoryComponent for CommentRow {
gtk::Box { gtk::Box {
set_orientation: gtk::Orientation::Horizontal, set_orientation: gtk::Orientation::Horizontal,
set_spacing: 10,
#[local_ref] #[local_ref]
voting_row -> gtk::Box {}, 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 { if self.comment.creator.id.0 == settings::get_current_account().id {
gtk::Button { gtk::Button {
set_icon_name: "document-edit", set_icon_name: "document-edit",
connect_clicked => CommentRowMsg::OpenEditCommentDialog, connect_clicked => CommentRowMsg::OpenEditor(false),
set_margin_start: 5,
} }
} else { } else {
gtk::Box {} gtk::Box {}
@ -104,11 +115,10 @@ impl FactoryComponent for CommentRow {
gtk::Button { gtk::Button {
set_icon_name: "edit-delete", set_icon_name: "edit-delete",
connect_clicked => CommentRowMsg::DeleteComment, connect_clicked => CommentRowMsg::DeleteComment,
set_margin_start: 10,
} }
} else { } else {
gtk::Box {} gtk::Box {}
} },
}, },
gtk::Separator {} gtk::Separator {}
@ -133,7 +143,7 @@ impl FactoryComponent for CommentRow {
sender.input_sender(), sender.input_sender(),
|msg| match msg { |msg| match msg {
EditorOutput::EditRequest(data, _) => CommentRowMsg::EditCommentRequest(data), EditorOutput::EditRequest(data, _) => CommentRowMsg::EditCommentRequest(data),
_ => unreachable!(), EditorOutput::CreateRequest(data, _) => CommentRowMsg::CreateCommentRequest(data),
}, },
); );
@ -174,24 +184,28 @@ impl FactoryComponent for CommentRow {
)); ));
}); });
} }
CommentRowMsg::OpenEditCommentDialog => { CommentRowMsg::OpenEditor(is_new) => {
let data = EditorData { let data = if is_new {
name: String::from(""), EditorData::default()
body: self.comment.comment.content.clone(), } else {
url: None, EditorData {
id: Some(self.comment.comment.id.0), name: String::from(""),
body: self.comment.comment.content.clone(),
url: None,
}
}; };
let sender = self.comment_editor_dialog.sender(); let sender = self.comment_editor_dialog.sender();
sender.emit(DialogMsg::UpdateData(data)); sender.emit(DialogMsg::UpdateData(data));
sender.emit(DialogMsg::UpdateType(EditorType::Comment, false)); sender.emit(DialogMsg::UpdateType(EditorType::Comment, is_new));
sender.emit(DialogMsg::Show); sender.emit(DialogMsg::Show);
} }
CommentRowMsg::UpdateComment(comment) => { CommentRowMsg::UpdateComment(comment) => {
self.comment = comment; self.comment = comment;
} }
CommentRowMsg::EditCommentRequest(data) => { CommentRowMsg::EditCommentRequest(data) => {
let id = self.comment.comment.id;
std::thread::spawn(move || { 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)), Ok(comment) => Some(CommentRowMsg::UpdateComment(comment.comment_view)),
Err(err) => { Err(err) => {
println!("{}", err.to_string()); 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());
}
};
});
}
} }
} }
} }

View File

@ -303,7 +303,7 @@ impl SimpleComponent for PostPage {
self.comments.guard().push_front(comment); self.comments.guard().push_front(comment);
} }
PostInput::CreateCommentRequest(post) => { PostInput::CreateCommentRequest(post) => {
let id = self.info.post_view.post.id.0; let id = self.info.post_view.post.id;
std::thread::spawn(move || { std::thread::spawn(move || {
let message = match api::comment::create_comment(id, post.body, None) { let message = match api::comment::create_comment(id, post.body, None) {
Ok(comment) => Some(PostInput::CreatedComment(comment.comment_view)), Ok(comment) => Some(PostInput::CreatedComment(comment.comment_view)),
@ -340,8 +340,7 @@ impl SimpleComponent for PostPage {
.body .body
.clone() .clone()
.unwrap_or(String::from("")), .unwrap_or(String::from("")),
url: reqwest::Url::parse(&url).ok(), url: reqwest::Url::parse(&url).ok()
id: None,
}; };
let sender = self.create_comment_dialog.sender(); let sender = self.create_comment_dialog.sender();
sender.emit(DialogMsg::UpdateData(data)); sender.emit(DialogMsg::UpdateData(data));

View File

@ -10,8 +10,7 @@ use crate::api;
pub struct EditorData { pub struct EditorData {
pub name: String, pub name: String,
pub body: String, pub body: String,
pub url: Option<reqwest::Url>, pub url: Option<reqwest::Url>
pub id: Option<i32>,
} }
pub struct EditorDialog { pub struct EditorDialog {
@ -21,8 +20,6 @@ pub struct EditorDialog {
name_buffer: gtk::EntryBuffer, name_buffer: gtk::EntryBuffer,
url_buffer: gtk::EntryBuffer, url_buffer: gtk::EntryBuffer,
body_buffer: gtk::TextBuffer, body_buffer: gtk::TextBuffer,
// Optional field to temporarily store the post or comment id
id: Option<i32>,
window: gtk::Window, window: gtk::Window,
} }
@ -172,7 +169,6 @@ impl SimpleComponent for EditorDialog {
name_buffer, name_buffer,
url_buffer, url_buffer,
body_buffer, body_buffer,
id: None,
window, window,
}; };
let widgets = view_output!(); let widgets = view_output!();
@ -194,12 +190,7 @@ impl SimpleComponent for EditorDialog {
let (start, end) = &self.body_buffer.bounds(); let (start, end) = &self.body_buffer.bounds();
let body = self.body_buffer.text(start, end, true).to_string(); let body = self.body_buffer.text(start, end, true).to_string();
let url = reqwest::Url::parse(&url).ok(); let url = reqwest::Url::parse(&url).ok();
let post = EditorData { let post = EditorData { name, body, url };
name,
body,
url,
id: self.id,
};
let message = match self.is_new { let message = match self.is_new {
true => EditorOutput::CreateRequest(post, self.type_), true => EditorOutput::CreateRequest(post, self.type_),
false => EditorOutput::EditRequest(post, self.type_), false => EditorOutput::EditRequest(post, self.type_),