From: Dessalines Date: Fri, 29 Mar 2019 04:56:23 +0000 (-0700) Subject: Adding comment editing X-Git-Url: http://these/git/%22https:/image.com/static/gitweb.css?a=commitdiff_plain;h=1e12e03cc825237899d09b3bc8afbdb37fa7e507;p=lemmy.git Adding comment editing - Fixes #8 --- diff --git a/server/src/actions/comment.rs b/server/src/actions/comment.rs index 6a1a4671..089c384c 100644 --- a/server/src/actions/comment.rs +++ b/server/src/actions/comment.rs @@ -174,7 +174,7 @@ impl CommentView { post_id: comment.post_id, attributed_to: comment.attributed_to.to_owned(), published: comment.published, - updated: None, + updated: comment.updated, upvotes: upvotes, score: score, downvotes: downvotes, diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index cb2b619b..e8120206 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -6,12 +6,11 @@ use actix::prelude::*; use rand::{rngs::ThreadRng, Rng}; use std::collections::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; -use serde_json::{Result, Value}; +use serde_json::{Value}; use bcrypt::{verify}; use std::str::FromStr; -use std::{thread, time}; -use {Crud, Joinable, Likeable, establish_connection}; +use {Crud, Joinable, Likeable, establish_connection, naive_now}; use actions::community::*; use actions::user::*; use actions::post::*; @@ -20,7 +19,7 @@ use actions::comment::*; #[derive(EnumString,ToString,Debug)] pub enum UserOperation { - Login, Register, Logout, CreateCommunity, ListCommunities, CreatePost, GetPost, GetCommunity, CreateComment, CreateCommentLike, Join, Edit, Reply, Vote, Delete, NextPage, Sticky + Login, Register, Logout, CreateCommunity, ListCommunities, CreatePost, GetPost, GetCommunity, CreateComment, EditComment, CreateCommentLike, Join, Edit, Reply, Vote, Delete, NextPage, Sticky } @@ -178,6 +177,7 @@ pub struct GetCommunityResponse { pub struct CreateComment { content: String, parent_id: Option, + edit_id: Option, post_id: i32, auth: String } @@ -189,6 +189,21 @@ pub struct CreateCommentResponse { } +#[derive(Serialize, Deserialize)] +pub struct EditComment { + content: String, + parent_id: Option, + edit_id: i32, + post_id: i32, + auth: String +} + +#[derive(Serialize, Deserialize)] +pub struct EditCommentResponse { + op: String, + comment: CommentView +} + #[derive(Serialize, Deserialize)] pub struct CreateCommentLike { comment_id: i32, @@ -360,6 +375,10 @@ impl Handler for ChatServer { let create_comment: CreateComment = serde_json::from_str(&data.to_string()).unwrap(); create_comment.perform(self, msg.id) }, + UserOperation::EditComment => { + let edit_comment: EditComment = serde_json::from_str(&data.to_string()).unwrap(); + edit_comment.perform(self, msg.id) + }, UserOperation::CreateCommentLike => { let create_comment_like: CreateCommentLike = serde_json::from_str(&data.to_string()).unwrap(); create_comment_like.perform(self, msg.id) @@ -483,7 +502,9 @@ impl Perform for CreateCommunity { }; let user_id = claims.id; + let username = claims.username; let iss = claims.iss; + let fedi_user_id = format!("{}/{}", iss, username); let community_form = CommunityForm { name: self.name.to_owned(), @@ -499,7 +520,7 @@ impl Perform for CreateCommunity { let community_user_form = CommunityUserForm { community_id: inserted_community.id, - fedi_user_id: format!("{}/{}", iss, user_id) + fedi_user_id: fedi_user_id }; let inserted_community_user = match CommunityUser::join(&conn, &community_user_form) { @@ -558,15 +579,16 @@ impl Perform for CreatePost { }; let user_id = claims.id; + let username = claims.username; let iss = claims.iss; - + let fedi_user_id = format!("{}/{}", iss, username); let post_form = PostForm { name: self.name.to_owned(), url: self.url.to_owned(), body: self.body.to_owned(), community_id: self.community_id, - attributed_to: format!("{}/{}", iss, user_id), + attributed_to: fedi_user_id, updated: None }; @@ -603,9 +625,10 @@ impl Perform for GetPost { Some(auth) => { match Claims::decode(&auth) { Ok(claims) => { - let user_id = claims.claims.id; + let username = claims.claims.username; let iss = claims.claims.iss; - Some(format!("{}/{}", iss, user_id)) + let fedi_user_id = format!("{}/{}", iss, username); + Some(fedi_user_id) } Err(e) => None } @@ -692,8 +715,9 @@ impl Perform for CreateComment { }; let user_id = claims.id; + let username = claims.username; let iss = claims.iss; - let fedi_user_id = format!("{}/{}", iss, user_id); + let fedi_user_id = format!("{}/{}", iss, username); let comment_form = CommentForm { content: self.content.to_owned(), @@ -729,7 +753,6 @@ impl Perform for CreateComment { let comment_view = CommentView::from_comment(&inserted_comment, &likes, &Some(fedi_user_id)); - let mut comment_sent = comment_view.clone(); comment_sent.my_vote = None; @@ -741,7 +764,6 @@ impl Perform for CreateComment { ) .unwrap(); - let comment_sent_out = serde_json::to_string( &CreateCommentLikeResponse { op: self.op_type().to_string(), @@ -756,6 +778,75 @@ impl Perform for CreateComment { } } +impl Perform for EditComment { + fn op_type(&self) -> UserOperation { + UserOperation::EditComment + } + + fn perform(&self, chat: &mut ChatServer, addr: usize) -> String { + + let conn = establish_connection(); + + let claims = match Claims::decode(&self.auth) { + Ok(claims) => claims.claims, + Err(e) => { + return self.error("Not logged in."); + } + }; + + let user_id = claims.id; + let username = claims.username; + let iss = claims.iss; + let fedi_user_id = format!("{}/{}", iss, username); + + let comment_form = CommentForm { + content: self.content.to_owned(), + parent_id: self.parent_id, + post_id: self.post_id, + attributed_to: fedi_user_id.to_owned(), + updated: Some(naive_now()) + }; + + let updated_comment = match Comment::update(&conn, self.edit_id, &comment_form) { + Ok(comment) => comment, + Err(e) => { + return self.error("Couldn't update Comment"); + } + }; + + let likes = match CommentLike::read(&conn, self.edit_id) { + Ok(likes) => likes, + Err(e) => { + return self.error("Couldn't get likes"); + } + }; + + let comment_view = CommentView::from_comment(&updated_comment, &likes, &Some(fedi_user_id)); + + let mut comment_sent = comment_view.clone(); + comment_sent.my_vote = None; + + let comment_out = serde_json::to_string( + &CreateCommentResponse { + op: self.op_type().to_string(), + comment: comment_view + } + ) + .unwrap(); + + let comment_sent_out = serde_json::to_string( + &CreateCommentLikeResponse { + op: self.op_type().to_string(), + comment: comment_sent + } + ) + .unwrap(); + + chat.send_room_message(self.post_id, &comment_sent_out, addr); + + comment_out + } +} impl Perform for CreateCommentLike { fn op_type(&self) -> UserOperation { @@ -774,8 +865,9 @@ impl Perform for CreateCommentLike { }; let user_id = claims.id; + let username = claims.username; let iss = claims.iss; - let fedi_user_id = format!("{}/{}", iss, user_id); + let fedi_user_id = format!("{}/{}", iss, username); let like_form = CommentLikeForm { comment_id: self.comment_id, diff --git a/ui/src/components/post.tsx b/ui/src/components/post.tsx index 2a780cf7..c5c8a53f 100644 --- a/ui/src/components/post.tsx +++ b/ui/src/components/post.tsx @@ -91,7 +91,7 @@ export class Post extends Component { newComments() { return (
-

