THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Browse Source

sub-comment: Refactor Sub-comment related codes

tags/v1.11.0
Suwon Chae 1 year ago
parent
commit
7c74568027
6 changed files with 95 additions and 28 deletions
  1. +21
    -9
      app/controllers/BoardApp.java
  2. +20
    -9
      app/controllers/IssueApp.java
  3. +3
    -0
      app/models/Comment.java
  4. +22
    -0
      app/models/IssueComment.java
  5. +22
    -0
      app/models/PostingComment.java
  6. +7
    -10
      app/views/common/childComments.scala.html

+ 21
- 9
app/controllers/BoardApp.java View File

@@ -417,20 +417,32 @@ public class BoardApp extends AbstractPostingApp {
comment.setParentComment(PostingComment.find.byId(Long.valueOf(comment.parentCommentId)));
}

if(posting.comments.size() == 0) {
User user = User.find.byId(posting.authorId);
comment.previousContents = getPrevious("Original issue", posting.body, posting.updatedDate, user.loginId);
} else {
Comment previousComment = posting.comments.get(posting.comments.size() - 1);
User user = User.find.byId(previousComment.authorId);
comment.previousContents = getPrevious("Previous comment", previousComment.contents, previousComment.createdDate, user.loginId);
}
AddPreviousContent(posting, comment);

Comment savedComment = saveComment(project, posting, comment);

return redirect(RouteUtil.getUrl(savedComment));
}

private static void AddPreviousContent(Posting posting, PostingComment comment) {
if(posting.numOfComments == 0) {
comment.previousContents = getPrevious("Original posting", posting.body, posting.updatedDate, posting.authorLoginId);
} else {
Comment previousComment;
if (comment.parentCommentId != null) {
List<PostingComment> siblingComments = comment.getSiblingComments();
if (siblingComments.size() > 0) {
previousComment = siblingComments.get(siblingComments.size() - 1);
} else {
previousComment = comment.getParentComment();
}
} else {
previousComment = posting.comments.get(posting.comments.size() - 1);
}
comment.previousContents = getPrevious("Previous comment", previousComment.contents, previousComment.createdDate, previousComment.authorLoginId);
}
}

