From acd23afcb7e61db7f83872991ae1a4e0f214d667 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 4 Feb 2023 16:55:12 +0300 Subject: Notifications API: check if token is already exists --- src/main/java/com/juick/www/api/Notifications.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/www/api/Notifications.java b/src/main/java/com/juick/www/api/Notifications.java index ac5a635b..df2fb507 100644 --- a/src/main/java/com/juick/www/api/Notifications.java +++ b/src/main/java/com/juick/www/api/Notifications.java @@ -123,9 +123,7 @@ public class Notifications { if (!visitor.equals(serviceUser)) { throw new HttpForbiddenException(); } - list.forEach(t -> { - userService.deleteToken(t.type(), t.token()); - }); + list.forEach(t -> userService.deleteToken(t.type(), t.token())); return Status.OK; } @@ -135,8 +133,11 @@ public class Notifications { public Status doPut( @Parameter(hidden = true) User visitor, @RequestBody List list) { + var userTokens = collectTokens(visitor.getUid()); list.forEach(t -> { - userService.addToken(visitor.getUid(), t.type(), t.token()); + if (userTokens.getTokens().stream().noneMatch(existing -> existing.token().equals(t.token()))) { + userService.addToken(visitor.getUid(), t.type(), t.token()); + } }); return Status.OK; } @@ -146,7 +147,10 @@ public class Notifications { public Status doAndroidRegister( @Parameter(hidden = true) User visitor, @RequestParam(name = "regid") String regId) { - userService.addToken(visitor.getUid(),"fcm", regId); + var userTokens = collectTokens(visitor.getUid()); + if (userTokens.getTokens().stream().noneMatch(existing -> existing.token().equals(regId))) { + userService.addToken(visitor.getUid(), "fcm", regId); + } return Status.OK; } @@ -155,7 +159,10 @@ public class Notifications { public Status doWinphoneRegister( @Parameter(hidden = true) User visitor, @RequestParam(name = "url") String regId) { - userService.addToken(visitor.getUid(), "mpns", regId); + var userTokens = collectTokens(visitor.getUid()); + if (userTokens.getTokens().stream().noneMatch(existing -> existing.token().equals(regId))) { + userService.addToken(visitor.getUid(), "mpns", regId); + } return Status.OK; } } -- cgit v1.2.3