Search in sources :

Example 6 with User

use of com.erudika.para.core.User 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("&nbsp;"));
        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();
    }
}
Also used : Question(com.erudika.scoold.core.Question) Arrays(java.util.Arrays) Webhook(com.erudika.para.core.Webhook) ServletException(javax.servlet.ServletException) Date(java.util.Date) HttpUtils.getCookieValue(com.erudika.scoold.utils.HttpUtils.getCookieValue) JOSEException(com.nimbusds.jose.JOSEException) LoggerFactory(org.slf4j.LoggerFactory) Vote(com.erudika.para.core.Vote) Scanner(java.util.Scanner) ParaClient(com.erudika.para.client.ParaClient) Revision(com.erudika.scoold.core.Revision) StringUtils(org.apache.commons.lang3.StringUtils) Base64(org.apache.commons.codec.binary.Base64) MACVerifier(com.nimbusds.jose.crypto.MACVerifier) Matcher(java.util.regex.Matcher) Locale(java.util.Locale) Map(java.util.Map) ParseException(java.text.ParseException) Config(com.erudika.para.core.utils.Config) ConstraintViolation(javax.validation.ConstraintViolation) ValidationUtils(com.erudika.para.core.validation.ValidationUtils) ParaObject(com.erudika.para.core.ParaObject) Predicate(java.util.function.Predicate) HttpHeaders(org.springframework.http.HttpHeaders) AvatarFormat(com.erudika.scoold.utils.avatars.AvatarFormat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigObject(com.typesafe.config.ConfigObject) Set(java.util.Set) JWSAlgorithm(com.nimbusds.jose.JWSAlgorithm) UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) Emailer(com.erudika.para.core.email.Emailer) JWSHeader(com.nimbusds.jose.JWSHeader) SignedJWT(com.nimbusds.jwt.SignedJWT) Collectors(java.util.stream.Collectors) Comment(com.erudika.scoold.core.Comment) List(java.util.List) JWSSigner(com.nimbusds.jose.JWSSigner) Tag(com.erudika.para.core.Tag) GravatarAvatarGenerator(com.erudika.scoold.utils.avatars.GravatarAvatarGenerator) Optional(java.util.Optional) DEFAULT_SPACE(com.erudika.scoold.core.Post.DEFAULT_SPACE) Para(com.erudika.para.core.utils.Para) Pattern(java.util.regex.Pattern) Feedback(com.erudika.scoold.core.Feedback) Sysprop(com.erudika.para.core.Sysprop) ParaObjectUtils(com.erudika.para.core.utils.ParaObjectUtils) ALL_MY_SPACES(com.erudika.scoold.core.Post.ALL_MY_SPACES) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) AvatarRepositoryProxy(com.erudika.scoold.utils.avatars.AvatarRepositoryProxy) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Pager(com.erudika.para.core.utils.Pager) ENTHUSIAST(com.erudika.scoold.core.Profile.Badge.ENTHUSIAST) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) Named(javax.inject.Named) LinkedList(java.util.LinkedList) TEACHER(com.erudika.scoold.core.Profile.Badge.TEACHER) UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) LinkedHashSet(java.util.LinkedHashSet) MACSigner(com.nimbusds.jose.crypto.MACSigner) JWSVerifier(com.nimbusds.jose.JWSVerifier) RegExUtils(org.apache.commons.lang3.RegExUtils) Logger(org.slf4j.Logger) AvatarRepository(com.erudika.scoold.utils.avatars.AvatarRepository) Address(com.erudika.para.core.Address) Post(com.erudika.scoold.core.Post) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) User(com.erudika.para.core.User) Utils(com.erudika.para.core.utils.Utils) Report(com.erudika.scoold.core.Report) Component(org.springframework.stereotype.Component) TreeMap(java.util.TreeMap) ScooldServer(com.erudika.scoold.ScooldServer) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Reply(com.erudika.scoold.core.Reply) Collections(java.util.Collections) Profile(com.erudika.scoold.core.Profile) InputStream(java.io.InputStream) UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Report(com.erudika.scoold.core.Report) ParaObject(com.erudika.para.core.ParaObject) ConfigObject(com.typesafe.config.ConfigObject) Profile(com.erudika.scoold.core.Profile) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 7 with User

use of com.erudika.para.core.User in project scoold by Erudika.

the class ScooldUtils method getFavTagsSubscribers.

