選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

81 行
3.1KB

  1. package io.github.norbipeti.gcdc.controller;
  2. import bell.oauth.discord.main.OAuthBuilder;
  3. import bell.oauth.discord.main.Response;
  4. import discord4j.core.object.util.Snowflake;
  5. import io.github.norbipeti.gcdc.Application;
  6. import io.github.norbipeti.gcdc.model.Session;
  7. import io.github.norbipeti.gcdc.service.DiscordService;
  8. import io.github.norbipeti.gcdc.service.SessionService;
  9. import lombok.RequiredArgsConstructor;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import org.springframework.web.context.request.async.DeferredResult;
  17. import org.springframework.web.server.ResponseStatusException;
  18. import javax.annotation.PostConstruct;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.util.Map;
  21. import java.util.UUID;
  22. @RestController
  23. @RequiredArgsConstructor
  24. public class DCController {
  25. private final SessionService service;
  26. private final DiscordService discordService;
  27. @Value("${discord.secret}")
  28. private String secret;
  29. private OAuthBuilder builder;
  30. @PostConstruct
  31. public void init() {
  32. builder = new OAuthBuilder("680138144812892371", secret);
  33. }
  34. @GetMapping("/api/users/register")
  35. public String register(@RequestParam String state, @RequestParam String code, HttpServletRequest request) {
  36. if (state == null || code == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
  37. long channel;
  38. try { channel = Long.parseLong(state); } catch (NumberFormatException e) {
  39. throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
  40. }
  41. builder.setRedirectURI(request.getRequestURL().toString());
  42. if (builder.exchange(code) == Response.ERROR)
  43. throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
  44. long user = Snowflake.of(builder.getUser().getId()).asLong();
  45. service.deleteSession(user);
  46. String token = UUID.randomUUID().toString();
  47. service.insertSession(new Session(token, Snowflake.of(channel), user));
  48. return "Run the following command:<br/>\ndcsetup \"" + token + "\"\n<br>Then you can close this window.";
  49. }
  50. @GetMapping("/api/users/get")
  51. public Map<String, String> getUser(@RequestParam String token) {
  52. getSession(token); //Returns unauthorized if needed
  53. return Application.RESULT_OK;
  54. }
  55. @PostMapping("/api/messages/send")
  56. public DeferredResult<Map<String, String>> sendMessage(@RequestParam String token, @RequestParam String message) {
  57. var sess = getSession(token);
  58. return discordService.sendMessage(sess.getChannel().asLong(), sess.getUser(), message);
  59. }
  60. @GetMapping("/api/messages/get")
  61. public DeferredResult<Map<String, String>> getMessage(@RequestParam String token) {
  62. var sess = getSession(token);
  63. return discordService.getMessage(sess.getChannel().asLong());
  64. }
  65. private Session getSession(String token) {
  66. var sess = service.getSession(token);
  67. if (sess == null)
  68. throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
  69. return sess;
  70. }
  71. }