package com.juick.api.controllers; import com.juick.Message; import com.juick.Status; import com.juick.DeviceRegistration; import com.juick.User; import com.juick.server.helpers.AnonymousUser; 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.server.util.UserUtils; import com.juick.service.UserService; 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.Collections; 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; @Inject UserService userService; private User collectTokens(Integer uid) { User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE); pushQueriesService.getGCMRegID(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("gcm", t))); pushQueriesService.getAPNSToken(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("apns", t))); pushQueriesService.getMPNSURL(uid).forEach(t -> user.getDevices().add(new DeviceRegistration("mpns", t))); return user; } @RequestMapping(value = "/notifications", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseEntity> 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(); } if (uid > 0 && mid == 0) { // PM return ResponseEntity.ok(Collections.singletonList(collectTokens(uid))); } else { if (mid > 0) { Message msg = messagesService.getMessage(mid); if (msg != null) { List 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); } return ResponseEntity.ok(users.stream().map(User::getUid) .map(this::collectTokens).collect(Collectors.toList())); } } } throw new HttpBadRequestException(); } @RequestMapping(value = "/notifications", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Status doDelete( @RequestBody List 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(); } list.forEach(t -> { switch (t.getType()) { case "gcm": pushQueriesService.deleteGCMToken(t.getToken()); break; case "apns": pushQueriesService.deleteAPNSToken(t.getToken()); break; case "mpns": pushQueriesService.deleteMPNSToken(t.getToken()); break; default: throw new HttpBadRequestException(); } }); return Status.OK; } @RequestMapping(value = "/notifications", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Status doPut( @RequestBody List list) throws IOException { User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { throw new HttpForbiddenException(); } list.forEach(t -> { switch (t.getType()) { case "gcm": pushQueriesService.addGCMToken(visitor.getUid(), t.getToken()); break; case "apns": pushQueriesService.addAPNSToken(visitor.getUid(), t.getToken()); break; case "mpns": pushQueriesService.addMPNSToken(visitor.getUid(), t.getToken()); 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; } }