Search in sources :

Example 6 with AccessToken

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

the class UserAuthorDataUtil method saveUserAuthorFollow.

public static UserAuthorData saveUserAuthorFollow(Long userId, Long authorId, UserFollowState followState) throws InvalidArgumentException, InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserAuthor userAuthor = dataAccessor.getUserAuthor(userId, authorId);
    if (userAuthor == null) {
        userAuthor = dataAccessor.newUserAuthor();
        userAuthor.setUserId(userId);
        userAuthor.setAuthorId(authorId);
    }
    if (!hasAccessToUpdateUserAuthorData(userAuthor, AccessType.USER_AUTHOR_FOLLOWING))
        throw new InsufficientAccessException();
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    AuditLog auditLog = dataAccessor.newAuditLog(accessToken, AccessType.USER_AUTHOR_FOLLOWING, userAuthor);
    userAuthor.setFollowState(followState);
    userAuthor.setFollowDate(new Date());
    userAuthor = dataAccessor.createOrUpdateUserAuthor(userAuthor, auditLog);
    return createUserAuthorData(userAuthor);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) AccessToken(com.pratilipi.data.type.AccessToken) UserAuthor(com.pratilipi.data.type.UserAuthor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 7 with AccessToken

use of com.pratilipi.data.type.AccessToken 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) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog)

Example 8 with AccessToken

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

the class DeleteTagsApi method addTags.

@Post
public GenericResponse addTags(PostRequest request) throws InsufficientAccessException {
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    if (accessToken.getUserId() != 5073076857339904L) {
        Logger.getLogger(TagsApi.class.getSimpleName()).log(Level.SEVERE, "AccessToken : " + accessToken.getId());
        Logger.getLogger(TagsApi.class.getSimpleName()).log(Level.SEVERE, "User Id : " + accessToken.getUserId());
        throw new InsufficientAccessException();
    }
    TagDataUtil.removeTags(request.getIds());
    return new GenericResponse();
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) AccessToken(com.pratilipi.data.type.AccessToken) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Post(com.pratilipi.api.annotation.Post)

Example 9 with AccessToken

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

the class DataStoreCleanupUtil method delete.

