Search in sources :

Example 16 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class UserAuthorDataUtil method getAuthorFollowList.

public static DataListCursorTuple<UserData> getAuthorFollowList(Long authorId, String cursor, Integer offset, Integer resultCount) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    if (author.getFollowCount() == 0L)
        return new DataListCursorTuple<>(new ArrayList<UserData>(0), null, 0L);
    DataListCursorTuple<Long> userIdListCursorTuple = dataAccessor.getUserAuthorFollowList(null, authorId, cursor, offset, resultCount);
    List<Long> userIdList = userIdListCursorTuple.getDataList();
    Map<Long, UserData> users = UserDataUtil.createUserDataList(userIdList, true);
    List<UserData> userDataList = new ArrayList<>(userIdList.size());
    for (Long userId : userIdList) userDataList.add(users.get(userId));
    // Setting UserData.isFollowing flag
    if (AccessTokenFilter.getAccessToken().getUserId().equals(author.getUserId())) {
        for (UserData userData : userDataList) userData.setFollowing(true);
    } else {
        Author authorProfile = dataAccessor.getAuthorByUserId(AccessTokenFilter.getAccessToken().getUserId());
        if (authorProfile != null) {
            List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(userIdList, authorProfile.getId());
            for (int i = 0; i < userIdList.size(); i++) if (userAuthorList.get(i) != null && userAuthorList.get(i).getFollowState() == UserFollowState.FOLLOWING)
                userDataList.get(i).setFollowing(true);
        }
    }
    // Setting AuthorData.isFollowing flag
    List<Long> authorIdList = new ArrayList<>(userDataList.size());
    for (UserData userData : userDataList) authorIdList.add(userData.getAuthor().getId());
    List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(AccessTokenFilter.getAccessToken().getUserId(), authorIdList);
    if (userAuthorList.size() != 0)
        for (int i = 0; i < authorIdList.size(); i++) if (userAuthorList.get(i) != null && userAuthorList.get(i).getFollowState() == UserFollowState.FOLLOWING)
            userDataList.get(i).getAuthor().setFollowing(true);
    return new DataListCursorTuple<>(userDataList, userIdListCursorTuple.getCursor(), author.getFollowCount());
}
Also used : UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author) UserAuthor(com.pratilipi.data.type.UserAuthor)

Example 17 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class GoogleApi method getUserData.

public static UserData getUserData(String googleIdToken) throws InvalidArgumentException, UnexpectedServerException {
    try {
        GoogleIdToken idToken = UxModeFilter.isAndroidApp() ? getAndroidIdTokenVerifier().verify(googleIdToken) : getWebIdTokenVerifier().verify(googleIdToken);
        String authorisedParty = UxModeFilter.isAndroidApp() ? getAppClientId() : getWebClientId();
        if (idToken == null || idToken.getPayload() == null || !idToken.getPayload().getAuthorizedParty().equals(authorisedParty)) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("googleIdToken", "Invalid GoogleIdToken !");
            throw new InvalidArgumentException(jsonObject);
        }
        Payload payload = idToken.getPayload();
        logger.log(Level.INFO, "GoogleApi Payload : " + new Gson().toJson(payload));
        if (payload.get("given_name") == null || ((String) payload.get("given_name")).isEmpty()) {
            logger.log(Level.SEVERE, "Google given_name is missing for GoogleUser: " + payload.getSubject());
            throw new UnexpectedServerException();
        }
        UserData userData = new UserData();
        userData.setGoogleId(payload.getSubject());
        userData.setFirstName((String) payload.get("given_name"));
        userData.setLastName((String) payload.get("family_name"));
        userData.setEmail(payload.getEmail());
        return userData;
    } catch (GeneralSecurityException | IOException e) {
        logger.log(Level.SEVERE, "Google id token verification failed: " + e);
        throw new UnexpectedServerException();
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) UserData(com.pratilipi.data.client.UserData) GeneralSecurityException(java.security.GeneralSecurityException) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) IOException(java.io.IOException) GoogleIdToken(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken)

Example 18 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class FacebookApi method getUserData.

