2. @Service
public class PostService {
private static final int DAY = 24 * 60 * 60000;
@Autowired
private UserRepository users;
@Autowired
private PostRepository posts;
@Autowired
private NewsfeedRepository news;
@Autowired
private CommentRepository comments;
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
PostService
3. @Service
public class PostService {
private static final int DAY = 24 * 60 * 60000;
@Autowired
private UserRepository users;
@Autowired
private PostRepository posts;
@Autowired
private NewsfeedRepository news;
@Autowired
private CommentRepository comments;
@Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
PostService
4. @Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
5. @Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
6. @Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
7. @Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
8. @Autowired
private NotificationService notifications;
public List<PostDAO> postsOf(String authToken, String userId,
int fromPage, int pageSize) {
User u = users.findByAuthtoken(authToken).get(0);
Page<Post> postPage;
if(u.getId().equals(userId) || u.isFriendById(userId)) {
postPage = posts.findAllPostsByUser(
userId, PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
} else {
postPage = posts.findPostsByUser(
userId, VisibilityConstants.PUBLIC.asString(),
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Direction.DESC, "date")));
}
List<PostDAO> response = new ArrayList<>();
for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
PostService
9. for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
public List<PostDAO> newsfeed(String authToken, int fromPage,
int pageSize) {
Page<Newsfeed> n = news.findNewsfeedForUser(authToken,
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Order.desc("postDay"),
Sort.Order.asc("rank"),
Sort.Order.desc("postTimestamp"))));
List<PostDAO> response = new ArrayList<>();
for(Newsfeed c : n) {
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
PostService
10. for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
public List<PostDAO> newsfeed(String authToken, int fromPage,
int pageSize) {
Page<Newsfeed> n = news.findNewsfeedForUser(authToken,
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Order.desc("postDay"),
Sort.Order.asc("rank"),
Sort.Order.desc("postTimestamp"))));
List<PostDAO> response = new ArrayList<>();
for(Newsfeed c : n) {
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
PostService
11. for(Post p : postPage) {
response.add(p.getDAO());
}
return response;
}
public List<PostDAO> newsfeed(String authToken, int fromPage,
int pageSize) {
Page<Newsfeed> n = news.findNewsfeedForUser(authToken,
PageRequest.of(fromPage, pageSize,
Sort.by(Sort.Order.desc("postDay"),
Sort.Order.asc("rank"),
Sort.Order.desc("postTimestamp"))));
List<PostDAO> response = new ArrayList<>();
for(Newsfeed c : n) {
response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
PostService
12. response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
13. response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
14. response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
15. response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
16. response.add(c.getEntry().getDAO());
}
return response;
}
public String post(String authToken, PostDAO pd) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = new Post();
p.setContent(pd.getContent());
p.setDate(System.currentTimeMillis());
p.setStyling(pd.getStyling());
p.setTitle(pd.getTitle());
p.setUser(u);
p.setVisibility(pd.getVisibility());
posts.save(p);
addPostToNewsfeed(u, p);
if(u.getFriends() != null) {
for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
PostService
17. for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
private void addPostToNewsfeed(User u, Post p) {
Newsfeed n = new Newsfeed();
n.setEntry(p);
n.setParent(u);
n.setPostDay(System.currentTimeMillis() / DAY);
n.setPostTimestamp(System.currentTimeMillis());
news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PostService
18. for(User c : u.getFriends()) {
addPostToNewsfeed(c, p);
}
}
return p.getId();
}
private void addPostToNewsfeed(User u, Post p) {
Newsfeed n = new Newsfeed();
n.setEntry(p);
n.setParent(u);
n.setPostDay(System.currentTimeMillis() / DAY);
n.setPostTimestamp(System.currentTimeMillis());
news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PostService
19. news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
PostService
20. news.save(n);
}
public String comment(String authToken, String postId, CommentDAO cd)
throws PermissionException {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!VisibilityConstants.isPublic(p.getVisibility())) {
if(!u.getId().equals(p.getUser().getId())) {
if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
PostService
21. if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
PostService
22. if(!p.getUser().isFriendById(u.getId())) {
throw new
PermissionException("This is a private post!");
}
}
}
Comment c = new Comment();
c.setDate(System.currentTimeMillis());
if(cd.getParentComment() != null) {
c.setParentComment(comments.findById(cd.getParentComment()).
get());
}
c.setText(cd.getText());
c.setUser(u);
comments.save(c);
p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
PostService
23. p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
public void like(String authToken, String postId) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!p.getLikes().contains(u)) {
p.getLikes().add(u);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"liked your post", "uE8DC",
0x587EBE, 0, false,
postId, null));
}
}
}
PostService
24. p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
public void like(String authToken, String postId) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!p.getLikes().contains(u)) {
p.getLikes().add(u);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"liked your post", "uE8DC",
0x587EBE, 0, false,
postId, null));
}
}
}
PostService
25. p.getComments().add(c);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"commented on your post", "uE0B7",
0x00A400, 0, false,
postId, cd.getId()));
return c.getId();
}
public void like(String authToken, String postId) {
User u = users.findByAuthtoken(authToken).get(0);
Post p = posts.findById(postId).get();
if(!p.getLikes().contains(u)) {
p.getLikes().add(u);
posts.save(p);
notifications.sendNotification(u,
new NotificationDAO(null, u.getDAO(),
"liked your post", "uE8DC",
0x587EBE, 0, false,
postId, null));
}
}
}
PostService