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;
pub fn create_comment(
post_id: i32,
post_id: PostId,
content: String,
parent_id: Option<i32>,
parent_id: Option<CommentId>,
) -> Result<CommentResponse, reqwest::Error> {
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<CommentResponse
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 {
content: Some(body),
comment_id: CommentId(comment_id),
comment_id,
auth: settings::get_current_account().jwt.unwrap(),
..Default::default()
};

View File

@ -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 {
CommentRowMsg::OpenEditor(is_new) => {
let data = if is_new {
EditorData::default()
} else {
EditorData {
name: String::from(""),
body: self.comment.comment.content.clone(),
url: None,
id: Some(self.comment.comment.id.0),
}
};
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());
}
};
});
}
}
}
}

View File

@ -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));

View File

@ -10,8 +10,7 @@ use crate::api;
pub struct EditorData {
pub name: String,
pub body: String,
pub url: Option<reqwest::Url>,
pub id: Option<i32>,
pub url: Option<reqwest::Url>
}
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<i32>,
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_),