From 834fef2a7794d769144c7c087a8b1fa0a2a9bb21 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 14 Jan 2023 12:22:53 +0300 Subject: DB: merge ios, android, winphone tables to user_services --- src/main/java/com/juick/www/api/Notifications.java | 45 +++++++--------------- 1 file changed, 13 insertions(+), 32 deletions(-) (limited to 'src/main/java/com/juick/www/api') diff --git a/src/main/java/com/juick/www/api/Notifications.java b/src/main/java/com/juick/www/api/Notifications.java index e5e85eb7..1ebe3c61 100644 --- a/src/main/java/com/juick/www/api/Notifications.java +++ b/src/main/java/com/juick/www/api/Notifications.java @@ -19,10 +19,10 @@ package com.juick.www.api; import com.juick.model.*; import com.juick.service.*; -import com.juick.util.HttpBadRequestException; import com.juick.util.HttpForbiddenException; import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Parameter; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; @@ -31,6 +31,7 @@ import org.springframework.web.bind.annotation.*; import javax.inject.Inject; import java.util.Collections; import java.util.List; +import java.util.stream.Stream; /** * Created by vitalyster on 24.10.2016. @@ -55,17 +56,12 @@ public class Notifications { private User collectTokens(Integer uid) { User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE); user.setUnreadCount(messagesService.getUnread(user).size()); - pushQueriesService.getGCMRegID(uid).forEach(t -> user.getTokens().add(new ExternalToken(null, "gcm", t, null))); - pushQueriesService.getAPNSToken(uid).forEach(t -> user.getTokens().add(new ExternalToken(null, "apns", t, null))); - pushQueriesService.getMPNSURL(uid).forEach(t -> user.getTokens().add(new ExternalToken(null, "mpns", t, null))); - List xmppJids = userService.getJIDsbyUID(uid).stream() - .map(jid -> new ExternalToken(null, "xmpp", jid, null)) - .toList(); - user.getTokens().addAll(xmppJids); - List tgIds = telegramService.getTelegramIdentifiers(Collections.singletonList(user)).stream() - .map(tgId -> new ExternalToken(null, "durov", String.valueOf(tgId), null)) - .toList(); - user.getTokens().addAll(tgIds); + var tokens = pushQueriesService.getTokens(List.of(user.getUid())); + var xmppJids = userService.getJIDsbyUID(uid).stream() + .map(jid -> new ExternalToken(null, "xmpp", jid, null)); + var tgIds = telegramService.getTelegramIdentifiers(Collections.singletonList(user)).stream() + .map(tgId -> new ExternalToken(null, "durov", String.valueOf(tgId), null)); + user.setTokens(Stream.concat(Stream.concat(tokens.stream(), xmppJids), tgIds).toList()); return user; } @@ -117,12 +113,7 @@ public class Notifications { throw new HttpForbiddenException(); } list.forEach(t -> { - switch (t.type()) { - case "gcm" -> pushQueriesService.deleteGCMToken(t.token()); - case "apns" -> pushQueriesService.deleteAPNSToken(t.token()); - case "mpns" -> pushQueriesService.deleteMPNSToken(t.token()); - default -> throw new HttpBadRequestException(); - } + pushQueriesService.deleteToken(t.type(), t.token()); }); return Status.OK; @@ -136,12 +127,7 @@ public class Notifications { throw new HttpForbiddenException(); } list.forEach(t -> { - switch (t.type()) { - case "gcm" -> pushQueriesService.deleteGCMToken(t.token()); - case "apns" -> pushQueriesService.deleteAPNSToken(t.token()); - case "mpns" -> pushQueriesService.deleteMPNSToken(t.token()); - default -> throw new HttpBadRequestException(); - } + pushQueriesService.deleteToken(t.type(), t.token()); }); return Status.OK; @@ -153,12 +139,7 @@ public class Notifications { @Parameter(hidden = true) User visitor, @RequestBody List list) { list.forEach(t -> { - switch (t.type()) { - case "gcm" -> pushQueriesService.addGCMToken(visitor.getUid(), t.token()); - case "apns" -> pushQueriesService.addAPNSToken(visitor.getUid(), t.token()); - case "mpns" -> pushQueriesService.addMPNSToken(visitor.getUid(), t.token()); - default -> throw new HttpBadRequestException(); - } + pushQueriesService.addToken(visitor.getUid(), t.type(), t.token()); }); return Status.OK; } @@ -168,7 +149,7 @@ public class Notifications { public Status doAndroidRegister( @Parameter(hidden = true) User visitor, @RequestParam(name = "regid") String regId) { - pushQueriesService.addGCMToken(visitor.getUid(), regId); + pushQueriesService.addToken(visitor.getUid(),"fcm", regId); return Status.OK; } @@ -177,7 +158,7 @@ public class Notifications { public Status doWinphoneRegister( @Parameter(hidden = true) User visitor, @RequestParam(name = "url") String regId) { - pushQueriesService.addMPNSToken(visitor.getUid(), regId); + pushQueriesService.addToken(visitor.getUid(), "mpns", regId); return Status.OK; } } -- cgit v1.2.3