Search in sources :

Example 1 with Revision

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

the class RevisionsController method get.

@GetMapping("/{postid}")
public String get(@PathVariable String postid, HttpServletRequest req, Model model) {
    Post showPost = utils.getParaClient().read(postid);
    if (showPost == null) {
        return "redirect:" + questionslink;
    }
    Pager itemcount = utils.getPager("page", req);
    List<Revision> revisionslist = showPost.getRevisions(itemcount);
    // we need the first revision on the next page for diffing
    List<Revision> nextPage = showPost.getRevisions(new Pager(itemcount.getPage() + 1, itemcount.getLimit()));
    utils.fetchProfiles(revisionslist);
    model.addAttribute("path", "revisions.vm");
    model.addAttribute("title", utils.getLang(req).get("revisions.title"));
    model.addAttribute("showPost", showPost);
    model.addAttribute("itemcount", itemcount);
    model.addAttribute("revisionslist", revisionslist);
    model.addAttribute("lastOnPage", nextPage.isEmpty() ? null : nextPage.get(0));
    return "base";
}
Also used : Revision(com.erudika.scoold.core.Revision) Post(com.erudika.scoold.core.Post) Pager(com.erudika.para.utils.Pager) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with Revision

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

the class ApiController method getPostRevisions.

@GetMapping("/posts/{id}/revisions")
public List<Map<String, Object>> getPostRevisions(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
    Model model = new ExtendedModelMap();
    revisionsController.get(id, req, res, model);
    Post post = (Post) model.getAttribute("showPost");
    if (post == null) {
        res.setStatus(HttpStatus.NOT_FOUND.value());
        return null;
    }
    return ((List<Revision>) model.getAttribute("revisionslist")).stream().map(r -> {
        Map<String, Object> rev = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(r, false));
        rev.put("author", r.getAuthor());
        return rev;
    }).collect(Collectors.toList());
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Question(com.erudika.scoold.core.Question) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) Webhook(com.erudika.para.core.Webhook) PeopleController(com.erudika.scoold.controllers.PeopleController) WebRequest(org.springframework.web.context.request.WebRequest) LoggerFactory(org.slf4j.LoggerFactory) ParaClient(com.erudika.para.client.ParaClient) Revision(com.erudika.scoold.core.Revision) StringUtils(org.apache.commons.lang3.StringUtils) Model(org.springframework.ui.Model) CommentController(com.erudika.scoold.controllers.CommentController) PutMapping(org.springframework.web.bind.annotation.PutMapping) Map(java.util.Map) Config(com.erudika.para.core.utils.Config) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ValidationUtils(com.erudika.para.core.validation.ValidationUtils) ScooldUtils(com.erudika.scoold.utils.ScooldUtils) PostMapping(org.springframework.web.bind.annotation.PostMapping) BadRequestException(com.erudika.scoold.utils.BadRequestException) ParaObject(com.erudika.para.core.ParaObject) QuestionsController(com.erudika.scoold.controllers.QuestionsController) Collection(java.util.Collection) Set(java.util.Set) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) REST_ENTITY_ATTRIBUTE(com.erudika.scoold.ScooldServer.REST_ENTITY_ATTRIBUTE) Comment(com.erudika.scoold.core.Comment) List(java.util.List) Tag(com.erudika.para.core.Tag) ReportsController(com.erudika.scoold.controllers.ReportsController) Optional(java.util.Optional) AUTH_USER_ATTRIBUTE(com.erudika.scoold.ScooldServer.AUTH_USER_ATTRIBUTE) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Sysprop(com.erudika.para.core.Sysprop) TagsController(com.erudika.scoold.controllers.TagsController) ParaObjectUtils(com.erudika.para.core.utils.ParaObjectUtils) RevisionsController(com.erudika.scoold.controllers.RevisionsController) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) Pager(com.erudika.para.core.utils.Pager) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) AdminController(com.erudika.scoold.controllers.AdminController) HttpServletRequest(javax.servlet.http.HttpServletRequest) Locked(com.erudika.para.core.annotations.Locked) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) GetMapping(org.springframework.web.bind.annotation.GetMapping) UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) ProfileController(com.erudika.scoold.controllers.ProfileController) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Post(com.erudika.scoold.core.Post) VoteController(com.erudika.scoold.controllers.VoteController) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) User(com.erudika.para.core.User) CONTEXT_PATH(com.erudika.scoold.ScooldServer.CONTEXT_PATH) Utils(com.erudika.para.core.utils.Utils) Report(com.erudika.scoold.core.Report) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) ScooldServer(com.erudika.scoold.ScooldServer) NumberUtils(org.apache.commons.lang3.math.NumberUtils) QuestionController(com.erudika.scoold.controllers.QuestionController) Reply(com.erudika.scoold.core.Reply) MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) Profile(com.erudika.scoold.core.Profile) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Post(com.erudika.scoold.core.Post) Model(org.springframework.ui.Model) List(java.util.List) Map(java.util.Map) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with Revision

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

