use of de.notecho.spotify.database.user.entities.BotUser in project spotifybot by NotEchoDE.
the class Reward method setRedemptionStatus.
protected void setRedemptionStatus(RewardRedeemedEvent event, RedemptionStatus status) {
BotUser user = getRoot().getUser();
getRoot().getClient().getHelix().updateRedemptionStatus(user.twitchTokens().getAccessToken(), user.getTwitchId(), event.getRedemption().getReward().getId(), Collections.singletonList(event.getRedemption().getId()), status).execute();
}
use of de.notecho.spotify.database.user.entities.BotUser in project spotifybot by NotEchoDE.
the class CallbackController method spotifyCallback.
@SneakyThrows
@GetMapping("/spotify/callback")
public String spotifyCallback(@RequestParam(name = "code", defaultValue = "null") String code, @CookieValue(name = "session", defaultValue = "null") String session, Model model) {
if (code.equals("null"))
return "redirect:/erorr?code=503";
if (sessionManagementService.getUser(session) == null)
return "redirect:/erorr?code=503";
BotUser user = sessionManagementService.getUser(session);
AuthorizationCodeCredentials codeCredentials = spotifyApi.authorizationCode(code).build().execute();
if (user.spotifyTokens() != null) {
user.spotifyTokens().setAccessToken(codeCredentials.getAccessToken());
user.spotifyTokens().setRefreshToken(codeCredentials.getRefreshToken());
} else
user.addTokenPair(new TokenPair(0L, codeCredentials.getAccessToken(), codeCredentials.getRefreshToken(), TokenType.SPOTIFY));
repository.saveAndFlush(user);
botInstanceManagementService.startInstance(user);
return "redirect:/dashboard";
}
use of de.notecho.spotify.database.user.entities.BotUser in project spotifybot by NotEchoDE.
the class CallbackController method twitchCallback.
@GetMapping("/twitch/callback")
public String twitchCallback(@RequestParam(name = "code", defaultValue = "null") String code, HttpServletResponse response, Model model) {
if (code.equals("null"))
return "redirect:/erorr?code=503";
OAuth2Credential credentialByCode = oAuth2IdentityProvider.getCredentialByCode(code);
List<User> users = twitchClient.getHelix().getUsers(credentialByCode.getAccessToken(), null, null).execute().getUsers();
if (users.size() == 0)
return "redirect:/erorr?code=503";
User twitchUser = users.get(0);
BotUser user = repository.findByTwitchId(twitchUser.getId());
if (user == null) {
user = new BotUser(0L, twitchUser.getId(), DefaultModules.defaultList(), Collections.singletonList(new TokenPair(0L, credentialByCode.getAccessToken(), credentialByCode.getRefreshToken(), TokenType.TWITCH)));
} else {
user.twitchTokens().setAccessToken(credentialByCode.getAccessToken());
user.twitchTokens().setRefreshToken(credentialByCode.getRefreshToken());
}
repository.saveAndFlush(user);
Cookie cookie = new Cookie("session", sessionManagementService.createSession(user));
cookie.setPath("/");
cookie.setMaxAge(3600);
response.addCookie(cookie);
return "redirect:/dashboard";
}
use of de.notecho.spotify.database.user.entities.BotUser in project spotifybot by NotEchoDE.
the class DashboardController method dashboard.
@GetMapping("/dashboard")
public String dashboard(@CookieValue(name = "session", defaultValue = "null") String session, Model model) {
BotUser user = sessionManagementService.getUser(session);
if (session.equals("null") || user == null)
return "redirect:/login";
List<User> twitchUsers = twitchClient.getHelix().getUsers(user.twitchTokens().getAccessToken(), null, null).execute().getUsers();
User twitchUser = twitchUsers.get(0);
model.addAttribute("username", twitchUser.getLogin());
model.addAttribute("spotifyConnected", user.spotifyTokens() != null);
return "dashboard";
}
Aggregations