use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class FeedbackController method get.
@GetMapping
public String get(@RequestParam(required = false, defaultValue = Config._TIMESTAMP) String sortby, HttpServletRequest req, Model model) {
Pager itemcount = utils.getPager("page", req);
itemcount.setSortby(sortby);
List<Post> feedbacklist = pc.findQuery(Utils.type(Feedback.class), "*", itemcount);
utils.fetchProfiles(feedbacklist);
model.addAttribute("path", "feedback.vm");
model.addAttribute("title", utils.getLang(req).get("feedback.title"));
model.addAttribute("itemcount", itemcount);
model.addAttribute("feedbacklist", feedbacklist);
return "base";
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class FeedbackController method createAjax.
@PostMapping
public String createAjax(HttpServletRequest req, Model model) {
model.addAttribute("path", "feedback.vm");
if (utils.isAuthenticated(req)) {
Profile authUser = utils.getAuthUser(req);
Post post = utils.populate(req, new Feedback(), "title", "body", "tags|,");
Map<String, String> error = utils.validate(post);
if (error.isEmpty()) {
post.setCreatorid(authUser.getId());
post.create();
authUser.setLastseen(System.currentTimeMillis());
return "redirect:" + feedbacklink;
} else {
model.addAttribute("error", error);
return "base";
}
}
return "redirect:" + feedbacklink;
}
use of com.erudika.scoold.core.Post in project scoold by Erudika.
the class FeedbackController method getTagged.
@GetMapping("/tag/{tag}")
public String getTagged(@PathVariable String tag, HttpServletRequest req, Model model) {
Pager itemcount = utils.getPager("page", req);
List<Post> feedbacklist = pc.findTagged(Utils.type(Feedback.class), new String[] { tag }, itemcount);
model.addAttribute("path", "feedback.vm");
model.addAttribute("title", utils.getLang(req).get("feedback.title"));
model.addAttribute("tag", tag);
model.addAttribute("itemcount", itemcount);
model.addAttribute("feedbacklist", feedbacklist);
return "base";
}
use of com.erudika.scoold.core.Post 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.Post in project scoold by Erudika.
the class ScooldUtils method fetchProfiles.
public void fetchProfiles(List<? extends ParaObject> objects) {
if (objects == null || objects.isEmpty()) {
return;
}
Map<String, String> authorids = new HashMap<String, String>(objects.size());
Map<String, Profile> authors = new HashMap<String, Profile>(objects.size());
for (ParaObject obj : objects) {
if (obj.getCreatorid() != null) {
authorids.put(obj.getId(), obj.getCreatorid());
}
}
List<String> ids = new ArrayList<String>(new HashSet<String>(authorids.values()));
if (ids.isEmpty()) {
return;
}
// read all post authors in batch
for (ParaObject author : pc.readAll(ids)) {
authors.put(author.getId(), (Profile) author);
}
// set author object for each post
for (ParaObject obj : objects) {
if (obj instanceof Post) {
((Post) obj).setAuthor(authors.get(authorids.get(obj.getId())));
} else if (obj instanceof Revision) {
((Revision) obj).setAuthor(authors.get(authorids.get(obj.getId())));
}
}
}
Aggregations