Finished Spring application - and it works!

This commit is contained in:
Norbi Peti 2020-04-28 00:17:37 +02:00
parent b81c17e5b6
commit 1be4f92b1b
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 36 additions and 22 deletions

View file

@ -3,8 +3,13 @@ package io.github.norbipeti.gcdc;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Collections;
import java.util.Map;
@SpringBootApplication @SpringBootApplication
public class Application { public class Application {
public static final Map<String, String> RESULT_OK = Collections.singletonMap("response", "OK");
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }

View file

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

View file

@ -5,9 +5,8 @@ import discord4j.core.DiscordClientBuilder;
import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.GuildMessageChannel; import discord4j.core.object.entity.GuildMessageChannel;
import discord4j.core.object.entity.Member; import discord4j.core.object.entity.Member;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.util.Snowflake; import discord4j.core.object.util.Snowflake;
import io.github.norbipeti.gcdc.Application;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResult;
@ -39,11 +38,15 @@ public class DiscordService {
client.login().subscribe(); client.login().subscribe();
} }
public void sendMessage(long channel, long user, String message) { public DeferredResult<Map<String, String>> sendMessage(long channel, long user, String message) {
var result = new DeferredResult<Map<String, String>>();
client.getChannelById(Snowflake.of(channel)).cast(GuildMessageChannel.class) client.getChannelById(Snowflake.of(channel)).cast(GuildMessageChannel.class)
.flatMap(ch -> getUsername(user, ch) .flatMap(ch -> getUsername(user, ch)
.flatMap(name -> ch.createEmbed(ecs -> ecs.setAuthor(name, null, null) .flatMap(name -> ch.createEmbed(ecs -> ecs.setAuthor(name, null, null)
.setColor(Color.BLUE).setDescription(message)))).subscribe(); .setColor(Color.BLUE).setDescription(message))))
.doOnError(result::setErrorResult)
.subscribe(msg -> result.setResult(Application.RESULT_OK));
return result;
} }
private final HashMap<Long, DeferredResult<Map<String, String>>> requests = new HashMap<>(); private final HashMap<Long, DeferredResult<Map<String, String>>> requests = new HashMap<>();

View file

@ -23,8 +23,8 @@ public class SessionService extends JdbcDaoSupport {
} }
public void insertSession(Session session) { public void insertSession(Session session) {
String sql = "INSERT INTO sessions(token, channel) VALUES (?, ?)"; String sql = "INSERT INTO sessions(token, channel, \"user\") VALUES (?, ?, ?)";
getJdbcTemplate().update(sql, session.getToken(), session.getChannel().asLong()); getJdbcTemplate().update(sql, session.getToken(), session.getChannel().asLong(), session.getUser());
} }
public Session getSession(String token) { public Session getSession(String token) {
@ -33,7 +33,7 @@ public class SessionService extends JdbcDaoSupport {
} }
public Session getSession(long user) { public Session getSession(long user) {
String sql = "SELECT * FROM sessions WHERE user=? LIMIT 1"; String sql = "SELECT * FROM sessions WHERE \"user\"=? LIMIT 1";
return getSession(getJdbcTemplate().queryForList(sql, user)); return getSession(getJdbcTemplate().queryForList(sql, user));
} }
@ -49,8 +49,9 @@ public class SessionService extends JdbcDaoSupport {
String sql = "DELETE FROM sessions WHERE token=?"; String sql = "DELETE FROM sessions WHERE token=?";
getJdbcTemplate().update(sql, token); getJdbcTemplate().update(sql, token);
} }
public void deleteSession(long user) { public void deleteSession(long user) {
String sql = "DELETE FROM sessions WHERE user=?"; String sql = "DELETE FROM sessions WHERE \"user\"=?";
getJdbcTemplate().update(sql, user); getJdbcTemplate().update(sql, user);
} }