Search in sources :

Example 76 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class AdminController method deleteWebhook.

@PostMapping("/delete-webhook")
public String deleteWebhook(@RequestParam String id, HttpServletRequest req, HttpServletResponse res) {
    Profile authUser = utils.getAuthUser(req);
    if (!StringUtils.isBlank(id) && utils.isAdmin(authUser) && utils.isWebhooksEnabled()) {
        Webhook webhook = new Webhook();
        webhook.setId(id);
        pc.delete(webhook);
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "base";
    } else {
        return "redirect:" + ADMINLINK;
    }
}
Also used : Webhook(com.erudika.para.core.Webhook) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 77 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class CommentController method deleteAjax.

@PostMapping("/{id}/delete")
public void deleteAjax(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
    if (utils.isAuthenticated(req)) {
        Comment comment = pc.read(id);
        Profile authUser = utils.getAuthUser(req);
        boolean isMod = utils.isMod(authUser);
        if (comment != null && (comment.getCreatorid().equals(authUser.getId()) || isMod)) {
            // check parent and correct (for multi-parent-object pages)
            comment.delete();
            if (!isMod) {
                utils.addBadgeAndUpdate(authUser, DISCIPLINED, true);
            }
        }
    }
    res.setStatus(200);
}
Also used : Comment(com.erudika.scoold.core.Comment) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 78 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class FeedbackController method replyAjax.

@PostMapping({ "/{id}", "/{id}/{title}" })
public String replyAjax(@PathVariable String id, @PathVariable(required = false) String title, HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
    if (!utils.isFeedbackEnabled()) {
        return "redirect:" + HOMEPAGE;
    }
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (authUser != null && 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, Profile.Badge.EUREKA, answer.getCreatorid().equals(showPost.getCreatorid()));
            answer.setAuthor(authUser);
            model.addAttribute("showPost", showPost);
            model.addAttribute("answerslist", Collections.singletonList(answer));
        } else {
            model.addAttribute("error", error);
            model.addAttribute("path", "feedback.vm");
            res.setStatus(400);
        }
        return "reply";
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "reply";
    } else {
        return "redirect:" + FEEDBACKLINK + "/" + id;
    }
}
Also used : Post(com.erudika.scoold.core.Post) Reply(com.erudika.scoold.core.Reply) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 79 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class ApiController method createComment.

@PostMapping("/comments")
public Comment createComment(HttpServletRequest req, HttpServletResponse res) {
    Map<String, Object> entity = readEntity(req);
    if (entity.isEmpty()) {
        badReq("Missing request body.");
    }
    String comment = (String) entity.get("comment");
    String parentid = (String) entity.get(Config._PARENTID);
    String creatorid = (String) entity.get(Config._CREATORID);
    ParaObject parent = pc.read(parentid);
    if (parent == null) {
        badReq("Parent object not found. Provide a valid parentid.");
        return null;
    }
    if (!StringUtils.isBlank(creatorid)) {
        Profile authUser = pc.read(Profile.id(creatorid));
        if (authUser != null) {
            req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
        }
    }
    Model model = new ExtendedModelMap();
    commentController.createAjax(comment, parentid, req, model);
    Comment created = (Comment) model.getAttribute("showComment");
    if (created == null || StringUtils.isBlank(comment)) {
        badReq("Failed to create comment.");
        return null;
    }
    res.setStatus(HttpStatus.CREATED.value());
    return created;
}
Also used : Comment(com.erudika.scoold.core.Comment) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) ParaObject(com.erudika.para.core.ParaObject) Model(org.springframework.ui.Model) ParaObject(com.erudika.para.core.ParaObject) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 80 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class ApiController method updatePost.

@PatchMapping("/posts/{id}")
public Post updatePost(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
    Map<String, Object> entity = readEntity(req);
    if (entity.isEmpty()) {
        badReq("Missing request body.");
    }
    String editorid = (String) entity.get("lasteditby");
    if (!StringUtils.isBlank(editorid)) {
        Profile authUser = pc.read(Profile.id(editorid));
        if (authUser != null) {
            req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
        }
    }
    String space = (String) entity.get("space");
    String title = (String) entity.get("title");
    String body = (String) entity.get("body");
    String location = (String) entity.get("location");
    String latlng = (String) entity.get("latlng");
    List<String> spaces = readSpaces(space);
    space = spaces.iterator().hasNext() ? spaces.iterator().next() : null;
    Model model = new ExtendedModelMap();
    questionController.edit(id, title, body, String.join(",", (List<String>) entity.get("tags")), location, latlng, space, req, res, model);
    Post post = (Post) model.getAttribute("post");
    if (post == null) {
        res.setStatus(HttpStatus.NOT_FOUND.value());
    } else if (!utils.canEdit(post, utils.getAuthUser(req))) {
        badReq("Update failed - user " + editorid + " is not allowed to update post.");
    }
    return post;
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Post(com.erudika.scoold.core.Post) Model(org.springframework.ui.Model) ParaObject(com.erudika.para.core.ParaObject) List(java.util.List) Profile(com.erudika.scoold.core.Profile) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Aggregations

Profile (com.erudika.scoold.core.Profile)85 PostMapping (org.springframework.web.bind.annotation.PostMapping)47 Post (com.erudika.scoold.core.Post)29 ParaObject (com.erudika.para.core.ParaObject)25 User (com.erudika.para.core.User)19 Pager (com.erudika.para.core.utils.Pager)19 HashMap (java.util.HashMap)15 LinkedHashMap (java.util.LinkedHashMap)15 Question (com.erudika.scoold.core.Question)13 Reply (com.erudika.scoold.core.Reply)13 Report (com.erudika.scoold.core.Report)11 IOException (java.io.IOException)11 List (java.util.List)11 Map (java.util.Map)11 UnapprovedReply (com.erudika.scoold.core.UnapprovedReply)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 Sysprop (com.erudika.para.core.Sysprop)9 Config (com.erudika.para.core.utils.Config)9 Utils (com.erudika.para.core.utils.Utils)9 Comment (com.erudika.scoold.core.Comment)9