Search in sources :

Example 1 with BotUser

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();
}
Also used : BotUser(de.notecho.spotify.database.user.entities.BotUser)

Example 2 with BotUser

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";
}
Also used : BotUser(de.notecho.spotify.database.user.entities.BotUser) TokenPair(de.notecho.spotify.database.user.entities.TokenPair) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials) GetMapping(org.springframework.web.bind.annotation.GetMapping) SneakyThrows(lombok.SneakyThrows)

Example 3 with BotUser

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";
}
Also used : Cookie(javax.servlet.http.Cookie) BotUser(de.notecho.spotify.database.user.entities.BotUser) OAuth2Credential(com.github.philippheuer.credentialmanager.domain.OAuth2Credential) BotUser(de.notecho.spotify.database.user.entities.BotUser) User(com.github.twitch4j.helix.domain.User) TokenPair(de.notecho.spotify.database.user.entities.TokenPair) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with BotUser

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";
}
Also used : BotUser(de.notecho.spotify.database.user.entities.BotUser) User(com.github.twitch4j.helix.domain.User) BotUser(de.notecho.spotify.database.user.entities.BotUser) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

BotUser (de.notecho.spotify.database.user.entities.BotUser)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 User (com.github.twitch4j.helix.domain.User)2 TokenPair (de.notecho.spotify.database.user.entities.TokenPair)2 OAuth2Credential (com.github.philippheuer.credentialmanager.domain.OAuth2Credential)1 Cookie (javax.servlet.http.Cookie)1 SneakyThrows (lombok.SneakyThrows)1 AuthorizationCodeCredentials (se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)1