Search in sources :

Example 6 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class UserDataUtil method _loginFederatedUser.

private static UserData _loginFederatedUser(UserData apiUserData, User user, UserSignUpSource signUpSource) throws InsufficientAccessException {
    // Do nothing if a user is already logged in
    if (!AccessTokenFilter.getAccessToken().getUserId().equals(0L))
        return getCurrentUser();
    // Note: Users having Facebook or Google Id can never be in GUEST or REFERRAL state.
    // Note: No action required for REGISTERED or ACTIVE users.
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    boolean isNew = false;
    if (user == null || user.getState() == UserState.DELETED) {
        if (apiUserData.getEmail() != null)
            user = dataAccessor.getUserByEmail(apiUserData.getEmail());
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), null, null);
        if (user == null || user.getState() == UserState.DELETED) {
            user = dataAccessor.newUser();
            auditLog.setAccessType(AccessType.USER_ADD);
            auditLog.setEventDataOld(user);
            user.setEmail(apiUserData.getEmail());
            // Counting on Facebook / Google for e-mail/user verification
            user.setState(UserState.ACTIVE);
            user.setSignUpDate(new Date());
            user.setSignUpSource(signUpSource);
            isNew = true;
        } else if (user.getState() == UserState.REFERRAL) {
            auditLog.setAccessType(AccessType.USER_ADD);
            auditLog.setEventDataOld(user);
            // Counting on Facebook / Google for e-mail/user verification
            user.setState(UserState.ACTIVE);
            user.setSignUpDate(new Date());
            user.setSignUpSource(signUpSource);
            isNew = true;
        } else if (user.getState() == UserState.REGISTERED) {
            auditLog.setAccessType(AccessType.USER_UPDATE);
            auditLog.setEventDataOld(user);
            // Counting on Facebook / google for e-mail/user verification
            user.setState(UserState.ACTIVE);
        } else {
            // user.getState() == UserState.ACTIVE || user.getState() == UserState.BLOCKED
            auditLog.setAccessType(AccessType.USER_UPDATE);
            auditLog.setEventDataOld(user);
        }
        if (apiUserData.getFacebookId() != null)
            user.setFacebookId(apiUserData.getFacebookId());
        if (apiUserData.getGoogleId() != null)
            user.setGoogleId(apiUserData.getGoogleId());
        user.setLastUpdated(new Date());
        user = dataAccessor.createOrUpdateUser(user, auditLog);
    }
    if (user.getState() == UserState.BLOCKED)
        throw new InsufficientAccessException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    _loginUser(AccessTokenFilter.getAccessToken(), user);
    UserData userData = createUserData(user);
    if (isNew) {
        userData.setFirstName(apiUserData.getFirstName());
        userData.setLastName(apiUserData.getLastName());
        userData.setGender(apiUserData.getGender());
        userData.setDateOfBirth(apiUserData.getDateOfBirth());
    }
    return userData;
}
Also used : UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 7 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class AuditLogProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Fetching AppProperty
    String appPropertyId = "Api.AuditLogProcess";
    AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
    if (appProperty == null)
        appProperty = dataAccessor.newAppProperty(appPropertyId);
    // Fetching list of audit logs
    DataListCursorTuple<AuditLog> auditLogDataListCursorTuple = dataAccessor.getAuditLogList(// Tuesday, 1 August 2017 00:00:00 GMT+05:30
    new Date(1501525800000L), (String) appProperty.getValue(), 5000);
    // Make sets of PrimaryContent ids
    Map<Long, Set<Long>> pratilipiUpdateIds = new HashMap<>();
    Set<String> userPratilipiUpdateIds = new HashSet<>();
    Set<String> userAuthorUpdateIds = new HashSet<>();
    Map<String, UserAuthor> userAuthors = new HashMap<>();
    Set<Long> commentUpdateIds = new HashSet<>();
    Set<String> voteUpdateIds = new HashSet<>();
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonLongDateAdapter()).create();
    for (AuditLog auditLog : auditLogDataListCursorTuple.getDataList()) {
        // TODO: Delete following condition as soon as 'legacy' module is removed
        if (auditLog.getUserId() == null || auditLog.getPrimaryContentId() == null) {
            continue;
        }
        if (auditLog.getUserId().equals(SystemProperty.SYSTEM_USER_ID)) {
            continue;
        }
        if (auditLog.getAccessType() == AccessType.PRATILIPI_UPDATE) {
            Pratilipi oldPratilipi = gson.fromJson(auditLog.getEventDataOld(), PratilipiEntity.class);
            Pratilipi newPratilipi = gson.fromJson(auditLog.getEventDataNew(), PratilipiEntity.class);
            if (oldPratilipi.getState() == PratilipiState.DRAFTED && newPratilipi.getState() == PratilipiState.PUBLISHED) {
                Set<Long> userIdSet = pratilipiUpdateIds.get(auditLog.getPrimaryContentIdLong());
                if (userIdSet == null) {
                    userIdSet = new HashSet<>();
                    pratilipiUpdateIds.put(auditLog.getPrimaryContentIdLong(), userIdSet);
                }
                userIdSet.add(auditLog.getUserId());
            }
        } else if (auditLog.getAccessType() == AccessType.USER_PRATILIPI_REVIEW) {
            UserPratilipi oldUserPratilipi = gson.fromJson(auditLog.getEventDataOld(), UserPratilipiEntity.class);
            UserPratilipi newUserPratilipi = gson.fromJson(auditLog.getEventDataNew(), UserPratilipiEntity.class);
            if (oldUserPratilipi.getRating() == null && oldUserPratilipi.getReview() == null && (newUserPratilipi.getRating() != null || newUserPratilipi.getReview() != null))
                userPratilipiUpdateIds.add(auditLog.getPrimaryContentId());
        } else if (auditLog.getAccessType() == AccessType.USER_AUTHOR_FOLLOWING) {
            UserAuthor oldUserAuthor = gson.fromJson(auditLog.getEventDataOld(), UserAuthorEntity.class);
            UserAuthor newUserAuthor = gson.fromJson(auditLog.getEventDataNew(), UserAuthorEntity.class);
            if (oldUserAuthor.getFollowState() == null && newUserAuthor.getFollowState() == UserFollowState.FOLLOWING) {
                userAuthorUpdateIds.add(auditLog.getPrimaryContentId());
                userAuthors.put(auditLog.getPrimaryContentId(), newUserAuthor);
            }
        } else if (auditLog.getAccessType() == AccessType.COMMENT_ADD) {
            commentUpdateIds.add(auditLog.getPrimaryContentIdLong());
        } else if (auditLog.getAccessType() == AccessType.VOTE) {
            Vote newVote = gson.fromJson(auditLog.getEventDataNew(), VoteEntity.class);
            if (newVote.getType() == VoteType.LIKE)
                voteUpdateIds.add(auditLog.getPrimaryContentId());
        }
    }
    // Batch get Vote entities
    logger.log(Level.INFO, "Fetching " + voteUpdateIds.size() + " Vote Entities.");
    Map<String, Vote> votes = dataAccessor.getVotes(voteUpdateIds);
    // Batch get Comment and entities
    Set<Long> commentIds = new HashSet<>(commentUpdateIds);
    for (Vote vote : votes.values()) if (vote.getParentType() == VoteParentType.COMMENT)
        commentIds.add(vote.getParentIdLong());
    logger.log(Level.INFO, "Fetching " + commentIds.size() + " Comment Entities.");
    Map<Long, Comment> comments = dataAccessor.getComments(commentIds);
    // Batch get UserPratilipi entities
    Set<String> userPratilipiIds = new HashSet<>(userPratilipiUpdateIds);
    for (Comment comment : comments.values()) if (comment.getParentType() == CommentParentType.REVIEW)
        userPratilipiIds.add(comment.getParentId());
    for (Vote vote : votes.values()) if (vote.getParentType() == VoteParentType.REVIEW)
        userPratilipiIds.add(vote.getParentId());
    logger.log(Level.INFO, "Fetching " + userPratilipiIds.size() + " UserPratilipi Entities.");
    Map<String, UserPratilipi> userPratilipis = dataAccessor.getUserPratilipis(userPratilipiIds);
    // Batch get Pratilipi entities
    Set<Long> pratilipiIds = new HashSet<>(pratilipiUpdateIds.keySet());
    for (UserPratilipi userPratilipi : userPratilipis.values()) pratilipiIds.add(userPratilipi.getPratilipiId());
    logger.log(Level.INFO, "Fetching " + pratilipiIds.size() + " Pratilipi Entities.");
    Map<Long, Pratilipi> pratilipis = dataAccessor.getPratilipis(pratilipiIds);
    // Batch get UserAuthor entities
    logger.log(Level.INFO, "Fetching " + userAuthorUpdateIds.size() + " UserAuthor Entities.");
    // Batch get Author entities
    Set<Long> authorIds = new HashSet<>();
    for (Pratilipi pratilipi : pratilipis.values()) authorIds.add(pratilipi.getAuthorId());
    for (UserAuthor userAuthor : userAuthors.values()) authorIds.add(userAuthor.getAuthorId());
    logger.log(Level.INFO, "Fetching " + authorIds.size() + " Author Entities.");
    Map<Long, Author> authors = dataAccessor.getAuthors(authorIds);
    List<Email> totalEmailList = new ArrayList<>();
    // Get userIds of followers for authors who had published
    List<Long> pratilipiUpdateAuthorIds = new ArrayList<>();
    for (Long pratilipiId : pratilipiUpdateIds.keySet()) pratilipiUpdateAuthorIds.add(pratilipis.get(pratilipiId).getAuthorId());
    Map<Long, List<Long>> authorsFollowersMap = _getUserAuthorFollowList(pratilipiUpdateAuthorIds);
    // auditLog.getAccessType() == AccessType.PRATILIPI_UPDATE
    for (Long pratilipiId : pratilipiUpdateIds.keySet()) {
        Pratilipi pratilipi = pratilipis.get(pratilipiId);
        Set<Long> followerUserIdList = new HashSet<>(authorsFollowersMap.get(pratilipi.getAuthorId()));
        Email email = _createPratilipiPublishedEmail(pratilipi, authors.get(pratilipi.getAuthorId()));
        if (email != null)
            totalEmailList.add(email);
        totalEmailList.addAll(_createPratilipiPublishedEmails(pratilipi, followerUserIdList));
        // Send notification to all AEEs as well
        // only if the content is self-published
        List<Long> aeeUserIdList = _getAeeUserIdList(pratilipi.getLanguage());
        Set<Long> userIdSet = pratilipiUpdateIds.get(pratilipiId);
        for (Long userId : userIdSet) {
            if (!aeeUserIdList.contains(userId)) {
                followerUserIdList.addAll(aeeUserIdList);
                break;
            }
        }
        _createPratilipiPublishedNotification(pratilipi, authors.get(pratilipi.getAuthorId()));
        _createPratilipiPublishedNotifications(pratilipi, followerUserIdList);
    }
    // auditLog.getAccessType() == AccessType.USER_PRATILIPI_REVIEW
    for (String userPratilipiId : userPratilipiUpdateIds) {
        UserPratilipi userPratilipi = userPratilipis.get(userPratilipiId);
        if (userPratilipi.getReviewState() != UserReviewState.PUBLISHED)
            continue;
        Long pratilipiId = userPratilipi.getPratilipiId();
        Email email = _createUserPratilipiReviewEmail(userPratilipi, authors.get(pratilipis.get(pratilipiId).getAuthorId()));
        if (email != null)
            totalEmailList.add(email);
    }
    // auditLog.getAccessType() == AccessType.USER_AUTHOR_FOLLOWING
    for (String userAuthorId : userAuthorUpdateIds) {
        UserAuthor userAuthor = userAuthors.get(userAuthorId);
        _createUserAuthorFollowingNotifications(userAuthor, authors.get(userAuthor.getAuthorId()));
        Email email = _createUserAuthorFollowingEmail(userAuthor, authors.get(userAuthor.getAuthorId()));
        if (email != null)
            totalEmailList.add(email);
    }
    // auditLog.getAccessType() == AccessType.COMMENT_ADD
    for (Long commentId : commentUpdateIds) {
        Comment comment = comments.get(commentId);
        if (comment.getParentType() != CommentParentType.REVIEW)
            continue;
        UserPratilipi userPratilipi = userPratilipis.get(comment.getParentId());
        Email email = _createCommentAddedReviewerEmail(userPratilipi, comment);
        if (email != null)
            totalEmailList.add(email);
    /*	// Business call - Not to send CommentAddedAuthorEmail.
			Pratilipi pratilipi = pratilipis.get( userPratilipi.getPratilipiId() );
			Author author = authors.get( pratilipi.getAuthorId() );
			email = _createCommentAddedAuthorEmail( author, comment );
			if( email != null )
				totalEmailList.add( email );
			*/
    }
    // auditLog.getAccessType() == AccessType.VOTE
    for (String voteId : voteUpdateIds) {
        Vote vote = votes.get(voteId);
        if (vote.getParentType() == VoteParentType.REVIEW) {
            UserPratilipi userPratilipi = userPratilipis.get(vote.getParentId());
            // To the reviewer
            Email email = _createVoteOnReviewReviewerEmail(userPratilipi, vote);
            if (email != null)
                totalEmailList.add(email);
        } else if (vote.getParentType() == VoteParentType.COMMENT) {
            // To the commentor
            Email email = _createVoteOnCommentCommentorEmail(comments.get(vote.getParentIdLong()), vote);
            if (email != null)
                totalEmailList.add(email);
        }
    }
    _updateEmailTable(totalEmailList);
    // Updating AppProperty.
    if (auditLogDataListCursorTuple.getDataList().size() > 0) {
        appProperty.setValue(auditLogDataListCursorTuple.getCursor());
        appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
    }
    return new GenericResponse();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Email(com.pratilipi.data.type.Email) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) AuditLog(com.pratilipi.data.type.AuditLog) UserPratilipi(com.pratilipi.data.type.UserPratilipi) UserAuthor(com.pratilipi.data.type.UserAuthor) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Comment(com.pratilipi.data.type.Comment) Vote(com.pratilipi.data.type.Vote) GenericResponse(com.pratilipi.api.shared.GenericResponse) Date(java.util.Date) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) AppProperty(com.pratilipi.data.type.AppProperty) GsonLongDateAdapter(com.pratilipi.common.util.GsonLongDateAdapter) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity) Get(com.pratilipi.api.annotation.Get)

