aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/controllers/Notifications.java
blob: 2f0dc3350a34b6ee616418beb63cefc901658e22 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.juick.api.controllers;

import com.juick.Message;
import com.juick.Status;
import com.juick.TokensList;
import com.juick.User;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.service.MessagesService;
import com.juick.service.PushQueriesService;
import com.juick.service.SubscriptionService;
import com.juick.util.UserUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

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

    @RequestMapping(value = "/notifications", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<List<TokensList>> doGet(
            @RequestParam(required = false, defaultValue = "0") int uid,
            @RequestParam(required = false, defaultValue = "0") int mid,
            @RequestParam(required = false, defaultValue = "0") int rid) {
        User visitor = UserUtils.getCurrentUser();
        if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
            throw new HttpForbiddenException();
        }
        List<TokensList> tokensLists = new ArrayList<>();
        if (uid > 0 && mid == 0) {
            // PM
            TokensList gcmTokens = new TokensList();
            gcmTokens.setType("gcm");
            gcmTokens.setTokens(pushQueriesService.getGCMRegID(uid));
            tokensLists.add(gcmTokens);
            TokensList apnsTokens = new TokensList();
            apnsTokens.setType("apns");
            apnsTokens.setTokens(pushQueriesService.getAPNSToken(uid));
            tokensLists.add(apnsTokens);
            TokensList mpnsTokens = new TokensList();
            mpnsTokens.setType("mpns");
            mpnsTokens.setTokens(pushQueriesService.getMPNSURL(uid));
            tokensLists.add(mpnsTokens);
            return ResponseEntity.ok(tokensLists);

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

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

                    TokensList gcmTokens = new TokensList();
                    gcmTokens.setType("gcm");
                    gcmTokens.setTokens(pushQueriesService.getGCMTokens(uids));
                    tokensLists.add(gcmTokens);
                    TokensList apnsTokens = new TokensList();
                    apnsTokens.setType("apns");
                    apnsTokens.setTokens(pushQueriesService.getAPNSTokens(uids));
                    tokensLists.add(apnsTokens);
                    TokensList mpnsTokens = new TokensList();
                    mpnsTokens.setType("mpns");
                    mpnsTokens.setTokens(pushQueriesService.getMPNSTokens(uids));
                    tokensLists.add(mpnsTokens);
                    return ResponseEntity.ok(tokensLists);
                }
            }
        }
        throw new HttpBadRequestException();
    }

    @RequestMapping(value = "/notifications", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Status doDelete(
            @RequestBody TokensList list) throws IOException {
        User visitor = UserUtils.getCurrentUser();
        // FIXME: it is possible to delete other user's tokens
        if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) {
            throw new HttpForbiddenException();
        }
        switch (list.getType()) {
            case "gcm":
                list.getTokens().forEach(t -> pushQueriesService.deleteGCMToken(t));
                break;
            case "apns":
                list.getTokens().forEach(t -> pushQueriesService.deleteAPNSToken(t));
                break;
            case "mpns":
                list.getTokens().forEach(t -> pushQueriesService.deleteMPNSToken(t));
                break;
            default:
                throw new HttpBadRequestException();
        }

        return Status.OK;
    }

    @RequestMapping(value = "/notifications", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Status doPut(
            @RequestBody TokensList list) throws IOException {
        User visitor = UserUtils.getCurrentUser();
        if (visitor.getUid() == 0) {
            throw new HttpForbiddenException();
        }
        switch (list.getType()) {
            case "gcm":
                list.getTokens().forEach(t -> pushQueriesService.addGCMToken(visitor.getUid(), t));
                break;
            case "apns":
                list.getTokens().forEach(t -> pushQueriesService.addAPNSToken(visitor.getUid(), t));
                break;
            case "mpns":
                list.getTokens().forEach(t -> pushQueriesService.addMPNSToken(visitor.getUid(), t));
                break;
            default:
                throw new HttpBadRequestException();
        }
        return Status.OK;
    }

    @Deprecated
    @RequestMapping(value = "/android/register", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Status doAndroidRegister(
            @RequestParam(name = "regid") String regId) {
        User visitor = UserUtils.getCurrentUser();
        if (visitor.getUid() == 0) {
            throw new HttpForbiddenException();
        }
        pushQueriesService.addGCMToken(visitor.getUid(), regId);
        return Status.OK;
    }

    @Deprecated
    @RequestMapping(value = "/android/unregister", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Status doAndroidUnRegister(@RequestParam(name = "regid") String regId) {
        pushQueriesService.deleteGCMToken(regId);
        return Status.OK;
    }

    @Deprecated
    @RequestMapping(value = "/winphone/register", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Status doWinphoneRegister(
            Principal principal,
            @RequestParam(name = "url") String regId) {
        User visitor = UserUtils.getCurrentUser();
        pushQueriesService.addMPNSToken(visitor.getUid(), regId);
        return Status.OK;
    }

    @Deprecated
    @RequestMapping(value = "/winphone/unregister", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Status doWinphoneUnRegister(@RequestParam(name = "url") String regId) {
        pushQueriesService.deleteMPNSToken(regId);
        return Status.OK;
    }
}