From 767e62fbf23460e6fb99a9c75e00798d85036326 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 5 Feb 2017 21:57:02 +0300 Subject: juick-api: update notifications api --- .../com/juick/api/controllers/Notifications.java | 59 +++++++++++++--------- 1 file changed, 34 insertions(+), 25 deletions(-) (limited to 'juick-api/src') diff --git a/juick-api/src/main/java/com/juick/api/controllers/Notifications.java b/juick-api/src/main/java/com/juick/api/controllers/Notifications.java index 7b901a15..2f0dc335 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Notifications.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Notifications.java @@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.*; import javax.inject.Inject; import java.io.IOException; import java.security.Principal; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -34,50 +35,58 @@ public class Notifications { SubscriptionService subscriptionService; @RequestMapping(value = "/notifications", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public ResponseEntity> doGet( - @RequestParam String type, + public ResponseEntity> doGet( @RequestParam(required = false, defaultValue = "0") int uid, - @RequestParam(required = false, defaultValue = "0") int mid) { + @RequestParam(required = false, defaultValue = "0") int mid, + @RequestParam(required = false, defaultValue = "0") int rid) { User visitor = UserUtils.getCurrentUser(); if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) { throw new HttpForbiddenException(); } + List tokensLists = new ArrayList<>(); if (uid > 0 && mid == 0) { // PM - switch (type) { - case "gcm": - return ResponseEntity.ok(pushQueriesService.getGCMRegID(uid)); - case "apns": - return ResponseEntity.ok(pushQueriesService.getAPNSToken(uid)); - case "mpns": - return ResponseEntity.ok(pushQueriesService.getMPNSURL(uid)); - default: - throw new HttpBadRequestException(); - } + TokensList gcmTokens = new TokensList(); + gcmTokens.setType("gcm"); + gcmTokens.setTokens(pushQueriesService.getGCMRegID(uid)); + tokensLists.add(gcmTokens); + TokensList apnsTokens = new TokensList(); + apnsTokens.setType("apns"); + apnsTokens.setTokens(pushQueriesService.getAPNSToken(uid)); + tokensLists.add(apnsTokens); + TokensList mpnsTokens = new TokensList(); + mpnsTokens.setType("mpns"); + mpnsTokens.setTokens(pushQueriesService.getMPNSURL(uid)); + tokensLists.add(mpnsTokens); + return ResponseEntity.ok(tokensLists); } else { if (mid > 0) { Message msg = messagesService.getMessage(mid); if (msg != null) { List users; - if (msg.getRid() > 0) { - users = subscriptionService.getUsersSubscribedToComments(mid, uid); + if (rid > 0) { + Message reply = messagesService.getReply(mid, rid); + users = subscriptionService.getUsersSubscribedToComments(mid, reply.getUser().getUid()); } else { users = subscriptionService.getSubscribedUsers(msg.getUser().getUid(), mid); } List uids = users.stream().map(User::getUid).collect(Collectors.toList()); - switch (type) { - case "gcm": - return ResponseEntity.ok(pushQueriesService.getGCMTokens(uids)); - case "apns": - return ResponseEntity.ok(pushQueriesService.getAPNSTokens(uids)); - case "mpns": - return ResponseEntity.ok(pushQueriesService.getMPNSTokens(uids)); - default: - throw new HttpBadRequestException(); - } + TokensList gcmTokens = new TokensList(); + gcmTokens.setType("gcm"); + gcmTokens.setTokens(pushQueriesService.getGCMTokens(uids)); + tokensLists.add(gcmTokens); + TokensList apnsTokens = new TokensList(); + apnsTokens.setType("apns"); + apnsTokens.setTokens(pushQueriesService.getAPNSTokens(uids)); + tokensLists.add(apnsTokens); + TokensList mpnsTokens = new TokensList(); + mpnsTokens.setType("mpns"); + mpnsTokens.setTokens(pushQueriesService.getMPNSTokens(uids)); + tokensLists.add(mpnsTokens); + return ResponseEntity.ok(tokensLists); } } } -- cgit v1.2.3