From a19d224e5de6a067cc559a23cf6b4afdef060d56 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 1 Dec 2016 15:06:08 +0300 Subject: juick-api: Notifications controller from perl, refactoring --- .../com/juick/api/controllers/Notifications.java | 53 ++++++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'juick-api/src/main/java') 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 35298095..789adf5f 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 @@ -54,11 +54,11 @@ public class Notifications { if (uid > 0) { switch (type) { case "gcm": - return ResponseEntity.ok(pushQueriesService.getAndroidRegID(uid)); + return ResponseEntity.ok(pushQueriesService.getGCMRegID(uid)); case "apns": return ResponseEntity.ok(pushQueriesService.getAPNSToken(uid)); case "mpns": - return ResponseEntity.ok(pushQueriesService.getWinPhoneURL(uid)); + return ResponseEntity.ok(pushQueriesService.getMPNSURL(uid)); default: throw new HttpBadRequestException(); } @@ -78,11 +78,11 @@ public class Notifications { switch (type) { case "gcm": - return ResponseEntity.ok(pushQueriesService.getAndroidTokens(uids)); + return ResponseEntity.ok(pushQueriesService.getGCMTokens(uids)); case "apns": return ResponseEntity.ok(pushQueriesService.getAPNSTokens(uids)); case "mpns": - return ResponseEntity.ok(pushQueriesService.getWindowsTokens(uids)); + return ResponseEntity.ok(pushQueriesService.getMPNSTokens(uids)); default: throw new HttpBadRequestException(); } @@ -98,6 +98,7 @@ public class Notifications { @RequestBody String requestBody) throws IOException { String name = UserUtils.getUsername(principal, null); User visitor = userService.getUserByName(name); + // FIXME: it is possible to delete other user's tokens if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) { throw new HttpForbiddenException(); } @@ -106,7 +107,49 @@ public class Notifications { mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); TokensList list = mapper.readValue(requestBody, TokensList.class); - list.getTokens().forEach(t -> pushQueriesService.deleteAPNSToken(t)); + 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(); + } + + return Status.OK; + } + @RequestMapping(value = "/notifications", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public Status doPut( + Principal principal, + @RequestBody String requestBody) throws IOException { + String name = UserUtils.getUsername(principal, null); + User visitor = userService.getUserByName(name); + if (visitor.getUid() == 0) { + throw new HttpForbiddenException(); + } + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); + TokensList list = mapper.readValue(requestBody, TokensList.class); + 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(); + } return Status.OK; } } -- cgit v1.2.3