use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class QuestionController method replyAjax.
@PostMapping({ "/{id}", "/{id}/{title}" })
public String replyAjax(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) Boolean emailme, HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (showPost != null && emailme != null) {
if (emailme) {
showPost.addFollower(authUser.getUser());
} else {
showPost.removeFollower(authUser.getUser());
}
// update without adding revisions
pc.update(showPost);
} else if (showPost != null && !showPost.isClosed() && !showPost.isReply()) {
//create new answer
Reply answer = utils.populate(req, new Reply(), "body");
Map<String, String> error = utils.validate(answer);
if (!error.containsKey("body")) {
answer.setTitle(showPost.getTitle());
answer.setCreatorid(authUser.getId());
answer.setParentid(showPost.getId());
answer.create();
showPost.setAnswercount(showPost.getAnswercount() + 1);
if (showPost.getAnswercount() >= MAX_REPLIES_PER_POST) {
showPost.setCloserid("0");
}
// update without adding revisions
pc.update(showPost);
utils.addBadgeAndUpdate(authUser, Badge.EUREKA, answer.getCreatorid().equals(showPost.getCreatorid()));
answer.setAuthor(authUser);
model.addAttribute("showPost", showPost);
model.addAttribute("answerslist", Collections.singletonList(answer));
// send email to the question author
sendReplyNotifications(showPost, answer);
return "reply";
}
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "base";
} else {
return "redirect:" + questionslink + "/" + id;
}
}
use of com.erudika.scoold.core.Post 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.Post in project scoold by Erudika.
the class QuestionController method restore.
@PostMapping("/{id}/restore/{revisionid}")
public String restore(@PathVariable String id, @PathVariable String revisionid, HttpServletRequest req) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (!utils.canEdit(showPost, authUser) || showPost == null) {
return "redirect:" + req.getRequestURI();
}
if (utils.canEdit(showPost, authUser)) {
utils.addBadgeAndUpdate(authUser, Badge.BACKINTIME, true);
showPost.restoreRevisionAndUpdate(revisionid);
}
return "redirect:" + showPost.getPostLink(false, false);
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class QuestionController method approve.
@PostMapping("/{id}/approve/{answerid}")
public String approve(@PathVariable String id, @PathVariable String answerid, HttpServletRequest req) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (!utils.canEdit(showPost, authUser) || showPost == null) {
return "redirect:" + req.getRequestURI();
}
if (utils.canEdit(showPost, authUser) && answerid != null && utils.isMine(showPost, authUser)) {
Reply answer = (Reply) pc.read(answerid);
if (answer != null && answer.isReply()) {
Profile author = pc.read(answer.getCreatorid());
if (author != null && utils.isAuthenticated(req)) {
boolean same = author.equals(authUser);
if (answerid.equals(showPost.getAnswerid())) {
// Answer approved award - UNDO
showPost.setAnswerid("");
if (!same) {
author.removeRep(ANSWER_APPROVE_REWARD_AUTHOR);
authUser.removeRep(ANSWER_APPROVE_REWARD_VOTER);
pc.updateAll(Arrays.asList(author, authUser));
}
} else {
// Answer approved award - GIVE
showPost.setAnswerid(answerid);
if (!same) {
author.addRep(ANSWER_APPROVE_REWARD_AUTHOR);
authUser.addRep(ANSWER_APPROVE_REWARD_VOTER);
utils.addBadgeOnce(authUser, Badge.NOOB, true);
pc.updateAll(Arrays.asList(author, authUser));
}
}
showPost.update();
}
}
}
return "redirect:" + showPost.getPostLink(false, false);
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class RevisionsController method get.
@GetMapping("/{postid}")
public String get(@PathVariable String postid, HttpServletRequest req, Model model) {
Post showPost = utils.getParaClient().read(postid);
if (showPost == null) {
return "redirect:" + questionslink;
}
Pager itemcount = utils.getPager("page", req);
List<Revision> revisionslist = showPost.getRevisions(itemcount);
// we need the first revision on the next page for diffing
List<Revision> nextPage = showPost.getRevisions(new Pager(itemcount.getPage() + 1, itemcount.getLimit()));
utils.fetchProfiles(revisionslist);
model.addAttribute("path", "revisions.vm");
model.addAttribute("title", utils.getLang(req).get("revisions.title"));
model.addAttribute("showPost", showPost);
model.addAttribute("itemcount", itemcount);
model.addAttribute("revisionslist", revisionslist);
model.addAttribute("lastOnPage", nextPage.isEmpty() ? null : nextPage.get(0));
return "base";
}
Aggregations