diff options
author | Vitaly Takmazov | 2017-02-05 21:57:02 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-02-05 21:57:02 +0300 |
commit | 767e62fbf23460e6fb99a9c75e00798d85036326 (patch) | |
tree | a4b4390babc37c168488f41b912c8557dad3ba90 | |
parent | ce16a2282d24df8ceb35cbf34fe44783cfdbba02 (diff) |
juick-api: update notifications api
-rw-r--r-- | juick-api/src/main/java/com/juick/api/controllers/Notifications.java | 59 | ||||
-rw-r--r-- | juick-notifications/src/main/java/com/juick/components/Notifications.java | 54 |
2 files changed, 56 insertions, 57 deletions
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<List<String>> doGet( - @RequestParam String type, + public ResponseEntity<List<TokensList>> 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<TokensList> 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<User> 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<Integer> 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); } } } diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java index f41db9e3..e7adac74 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -20,6 +20,7 @@ package com.juick.components; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.android.gcm.server.*; +import com.juick.TokensList; import com.juick.components.mpns.MPNSError; import com.juick.components.mpns.MPNSToken; import com.notnoop.apns.APNS; @@ -109,20 +110,29 @@ public class Notifications implements AutoCloseable { boolean isReply = jmsg.getRid() > 0; int pmTo = NumberUtils.toInt(msg.getTo().getLocal(), 0); - /*** ANDROID ***/ - final List<String> regids = new ArrayList<>(); + final List<TokensList> tokensList = new ArrayList<>(); if (isPM) { - regids.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%d", + tokensList.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%d", pmTo), - HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() { + HttpMethod.GET, null, new ParameterizedTypeReference<List<TokensList>>() { }).getBody()); } else { - regids.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%s&mid=%s", - jmsg.getUser().getUid(), jmsg.getMid()), - HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() { - }).getBody()); + if (isReply) { + tokensList.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%d&mid=%d&rid=%d", + jmsg.getUser().getUid(), jmsg.getMid(), jmsg.getRid()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<TokensList>>() { + }).getBody()); + } else { + tokensList.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%s&mid=%s", + jmsg.getUser().getUid(), jmsg.getMid()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<TokensList>>() { + }).getBody()); + } } + // GCM + List<String> regids = tokensList.stream().filter(t -> t.getType().equals("gcm")) + .flatMap(t -> t.getTokens().stream()).collect(Collectors.toList()); if (!regids.isEmpty()) { try { ObjectMapper messageSerializer = new ObjectMapper(); @@ -156,18 +166,8 @@ public class Notifications implements AutoCloseable { } /*** WinPhone ***/ - final List<String> urls = new ArrayList<>(); - if (isPM) { - urls.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=mpns&uid=%s", - pmTo), - HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() { - }).getBody()); - } else { - urls.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=mpns&uid=%s&mid=%s", - jmsg.getUser().getUid(), jmsg.getMid()), - HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() { - }).getBody()); - } + List<String> urls = tokensList.stream().filter(t -> t.getType().equals("mpns")) + .flatMap(t -> t.getTokens().stream()).collect(Collectors.toList()); if (urls.isEmpty()) { logger.info("WNS: no recipients"); @@ -203,18 +203,8 @@ public class Notifications implements AutoCloseable { } /*** iOS ***/ - final List<String> tokens = new ArrayList<>(); - if (isPM) { - tokens.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=apns&uid=%s", - pmTo), - HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() { - }).getBody()); - } else { - tokens.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=apns&uid=%s&mid=%s", - jmsg.getUser().getUid(), jmsg.getMid()), - HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() { - }).getBody()); - } + List<String> tokens = tokensList.stream().filter(t -> t.getType().equals("mpns")) + .flatMap(t -> t.getTokens().stream()).collect(Collectors.toList()); if (!tokens.isEmpty()) { for (String token : tokens) { String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getName()).alertBody(jmsg.getText()).build(); |