private static String getPrevious(String templateTitle, String contents, Date updatedDate, String authorLoginId) {
return "\n\n<br />\n\n--- " + templateTitle + " from @" + authorLoginId + " " + getOptionalShortDate(updatedDate) + " ---\n\n<br />\n\n" + contents;
}
@@ -442,7 +454,7 @@ public class BoardApp extends AbstractPostingApp {

private static Comment saveComment(Project project, Posting posting, PostingComment comment) {
Comment savedComment;
PostingComment existingComment = PostingComment.find.where().eq("id", comment.id).findUnique();
PostingComment existingComment = (PostingComment)PostingComment.find.where().eq("id", comment.id).findUnique();
if (existingComment != null) {
existingComment.contents = comment.contents;
savedComment = saveComment(existingComment, getContainerUpdater(posting, comment));


+ 20
- 9
app/controllers/IssueApp.java View File

@@ -876,7 +876,6 @@ public class IssueApp extends AbstractPostingApp {
public static Result newComment(String ownerName, String projectName, Long number) throws IOException {
Project project = Project.findByOwnerAndProjectName(ownerName, projectName);
final Issue issue = Issue.findByNumber(project, number);
Call redirectTo = routes.IssueApp.issue(project.owner, project.name, number);
Form<IssueComment> commentForm = new Form<>(IssueComment.class).bindFromRequest();

if (!AccessControl.isResourceCreatable(
@@ -900,14 +899,7 @@ public class IssueApp extends AbstractPostingApp {
comment.setParentComment(IssueComment.find.byId(Long.valueOf(comment.parentCommentId)));
}

if(issue.comments.size() == 0) {
User user = User.find.byId(issue.authorId);
comment.previousContents = getPrevious("Original issue", issue.body, issue.updatedDate, user.loginId);
} else {
Comment previousComment = issue.comments.get(issue.comments.size() - 1);
User user = User.find.byId(previousComment.authorId);
comment.previousContents = getPrevious("Previous comment", previousComment.contents, previousComment.createdDate, user.loginId);
}
AddPreviousContent(issue, comment);

Comment savedComment = saveComment(project, issue, comment);

@@ -924,6 +916,25 @@ public class IssueApp extends AbstractPostingApp {
return redirect(RouteUtil.getUrl(savedComment));
}

private static void AddPreviousContent(Issue issue, IssueComment comment) {
if(issue.numOfComments == 0) {
comment.previousContents = getPrevious("Original issue", issue.body, issue.updatedDate, issue.authorLoginId);
} else {
Comment previousComment;
if (comment.parentCommentId != null) {
List<IssueComment> siblingComments = comment.getSiblingComments();
if (siblingComments.size() > 0) {
previousComment = siblingComments.get(siblingComments.size() - 1);
} else {
previousComment = comment.getParentComment();
}
} else {
previousComment = issue.comments.get(issue.comments.size() - 1);
}
comment.previousContents = getPrevious("Previous comment", previousComment.contents, previousComment.createdDate, previousComment.authorLoginId);
}
}

private static String getPrevious(String templateTitle, String contents, Date updatedDate, String authorLoginId) {
return "\n\n<br />\n\n--- " + templateTitle + " from @" + authorLoginId + " " + getOptionalShortDate(updatedDate) + " ---\n\n<br />\n\n" + contents;
}


+ 3
- 0
app/models/Comment.java View File

@@ -22,6 +22,7 @@ import javax.annotation.Nonnull;
import javax.persistence.*;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@MappedSuperclass
abstract public class Comment extends Model implements TimelineItem, ResourceConvertible {
@@ -112,6 +113,8 @@ abstract public class Comment extends Model implements TimelineItem, ResourceCon

abstract public Comment getParentComment();
abstract public void setParentComment(Comment comment);
abstract public List<? extends Comment> getSiblingComments();
abstract public List<? extends Comment> getChildComments();

@Override
public boolean equals(Object obj) {


+ 22
- 0
app/models/IssueComment.java View File

@@ -24,7 +24,9 @@ import models.enumeration.ResourceType;
import models.resource.Resource;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@@ -69,6 +71,26 @@ public class IssueComment extends Comment {
this.parentComment = (IssueComment)comment;
}

@Override
public List<IssueComment> getSiblingComments() {
if (parentComment == null) {
return null;
}

List<IssueComment> comments = find.where()
.eq("parentComment.id", parentComment.id)
.findList();
return comments;
}

@Override
public List<IssueComment> getChildComments() {
List<IssueComment> comments = find.where()
.eq("parentComment.id", id)
.findList();
return comments;
}

/**
* @see Comment#asResource()
*/


+ 22
- 0
app/models/PostingComment.java View File

@@ -13,6 +13,8 @@ import models.resource.Resource;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import java.util.ArrayList;
import java.util.List;

@Entity
public class PostingComment extends Comment {
@@ -48,6 +50,26 @@ public class PostingComment extends Comment {
this.parentComment = (PostingComment)comment;
}

@Override
public List<PostingComment> getSiblingComments() {
if (parentComment == null) {
return null;
}

List<PostingComment> comments = find.where()
.eq("parentComment.id", parentComment.id)
.findList();
return comments;
}

@Override
public List<PostingComment> getChildComments() {
List<PostingComment> comments = find.where()
.eq("parentComment.id", id)
.findList();
return comments;
}

/**
* @see Comment#asResource()
*/


+ 7
- 10
app/views/common/childComments.scala.html View File

@@ -62,17 +62,14 @@
<div class="add-a-comment pull-right">@Messages("comment.oneline.comment.placeholder")</div>
<div class="subcomment-media-body">
<div class="child-comments">
@for(comment <- posting.getComments) {
@if(comment.getParentComment != null
&& comment.getParentComment.id == currentComment.id) {
<div class="one-line-comment">
<div class="contents">
@Html(Markdown.render(comment.contents, posting.project))
<span class="subcomment-author hide">
- @linkToUser(getAuthor(comment.authorId).loginId, getAuthor(comment.authorId).getPureNameOnly, false) @additionalConetents(comment)</span>
</div>
@for(comment <- currentComment.getChildComments) {
<div class="one-line-comment">
<div class="contents">
@Html(Markdown.render(comment.contents, posting.project))
<span class="subcomment-author hide">
- @linkToUser(getAuthor(comment.authorId).loginId, getAuthor(comment.authorId).getPureNameOnly, false) @additionalConetents(comment)</span>
</div>
}
</div>
}
</div>
@if(isResourceCreatable(UserApp.currentUser, posting.asResource(), resourceType)) {


Loading…
Cancel
Save