public static void delete(User user, boolean preview) {
    System.out.println();
    System.out.println("User id: " + user.getId() + ", state: " + user.getState() + ", signUpDate:" + user.getSignUpDate() + ", signUpSource:" + user.getSignUpSource());
    if (!preview && user.getState() != UserState.DELETED && user.getState() != UserState.BLOCKED) {
        user.setState(UserState.DELETED);
        // Save
        ObjectifyService.ofy().save().entity(user).now();
    }
    // ACCESS_TOKEN Table
    List<AccessTokenEntity> accessTokenList = ObjectifyService.ofy().load().type(AccessTokenEntity.class).filter("USER_ID", user.getId()).filter("EXPIRY >", new Date()).list();
    System.out.println("AccessTokenEntity # " + accessTokenList.size());
    if (!preview) {
        for (AccessToken accessToken : accessTokenList) {
            accessToken.setExpiry(new Date());
            // Save
            ObjectifyService.ofy().save().entity(accessToken).now();
        }
    }
    // USER_PRATILIPI Table
    List<UserPratilipiEntity> userPratilipiList = ObjectifyService.ofy().load().type(UserPratilipiEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("UserPratilipiEntity # " + userPratilipiList.size());
    int reviewCount = 0;
    int ratingCount = 0;
    for (UserPratilipi userPratilipi : userPratilipiList) {
        if (userPratilipi.getReviewState() != UserReviewState.DELETED && userPratilipi.getReviewState() != UserReviewState.BLOCKED)
            reviewCount++;
        if (userPratilipi.getRating() != null && userPratilipi.getRating() != 0)
            ratingCount++;
    }
    System.out.println("Review ## " + reviewCount);
    System.out.println("Rating ## " + ratingCount);
    if (!preview) {
        for (UserPratilipi userPratilipi : userPratilipiList) {
            boolean save = false;
            if (userPratilipi.getReviewState() != UserReviewState.DELETED && userPratilipi.getReviewState() != UserReviewState.BLOCKED) {
                userPratilipi.setReviewState(UserReviewState.DELETED);
                save = true;
            }
            if (userPratilipi.getRating() != null && userPratilipi.getRating() != 0) {
                userPratilipi.setRating(null);
                save = true;
            }
            if (save) {
                // Save
                ObjectifyService.ofy().save().entity(userPratilipi).now();
                Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", userPratilipi.getPratilipiId().toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
                TaskQueueFactory.getPratilipiTaskQueue().add(task);
            }
        }
    }
    // USER_AUTHOR Table
    List<UserAuthorEntity> userAuthorList = ObjectifyService.ofy().load().type(UserAuthorEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("UserAuthorEntity # " + userAuthorList.size());
    int followCount = 0;
    for (UserAuthor userAuthor : userAuthorList) if (userAuthor.getFollowState() == UserFollowState.FOLLOWING)
        followCount++;
    System.out.println("Follow ## " + followCount);
    if (!preview) {
        for (UserAuthor userAuthor : userAuthorList) {
            if (userAuthor.getFollowState() != null) {
                userAuthor.setFollowState(null);
                // Save
                ObjectifyService.ofy().save().entity(userAuthor).now();
            }
        }
    }
    // COMMENT Table
    List<CommentEntity> commentList = ObjectifyService.ofy().load().type(CommentEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("CommentEntity # " + commentList.size());
    int commentCount = 0;
    for (Comment comment : commentList) if (comment.getState() == CommentState.ACTIVE)
        commentCount++;
    System.out.println("Comment ## " + commentCount);
    if (!preview) {
        for (Comment comment : commentList) {
            if (comment.getState() == CommentState.ACTIVE) {
                comment.setState(CommentState.DELETED);
                // Save
                ObjectifyService.ofy().save().entity(comment).now();
            }
        }
    }
    // VOTE Table
    List<VoteEntity> voteList = ObjectifyService.ofy().load().type(VoteEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println("VoteEntity # " + voteList.size());
    int voteCount = 0;
    for (Vote vote : voteList) if (vote.getType() != VoteType.NONE)
        voteCount++;
    System.out.println("Vote ## " + voteCount);
    if (!preview)
        for (Vote vote : voteList) // Delete
        ObjectifyService.ofy().delete().entity(vote).now();
    // AUTHOR Table
    List<AuthorEntity> authorList = ObjectifyService.ofy().load().type(AuthorEntity.class).filter("USER_ID", user.getId()).list();
    System.out.println();
    System.out.println("AuthorEntity # " + authorList.size());
    if (authorList.size() == 0)
        return;
    for (Author author : authorList) delete(author, preview);
}
Also used : UserAuthorEntity(com.pratilipi.data.type.gae.UserAuthorEntity) Comment(com.pratilipi.data.type.Comment) Task(com.pratilipi.taskqueue.Task) Vote(com.pratilipi.data.type.Vote) AccessTokenEntity(com.pratilipi.data.type.gae.AccessTokenEntity) Date(java.util.Date) UserAuthorEntity(com.pratilipi.data.type.gae.UserAuthorEntity) AuthorEntity(com.pratilipi.data.type.gae.AuthorEntity) CommentEntity(com.pratilipi.data.type.gae.CommentEntity) UserPratilipi(com.pratilipi.data.type.UserPratilipi) VoteEntity(com.pratilipi.data.type.gae.VoteEntity) AccessToken(com.pratilipi.data.type.AccessToken) UserAuthor(com.pratilipi.data.type.UserAuthor) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity)

Example 10 with AccessToken

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

the class UserPratilipiDataUtil method hasAccessToUpdateUserPratilipiData.

public static boolean hasAccessToUpdateUserPratilipiData(UserPratilipi userPratilipi, AccessType accessType) {
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    if (!userPratilipi.getUserId().equals(accessToken.getUserId()))
        return false;
    if (!UserAccessUtil.hasUserAccess(accessToken.getUserId(), null, accessType))
        return false;
    // Review can not be created for content pieces created by the user
    if (accessType == AccessType.USER_PRATILIPI_REVIEW) {
        if (userPratilipi.getReviewState() == UserReviewState.BLOCKED)
            return false;
        DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
        Pratilipi pratilipi = dataAccessor.getPratilipi(userPratilipi.getPratilipiId());
        Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
        if (author != null && userPratilipi.getUserId().equals(author.getUserId()))
            return false;
    }
    return true;
}
Also used : AccessToken(com.pratilipi.data.type.AccessToken) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Aggregations

AccessToken (com.pratilipi.data.type.AccessToken)17 DataAccessor (com.pratilipi.data.DataAccessor)8 Author (com.pratilipi.data.type.Author)8 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)5 Date (java.util.Date)5 UserAuthor (com.pratilipi.data.type.UserAuthor)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 Post (com.pratilipi.api.annotation.Post)2 GenericResponse (com.pratilipi.api.shared.GenericResponse)2 AuditLog (com.pratilipi.data.type.AuditLog)2 Pratilipi (com.pratilipi.data.type.Pratilipi)2 AccessTokenEntity (com.pratilipi.data.type.gae.AccessTokenEntity)2 Gson (com.google.gson.Gson)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 AuthorData (com.pratilipi.data.client.AuthorData)1 PratilipiData (com.pratilipi.data.client.PratilipiData)1 TagData (com.pratilipi.data.client.TagData)1 UserData (com.pratilipi.data.client.UserData)1 Comment (com.pratilipi.data.type.Comment)1 Vote (com.pratilipi.data.type.Vote)1