use of com.erudika.scoold.core.Profile 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.Profile in project scoold by Erudika.
the class QuestionController method sendReplyNotifications.
private void sendReplyNotifications(Post parentPost, Post reply) {
// send email notification to author of post except when the reply is by the same person
if (parentPost != null && reply != null && !StringUtils.equals(parentPost.getCreatorid(), reply.getCreatorid())) {
// the current user - same as utils.getAuthUser(req)
Profile replyAuthor = reply.getAuthor();
Map<String, Object> model = new HashMap<String, Object>();
String name = replyAuthor.getName();
String body = Utils.markdownToHtml(Utils.abbreviate(reply.getBody(), 500));
String picture = Utils.formatMessage("<img src='{0}' width='25'>", replyAuthor.getPicture());
String postURL = getServerURL() + parentPost.getPostLink(false, false);
model.put("logourl", Config.getConfigParam("small_logo_url", "https://scoold.com/logo.png"));
model.put("heading", Utils.formatMessage("New reply to <a href='{0}'>{1}</a>", postURL, parentPost.getTitle()));
model.put("body", Utils.formatMessage("<h2>{0} {1}:</h2><div class='panel'>{2}</div>", picture, name, body));
Profile authorProfile = pc.read(parentPost.getCreatorid());
if (authorProfile != null) {
User author = authorProfile.getUser();
if (author != null) {
if (authorProfile.getReplyEmailsEnabled()) {
parentPost.addFollower(author);
}
}
}
if (parentPost.hasFollowers()) {
emailer.sendEmail(new ArrayList<String>(parentPost.getFollowers().values()), name + " replied to '" + Utils.abbreviate(reply.getTitle(), 50) + "...'", Utils.compileMustache(model, utils.loadEmailTemplate("notify")));
}
}
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class TranslateController method delete.
@PostMapping("/delete/{id}")
public String delete(@PathVariable String id, HttpServletRequest req, Model model) {
if (id != null && utils.isAuthenticated(req)) {
Translation trans = (Translation) pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (authUser.getId().equals(trans.getCreatorid()) || utils.isAdmin(authUser)) {
utils.getLangutils().disapproveTranslation(trans.getLocale(), trans.getId());
pc.delete(trans);
}
if (!utils.isAjaxRequest(req)) {
return "redirect:" + req.getRequestURI();
}
}
return "base";
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class QuestionController method edit.
@PostMapping("/{id}/edit")
public String edit(@PathVariable String id, @RequestParam(required = false) String title, @RequestParam(required = false) String body, @RequestParam(required = false) String tags, HttpServletRequest req) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (!utils.canEdit(showPost, authUser) || showPost == null) {
return "redirect:" + req.getRequestURI();
}
Post beforeUpdate = null;
try {
beforeUpdate = (Post) BeanUtils.cloneBean(showPost);
} catch (Exception ex) {
logger.error(null, ex);
}
if (!StringUtils.isBlank(title) && title.length() > 10) {
showPost.setTitle(title);
}
if (!StringUtils.isBlank(body)) {
showPost.setBody(body);
}
if (!StringUtils.isBlank(tags) && showPost.isQuestion()) {
showPost.setTags(Arrays.asList(StringUtils.split(tags, ",")));
}
showPost.setLasteditby(authUser.getId());
//note: update only happens if something has changed
if (!showPost.equals(beforeUpdate)) {
showPost.update();
utils.addBadgeOnceAndUpdate(authUser, Badge.EDITOR, true);
}
return "redirect:" + showPost.getPostLink(false, false);
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class ReportsController method create.
@PostMapping
public void create(HttpServletRequest req, HttpServletResponse res) {
Report rep = utils.populate(req, new Report(), "link", "description", "parentid", "subType", "authorName");
Map<String, String> error = utils.validate(rep);
if (error.isEmpty()) {
if (utils.isAuthenticated(req)) {
Profile authUser = utils.getAuthUser(req);
rep.setAuthorName(authUser.getName());
rep.setCreatorid(authUser.getId());
utils.addBadgeAndUpdate(authUser, REPORTER, true);
} else {
//allow anonymous reports
rep.setAuthorName(utils.getLang(req).get("anonymous"));
}
rep.create();
res.setStatus(200);
} else {
res.setStatus(400);
}
}
Aggregations