private Set<String> getFavTagsSubscribers(List<String> tags) {
    if (!tags.isEmpty()) {
        Set<String> emails = new LinkedHashSet<>();
        // find all user objects even if there are more than 10000 users in the system
        Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
        List<Profile> profiles;
        do {
            profiles = pc.findQuery(Utils.type(Profile.class), "properties.favtags:(" + tags.stream().map(t -> "\"".concat(t).concat("\"")).distinct().collect(Collectors.joining(" ")) + ") AND properties.favtagsEmailsEnabled:true", pager);
            if (!profiles.isEmpty()) {
                List<User> users = pc.readAll(profiles.stream().map(p -> p.getCreatorid()).distinct().collect(Collectors.toList()));
                users.stream().forEach(u -> emails.add(u.getEmail()));
            }
        } while (!profiles.isEmpty());
        return emails;
    }
    return Collections.emptySet();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Question(com.erudika.scoold.core.Question) Arrays(java.util.Arrays) Webhook(com.erudika.para.core.Webhook) ServletException(javax.servlet.ServletException) Date(java.util.Date) HttpUtils.getCookieValue(com.erudika.scoold.utils.HttpUtils.getCookieValue) JOSEException(com.nimbusds.jose.JOSEException) LoggerFactory(org.slf4j.LoggerFactory) Vote(com.erudika.para.core.Vote) Scanner(java.util.Scanner) ParaClient(com.erudika.para.client.ParaClient) Revision(com.erudika.scoold.core.Revision) StringUtils(org.apache.commons.lang3.StringUtils) Base64(org.apache.commons.codec.binary.Base64) MACVerifier(com.nimbusds.jose.crypto.MACVerifier) Matcher(java.util.regex.Matcher) Locale(java.util.Locale) Map(java.util.Map) ParseException(java.text.ParseException) Config(com.erudika.para.core.utils.Config) ConstraintViolation(javax.validation.ConstraintViolation) ValidationUtils(com.erudika.para.core.validation.ValidationUtils) ParaObject(com.erudika.para.core.ParaObject) Predicate(java.util.function.Predicate) HttpHeaders(org.springframework.http.HttpHeaders) AvatarFormat(com.erudika.scoold.utils.avatars.AvatarFormat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigObject(com.typesafe.config.ConfigObject) Set(java.util.Set) JWSAlgorithm(com.nimbusds.jose.JWSAlgorithm) UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) Emailer(com.erudika.para.core.email.Emailer) JWSHeader(com.nimbusds.jose.JWSHeader) SignedJWT(com.nimbusds.jwt.SignedJWT) Collectors(java.util.stream.Collectors) Comment(com.erudika.scoold.core.Comment) List(java.util.List) JWSSigner(com.nimbusds.jose.JWSSigner) Tag(com.erudika.para.core.Tag) GravatarAvatarGenerator(com.erudika.scoold.utils.avatars.GravatarAvatarGenerator) Optional(java.util.Optional) DEFAULT_SPACE(com.erudika.scoold.core.Post.DEFAULT_SPACE) Para(com.erudika.para.core.utils.Para) Pattern(java.util.regex.Pattern) Feedback(com.erudika.scoold.core.Feedback) Sysprop(com.erudika.para.core.Sysprop) ParaObjectUtils(com.erudika.para.core.utils.ParaObjectUtils) ALL_MY_SPACES(com.erudika.scoold.core.Post.ALL_MY_SPACES) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) AvatarRepositoryProxy(com.erudika.scoold.utils.avatars.AvatarRepositoryProxy) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Pager(com.erudika.para.core.utils.Pager) ENTHUSIAST(com.erudika.scoold.core.Profile.Badge.ENTHUSIAST) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) Named(javax.inject.Named) LinkedList(java.util.LinkedList) TEACHER(com.erudika.scoold.core.Profile.Badge.TEACHER) UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) LinkedHashSet(java.util.LinkedHashSet) MACSigner(com.nimbusds.jose.crypto.MACSigner) JWSVerifier(com.nimbusds.jose.JWSVerifier) RegExUtils(org.apache.commons.lang3.RegExUtils) Logger(org.slf4j.Logger) AvatarRepository(com.erudika.scoold.utils.avatars.AvatarRepository) Address(com.erudika.para.core.Address) Post(com.erudika.scoold.core.Post) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) User(com.erudika.para.core.User) Utils(com.erudika.para.core.utils.Utils) Report(com.erudika.scoold.core.Report) Component(org.springframework.stereotype.Component) TreeMap(java.util.TreeMap) ScooldServer(com.erudika.scoold.ScooldServer) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Reply(com.erudika.scoold.core.Reply) Collections(java.util.Collections) Profile(com.erudika.scoold.core.Profile) InputStream(java.io.InputStream) User(com.erudika.para.core.User) Pager(com.erudika.para.core.utils.Pager) Profile(com.erudika.scoold.core.Profile)

