package org.royaldev.thehumanity.server.controllers;
import org.kitteh.irc.client.library.Client;
import org.kitteh.irc.client.library.element.Channel;
import org.kitteh.irc.client.library.element.User;
import org.royaldev.thehumanity.server.services.channel.ChannelService;
import org.royaldev.thehumanity.server.services.client.ClientService;
import org.royaldev.thehumanity.server.services.server.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.cardstock.cardstock.configuration.Server;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Controller
public class ChannelController {
@Autowired
private ClientService clientService;
@Autowired
private ServerService serverService;
@Autowired
private ChannelService channelService;
@ResponseBody
@RequestMapping(value = "/api/channels/{server}", method = RequestMethod.GET, produces = APIHelper.PRODUCES)
public String apiViewAllChannels(@PathVariable String server, final HttpServletResponse response) {
final Client client = this.clientService.getFromHostname(server);
return APIHelper.makeObjectMapperJSON(
response,
om -> om.writeValueAsString(this.channelService.getAll(client).stream().map(Channel::getName).collect(Collectors.toSet()))
);
}
@ResponseBody
@RequestMapping(value = "/api/channel/{server}/{channel}", method = RequestMethod.GET, produces = APIHelper.PRODUCES)
public String apiViewChannel(@PathVariable String server, @PathVariable final String channel, final HttpServletResponse response) {
final Client client = this.clientService.getFromHostname(server);
final Channel c = this.channelService.getFromName(client, "#" + channel);
if (c == null || !this.channelService.getAll(client).contains(c)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return APIHelper.makeError("No such channel.");
}
return APIHelper.makeJSON(jw -> {
jw.array();
for (final User user : c.getUsers()) {
jw
.object()
.key("nickname")
.value(user.getNick())
.key("user")
.value(user.getUserString())
.key("hostname")
.value(user.getHost())
.endObject();
}
jw.endArray();
});
}
@RequestMapping(value = "/channels", method = RequestMethod.GET)
public String viewAllChannels(final Model model) {
final Map<Server, Set<Channel>> channels = this.clientService.getClients().stream()
.collect(Collectors.toMap(
client -> this.serverService.getServer(client),
client -> this.channelService.getAll(client)
));
model.addAttribute("channels", channels);
return "channels/index";
}
@RequestMapping(value = "/channel/{server}/{name}", method = RequestMethod.GET)
public String viewChannel(@PathVariable String server, @PathVariable String name, final Model model) {
final Client client = this.clientService.getFromHostname(server);
final Channel channel = this.channelService.getFromName(client, "#" + name);
if (channel == null || !this.channelService.getAll(client).contains(channel)) {
return "redirect:/";
}
model.addAttribute("channel", channel);
return "channels/channel";
}
}