use of com.erudika.scoold.core.Profile 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.Profile in project scoold by Erudika.
the class SettingsController method post.
@PostMapping
public String post(@RequestParam(required = false) String tags, @RequestParam(required = false) String latlng, @RequestParam(required = false) String replyEmailsOn, @RequestParam(required = false) String commentEmailsOn, HttpServletRequest req) {
if (utils.isAuthenticated(req)) {
Profile authUser = utils.getAuthUser(req);
if (!StringUtils.isBlank(tags)) {
Set<String> ts = new LinkedHashSet<String>();
for (String tag : tags.split(",")) {
if (!StringUtils.isBlank(tag) && ts.size() <= MAX_FAV_TAGS) {
ts.add(tag);
}
}
authUser.setFavtags(new LinkedList<String>(ts));
}
if (!StringUtils.isBlank(latlng)) {
authUser.setLatlng(latlng);
}
authUser.setReplyEmailsEnabled(Boolean.valueOf(replyEmailsOn));
authUser.setCommentEmailsEnabled(Boolean.valueOf(commentEmailsOn));
authUser.update();
}
return "redirect:" + settingslink;
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class TranslateController method post.
@PostMapping("/{locale}/{index}")
public String post(@PathVariable String locale, @PathVariable String index, @RequestParam String value, HttpServletRequest req, Model model) {
Locale showLocale = utils.getLangutils().getProperLocale(locale);
if (utils.isAuthenticated(req) && showLocale != null && !"en".equals(showLocale.getLanguage())) {
Set<String> approved = utils.getLangutils().getApprovedTransKeys(showLocale.getLanguage());
Profile authUser = utils.getAuthUser(req);
String langkey = langkeys.get(getIndex(index, langkeys));
boolean isTranslated = approved.contains(langkey);
if (!StringUtils.isBlank(value) && (!isTranslated || utils.isAdmin(authUser))) {
Translation trans = new Translation(showLocale.getLanguage(), langkey, StringUtils.trim(value));
trans.setCreatorid(authUser.getId());
trans.setAuthorName(authUser.getName());
trans.setTimestamp(System.currentTimeMillis());
pc.create(trans);
model.addAttribute("newtranslation", trans);
}
if (!utils.isAjaxRequest(req)) {
return "redirect:" + translatelink + "/" + showLocale.getLanguage() + "/" + getNextIndex(getIndex(index, langkeys), approved, langkeys);
}
}
return "base";
}
use of com.erudika.scoold.core.Profile 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.Profile 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