Example 8 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updatePratilipiTags.

public static boolean updatePratilipiTags(long pratilipiId, PratilipiType pratilipiType, List<Long> tagIds, List<String> suggestedTags) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi == null)
        return false;
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    PratilipiData pratilipiData = createPratilipiData(pratilipi);
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    logger.log(Level.INFO, "User Id : " + accessToken.getUserId());
    if (!hasAccessToUpdatePratilipiData(pratilipi, pratilipiData)) {
        throw new InsufficientAccessException();
    }
    if (pratilipiType != null)
        pratilipi.setType(pratilipiType);
    if (tagIds != null)
        pratilipi.setTagIds(tagIds);
    if (suggestedTags != null)
        pratilipi.setSuggestedTags(suggestedTags);
    dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return true;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) AccessToken(com.pratilipi.data.type.AccessToken) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog)

Example 9 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class PratilipiDataUtil method savePratilipiData.

public static PratilipiData savePratilipiData(PratilipiData pratilipiData) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    _validatePratilipiDataForSave(pratilipiData);
    boolean isNew = pratilipiData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = isNew ? dataAccessor.newPratilipi() : dataAccessor.getPratilipi(pratilipiData.getId());
    if (isNew && !hasAccessToAddPratilipiData(pratilipiData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdatePratilipiData(pratilipi, pratilipiData))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.PRATILIPI_ADD : AccessType.PRATILIPI_UPDATE, pratilipi);
    if (pratilipiData.hasTitle())
        pratilipi.setTitle(pratilipiData.getTitle());
    if (pratilipiData.hasTitleEn())
        pratilipi.setTitleEn(pratilipiData.getTitleEn());
    if (pratilipiData.hasLanguage())
        pratilipi.setLanguage(pratilipiData.getLanguage());
    // Do NOT update Author for existing content pieces.
    if (isNew && pratilipiData.hasAuthorId())
        pratilipi.setAuthorId(pratilipiData.getAuthorId());
    if (pratilipiData.hasSummary())
        pratilipi.setSummary(pratilipiData.getSummary());
    if (pratilipiData.hasType())
        pratilipi.setType(pratilipiData.getType());
    if (pratilipiData.hasContentType())
        pratilipi.setContentType(pratilipiData.getContentType());
    else if (isNew)
        pratilipi.setContentType(PratilipiContentType.PRATILIPI);
    if (pratilipiData.hasState())
        pratilipi.setState(pratilipiData.getState());
    else if (isNew)
        pratilipi.setState(PratilipiState.DRAFTED);
    if (pratilipiData.hasOldContentFlag())
        pratilipi.setOldContent(pratilipiData.isOldContent());
    if (isNew)
        pratilipi.setListingDate(new Date());
    pratilipi.setLastUpdated(new Date());
    if (isNew) {
        pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
        dataAccessor.createOrUpdatePage(_updatePratilipiPageUrl(pratilipi));
    } else {
        pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, _updatePratilipiPageUrl(pratilipi), auditLog);
    }
    return createPratilipiData(pratilipi, dataAccessor.getAuthor(pratilipi.getAuthorId()), hasAccessToReadPratilipiMetaData(pratilipi));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 10 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updateUserPratilipiStats.

