diff options
author | Vitaly Takmazov | 2017-06-29 17:09:49 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-06-29 17:13:29 +0300 |
commit | b75b5a3d1a3bf001923d49923dcebee95ba2327e (patch) | |
tree | 5080eacdf348c9598885ad5d3b5c567632db3717 /juick-api/src/main/java/com/juick/api/controllers | |
parent | 02723131139806c761539a42a5fa80b68ecadee8 (diff) |
notifications and configurations refactoring
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/controllers')
-rw-r--r-- | juick-api/src/main/java/com/juick/api/controllers/Notifications.java | 112 |
1 files changed, 51 insertions, 61 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 c121efb6..96edde51 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 @@ -2,14 +2,16 @@ package com.juick.api.controllers; import com.juick.Message; import com.juick.Status; -import com.juick.TokensList; +import com.juick.DeviceRegistration; import com.juick.User; +import com.juick.server.helpers.AnonymousUser; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.PushQueriesService; import com.juick.service.SubscriptionService; import com.juick.server.util.UserUtils; +import com.juick.service.UserService; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -17,7 +19,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.Collections; import java.util.List; import java.util.stream.Collectors; @@ -33,9 +35,20 @@ public class Notifications { MessagesService messagesService; @Inject SubscriptionService subscriptionService; + @Inject + UserService userService; + + + private User collectTokens(Integer uid) { + User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE); + pushQueriesService.getGCMRegID(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("gcm", t))); + pushQueriesService.getAPNSToken(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("apns", t))); + pushQueriesService.getMPNSURL(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("mpns", t))); + return user; + } @RequestMapping(value = "/notifications", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public ResponseEntity<List<TokensList>> doGet( + public ResponseEntity<List<User>> doGet( @RequestParam(required = false, defaultValue = "0") int uid, @RequestParam(required = false, defaultValue = "0") int mid, @RequestParam(required = false, defaultValue = "0") int rid) { @@ -43,23 +56,9 @@ public class Notifications { if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) { throw new HttpForbiddenException(); } - List<TokensList> tokensLists = new ArrayList<>(); if (uid > 0 && mid == 0) { // PM - 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); - + return ResponseEntity.ok(Collections.singletonList(collectTokens(uid))); } else { if (mid > 0) { Message msg = messagesService.getMessage(mid); @@ -72,21 +71,8 @@ public class Notifications { users = subscriptionService.getSubscribedUsers(msg.getUser().getUid(), mid); } - List<Integer> uids = users.stream().map(User::getUid).collect(Collectors.toList()); - - 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); + return ResponseEntity.ok(users.stream().map(User::getUid) + .map(this::collectTokens).collect(Collectors.toList())); } } } @@ -95,49 +81,53 @@ public class Notifications { @RequestMapping(value = "/notifications", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Status doDelete( - @RequestBody TokensList list) throws IOException { + @RequestBody List<DeviceRegistration> list) throws IOException { User visitor = UserUtils.getCurrentUser(); // FIXME: it is possible to delete other user's tokens if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) { throw new HttpForbiddenException(); } - switch (list.getType()) { - case "gcm": - list.getTokens().forEach(t -> pushQueriesService.deleteGCMToken(t)); - break; - case "apns": - list.getTokens().forEach(t -> pushQueriesService.deleteAPNSToken(t)); - break; - case "mpns": - list.getTokens().forEach(t -> pushQueriesService.deleteMPNSToken(t)); - break; - default: - throw new HttpBadRequestException(); - } + list.forEach(t -> { + switch (t.getType()) { + case "gcm": + pushQueriesService.deleteGCMToken(t.getToken()); + break; + case "apns": + pushQueriesService.deleteAPNSToken(t.getToken()); + break; + case "mpns": + pushQueriesService.deleteMPNSToken(t.getToken()); + break; + default: + throw new HttpBadRequestException(); + } + }); return Status.OK; } @RequestMapping(value = "/notifications", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Status doPut( - @RequestBody TokensList list) throws IOException { + @RequestBody List<DeviceRegistration> list) throws IOException { User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { throw new HttpForbiddenException(); } - switch (list.getType()) { - case "gcm": - list.getTokens().forEach(t -> pushQueriesService.addGCMToken(visitor.getUid(), t)); - break; - case "apns": - list.getTokens().forEach(t -> pushQueriesService.addAPNSToken(visitor.getUid(), t)); - break; - case "mpns": - list.getTokens().forEach(t -> pushQueriesService.addMPNSToken(visitor.getUid(), t)); - break; - default: - throw new HttpBadRequestException(); - } + list.forEach(t -> { + switch (t.getType()) { + case "gcm": + pushQueriesService.addGCMToken(visitor.getUid(), t.getToken()); + break; + case "apns": + pushQueriesService.addAPNSToken(visitor.getUid(), t.getToken()); + break; + case "mpns": + pushQueriesService.addMPNSToken(visitor.getUid(), t.getToken()); + break; + default: + throw new HttpBadRequestException(); + } + }); return Status.OK; } |