New Comments

+
New Comments
{this.state.comments.map(comment => )} @@ -102,7 +102,7 @@ export class Post extends Component { sidebar() { return (
-

Sidebar

+
Sidebar

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

); @@ -155,7 +155,14 @@ export class Post extends Component { let res: CommentResponse = msg; this.state.comments.unshift(res.comment); this.setState(this.state); - } else if (op == UserOperation.CreateCommentLike) { + } else if (op == UserOperation.EditComment) { + let res: CommentResponse = msg; + let found = this.state.comments.find(c => c.id == res.comment.id); + found.content = res.comment.content; + found.updated = res.comment.updated; + this.setState(this.state); + } + else if (op == UserOperation.CreateCommentLike) { let res: CreateCommentLikeResponse = msg; let found: Comment = this.state.comments.find(c => c.id === res.comment.id); found.score = res.comment.score; @@ -163,7 +170,6 @@ export class Post extends Component { found.downvotes = res.comment.downvotes; if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote; - console.log(res.comment.my_vote); this.setState(this.state); } @@ -198,6 +204,7 @@ export class CommentNodes extends Component { private emptyState: CommentNodeState = { - showReply: false + showReply: false, + showEdit: false } constructor(props, context) { @@ -246,15 +254,25 @@ export class CommentNode extends Component { -

{node.comment.content}

-
    -
  • - reply -
  • -
  • - link -
  • -
+ {this.state.showEdit && } + {!this.state.showEdit && +
+

{node.comment.content}

+
    +
  • + reply +
  • + {this.myComment && +
  • + edit +
  • + } +
  • + link +
  • +
+
+ }
{this.state.showReply && } {this.props.node.children && } @@ -262,8 +280,8 @@ export class CommentNode extends Component { ) } - private getScore(): number { - return (this.props.node.comment.upvotes - this.props.node.comment.downvotes) || 0; + private get myComment(): boolean { + return this.props.node.comment.attributed_to == UserService.Instance.fediUserId; } handleReplyClick(i: CommentNode, event) { @@ -271,11 +289,18 @@ export class CommentNode extends Component { i.setState(i.state); } + handleEditClick(i: CommentNode, event) { + i.state.showEdit = true; + i.setState(i.state); + } + handleReplyCancel(): any { this.state.showReply = false; + this.state.showEdit = false; this.setState(this.state); } + handleCommentLike(i: CommentNodeI, event) { let form: CommentLikeForm = { @@ -300,10 +325,12 @@ interface CommentFormProps { postId?: number; node?: CommentNodeI; onReplyCancel?(); + edit?: boolean; } interface CommentFormState { commentForm: CommentFormI; + buttonTitle: string; } export class CommentForm extends Component { @@ -312,27 +339,33 @@ export class CommentForm extends Component { commentForm: { auth: null, content: null, - post_id: null, - parent_id: null - } + post_id: this.props.node ? this.props.node.comment.post_id : this.props.postId + }, + buttonTitle: !this.props.node ? "Post" : this.props.edit ? "Edit" : "Reply" } constructor(props, context) { super(props, context); this.state = this.emptyState; + if (this.props.node) { - this.state.commentForm.post_id = this.props.node.comment.post_id; - this.state.commentForm.parent_id = this.props.node.comment.id; - } else { - this.state.commentForm.post_id = this.props.postId; - } + if (this.props.edit) { + this.state.commentForm.edit_id = this.props.node.comment.id; + this.state.commentForm.parent_id = this.props.node.comment.parent_id; + this.state.commentForm.content = this.props.node.comment.content; + } else { + // A reply gets a new parent id + this.state.commentForm.parent_id = this.props.node.comment.id; + } + } } + render() { return (
-
+