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";
}
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;
}
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;
}
}
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);
}
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;
}
Aggregations