the class RevisionsController method get.

@GetMapping("/{postid}")
public String get(@PathVariable String postid, HttpServletRequest req, HttpServletResponse res, Model model) {
    Post showPost = utils.getParaClient().read(postid);
    if (showPost == null) {
        return "redirect:" + QUESTIONSLINK;
    }
    // https://github.com/Erudika/scoold/issues/254
    res.setHeader("X-Robots-Tag", "noindex, nofollow");
    Profile authUser = utils.getAuthUser(req);
    if (!utils.canAccessSpace(authUser, showPost.getSpace())) {
        return "redirect:" + QUESTIONSLINK;
    }
    Pager itemcount = utils.getPager("page", req);
    List<Revision> revisionslist = showPost.getRevisions(itemcount);
    // we need the first revision on the next page for diffing
    List<Revision> nextPage = showPost.getRevisions(new Pager(itemcount.getPage() + 1, itemcount.getLimit()));
    utils.fetchProfiles(revisionslist);
    model.addAttribute("path", "revisions.vm");
    model.addAttribute("title", utils.getLang(req).get("revisions.title"));
    model.addAttribute("showPost", showPost);
    model.addAttribute("itemcount", itemcount);
    model.addAttribute("revisionslist", revisionslist);
    model.addAttribute("lastOnPage", nextPage.isEmpty() ? null : nextPage.get(0));
    return "base";
}
Also used : Revision(com.erudika.scoold.core.Revision) Post(com.erudika.scoold.core.Post) Pager(com.erudika.para.core.utils.Pager) Profile(com.erudika.scoold.core.Profile) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with Revision

use of com.erudika.scoold.core.Revision 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);
    }
    // add system profile
    authors.put(API_USER.getId(), API_USER);
    // 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())));
        }
    }
}
Also used : Revision(com.erudika.scoold.core.Revision) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParaObject(com.erudika.para.core.ParaObject) Post(com.erudika.scoold.core.Post) ArrayList(java.util.ArrayList) Profile(com.erudika.scoold.core.Profile)

Aggregations

Post (com.erudika.scoold.core.Post)4 Revision (com.erudika.scoold.core.Revision)4 Profile (com.erudika.scoold.core.Profile)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 ParaObject (com.erudika.para.core.ParaObject)2 Pager (com.erudika.para.core.utils.Pager)2 ParaClient (com.erudika.para.client.ParaClient)1 Sysprop (com.erudika.para.core.Sysprop)1 Tag (com.erudika.para.core.Tag)1 User (com.erudika.para.core.User)1 Webhook (com.erudika.para.core.Webhook)1 Locked (com.erudika.para.core.annotations.Locked)1 Config (com.erudika.para.core.utils.Config)1 ParaObjectUtils (com.erudika.para.core.utils.ParaObjectUtils)1 Utils (com.erudika.para.core.utils.Utils)1 ValidationUtils (com.erudika.para.core.validation.ValidationUtils)1 Pager (com.erudika.para.utils.Pager)1 ScooldServer (com.erudika.scoold.ScooldServer)1 AUTH_USER_ATTRIBUTE (com.erudika.scoold.ScooldServer.AUTH_USER_ATTRIBUTE)1 CONTEXT_PATH (com.erudika.scoold.ScooldServer.CONTEXT_PATH)1