|
- package io.github.norbipeti.gcdc.controller;
-
- import bell.oauth.discord.main.OAuthBuilder;
- import bell.oauth.discord.main.Response;
- import discord4j.core.object.util.Snowflake;
- import io.github.norbipeti.gcdc.Application;
- import io.github.norbipeti.gcdc.model.Session;
- import io.github.norbipeti.gcdc.service.DiscordService;
- import io.github.norbipeti.gcdc.service.SessionService;
- import lombok.RequiredArgsConstructor;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.HttpStatus;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.context.request.async.DeferredResult;
- import org.springframework.web.server.ResponseStatusException;
-
- import javax.annotation.PostConstruct;
- import javax.servlet.http.HttpServletRequest;
- import java.util.Map;
- import java.util.UUID;
-
- @RestController
- @RequiredArgsConstructor
- public class DCController {
- private final SessionService service;
- private final DiscordService discordService;
- @Value("${discord.secret}")
- private String secret;
- private OAuthBuilder builder;
-
- @PostConstruct
- public void init() {
- builder = new OAuthBuilder("680138144812892371", secret);
- }
-
- @GetMapping("/api/users/register")
- public String register(@RequestParam String state, @RequestParam String code, HttpServletRequest request) {
- if (state == null || code == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
- long channel;
- try { channel = Long.parseLong(state); } catch (NumberFormatException e) {
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
- }
- builder.setRedirectURI(request.getRequestURL().toString());
- if (builder.exchange(code) == Response.ERROR)
- throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
- long user = Snowflake.of(builder.getUser().getId()).asLong();
- service.deleteSession(user);
- String token = UUID.randomUUID().toString();
- service.insertSession(new Session(token, Snowflake.of(channel), user));
- return "Run the following command:<br/>\ndcsetup \"" + token + "\"\n<br>Then you can close this window.";
- }
-
- @GetMapping("/api/users/get")
- public Map<String, String> getUser(@RequestParam String token) {
- getSession(token); //Returns unauthorized if needed
- return Application.RESULT_OK;
- }
-
- @PostMapping("/api/messages/send")
- public DeferredResult<Map<String, String>> sendMessage(@RequestParam String token, @RequestParam String message) {
- var sess = getSession(token);
- return discordService.sendMessage(sess.getChannel().asLong(), sess.getUser(), message);
- }
-
- @GetMapping("/api/messages/get")
- public DeferredResult<Map<String, String>> getMessage(@RequestParam String token) {
- var sess = getSession(token);
- return discordService.getMessage(sess.getChannel().asLong());
- }
-
- private Session getSession(String token) {
- var sess = service.getSession(token);
- if (sess == null)
- throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
- return sess;
- }
- }
|