diff options
4 files changed, 94 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 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; } } diff --git a/juick-server/src/main/java/com/juick/server/PushQueries.java b/juick-server/src/main/java/com/juick/server/PushQueries.java deleted file mode 100644 index 5ee3cef3..00000000 --- a/juick-server/src/main/java/com/juick/server/PushQueries.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package com.juick.server; - -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.util.StringUtils; - -import java.util.List; - -/** - * - * @author ugnich - */ -public class PushQueries { - - public static List<String> getAndroidRegID(JdbcTemplate sql, int uid) { - return sql.queryForList("SELECT regid FROM android WHERE user_id=?", String.class, uid); - } - - public static List<String> getAndroidTokens(JdbcTemplate sql, List<Integer> uids) { - return sql.queryForList("SELECT regid FROM android INNER JOIN users " + - "ON (users.id=android.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")", String.class); - } - - public static List<String> getWinPhoneURL(JdbcTemplate sql, int uid) { - return sql.queryForList("SELECT url FROM winphone WHERE user_id=?", String.class, uid); - } - - public static List<String> getWindowsTokens(JdbcTemplate sql, List<Integer> uids) { - return sql.queryForList("SELECT url FROM winphone INNER JOIN users " + - "ON (users.id=winphone.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")", String.class); - } - - public static List<String> getAPNSToken(JdbcTemplate sql, int uid) { - return sql.queryForList("SELECT token from ios WHERE user_id=?", String.class, uid); - } - - public static List<String> getAPNSTokens(JdbcTemplate sql, List<Integer> uids) { - return sql.queryForList("SELECT token FROM ios INNER JOIN users " + - "ON (users.id=ios.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")", String.class); - } - - public static boolean deleteAPNSToken(JdbcTemplate sql, String token) { - return sql.update("DELETE FROM ios WHERE token=?", token) > 0; - } -} diff --git a/juick-server/src/main/java/com/juick/service/PushQueriesService.java b/juick-server/src/main/java/com/juick/service/PushQueriesService.java index 2e2ac390..7d4bc295 100644 --- a/juick-server/src/main/java/com/juick/service/PushQueriesService.java +++ b/juick-server/src/main/java/com/juick/service/PushQueriesService.java @@ -7,17 +7,27 @@ import java.util.List; * Created by aalexeev on 11/13/16. */ public interface PushQueriesService { - List<String> getAndroidRegID(int uid); + List<String> getGCMRegID(int uid); - List<String> getAndroidTokens(Collection<Integer> uids); + List<String> getGCMTokens(Collection<Integer> uids); - List<String> getWinPhoneURL(int uid); + boolean addGCMToken(Integer uid, String token); - List<String> getWindowsTokens(Collection<Integer> uids); + boolean deleteGCMToken(String token); + + List<String> getMPNSURL(int uid); + + List<String> getMPNSTokens(Collection<Integer> uids); + + boolean addMPNSToken(Integer uid, String token); + + boolean deleteMPNSToken(String token); List<String> getAPNSToken(int uid); List<String> getAPNSTokens(Collection<Integer> uids); + boolean addAPNSToken(Integer uid, String token); + boolean deleteAPNSToken(String token); } diff --git a/juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java b/juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java index b11f0a79..aca48a19 100644 --- a/juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java @@ -24,7 +24,7 @@ public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueri } @Override - public List<String> getAndroidRegID(final int uid) { + public List<String> getGCMRegID(final int uid) { return getJdbcTemplate().queryForList( "SELECT regid FROM android WHERE user_id=?", String.class, @@ -32,7 +32,7 @@ public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueri } @Override - public List<String> getAndroidTokens(final Collection<Integer> uids) { + public List<String> getGCMTokens(final Collection<Integer> uids) { if (CollectionUtils.isEmpty(uids)) return Collections.emptyList(); @@ -43,7 +43,18 @@ public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueri } @Override - public List<String> getWinPhoneURL(final int uid) { + public boolean addGCMToken(Integer uid, String token) { + return getJdbcTemplate().update("INSERT IGNORE INTO android(user_id,regid) VALUES (?, ?)", + uid, token) > 0; + } + + @Override + public boolean deleteGCMToken(String token) { + return getJdbcTemplate().update("DELETE FROM android WHERE regid=?", token) > 0; + } + + @Override + public List<String> getMPNSURL(final int uid) { return getJdbcTemplate().queryForList( "SELECT url FROM winphone WHERE user_id=?", String.class, @@ -51,7 +62,7 @@ public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueri } @Override - public List<String> getWindowsTokens(final Collection<Integer> uids) { + public List<String> getMPNSTokens(final Collection<Integer> uids) { if (CollectionUtils.isEmpty(uids)) return Collections.emptyList(); @@ -62,6 +73,17 @@ public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueri } @Override + public boolean addMPNSToken(Integer uid, String token) { + return getJdbcTemplate().update("INSERT IGNORE INTO winphone(user_id,url) VALUES (?, ?)", + uid, token) > 0; + } + + @Override + public boolean deleteMPNSToken(String token) { + return getJdbcTemplate().update("DELETE FROM winphone WHERE url=?", token) > 0; + } + + @Override public List<String> getAPNSToken(final int uid) { return getJdbcTemplate().queryForList( "SELECT token from ios WHERE user_id=?", @@ -84,4 +106,10 @@ public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueri new MapSqlParameterSource("ids", uids), String.class); } + + @Override + public boolean addAPNSToken(Integer uid, String token) { + return getJdbcTemplate().update("INSERT IGNORE INTO ios(user_id,token) VALUES (?, ?)", + uid, token) > 0; + } } |