public static void updateUserPratilipiStats(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi.getState() != PratilipiState.PUBLISHED)
        return;
    PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(pratilipiId);
    if (pratilipi.getRatingCount().equals(reviewsDoc.getRatingCount()) && pratilipi.getTotalRating().equals(reviewsDoc.getTotalRating()) && pratilipi.getReviewCount().equals(reviewsDoc.getReviewCount()))
        return;
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    pratilipi.setRatingCount(reviewsDoc.getRatingCount());
    pratilipi.setTotalRating(reviewsDoc.getTotalRating());
    pratilipi.setReviewCount(reviewsDoc.getReviewCount());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) AuditLog(com.pratilipi.data.type.AuditLog) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Aggregations

AuditLog (com.pratilipi.data.type.AuditLog)29 DataAccessor (com.pratilipi.data.DataAccessor)28 Date (java.util.Date)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)17 UserPratilipi (com.pratilipi.data.type.UserPratilipi)15 Pratilipi (com.pratilipi.data.type.Pratilipi)10 User (com.pratilipi.data.type.User)8 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)7 DocAccessor (com.pratilipi.data.DocAccessor)5 BlobAccessor (com.pratilipi.data.BlobAccessor)4 JsonObject (com.google.gson.JsonObject)3 UserData (com.pratilipi.data.client.UserData)3 Comment (com.pratilipi.data.type.Comment)3 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)3 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)3 HashMap (java.util.HashMap)3 Get (com.pratilipi.api.annotation.Get)2 GenericResponse (com.pratilipi.api.shared.GenericResponse)2 AccessToken (com.pratilipi.data.type.AccessToken)2 BlobEntry (com.pratilipi.data.type.BlobEntry)2