public static UserData getUserData(String fbUserAccessToken) throws InvalidArgumentException, UnexpectedServerException {
    Map<String, String> paramsMap = new HashMap<String, String>();
    paramsMap.put("access_token", fbUserAccessToken);
    paramsMap.put("fields", "id,first_name,last_name,gender,birthday,email");
    String responsePayload = HttpUtil.doGet(GRAPH_API_2p6_URL + "/me", paramsMap);
    JsonObject responseJson = new Gson().fromJson(responsePayload, JsonElement.class).getAsJsonObject();
    if (responseJson.get("error") != null) {
        logger.log(Level.SEVERE, "Error response from Graph Api.");
        // Facebook returns code 190 if access-token is expired
        if (responseJson.get("error").getAsJsonObject().get("code").getAsInt() == 190)
            throw new InvalidArgumentException("Facebook AccessToken is expired.");
        else
            throw new UnexpectedServerException();
    } else {
        if (responseJson.get("first_name") == null || responseJson.get("first_name").getAsString().isEmpty()) {
            logger.log(Level.INFO, "HTTP Response : " + responseJson);
            logger.log(Level.SEVERE, "Facebook first_name is missing for FacebookUser: " + responseJson.get("id").getAsString());
            throw new UnexpectedServerException();
        }
        UserData userData = new UserData();
        userData.setFacebookId(responseJson.get("id").getAsString());
        userData.setFirstName(responseJson.get("first_name").getAsString());
        userData.setLastName(responseJson.get("last_name").getAsString());
        if (responseJson.get("gender") != null)
            userData.setGender(Gender.valueOf(responseJson.get("gender").getAsString().toUpperCase()));
        if (responseJson.get("birthday") != null) {
            String dob = responseJson.get("birthday").getAsString();
            String year = dob.substring(6);
            String month = dob.substring(0, 2);
            String date = dob.substring(3, 5);
            userData.setDateOfBirth(year + "-" + month + "-" + date);
        }
        if (responseJson.get("email") != null)
            userData.setEmail(responseJson.get("email").getAsString());
        return userData;
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) UserData(com.pratilipi.data.client.UserData) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 19 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class BlogPostDataUtil method createBlogPostData.

public static BlogPostData createBlogPostData(BlogPost blogPost, Page blogPostPage) {
    BlogPostData blogPostData = new BlogPostData(blogPost.getId());
    blogPostData.setBlogId(blogPost.getBlogId());
    blogPostData.setTitle(blogPost.getTitle());
    blogPostData.setTitleEn(blogPost.getTitleEn());
    blogPostData.setContent(blogPost.getContent());
    blogPostData.setLanguage(blogPost.getLanguage());
    blogPostData.setState(blogPost.getState());
    User user = DataAccessorFactory.getDataAccessor().getUser(blogPost.getCreatedBy());
    if (user != null) {
        UserData userData = UserDataUtil.createUserData(user);
        blogPostData.setCreatedBy(userData);
    }
    blogPostData.setCreationDate(blogPost.getCreationDate());
    blogPostData.setLastUpdated(blogPost.getLastUpdated());
    blogPostData.setPageUrl(blogPostPage.getUriAlias() == null ? blogPostPage.getUri() : blogPostPage.getUriAlias());
    blogPostData.setAccessToUpdate(hasAccessToUpdateBlogPostData(blogPost, null));
    return blogPostData;
}
Also used : User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) BlogPostData(com.pratilipi.data.client.BlogPostData)

Example 20 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class EmailDataUtil method _createDataModelForVoteReviewEmail.

private static Object[] _createDataModelForVoteReviewEmail(String voteId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Vote vote = dataAccessor.getVote(voteId);
    UserPratilipiData userPratilipi = UserPratilipiDataUtil.createUserPratilipiData(dataAccessor.getUserPratilipi(vote.getParentId()));
    PratilipiData pratilipi = PratilipiDataUtil.createPratilipiData(dataAccessor.getPratilipi(userPratilipi.getPratilipiId()));
    UserData voter = UserDataUtil.createUserData(dataAccessor.getUser(vote.getUserId()));
    Map<String, Object> dataModel = new HashMap<>();
    dataModel.put("pratilipi_title", pratilipi.getTitle() != null ? pratilipi.getTitle() : pratilipi.getTitleEn());
    dataModel.put("pratilipi_page_url", _getDomainName(pratilipi.getLanguage()) + pratilipi.getPageUrl());
    dataModel.put("reviewer_page_url", _getDomainName(userPratilipi.getUser().getLanguage()) + userPratilipi.getUser().getProfilePageUrl());
    dataModel.put("reviewer_image_url", userPratilipi.getUser().getAuthor().getProfileImageUrl(64));
    dataModel.put("reviewer_name", userPratilipi.getUser().getAuthor().getName() != null ? userPratilipi.getUser().getAuthor().getName() : userPratilipi.getUser().getAuthor().getNameEn());
    dataModel.put("review_creation_date", _getDateFormat(userPratilipi.getReviewDate()));
    dataModel.put("review_review", HtmlUtil.toPlainText(userPratilipi.getReview()));
    if (userPratilipi.getRating() != null)
        dataModel.put("rating", userPratilipi.getRating());
    dataModel.put("voter_name", voter.getAuthor().getName() != null ? voter.getAuthor().getName() : voter.getAuthor().getNameEn());
    return new Object[] { dataModel, pratilipi.getLanguage() };
}
Also used : Vote(com.pratilipi.data.type.Vote) UserData(com.pratilipi.data.client.UserData) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData) PratilipiData(com.pratilipi.data.client.PratilipiData) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData)

Aggregations

UserData (com.pratilipi.data.client.UserData)28 DataAccessor (com.pratilipi.data.DataAccessor)14 User (com.pratilipi.data.type.User)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 Post (com.pratilipi.api.annotation.Post)6 AuthorData (com.pratilipi.data.client.AuthorData)6 JsonObject (com.google.gson.JsonObject)5 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)5 Gson (com.google.gson.Gson)4 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 UserPratilipiData (com.pratilipi.data.client.UserPratilipiData)4 Author (com.pratilipi.data.type.Author)4 ArrayList (java.util.ArrayList)4 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)3 PratilipiData (com.pratilipi.data.client.PratilipiData)3 AuditLog (com.pratilipi.data.type.AuditLog)3 JsonElement (com.google.gson.JsonElement)2 GenericResponse (com.pratilipi.api.shared.GenericResponse)2 Language (com.pratilipi.common.type.Language)2