aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/controllers/Notifications.java
blob: 0d0d09654e47e2980a85ab414b10dda28c22c686 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.juick.api.controllers;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.Message;
import com.juick.User;
import com.juick.api.util.HttpBadRequestException;
import com.juick.api.util.HttpForbiddenException;
import com.juick.server.helpers.Status;
import com.juick.server.helpers.TokensList;
import com.juick.service.MessagesService;
import com.juick.service.PushQueriesService;
import com.juick.service.SubscriptionService;
import com.juick.service.UserService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by vitalyster on 24.10.2016.
 */
@Controller
@ResponseBody
public class Notifications {

    @Inject
    PushQueriesService pushQueriesService;
    @Inject
    UserService userService;
    @Inject
    MessagesService messagesService;
    @Inject
    SubscriptionService subscriptionService;

    @RequestMapping(value = "/notifications", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<String> doGet(HttpServletRequest request) {
        String auth = request.getHeader("Authorization");
        int vuid = userService.getUIDByHttpAuth(auth);
        if (vuid == -1) {
            throw new HttpForbiddenException();
        }
        if (vuid == 0) {
            String hash = request.getParameter("hash");
            if (hash != null && hash.length() == 16) {
                vuid  = userService.getUIDbyHash(hash);
            }
        }
        if (vuid == 0) {
            throw new HttpForbiddenException();
        }
        User visitor = userService.getUserByUID(vuid).orElse(new User());
        if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
            throw new HttpForbiddenException();
        }
        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":
                    return pushQueriesService.getAndroidRegID(uid);
                case "apns":
                    return pushQueriesService.getAPNSToken(uid);
                case "mpns":
                    return pushQueriesService.getWinPhoneURL(uid);
                default:
                    throw new HttpBadRequestException();
            }

        } else {
            if (mid > 0) {
                Message msg = messagesService.getMessage(mid);
                if (msg != null) {
                    List<User> users;
                    if (msg.getRid() > 0) {
                        users = subscriptionService.getUsersSubscribedToComments(mid, msg.getUser().getUid());
                    } else {
                        users = subscriptionService.getSubscribedUsers(msg.getUser().getUid(), mid);
                    }

                    List<Integer> uids = users.stream().map(User::getUid).collect(Collectors.toList());

                    switch (type) {
                        case "gcm":
                            return pushQueriesService.getAndroidTokens(uids);
                        case "apns":
                            return pushQueriesService.getAPNSTokens(uids);
                        case "mpns":
                            return pushQueriesService.getWindowsTokens(uids);
                        default:
                            throw new HttpBadRequestException();
                    }
                }
            }
        }
        throw new HttpBadRequestException();
    }

    @RequestMapping(value = "/notifications", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Status doDelete(HttpServletRequest request, @RequestBody String requestBody) throws IOException {
        // TODO: use spring-security
        String auth = request.getHeader("Authorization");
        int vuid = userService.getUIDByHttpAuth(auth);
        if (vuid == -1) {
            throw new HttpForbiddenException();
        }
        if (vuid == 0) {
            String hash = request.getParameter("hash");
            if (hash != null && hash.length() == 16) {
                vuid  = userService.getUIDbyHash(hash);
            }
        }
        if (vuid == 0) {
            throw new HttpForbiddenException();
        }
        User visitor = userService.getUserByUID(vuid).orElse(new User());
        if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
            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);
        list.getTokens().forEach(t -> pushQueriesService.deleteAPNSToken(t));
        return new Status("ok");
    }
}