|
|
@@ -2,37 +2,42 @@ package io.github.norbipeti.gcdc.controller; |
|
|
|
|
|
|
|
import bell.oauth.discord.main.OAuthBuilder; |
|
|
|
import bell.oauth.discord.main.Response; |
|
|
|
import discord4j.core.object.entity.Message; |
|
|
|
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.*; |
|
|
|
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 javax.validation.Valid; |
|
|
|
import java.security.Principal; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
@RestController |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class DCController { |
|
|
|
private static final Map<String, String> RESULT_OK = Collections.singletonMap("response", "OK"); |
|
|
|
private final SessionService service; |
|
|
|
private final DiscordService discordService; |
|
|
|
@Value("${discord.secret}") |
|
|
|
private String secret; |
|
|
|
private final OAuthBuilder builder = new OAuthBuilder("680138144812892371", secret); |
|
|
|
private OAuthBuilder builder; |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
builder = new OAuthBuilder("680138144812892371", secret); |
|
|
|
} |
|
|
|
|
|
|
|
@GetMapping("/api/users/register") |
|
|
|
public Map<String, String> register(@RequestParam String state, @RequestParam String code, HttpServletRequest request) { |
|
|
|
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) { |
|
|
@@ -43,21 +48,21 @@ public class DCController { |
|
|
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST); |
|
|
|
long user = Snowflake.of(builder.getUser().getId()).asLong(); |
|
|
|
service.deleteSession(user); |
|
|
|
service.insertSession(new Session(UUID.randomUUID().toString(), Snowflake.of(channel), user)); |
|
|
|
return RESULT_OK; |
|
|
|
String token = UUID.randomUUID().toString(); |
|
|
|
service.insertSession(new Session(token, Snowflake.of(channel), user)); |
|
|
|
return "Run the following command:<br/>\ndcsetup \"" + token + "\""; |
|
|
|
} |
|
|
|
|
|
|
|
@GetMapping("/api/users/get") |
|
|
|
public Map<String, String> getUser(@RequestParam String token) { |
|
|
|
getSession(token); //Returns unauthorized if needed |
|
|
|
return RESULT_OK; |
|
|
|
return Application.RESULT_OK; |
|
|
|
} |
|
|
|
|
|
|
|
@PostMapping("/api/messages/send") |
|
|
|
public Map<String, String> sendMessage(@RequestParam String token, @RequestParam String message) { |
|
|
|
public DeferredResult<Map<String, String>> sendMessage(@RequestParam String token, @RequestParam String message) { |
|
|
|
var sess = getSession(token); |
|
|
|
discordService.sendMessage(sess.getChannel().asLong(), sess.getUser(), message); |
|
|
|
return RESULT_OK; |
|
|
|
return discordService.sendMessage(sess.getChannel().asLong(), sess.getUser(), message); |
|
|
|
} |
|
|
|
|
|
|
|
@GetMapping("/api/messages/get") |
|
|
|