use of com.erudika.scoold.core.Question in project scoold by Erudika.
the class ApiController method listQuestions.
@GetMapping("/posts")
public List<Map<String, Object>> listQuestions(HttpServletRequest req) {
Model model = new ExtendedModelMap();
questionsController.getQuestions(req.getParameter("sortby"), req.getParameter("filter"), req, model);
return ((List<Question>) model.getAttribute("questionslist")).stream().map(p -> {
Map<String, Object> post = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(p, false));
post.put("author", p.getAuthor());
return post;
}).collect(Collectors.toList());
}
use of com.erudika.scoold.core.Question in project scoold by Erudika.
the class SearchController method getFeed.
private SyndFeed getFeed() throws IOException, FeedException {
List<Post> questions = pc.findQuery(Utils.type(Question.class), "*");
List<SyndEntry> entries = new ArrayList<SyndEntry>();
String baseurl = ScooldServer.getServerURL();
baseurl = baseurl.endsWith("/") ? baseurl : baseurl + "/";
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("atom_1.0");
feed.setTitle(Config.APP_NAME + " - Recent questions");
feed.setLink(baseurl);
feed.setDescription("A summary of the most recent questions on " + Config.APP_NAME);
for (Post post : questions) {
SyndEntry entry;
SyndContent description;
String baselink = baseurl.concat("question/").concat(post.getId());
entry = new SyndEntryImpl();
entry.setTitle(post.getTitle());
entry.setLink(baselink);
entry.setPublishedDate(new Date(post.getTimestamp()));
entry.setAuthor(baseurl.concat("profile/").concat(post.getCreatorid()));
entry.setUri(baselink.concat("/").concat(Utils.stripAndTrim(post.getTitle()).replaceAll("\\p{Z}+", "-").toLowerCase()));
description = new SyndContentImpl();
description.setType("text/html");
description.setValue(Utils.markdownToHtml(post.getBody()));
entry.setDescription(description);
entries.add(entry);
}
feed.setEntries(entries);
return feed;
}
use of com.erudika.scoold.core.Question in project scoold by Erudika.
the class ScooldUtils method sendNewPostNotifications.
@SuppressWarnings("unchecked")
public void sendNewPostNotifications(Post question, HttpServletRequest req) {
if (question == null) {
return;
}
// the current user - same as utils.getAuthUser(req)
Profile postAuthor = question.getAuthor() != null ? question.getAuthor() : pc.read(question.getCreatorid());
if (!question.getType().equals(Utils.type(UnapprovedQuestion.class))) {
if (!isNewPostNotificationAllowed()) {
return;
}
Map<String, Object> model = new HashMap<String, Object>();
Map<String, String> lang = getLang(req);
String name = postAuthor.getName();
String body = Utils.markdownToHtml(question.getBody());
String picture = Utils.formatMessage("<img src='{0}' width='25'>", escapeHtmlAttribute(avatarRepository.getLink(postAuthor, AvatarFormat.Square25)));
String postURL = getServerURL() + question.getPostLink(false, false);
String tagsString = Optional.ofNullable(question.getTags()).orElse(Collections.emptyList()).stream().map(t -> "<span class=\"tag\">" + escapeHtml(t) + "</span>").collect(Collectors.joining(" "));
String subject = Utils.formatMessage(lang.get("notification.newposts.subject"), name, Utils.abbreviate(question.getTitle(), 255));
model.put("subject", escapeHtml(subject));
model.put("logourl", getSmallLogoUrl());
model.put("heading", Utils.formatMessage(lang.get("notification.newposts.heading"), picture, escapeHtml(name)));
model.put("body", Utils.formatMessage("<h2><a href='{0}'>{1}</a></h2><div>{2}</div><br>{3}", postURL, escapeHtml(question.getTitle()), body, tagsString));
Set<String> emails = new HashSet<String>(getNotificationSubscribers(EMAIL_ALERTS_PREFIX + "new_post_subscribers"));
emails.addAll(getFavTagsSubscribers(question.getTags()));
sendEmailsToSubscribersInSpace(emails, question.getSpace(), subject, compileEmailTemplate(model));
} else if (postsNeedApproval() && question instanceof UnapprovedQuestion) {
Report rep = new Report();
rep.setDescription("New question awaiting approval");
rep.setSubType(Report.ReportType.OTHER);
rep.setLink(question.getPostLink(false, false));
rep.setAuthorName(postAuthor.getName());
rep.create();
}
}
use of com.erudika.scoold.core.Question in project scoold by Erudika.
the class QuestionsController method getQuestions.
public List<Question> getQuestions(String sortby, String filter, HttpServletRequest req, Model model) {
Pager itemcount = getPagerFromCookie(req, utils.getPager("page", req));
List<Question> questionslist = Collections.emptyList();
String type = Utils.type(Question.class);
Profile authUser = utils.getAuthUser(req);
String currentSpace = utils.getSpaceIdFromCookie(authUser, req);
String query = getQuestionsQuery(req, authUser, sortby, currentSpace, itemcount);
if (!StringUtils.isBlank(filter) && authUser != null) {
if ("favtags".equals(filter)) {
if (!authUser.hasFavtags() && req.getParameterValues("favtags") != null) {
// API override
authUser.setFavtags(Arrays.asList(req.getParameterValues("favtags")));
}
if (isSpaceFilteredRequest(authUser, currentSpace) && authUser.hasFavtags()) {
questionslist = pc.findQuery(type, getSpaceFilteredFavtagsQuery(currentSpace, authUser), itemcount);
} else {
questionslist = pc.findTermInList(type, Config._TAGS, authUser.getFavtags(), itemcount);
}
} else if ("local".equals(filter)) {
String latlng = Optional.ofNullable(authUser.getLatlng()).orElse(req.getParameter("latlng"));
String[] ll = latlng == null ? new String[0] : latlng.split(",");
if (ll.length == 2) {
double lat = NumberUtils.toDouble(ll[0]);
double lng = NumberUtils.toDouble(ll[1]);
questionslist = pc.findNearby(type, query, 25, lat, lng, itemcount);
}
}
model.addAttribute("localFilterOn", "local".equals(filter));
model.addAttribute("tagFilterOn", "favtags".equals(filter));
model.addAttribute("filter", "/" + Utils.stripAndTrim(filter));
} else {
questionslist = pc.findQuery(type, query, itemcount);
}
if (utils.postsNeedApproval() && utils.isMod(authUser)) {
Pager p = new Pager(itemcount.getPage(), itemcount.getLimit());
List<UnapprovedQuestion> uquestionslist = pc.findQuery(Utils.type(UnapprovedQuestion.class), query, p);
List<Question> qlist = new LinkedList<>(uquestionslist);
itemcount.setCount(itemcount.getCount() + p.getCount());
qlist.addAll(questionslist);
questionslist = qlist;
}
utils.fetchProfiles(questionslist);
model.addAttribute("itemcount", itemcount);
model.addAttribute("questionslist", questionslist);
return questionslist;
}
use of com.erudika.scoold.core.Question in project scoold by Erudika.
the class QuestionsController method post.
@PostMapping("/questions/ask")
public String post(@RequestParam(required = false) String location, @RequestParam(required = false) String latlng, @RequestParam(required = false) String address, String space, HttpServletRequest req, HttpServletResponse res, Model model) {
if (utils.isAuthenticated(req)) {
Profile authUser = utils.getAuthUser(req);
String currentSpace = utils.getValidSpaceIdExcludingAll(authUser, space, req);
boolean needsApproval = utils.postNeedsApproval(authUser);
Question q = utils.populate(req, needsApproval ? new UnapprovedQuestion() : new Question(), "title", "body", "tags|,", "location");
q.setCreatorid(authUser.getId());
q.setSpace(currentSpace);
if (StringUtils.isBlank(q.getTagsString())) {
q.setTags(Arrays.asList(Config.getConfigParam("default_question_tag", "question")));
}
Map<String, String> error = utils.validate(q);
if (error.isEmpty()) {
q.setLocation(location);
q.setAuthor(authUser);
String qid = q.create();
utils.sendNewPostNotifications(q, req);
if (!StringUtils.isBlank(latlng)) {
Address addr = new Address(qid + Config.SEPARATOR + Utils.type(Address.class));
addr.setAddress(address);
addr.setCountry(location);
addr.setLatlng(latlng);
addr.setParentid(qid);
addr.setCreatorid(authUser.getId());
pc.create(addr);
}
authUser.setLastseen(System.currentTimeMillis());
model.addAttribute("newpost", getNewQuestionPayload(q));
} else {
model.addAttribute("error", error);
model.addAttribute("draftQuestion", q);
model.addAttribute("defaultTag", "");
model.addAttribute("path", "questions.vm");
model.addAttribute("includeGMapsScripts", utils.isNearMeFeatureEnabled());
model.addAttribute("askSelected", "navbtn-hover");
return "base";
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
res.setContentType("application/json");
try {
res.getWriter().println("{\"url\":\"" + q.getPostLink(false, false) + "\"}");
} catch (IOException ex) {
}
return "blank";
} else {
return "redirect:" + q.getPostLink(false, false);
}
}
if (utils.isAjaxRequest(req)) {
res.setStatus(400);
return "blank";
} else {
return "redirect:" + SIGNINLINK + "?returnto=" + QUESTIONSLINK + "/ask";
}
}
Aggregations