aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/Notifications.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-24 21:38:52 +0300
committerGravatar Vitaly Takmazov2016-11-24 23:26:57 +0300
commitd233943fc29508dc37714852b11b96b0b46b55d9 (patch)
tree129cd564c021261247e72ac70605c1e559c6dac4 /juick-api/src/main/java/com/juick/api/Notifications.java
parentbcd9ae221522fa779ea1fd1ff1ab5b561ca0895f (diff)
juick-api: now on spring-webmvc
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/Notifications.java')
-rw-r--r--juick-api/src/main/java/com/juick/api/Notifications.java109
1 files changed, 0 insertions, 109 deletions
diff --git a/juick-api/src/main/java/com/juick/api/Notifications.java b/juick-api/src/main/java/com/juick/api/Notifications.java
deleted file mode 100644
index dbd128b0..00000000
--- a/juick-api/src/main/java/com/juick/api/Notifications.java
+++ /dev/null
@@ -1,109 +0,0 @@
-package com.juick.api;
-
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.juick.Message;
-import com.juick.User;
-import com.juick.server.MessagesQueries;
-import com.juick.server.PushQueries;
-import com.juick.server.SubscriptionsQueries;
-import com.juick.server.UserQueries;
-import com.juick.server.helpers.TokensList;
-import org.apache.commons.lang3.math.NumberUtils;
-import org.springframework.jdbc.core.JdbcTemplate;
-import spark.utils.IOUtils;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.util.List;
-import java.util.stream.Collectors;
-
-/**
- * Created by vitalyster on 24.10.2016.
- */
-public class Notifications {
- JdbcTemplate jdbc;
- ObjectMapper mapper = new ObjectMapper();
-
- public Notifications(JdbcTemplate jdbc) {
- this.jdbc = jdbc;
- }
-
- public void doGet(HttpServletRequest request, HttpServletResponse response, int vuid) throws IOException {
- User visitor = UserQueries.getUserByUID(jdbc, vuid).orElse(new User());
- if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
- response.sendError(HttpServletResponse.SC_FORBIDDEN);
- return;
- }
- String type = request.getParameter("type");
- int uid = NumberUtils.toInt(request.getParameter("uid"), 0);
- int mid = NumberUtils.toInt(request.getParameter("mid"), 0);
- if (uid > 0) {
- switch (type) {
- case "gcm":
- List<String> tokens = PushQueries.getAndroidRegID(jdbc, uid);
- Main.replyJSON(request, response, mapper.writeValueAsString(tokens));
- break;
- case "apns":
- List<String> apnsTokens = PushQueries.getAPNSToken(jdbc, uid);
- Main.replyJSON(request, response, mapper.writeValueAsString(apnsTokens));
- break;
- case "mpns":
- List<String> mpnsTokens = PushQueries.getWinPhoneURL(jdbc, uid);
- Main.replyJSON(request, response, mapper.writeValueAsString(mpnsTokens));
- break;
- default:
- response.sendError(HttpServletResponse.SC_BAD_REQUEST);
- }
-
- } else {
- if (mid > 0) {
- Message msg = MessagesQueries.getMessage(jdbc, mid);
- if (msg != null) {
- List<User> users;
- if (msg.getRid() > 0) {
- users = SubscriptionsQueries.getUsersSubscribedToComments(jdbc, mid, msg.getUser().getUid());
- } else {
- users = SubscriptionsQueries.getSubscribedUsers(jdbc, msg.getUser().getUid(), mid);
- }
-
- List<Integer> uids = users.stream().map(User::getUid).collect(Collectors.toList());
-
- switch (type) {
- case "gcm":
- List<String> tokens = PushQueries.getAndroidTokens(jdbc, uids);
- Main.replyJSON(request, response, mapper.writeValueAsString(tokens));
- break;
- case "apns":
- List<String> apnsTokens = PushQueries.getAPNSTokens(jdbc, uids);
- Main.replyJSON(request, response, mapper.writeValueAsString(apnsTokens));
- break;
- case "mpns":
- List<String> mpnsTokens = PushQueries.getWindowsTokens(jdbc, uids);
- Main.replyJSON(request, response, mapper.writeValueAsString(mpnsTokens));
- break;
- default:
- response.sendError(HttpServletResponse.SC_BAD_REQUEST);
- }
- }
- } else {
- response.sendError(HttpServletResponse.SC_BAD_REQUEST);
- }
- }
- }
-
- public void doDelete(HttpServletRequest request, HttpServletResponse response, int vuid) throws IOException {
- User visitor = UserQueries.getUserByUID(jdbc, vuid).orElse(new User());
- if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
- response.sendError(HttpServletResponse.SC_FORBIDDEN);
- return;
- }
- 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(IOUtils.toString(request.getInputStream()), TokensList.class);
- list.getTokens().forEach(t -> PushQueries.deleteAPNSToken(jdbc, t));
- }
-}