Example 8 with User

use of com.erudika.para.core.User in project scoold by Erudika.

the class AdminController method importUsersFromSO.

private void importUsersFromSO(List<Map<String, Object>> objs, List<ParaObject> toImport) throws ParseException {
    logger.info("Importing {} users...", objs.size());
    for (Map<String, Object> obj : objs) {
        User u = new User();
        u.setId("user_" + (Integer) obj.get("id"));
        u.setTimestamp(DateUtils.parseDate((String) obj.get("creationDate"), soDateFormat1, soDateFormat2).getTime());
        u.setActive(true);
        u.setCreatorid(((Integer) obj.get("accountId")).toString());
        u.setGroups("admin".equalsIgnoreCase((String) obj.get("userTypeId")) ? User.Groups.ADMINS.toString() : User.Groups.USERS.toString());
        u.setEmail(u.getId() + "@scoold.com");
        u.setIdentifier(u.getEmail());
        u.setName((String) obj.get("realName"));
        String lastLogin = (String) obj.get("lastLoginDate");
        u.setUpdated(StringUtils.isBlank(lastLogin) ? null : DateUtils.parseDate(lastLogin, soDateFormat1, soDateFormat2).getTime());
        u.setPicture((String) obj.get("profileImageUrl"));
        u.setPassword(Utils.generateSecurityToken(10));
        Profile p = Profile.fromUser(u);
        p.setVotes((Integer) obj.get("reputation"));
        p.setAboutme((String) obj.getOrDefault("title", ""));
        p.setLastseen(u.getUpdated());
        toImport.add(u);
        toImport.add(p);
    }
    pc.createAll(toImport);
}
Also used : User(com.erudika.para.core.User) ParaObject(com.erudika.para.core.ParaObject) Profile(com.erudika.scoold.core.Profile)

Example 9 with User

use of com.erudika.para.core.User in project scoold by Erudika.

the class GravatarAvatarRepositoryTest method setUp.

@Before
public void setUp() {
    this.profile = new Profile();
    this.profile.setUser(new User());
    this.defaultRepository = new DefaultAvatarRepository();
    this.gravatarGenerator = new GravatarAvatarGenerator();
    this.repository = new GravatarAvatarRepository(gravatarGenerator, defaultRepository);
}
Also used : User(com.erudika.para.core.User) Profile(com.erudika.scoold.core.Profile) Before(org.junit.Before)

Example 10 with User

use of com.erudika.para.core.User in project scoold by Erudika.

the class SigninController method loginWithIdToken.

private void loginWithIdToken(String jwt, HttpServletRequest req, HttpServletResponse res) {
    User u = pc.signIn("passwordless", jwt, false);
    if (u != null) {
        setAuthCookie(u.getPassword(), req, res);
        onAuthSuccess(u, req, res);
    }
}
Also used : User(com.erudika.para.core.User)

Aggregations

User (com.erudika.para.core.User)24 Profile (com.erudika.scoold.core.Profile)18 ParaObject (com.erudika.para.core.ParaObject)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Sysprop (com.erudika.para.core.Sysprop)6 Pager (com.erudika.para.core.utils.Pager)6 Post (com.erudika.scoold.core.Post)6 Reply (com.erudika.scoold.core.Reply)6 Map (java.util.Map)6 ParaClient (com.erudika.para.client.ParaClient)5 Tag (com.erudika.para.core.Tag)5 Webhook (com.erudika.para.core.Webhook)5 Config (com.erudika.para.core.utils.Config)5 ParaObjectUtils (com.erudika.para.core.utils.ParaObjectUtils)5 Utils (com.erudika.para.core.utils.Utils)5 Comment (com.erudika.scoold.core.Comment)5 Question (com.erudika.scoold.core.Question)5 UnapprovedReply (com.erudika.scoold.core.UnapprovedReply)5 Report (com.erudika.scoold.core.Report)4