use of com.erudika.scoold.core.Comment in project scoold by Erudika.
the class QuestionController method getComments.
//get the comments for each answer and the question
private void getComments(List<Post> allPosts) {
Map<String, List<Comment>> allComments = new HashMap<String, List<Comment>>();
List<String> allCommentIds = new ArrayList<String>();
List<Post> forUpdate = new ArrayList<Post>(allPosts.size());
// get the comment ids of the first 5 comments for each post
for (Post post : allPosts) {
// not set => read comments if any and embed ids in post object
if (post.getCommentIds() == null) {
forUpdate.add(reloadFirstPageOfComments(post));
allComments.put(post.getId(), post.getComments());
} else {
// ids are set => add them to list for bulk read
allCommentIds.addAll(post.getCommentIds());
}
}
if (!allCommentIds.isEmpty()) {
// read all comments for all posts on page in bulk
for (ParaObject comment : pc.readAll(allCommentIds)) {
List<Comment> postComments = allComments.get(comment.getParentid());
if (postComments == null) {
allComments.put(comment.getParentid(), new ArrayList<Comment>());
}
allComments.get(comment.getParentid()).add((Comment) comment);
}
}
// embed comments in each post for use within the view
for (Post post : allPosts) {
List<Comment> cl = allComments.get(post.getId());
int clSize = (cl == null) ? 0 : cl.size();
if (post.getCommentIds().size() != clSize) {
logger.info("OPAA neshto stava.. {} {}", post.getCommentIds().size(), clSize);
forUpdate.add(reloadFirstPageOfComments(post));
clSize = post.getComments().size();
} else {
post.setComments(cl);
}
// hack to show the "more" button
post.getItemcount().setCount(clSize + 1);
}
if (!forUpdate.isEmpty()) {
pc.updateAll(allPosts);
}
}
use of com.erudika.scoold.core.Comment in project scoold by Erudika.
the class CommentController method createAjax.
@PostMapping
public String createAjax(@RequestParam String comment, @RequestParam String parentid, HttpServletRequest req, Model model) {
Profile authUser = utils.getAuthUser(req);
if (utils.canComment(authUser, req) && !StringUtils.isBlank(comment) && !StringUtils.isBlank(parentid)) {
Comment showComment = utils.populate(req, new Comment(), "comment");
showComment.setCreatorid(authUser.getId());
Map<String, String> error = utils.validate(showComment);
if (error.isEmpty()) {
showComment.setComment(comment);
showComment.setParentid(parentid);
showComment.setAuthorName(authUser.getName());
if (showComment.create() != null) {
long commentCount = authUser.getComments();
utils.addBadgeOnce(authUser, COMMENTATOR, commentCount >= COMMENTATOR_IFHAS);
authUser.setComments(commentCount + 1);
authUser.update();
model.addAttribute("showComment", showComment);
// send email to the author of parent post
Post parentPost = pc.read(parentid);
if (parentPost != null) {
parentPost.addCommentId(showComment.getId());
parentPost.update();
}
sendCommentNotification(parentPost, showComment, authUser);
}
}
}
return "comment";
}
use of com.erudika.scoold.core.Comment in project scoold by Erudika.
the class CommentController method getAjax.
@GetMapping(params = { Config._PARENTID, "getcomments" })
public String getAjax(@RequestParam String parentid, @RequestParam Boolean getcomments, @RequestParam(required = false, defaultValue = "1") Integer page, HttpServletRequest req, Model model) {
Post parent = pc.read(parentid);
if (parent != null) {
parent.getItemcount().setPage(page);
List<Comment> commentslist = pc.getChildren(parent, Utils.type(Comment.class), parent.getItemcount());
parent.setComments(commentslist);
model.addAttribute("showpost", parent);
model.addAttribute("itemcount", parent.getItemcount());
}
return "comment";
}
use of com.erudika.scoold.core.Comment in project scoold by Erudika.
the class VoteController method processVoteRequest.
boolean processVoteRequest(boolean isUpvote, String type, String id, HttpServletRequest req) {
if (StringUtils.isBlank(id) || StringUtils.isBlank(type)) {
return false;
}
ParaObject votable = pc.read(id);
Profile author = null;
Profile authUser = utils.getAuthUser(req);
boolean result = false;
boolean updateAuthUser = false;
boolean updateVoter = false;
if (votable == null || authUser == null) {
return false;
}
try {
author = pc.read(votable.getCreatorid());
Integer votes = votable.getVotes() != null ? votable.getVotes() : 0;
if (isUpvote && (result = pc.voteUp(votable, authUser.getId()))) {
votes++;
authUser.incrementUpvotes();
updateAuthUser = true;
int reward;
if (votable instanceof Post) {
Post p = (Post) votable;
if (p.isReply()) {
utils.addBadge(author, GOODANSWER, votes >= GOODANSWER_IFHAS, false);
reward = ANSWER_VOTEUP_REWARD_AUTHOR;
} else if (p.isQuestion()) {
utils.addBadge(author, GOODQUESTION, votes >= GOODQUESTION_IFHAS, false);
reward = QUESTION_VOTEUP_REWARD_AUTHOR;
} else {
reward = VOTEUP_REWARD_AUTHOR;
}
} else {
reward = VOTEUP_REWARD_AUTHOR;
}
if (author != null && reward > 0) {
author.addRep(reward);
updateVoter = true;
}
} else if (!isUpvote && (result = pc.voteDown(votable, authUser.getId()))) {
votes--;
authUser.incrementDownvotes();
updateAuthUser = true;
if (votable instanceof Comment && votes <= -5) {
//treat comment as offensive or spam - hide
((Comment) votable).setHidden(true);
} else if (votable instanceof Post && votes <= -5) {
Post p = (Post) votable;
//mark post for closing
Report rep = new Report();
rep.setParentid(id);
rep.setLink(p.getPostLink(false, false));
rep.setDescription(utils.getLang(req).get("posts.forclosing"));
rep.setSubType(Report.ReportType.OTHER);
rep.setAuthorName("System");
rep.create();
}
if (author != null) {
author.removeRep(POST_VOTEDOWN_PENALTY_AUTHOR);
updateVoter = true;
//small penalty to voter
authUser.removeRep(POST_VOTEDOWN_PENALTY_VOTER);
}
}
} catch (Exception ex) {
logger.error(null, ex);
}
utils.addBadgeOnce(authUser, SUPPORTER, authUser.getUpvotes() >= SUPPORTER_IFHAS);
utils.addBadgeOnce(authUser, CRITIC, authUser.getDownvotes() >= CRITIC_IFHAS);
utils.addBadgeOnce(authUser, VOTER, authUser.getTotalVotes() >= VOTER_IFHAS);
if (updateVoter) {
pc.update(author);
}
if (updateAuthUser) {
pc.update(authUser);
}
if (updateAuthUser && updateVoter) {
pc.updateAll(Arrays.asList(author, authUser));
}
return result;
}
use of com.erudika.scoold.core.Comment in project scoold by Erudika.
the class CommentController method get.
@GetMapping("/{id}")
public String get(@PathVariable String id, HttpServletRequest req, Model model) {
Comment showComment = pc.read(id);
if (showComment == null || !ParaObjectUtils.typesMatch(showComment)) {
return "redirect:" + HOMEPAGE;
}
model.addAttribute("path", "comment.vm");
model.addAttribute("title", utils.getLang(req).get("comment.title"));
model.addAttribute("showComment", showComment);
return "base";
}
Aggregations