1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package com.juick.api;
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 org.apache.commons.lang3.math.NumberUtils;
import org.springframework.jdbc.core.JdbcTemplate;
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.getUName().equals("juick"))) {
response.sendError(403);
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));
case "apns":
List<String> apnsTokens = PushQueries.getAPNSToken(jdbc, uid);
Main.replyJSON(request, response, mapper.writeValueAsString(apnsTokens));
case "mpns":
List<String> mpnsTokens = PushQueries.getWinPhoneURL(jdbc, uid);
Main.replyJSON(request, response, mapper.writeValueAsString(mpnsTokens));
default:
response.sendError(400);
return;
}
} 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));
case "apns":
List<String> apnsTokens = PushQueries.getAPNSTokens(jdbc, uids);
Main.replyJSON(request, response, mapper.writeValueAsString(apnsTokens));
case "mpns":
List<String> mpnsTokens = PushQueries.getWindowsTokens(jdbc, uids);
Main.replyJSON(request, response, mapper.writeValueAsString(mpnsTokens));
default:
response.sendError(400);
return;
}
}
}
}
response.sendError(400);
}
}
|