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.server.helpers.Status; import com.juick.server.helpers.TokensList; 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.service.UserService; import com.juick.util.UserUtils; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.inject.Inject; import java.io.IOException; import java.security.Principal; 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 ResponseEntity> doGet( @RequestParam String type, @RequestParam(required = false, defaultValue = "0") int uid, @RequestParam(required = false, defaultValue = "0") int mid) { User visitor = UserUtils.getCurrentUser(); if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) { throw new HttpForbiddenException(); } if (uid > 0 && mid == 0) { // PM switch (type) { case "gcm": return ResponseEntity.ok(pushQueriesService.getGCMRegID(uid)); case "apns": return ResponseEntity.ok(pushQueriesService.getAPNSToken(uid)); case "mpns": return ResponseEntity.ok(pushQueriesService.getMPNSURL(uid)); default: throw new HttpBadRequestException(); } } else { if (mid > 0) { Message msg = messagesService.getMessage(mid); if (msg != null) { List users; if (msg.getRid() > 0) { users = subscriptionService.getUsersSubscribedToComments(mid, uid); } else { users = subscriptionService.getSubscribedUsers(msg.getUser().getUid(), mid); } List uids = users.stream().map(User::getUid).collect(Collectors.toList()); switch (type) { case "gcm": return ResponseEntity.ok(pushQueriesService.getGCMTokens(uids)); case "apns": return ResponseEntity.ok(pushQueriesService.getAPNSTokens(uids)); case "mpns": return ResponseEntity.ok(pushQueriesService.getMPNSTokens(uids)); default: throw new HttpBadRequestException(); } } } } throw new HttpBadRequestException(); } @RequestMapping(value = "/notifications", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Status doDelete( @RequestBody String requestBody) 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(); } 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); 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 String requestBody) throws IOException { User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { 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); 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; } }