private static void fetchLikesAndUpdateUsers(Post post, Set<User> users, Page page) { PagedList<Reference> likes = facebook.likeOperations().getLikes(post.getId()); int totalLikes = likes.size(); for (Reference userLike : likes) { User user = users .stream() .filter(f -> f.getId().equals(userLike.getId())) .findFirst() .orElse(new User(userLike.getId(), userLike.getName(), page)); user.addLike(post, page.getId()); users.add(user); } while (likes.getNextPage() != null) { likes = facebook.likeOperations().getLikes(post.getId(), likes.getNextPage()); totalLikes += likes.size(); for (Reference userLike : likes) { User user = users .stream() .filter(f -> f.getId().equals(userLike.getId())) .findFirst() .orElse(new User(userLike.getId(), userLike.getName(), page)); user.addLike(post, page.getId()); users.add(user); } } post.getExtraData().putIfAbsent("likesCount", totalLikes); }
private static void fetchCommentAndUpdateUsers( Post post, Set<Comment> commentsToSave, Set<User> users, Page page) { PagedList<Comment> comments = facebook.commentOperations().getComments(post.getId()); int totalComments = comments.size(); for (Comment comment : comments) { User user = users .stream() .filter(f -> f.getId().equals(comment.getFrom().getId())) .findFirst() .orElse(new User(comment.getFrom().getId(), comment.getFrom().getName(), page)); user.addComment(comment, post, page.getId()); users.add(user); commentsToSave.add(comment); if (comment.getCommentCount() != null && comment.getCommentCount() > 0) { PagedList<Comment> commentsComments = facebook.commentOperations().getComments(comment.getId()); for (Comment commentsComment : commentsComments) { user = users .stream() .filter(f -> f.getId().equals(commentsComment.getFrom().getId())) .findFirst() .orElse(new User(comment.getFrom().getId(), comment.getFrom().getName(), page)); user.addComment(commentsComment, post, page.getId()); users.add(user); commentsToSave.add(comment); } while (commentsComments.getNextPage() != null) { commentsComments = facebook.commentOperations().getComments(post.getId(), comments.getNextPage()); for (Comment commentsComment : commentsComments) { user = users .stream() .filter(f -> f.getId().equals(commentsComment.getFrom().getId())) .findFirst() .orElse(new User(comment.getFrom().getId(), comment.getFrom().getName(), page)); user.addComment(commentsComment, post, page.getId()); users.add(user); commentsToSave.add(comment); } } } } while (comments.getNextPage() != null) { comments = facebook.commentOperations().getComments(post.getId(), comments.getNextPage()); totalComments += comments.size(); for (Comment comment : comments) { User user = users .stream() .filter(f -> f.getId().equals(comment.getFrom().getId())) .findFirst() .orElse(new User(comment.getFrom().getId(), comment.getFrom().getName(), page)); user.addComment(comment, post, page.getId()); users.add(user); commentsToSave.add(comment); if (comment.getCommentCount() != null && comment.getCommentCount() > 0) { PagedList<Comment> commentsComments = facebook.commentOperations().getComments(comment.getId()); for (Comment commentsComment : commentsComments) { user = users .stream() .filter(f -> f.getId().equals(commentsComment.getFrom().getId())) .findFirst() .orElse(new User(comment.getFrom().getId(), comment.getFrom().getName(), page)); user.addComment(commentsComment, post, page.getId()); users.add(user); commentsToSave.add(comment); } while (commentsComments.getNextPage() != null) { commentsComments = facebook.commentOperations().getComments(post.getId(), comments.getNextPage()); for (Comment commentsComment : commentsComments) { user = users .stream() .filter(f -> f.getId().equals(commentsComment.getFrom().getId())) .findFirst() .orElse( new User(comment.getFrom().getId(), comment.getFrom().getName(), page)); user.addComment(commentsComment, post, page.getId()); users.add(user); commentsToSave.add(comment); } } } } } post.getExtraData().putIfAbsent("commentsCount", totalComments); }