Search in sources :

Example 31 with Profile

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())) {
                    // update without adding revisions
                    pc.update(parentPost);
                }
                utils.sendCommentNotifications(parentPost, showComment, authUser, req);
            }
        }
    }
    return "comment";
}
Also used : Comment(com.erudika.scoold.core.Comment) Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 32 with Profile

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

the class FeedbackController method createAjax.

@PostMapping
public String createAjax(HttpServletRequest req, Model model) {
    if (!utils.isFeedbackEnabled()) {
        return "redirect:" + HOMEPAGE;
    }
    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 (authUser != null && error.isEmpty()) {
            post.setCreatorid(authUser.getId());
            post.create();
            authUser.setLastseen(System.currentTimeMillis());
            return "redirect:" + FEEDBACKLINK;
        } else {
            model.addAttribute("error", error);
            model.addAttribute("write", true);
            return "base";
        }
    }
    return "redirect:" + FEEDBACKLINK;
}
Also used : Feedback(com.erudika.scoold.core.Feedback) Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 33 with Profile

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

the class AdminController method renameSpace.

@PostMapping("/rename-space")
public String renameSpace(@RequestParam String space, @RequestParam String newspace, HttpServletRequest req, HttpServletResponse res) {
    Profile authUser = utils.getAuthUser(req);
    Sysprop s = pc.read(utils.getSpaceId(space));
    if (s != null && utils.isAdmin(authUser)) {
        String origSpace = s.getId() + Config.SEPARATOR + s.getName();
        int index = utils.getAllSpaces().indexOf(s);
        s.setName(newspace);
        pc.update(s);
        if (index >= 0) {
            utils.getAllSpaces().get(index).setName(newspace);
        }
        Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
        LinkedList<Map<String, Object>> toUpdate = new LinkedList<>();
        List<Profile> profiles;
        do {
            String query = "properties.spaces:(\"" + origSpace + "\")";
            profiles = pc.findQuery(Utils.type(Profile.class), query, pager);
            profiles.stream().forEach(p -> {
                p.getSpaces().remove(origSpace);
                p.getSpaces().add(s.getId() + Config.SEPARATOR + s.getName());
                Map<String, Object> profile = new HashMap<>();
                profile.put(Config._ID, p.getId());
                profile.put("spaces", p.getSpaces());
                toUpdate.add(profile);
            });
            if (!toUpdate.isEmpty()) {
                pc.invokePatch("_batch", toUpdate, Map.class);
            }
        } while (!profiles.isEmpty());
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "space";
    } else {
        return "redirect:" + ADMINLINK;
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Pager(com.erudika.para.core.utils.Pager) Sysprop(com.erudika.para.core.Sysprop) ParaObject(com.erudika.para.core.ParaObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Profile(com.erudika.scoold.core.Profile) LinkedList(java.util.LinkedList) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 34 with Profile

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

the class AdminController method forceDelete.

@PostMapping
public String forceDelete(@RequestParam Boolean confirmdelete, @RequestParam String id, HttpServletRequest req) {
    Profile authUser = utils.getAuthUser(req);
    if (confirmdelete && utils.isAdmin(authUser)) {
        ParaObject object = pc.read(id);
        if (object != null) {
            object.delete();
            logger.info("{} #{} deleted {} #{}", authUser.getName(), authUser.getId(), object.getClass().getName(), object.getId());
        }
    }
    return "redirect:" + Optional.ofNullable(req.getParameter("returnto")).orElse(ADMINLINK);
}
Also used : ParaObject(com.erudika.para.core.ParaObject) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 35 with Profile

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

the class AdminController method restore.

@PostMapping("/import")
public String restore(@RequestParam("file") MultipartFile file, @RequestParam(required = false, defaultValue = "false") Boolean isso, HttpServletRequest req, HttpServletResponse res) {
    Profile authUser = utils.getAuthUser(req);
    if (!utils.isAdmin(authUser)) {
        res.setStatus(403);
        return null;
    }
    ObjectReader reader = ParaObjectUtils.getJsonMapper().readerFor(new TypeReference<List<Map<String, Object>>>() {
    });
    Map<String, String> comments2authors = new LinkedHashMap<>();
    int count = 0;
    int importBatchSize = Config.getConfigInt("import_batch_size", 100);
    String filename = file.getOriginalFilename();
    Sysprop s = new Sysprop();
    s.setType("scooldimport");
    try (InputStream inputStream = file.getInputStream()) {
        if (StringUtils.endsWithIgnoreCase(filename, ".zip")) {
            try (ZipInputStream zipIn = new ZipInputStream(inputStream)) {
                ZipEntry zipEntry;
                List<ParaObject> toCreate = new LinkedList<ParaObject>();
                while ((zipEntry = zipIn.getNextEntry()) != null) {
                    if (isso) {
                        count += importFromSOArchive(zipIn, zipEntry, reader, comments2authors).size();
                    } else if (zipEntry.getName().endsWith(".json")) {
                        List<Map<String, Object>> objects = reader.readValue(new FilterInputStream(zipIn) {

                            public void close() throws IOException {
                                zipIn.closeEntry();
                            }
                        });
                        objects.forEach(o -> toCreate.add(ParaObjectUtils.setAnnotatedFields(o)));
                        if (toCreate.size() >= importBatchSize) {
                            pc.createAll(toCreate);
                            toCreate.clear();
                        }
                        count += objects.size();
                    } else {
                        logger.error("Expected JSON but found unknown file type to import: {}", zipEntry.getName());
                    }
                }
                if (!toCreate.isEmpty()) {
                    pc.createAll(toCreate);
                }
                if (isso) {
                    updateSOCommentAuthors(comments2authors);
                }
            }
        } else if (StringUtils.endsWithIgnoreCase(filename, ".json")) {
            List<Map<String, Object>> objects = reader.readValue(inputStream);
            List<ParaObject> toCreate = new LinkedList<ParaObject>();
            objects.forEach(o -> toCreate.add(ParaObjectUtils.setAnnotatedFields(o)));
            count = objects.size();
            pc.createAll(toCreate);
        }
        s.setCreatorid(authUser.getCreatorid());
        s.setName(authUser.getName());
        s.addProperty("count", count);
        s.addProperty("file", filename);
        logger.info("Imported {} objects to {}. Executed by {}", count, Config.getConfigParam("access_key", "scoold"), authUser.getCreatorid() + " " + authUser.getName());
        if (count > 0) {
            pc.create(s);
        }
    } catch (Exception e) {
        logger.error("Failed to import " + filename, e);
        return "redirect:" + ADMINLINK + "?error=true&imported=" + count;
    }
    return "redirect:" + ADMINLINK + "?success=true&imported=" + count;
}
Also used : SIGNINLINK(com.erudika.scoold.ScooldServer.SIGNINLINK) Question(com.erudika.scoold.core.Question) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) Webhook(com.erudika.para.core.Webhook) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) ParaClient(com.erudika.para.client.ParaClient) StringUtils(org.apache.commons.lang3.StringUtils) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Model(org.springframework.ui.Model) Locale(java.util.Locale) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ParseException(java.text.ParseException) Config(com.erudika.para.core.utils.Config) ZipEntry(java.util.zip.ZipEntry) ScooldUtils(com.erudika.scoold.utils.ScooldUtils) PostMapping(org.springframework.web.bind.annotation.PostMapping) ParaObject(com.erudika.para.core.ParaObject) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ConfigValue(com.typesafe.config.ConfigValue) MediaType(org.springframework.http.MediaType) Set(java.util.Set) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) UUID(java.util.UUID) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) SignedJWT(com.nimbusds.jwt.SignedJWT) Collectors(java.util.stream.Collectors) Comment(com.erudika.scoold.core.Comment) List(java.util.List) Tag(com.erudika.para.core.Tag) Optional(java.util.Optional) ADMINLINK(com.erudika.scoold.ScooldServer.ADMINLINK) App(com.erudika.para.core.App) Sysprop(com.erudika.para.core.Sysprop) ZipOutputStream(java.util.zip.ZipOutputStream) ParaObjectUtils(com.erudika.para.core.utils.ParaObjectUtils) ZipInputStream(java.util.zip.ZipInputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) Pager(com.erudika.para.core.utils.Pager) Controller(org.springframework.stereotype.Controller) ArrayList(java.util.ArrayList) HOMEPAGE(com.erudika.scoold.ScooldServer.HOMEPAGE) Inject(javax.inject.Inject) LinkedHashMap(java.util.LinkedHashMap) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterInputStream(java.io.FilterInputStream) GetMapping(org.springframework.web.bind.annotation.GetMapping) LinkedList(java.util.LinkedList) Logger(org.slf4j.Logger) Post(com.erudika.scoold.core.Post) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) User(com.erudika.para.core.User) Utils(com.erudika.para.core.utils.Utils) DateUtils(org.apache.commons.lang3.time.DateUtils) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) Reply(com.erudika.scoold.core.Reply) MultipartFile(org.springframework.web.multipart.MultipartFile) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) Profile(com.erudika.scoold.core.Profile) InputStream(java.io.InputStream) FilterInputStream(java.io.FilterInputStream) ZipInputStream(java.util.zip.ZipInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Profile(com.erudika.scoold.core.Profile) LinkedList(java.util.LinkedList) ParseException(java.text.ParseException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ZipInputStream(java.util.zip.ZipInputStream) ParaObject(com.erudika.para.core.ParaObject) Sysprop(com.erudika.para.core.Sysprop) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ParaObject(com.erudika.para.core.ParaObject) PostMapping(org.springframework.web.bind.annotation.PostMapping)

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