@RequestMapping(value = "/api/group", method = RequestMethod.POST, produces = "application/json") public List<BroadcastGroup> createGroup( @RequestParam("name") String name, @RequestParam("setSize") Integer setSize) { if (broadcastRepository.findOne(name) == null) { AtomicInteger counter = new AtomicInteger(0); List<String> userIds = session .getUsers() .stream() .filter(slackUser -> !slackUser.isBot()) .map(SlackPersona::getId) .collect(Collectors.toList()); List<BroadcastSet> broadcastSets = Lists.partition(userIds, setSize) .stream() .map( set -> new BroadcastSet( counter.incrementAndGet(), set, new ArrayList<BroadcastMessage>())) .collect(Collectors.toList()); broadcastRepository.save(new BroadcastGroup(name, broadcastSets, new Date())); } else { log.error("Group with name {} already exist", name); } return broadcastRepository.findAll(); }
@RequestMapping( value = "/api/broadcast", method = RequestMethod.POST, produces = "application/json") public List<BroadcastGroup> broadcastMessage( @RequestParam("name") String name, @RequestParam("set") Integer setId, @RequestParam("message") String message) { BroadcastGroup group = broadcastRepository.findOne(name); if (group != null) { Optional<BroadcastSet> setOptional = group.getBroadcastSets().stream().filter(set -> set.getId() == setId).findFirst(); if (setOptional.isPresent()) { BroadcastSet set = setOptional.get(); set.getMessages().add(new BroadcastMessage(message, new Date())); broadcastRepository.save(group); executorService.execute( () -> { set.getUsers() .forEach( userId -> { SlackUser user = session.findUserById(userId); session.sendMessageToUser(user, message, null); }); }); } else { log.error("Can't find set with id {}", setId); } } else { log.error("Can't find group with name {}", name); } return broadcastRepository.findAll(); }