Start playlist api (create and add to plalist ready) and somme api change
This commit is contained in:
parent
812bdfc52b
commit
20e8750085
83
src/main/java/net/Broken/DB/Entity/PlaylistEntity.java
Normal file
83
src/main/java/net/Broken/DB/Entity/PlaylistEntity.java
Normal file
@ -0,0 +1,83 @@
|
||||
package net.Broken.DB.Entity;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.*;
|
||||
import javax.sound.midi.Track;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class PlaylistEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy= GenerationType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@JsonIgnore
|
||||
@ManyToOne
|
||||
@JoinColumn(name="userEntity_id", nullable=false)
|
||||
private UserEntity user;
|
||||
|
||||
|
||||
@OneToMany(mappedBy = "playlist")
|
||||
private List<TrackEntity> tracks;
|
||||
|
||||
|
||||
public PlaylistEntity() {
|
||||
}
|
||||
|
||||
public PlaylistEntity(String name, UserEntity user) {
|
||||
this.name = name;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public UserEntity getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(UserEntity user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public List<TrackEntity> getTracks() {
|
||||
return tracks;
|
||||
}
|
||||
|
||||
public void setTracks(List<TrackEntity> tracks) {
|
||||
this.tracks = tracks;
|
||||
}
|
||||
|
||||
public void addTracks(TrackEntity... tracks )
|
||||
{
|
||||
if(this.tracks == null)
|
||||
this.tracks = new ArrayList<>();
|
||||
|
||||
this.tracks.addAll(Arrays.asList(tracks));
|
||||
}
|
||||
|
||||
|
||||
}
|
86
src/main/java/net/Broken/DB/Entity/TrackEntity.java
Normal file
86
src/main/java/net/Broken/DB/Entity/TrackEntity.java
Normal file
@ -0,0 +1,86 @@
|
||||
package net.Broken.DB.Entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class TrackEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy= GenerationType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String url;
|
||||
|
||||
private String identifier;
|
||||
|
||||
private Integer pos;
|
||||
|
||||
@JsonIgnore
|
||||
@ManyToOne
|
||||
@JoinColumn(name="playlistEntity_id", nullable=false)
|
||||
private PlaylistEntity playlist;
|
||||
|
||||
public TrackEntity() {
|
||||
}
|
||||
|
||||
public TrackEntity(AudioTrackInfo trackInfo, int pos, PlaylistEntity playlist) {
|
||||
this.title = trackInfo.title;
|
||||
this.url = trackInfo.uri;
|
||||
this.identifier = trackInfo.identifier;
|
||||
this.playlist = playlist;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public PlaylistEntity getPlaylist() {
|
||||
return playlist;
|
||||
}
|
||||
|
||||
public void setPlaylist(PlaylistEntity playlist) {
|
||||
this.playlist = playlist;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public Integer getPos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public void setPos(Integer pos) {
|
||||
this.pos = pos;
|
||||
}
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
package net.Broken.DB.Entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Entity for DB. Represent confirmed user account.
|
||||
@ -20,8 +22,12 @@ public class UserEntity {
|
||||
|
||||
private String apiToken;
|
||||
|
||||
@JsonIgnore
|
||||
private String password;
|
||||
|
||||
@OneToMany(mappedBy = "user")
|
||||
private List<PlaylistEntity> playlists;
|
||||
|
||||
public UserEntity() {
|
||||
}
|
||||
|
||||
@ -71,4 +77,19 @@ public class UserEntity {
|
||||
public void setApiToken(String apiToken) {
|
||||
this.apiToken = apiToken;
|
||||
}
|
||||
|
||||
public List<PlaylistEntity> getPlaylists() {
|
||||
return playlists;
|
||||
}
|
||||
|
||||
public void setPlaylists(List<PlaylistEntity> playlists) {
|
||||
this.playlists = playlists;
|
||||
}
|
||||
|
||||
public void addPlaylist(PlaylistEntity... playlists){
|
||||
if(this.playlists == null)
|
||||
this.playlists = new ArrayList<>();
|
||||
|
||||
this.playlists.addAll(Arrays.asList(playlists));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package net.Broken.DB.Repository;
|
||||
|
||||
import net.Broken.DB.Entity.PlaylistEntity;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface PlaylistRepository extends CrudRepository<PlaylistEntity, Integer> {
|
||||
}
|
11
src/main/java/net/Broken/DB/Repository/TrackRepository.java
Normal file
11
src/main/java/net/Broken/DB/Repository/TrackRepository.java
Normal file
@ -0,0 +1,11 @@
|
||||
package net.Broken.DB.Repository;
|
||||
|
||||
import net.Broken.DB.Entity.PlaylistEntity;
|
||||
import net.Broken.DB.Entity.TrackEntity;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TrackRepository extends CrudRepository<TrackEntity, Integer> {
|
||||
List<TrackEntity> findDistinctByPlaylistOrderByPos(PlaylistEntity playlistEntity);
|
||||
}
|
@ -14,6 +14,6 @@ import org.springframework.http.ResponseEntity;
|
||||
public class Add implements CommandInterface {
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
return new WebLoadUtils(musicCommande ,data, user).getResponse();
|
||||
return new WebLoadUtils(data, user, true).getResponse();
|
||||
}
|
||||
}
|
||||
|
@ -18,13 +18,8 @@ public class AutoFlowOff implements CommandInterface{
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
AudioM audioM = AudioM.getInstance(null);
|
||||
try {
|
||||
TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler;
|
||||
scheduler.setAutoFlow(false);
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
LogManager.getLogger().catching(nullMusicManager);
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"Not connected", "connect"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler;
|
||||
scheduler.setAutoFlow(false);
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
@ -18,14 +18,9 @@ public class AutoFlowOn implements CommandInterface{
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
AudioM audioM = AudioM.getInstance(null);
|
||||
try {
|
||||
TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler;
|
||||
scheduler.setAutoFlow(true);
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
LogManager.getLogger().catching(nullMusicManager);
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"Not connected", "connect"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
TrackScheduler scheduler = audioM.getGuildMusicManager().scheduler;
|
||||
scheduler.setAutoFlow(true);
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"ok"), HttpStatus.OK);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -17,15 +17,11 @@ public class Dell implements CommandInterface {
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
if(data.url != null) {
|
||||
try {
|
||||
if(musicCommande.getAudioManager().getGuildMusicManager().scheduler.remove(data.url)){
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
}
|
||||
else
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"URL not found"), HttpStatus.NOT_FOUND);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
|
||||
if(musicCommande.getAudioManager().getGuildMusicManager().scheduler.remove(data.url)){
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
}
|
||||
else
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command,"URL not found"), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Missing URL"), HttpStatus.NOT_ACCEPTABLE);
|
||||
|
||||
|
@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
|
||||
public class Flush implements CommandInterface {
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
try {
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.flush();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.flush();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
|
||||
public class Next implements CommandInterface {
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
try {
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.nextTrack();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.nextTrack();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
|
||||
public class Pause implements CommandInterface {
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
try {
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.pause();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.pause();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
@ -16,11 +16,7 @@ import org.springframework.http.ResponseEntity;
|
||||
public class Play implements CommandInterface {
|
||||
@Override
|
||||
public ResponseEntity<CommandResponseData> action(Music musicCommande, CommandPostData data, User user) {
|
||||
try {
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.resume();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Not connected to vocal!"), HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
musicCommande.getAudioManager().getGuildMusicManager().scheduler.resume();
|
||||
return new ResponseEntity<>(new CommandResponseData(data.command, "Accepted"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package net.Broken.RestApi.Data.Playlist;
|
||||
|
||||
import net.Broken.RestApi.Data.CommandPostData;
|
||||
|
||||
public class AddToPlaylistData extends CommandPostData{
|
||||
|
||||
public int playlistId;
|
||||
|
||||
|
||||
public int pos;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package net.Broken.RestApi.Data.Playlist;
|
||||
|
||||
public class CreatePlaylistData {
|
||||
public String name;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package net.Broken.RestApi.Data.Playlist;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import net.Broken.DB.Entity.PlaylistEntity;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class PlaylistResponseData {
|
||||
public String message;
|
||||
public String error;
|
||||
public PlaylistEntity playlist;
|
||||
|
||||
public PlaylistResponseData(String message, PlaylistEntity playlist) {
|
||||
this.message = message;
|
||||
this.playlist = playlist;
|
||||
}
|
||||
public PlaylistResponseData(String message, String error) {
|
||||
this.message = message;
|
||||
this.error = error;
|
||||
}
|
||||
}
|
@ -46,18 +46,14 @@ public class MusicWebAPIController {
|
||||
Music musicCommande = (Music) MainBot.commandes.get("music");
|
||||
|
||||
if(musicCommande.audio.getGuild().getAudioManager().isConnected()){
|
||||
try {
|
||||
AudioPlayer player = musicCommande.audio.getGuildMusicManager().player;
|
||||
AudioTrack currentTrack = player.getPlayingTrack();
|
||||
if(currentTrack == null)
|
||||
{
|
||||
return new CurrentMusicData(null,0, "STOP",false, musicCommande.audio.getGuildMusicManager().scheduler.isAutoFlow());
|
||||
}
|
||||
UserAudioTrackData uat = new UserAudioTrackData(musicCommande.audio.getGuildMusicManager().scheduler.getCurrentPlayingTrack());
|
||||
return new CurrentMusicData(uat, currentTrack.getPosition(), currentTrack.getState().toString(), player.isPaused(), musicCommande.audio.getGuildMusicManager().scheduler.isAutoFlow());
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
return new CurrentMusicData(null,0, "STOP",false, false);
|
||||
AudioPlayer player = musicCommande.audio.getGuildMusicManager().player;
|
||||
AudioTrack currentTrack = player.getPlayingTrack();
|
||||
if(currentTrack == null)
|
||||
{
|
||||
return new CurrentMusicData(null,0, "STOP",false, musicCommande.audio.getGuildMusicManager().scheduler.isAutoFlow());
|
||||
}
|
||||
UserAudioTrackData uat = new UserAudioTrackData(musicCommande.audio.getGuildMusicManager().scheduler.getCurrentPlayingTrack());
|
||||
return new CurrentMusicData(uat, currentTrack.getPosition(), currentTrack.getState().toString(), player.isPaused(), musicCommande.audio.getGuildMusicManager().scheduler.isAutoFlow());
|
||||
}else
|
||||
{
|
||||
return new CurrentMusicData(null,0, "DISCONNECTED",false, false);
|
||||
@ -68,12 +64,8 @@ public class MusicWebAPIController {
|
||||
public PlaylistData getPlaylist(){
|
||||
Music musicCommande = (Music) MainBot.commandes.get("music");
|
||||
List<UserAudioTrackData> list = null;
|
||||
try {
|
||||
list = musicCommande.getAudioManager().getGuildMusicManager().scheduler.getList();
|
||||
return new PlaylistData(list);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
return new PlaylistData(list);
|
||||
}
|
||||
list = musicCommande.getAudioManager().getGuildMusicManager().scheduler.getList();
|
||||
return new PlaylistData(list);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/command", method = RequestMethod.POST)
|
||||
|
90
src/main/java/net/Broken/RestApi/PlaylistAPIController.java
Normal file
90
src/main/java/net/Broken/RestApi/PlaylistAPIController.java
Normal file
@ -0,0 +1,90 @@
|
||||
package net.Broken.RestApi;
|
||||
|
||||
|
||||
import net.Broken.DB.Entity.PlaylistEntity;
|
||||
import net.Broken.DB.Entity.UserEntity;
|
||||
import net.Broken.DB.Repository.PlaylistRepository;
|
||||
import net.Broken.DB.Repository.TrackRepository;
|
||||
import net.Broken.DB.Repository.UserRepository;
|
||||
import net.Broken.MainBot;
|
||||
import net.Broken.RestApi.Data.CommandPostData;
|
||||
import net.Broken.RestApi.Data.CommandResponseData;
|
||||
import net.Broken.RestApi.Data.Playlist.AddToPlaylistData;
|
||||
import net.Broken.RestApi.Data.Playlist.CreatePlaylistData;
|
||||
import net.Broken.RestApi.Data.Playlist.PlaylistResponseData;
|
||||
import net.Broken.audio.Playlist.PlaylistManager;
|
||||
import net.Broken.audio.WebLoadUtils;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.CookieValue;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/playlist/")
|
||||
public class PlaylistAPIController {
|
||||
|
||||
private final
|
||||
UserRepository userRepository;
|
||||
|
||||
private final
|
||||
PlaylistRepository playlistRepository;
|
||||
|
||||
private final
|
||||
TrackRepository trackRepository;
|
||||
|
||||
private Logger logger = LogManager.getLogger();
|
||||
|
||||
@Autowired
|
||||
public PlaylistAPIController(UserRepository userRepository, PlaylistRepository playlistRepository, TrackRepository trackRepository) {
|
||||
this.userRepository = userRepository;
|
||||
this.playlistRepository = playlistRepository;
|
||||
this.trackRepository = trackRepository;
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/myPlaylist")
|
||||
public List<PlaylistEntity> myPlaylist(@CookieValue(value = "token", defaultValue = "") String token){
|
||||
if(token.isEmpty())
|
||||
return null;
|
||||
else{
|
||||
UserEntity user = userRepository.findByApiToken(token).get(0);
|
||||
return user.getPlaylists();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("/createPlaylist")
|
||||
public ResponseEntity<PlaylistResponseData> createPlaylist(@CookieValue(value = "token", defaultValue = "") String token, @RequestBody CreatePlaylistData data){
|
||||
|
||||
if(token.isEmpty())
|
||||
return new ResponseEntity<>(new PlaylistResponseData("Unknown Token!\nPlease Re-connect.", "token"), HttpStatus.UNAUTHORIZED);
|
||||
else{
|
||||
UserEntity user = userRepository.findByApiToken(token).get(0);
|
||||
PlaylistEntity playlistEntity = new PlaylistEntity(data.name, user);
|
||||
playlistEntity = playlistRepository.save(playlistEntity);
|
||||
user.addPlaylist(playlistEntity);
|
||||
userRepository.save(user);
|
||||
return new ResponseEntity<>(new PlaylistResponseData("Ok", playlistEntity), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("/addToPlaylist")
|
||||
public ResponseEntity<PlaylistResponseData> addToPlaylist(@CookieValue(value = "token", defaultValue = "") String token, @RequestBody AddToPlaylistData data){
|
||||
PlaylistManager playlistManager = PlaylistManager.getINSTANCE();
|
||||
|
||||
return playlistManager.addToPlaylist(token, data);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
24
src/main/java/net/Broken/SpringContext.java
Normal file
24
src/main/java/net/Broken/SpringContext.java
Normal file
@ -0,0 +1,24 @@
|
||||
package net.Broken;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SpringContext implements ApplicationContextAware{
|
||||
|
||||
@Autowired
|
||||
static ApplicationContext context;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
context = applicationContext;
|
||||
}
|
||||
|
||||
|
||||
public static ApplicationContext getAppContext(){
|
||||
return context;
|
||||
}
|
||||
}
|
@ -337,11 +337,9 @@ public class AudioM {
|
||||
guild.getAudioManager().closeAudioConnection();
|
||||
}
|
||||
|
||||
public GuildMusicManager getGuildMusicManager() throws NullMusicManager, NotConnectedException {
|
||||
public GuildMusicManager getGuildMusicManager(){
|
||||
if( musicManager == null)
|
||||
throw new NullMusicManager();
|
||||
else if( playedChanel == null)
|
||||
throw new NotConnectedException();
|
||||
musicManager = getGuildAudioPlayer(guild);
|
||||
return musicManager;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
package net.Broken.audio.Playlist.Exception;
|
||||
|
||||
public class PlaylistNotFoundException extends Exception {
|
||||
}
|
139
src/main/java/net/Broken/audio/Playlist/PlaylistManager.java
Normal file
139
src/main/java/net/Broken/audio/Playlist/PlaylistManager.java
Normal file
@ -0,0 +1,139 @@
|
||||
package net.Broken.audio.Playlist;
|
||||
|
||||
import net.Broken.DB.Entity.PlaylistEntity;
|
||||
import net.Broken.DB.Entity.TrackEntity;
|
||||
import net.Broken.DB.Entity.UserEntity;
|
||||
import net.Broken.DB.Repository.PlaylistRepository;
|
||||
import net.Broken.DB.Repository.TrackRepository;
|
||||
import net.Broken.DB.Repository.UserRepository;
|
||||
import net.Broken.MainBot;
|
||||
import net.Broken.RestApi.Data.CommandResponseData;
|
||||
import net.Broken.RestApi.Data.Playlist.AddToPlaylistData;
|
||||
import net.Broken.RestApi.Data.Playlist.PlaylistResponseData;
|
||||
import net.Broken.SpringContext;
|
||||
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
|
||||
import net.Broken.Tools.UserManager.UserUtils;
|
||||
import net.Broken.audio.Playlist.Exception.PlaylistNotFoundException;
|
||||
import net.Broken.audio.WebLoadUtils;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import sun.applet.Main;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PlaylistManager {
|
||||
|
||||
private final ResponseEntity<PlaylistResponseData> TOKEN_ERROR = new ResponseEntity<>(new PlaylistResponseData("Unknown Token!\nPlease Re-connect.", "token"), HttpStatus.UNAUTHORIZED);
|
||||
|
||||
private final ResponseEntity<PlaylistResponseData> PLAYLIST_NOT_FOUND = new ResponseEntity<>(new PlaylistResponseData("Playlist not found", "playlist"), HttpStatus.NOT_FOUND);
|
||||
|
||||
private final ResponseEntity<PlaylistResponseData> TRACK_NOT_FOUND = new ResponseEntity<>(new PlaylistResponseData("Can't find media!", "track"), HttpStatus.NOT_FOUND);
|
||||
|
||||
|
||||
|
||||
private PlaylistRepository playlistRepository;
|
||||
|
||||
private TrackRepository trackRepository;
|
||||
|
||||
private UserRepository userRepository;
|
||||
|
||||
Logger logger = LogManager.getLogger();
|
||||
|
||||
private static PlaylistManager INSTANCE = new PlaylistManager();
|
||||
|
||||
private PlaylistManager() {
|
||||
ApplicationContext context = SpringContext.getAppContext();
|
||||
|
||||
playlistRepository = (PlaylistRepository) context.getBean("playlistRepository");
|
||||
trackRepository = (TrackRepository) context.getBean("trackRepository");
|
||||
userRepository = (UserRepository) context.getBean("userRepository");
|
||||
}
|
||||
|
||||
public static PlaylistManager getINSTANCE() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public ResponseEntity<PlaylistResponseData> addToPlaylist(String token, AddToPlaylistData data) {
|
||||
UserUtils userUtils = UserUtils.getInstance();
|
||||
|
||||
try {
|
||||
UserEntity user = userUtils.getUserWithApiToken(userRepository, token);
|
||||
PlaylistEntity playlist = getPlaylist(data.playlistId);
|
||||
|
||||
User jdaUser = MainBot.jda.getUserById(user.getJdaId());
|
||||
|
||||
WebLoadUtils webLoadUtils = new WebLoadUtils(data, jdaUser, false);
|
||||
webLoadUtils.getResponse();
|
||||
|
||||
if(webLoadUtils.userAudioTrack == null){
|
||||
return TRACK_NOT_FOUND;
|
||||
}
|
||||
else
|
||||
{
|
||||
TrackEntity trackEntity = new TrackEntity(webLoadUtils.userAudioTrack.getAudioTrack().getInfo(), data.pos, playlist);
|
||||
|
||||
playlist = insert(playlist, trackEntity);
|
||||
return new ResponseEntity<>(new PlaylistResponseData("Ok", playlist),HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (UnknownTokenException e) {
|
||||
logger.warn("Unknown token: "+ token);
|
||||
return TOKEN_ERROR;
|
||||
} catch (PlaylistNotFoundException e) {
|
||||
logger.debug("Playlist not found: "+ data.playlistId);
|
||||
return PLAYLIST_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private PlaylistEntity getPlaylist(int id) throws PlaylistNotFoundException{
|
||||
PlaylistEntity playlist = playlistRepository.findOne(id);
|
||||
if(playlist == null)
|
||||
throw new PlaylistNotFoundException();
|
||||
else
|
||||
return playlist;
|
||||
|
||||
}
|
||||
|
||||
private PlaylistEntity insert(PlaylistEntity playlistEntity, TrackEntity trackEntity){
|
||||
List<TrackEntity> tracks = trackRepository.findDistinctByPlaylistOrderByPos(playlistEntity);
|
||||
|
||||
|
||||
boolean increase = false;
|
||||
for(TrackEntity track : tracks){
|
||||
if(track.getPos().equals(trackEntity.getPos())){
|
||||
logger.debug("Need re-organisation");
|
||||
increase = true;
|
||||
}
|
||||
|
||||
|
||||
if(increase){
|
||||
track.setPos(track.getPos() + 1);
|
||||
trackRepository.save(track);
|
||||
}
|
||||
}
|
||||
|
||||
if(!increase)
|
||||
{
|
||||
trackEntity.setPos(tracks.size());
|
||||
}
|
||||
|
||||
trackRepository.save(trackEntity);
|
||||
|
||||
playlistEntity.addTracks(trackEntity);
|
||||
|
||||
|
||||
return playlistRepository.save(playlistEntity);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -5,7 +5,6 @@ import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
||||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import net.Broken.Commands.Music;
|
||||
import net.Broken.RestApi.Data.CommandPostData;
|
||||
import net.Broken.RestApi.Data.CommandResponseData;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
@ -18,47 +17,52 @@ import org.springframework.http.ResponseEntity;
|
||||
* Interface between WebApi and Music bot for submitting track
|
||||
*/
|
||||
public class WebLoadUtils {
|
||||
ResponseEntity<CommandResponseData> response;
|
||||
Logger logger = LogManager.getLogger();
|
||||
private ResponseEntity<CommandResponseData> response;
|
||||
private Logger logger = LogManager.getLogger();
|
||||
public UserAudioTrack userAudioTrack;
|
||||
|
||||
/**
|
||||
* Submit a track or playlist to Music bot
|
||||
* @param musicCommand The current guild music command.
|
||||
* @param data Received data from API
|
||||
* @param user User who submit the track
|
||||
*/
|
||||
public WebLoadUtils(Music musicCommand, CommandPostData data, User user){
|
||||
AudioPlayerManager playerM = musicCommand.getAudioManager().getPlayerManager();
|
||||
public WebLoadUtils(CommandPostData data, User user, boolean submit){
|
||||
AudioPlayerManager playerM = AudioM.getInstance(null).getPlayerManager();
|
||||
|
||||
try {
|
||||
|
||||
AudioM audioM = AudioM.getInstance(null);
|
||||
playerM.loadItemOrdered(musicCommand.getAudioManager().getGuildMusicManager(), data.url, new AudioLoadResultHandler() {
|
||||
playerM.loadItemOrdered(audioM.getGuildMusicManager(), data.url, new AudioLoadResultHandler() {
|
||||
@Override
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
logger.info("Single Track detected from web!");
|
||||
|
||||
try {
|
||||
UserAudioTrack userAudioTrack = new UserAudioTrack(user, track); //TODO
|
||||
userAudioTrack = new UserAudioTrack(user, track); //TODO
|
||||
if(submit)
|
||||
audioM.play(audioM.getGuild(), audioM.getPlayedChanel(), audioM.getGuildMusicManager(), userAudioTrack, data.onHead);
|
||||
response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK);
|
||||
} catch (NullMusicManager | NotConnectedException nullMusicManager) {
|
||||
nullMusicManager.printStackTrace();
|
||||
}
|
||||
response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
|
||||
logger.info("Playlist detected from web! Limit: " + data.playlistLimit);
|
||||
audioM.playListLoader(playlist, data.playlistLimit, user, data.onHead);
|
||||
response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK);
|
||||
if(submit)
|
||||
{
|
||||
logger.info("Playlist detected from web! Limit: " + data.playlistLimit);
|
||||
audioM.playListLoader(playlist, data.playlistLimit, user, data.onHead);
|
||||
response = new ResponseEntity<>(new CommandResponseData("ADD", "Loaded"), HttpStatus.OK);
|
||||
}else
|
||||
{
|
||||
response = new ResponseEntity<>(new CommandResponseData("ADD", "Adding a list on saved playlist is currently not supported"), HttpStatus.NOT_ACCEPTABLE);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noMatches() {
|
||||
logger.warn("Cant find media ! (web)");
|
||||
logger.warn("Cant find media ! (web) url: "+ data.url);
|
||||
response = new ResponseEntity<>(new CommandResponseData("ADD", "Can't find media!"), HttpStatus.NOT_FOUND);
|
||||
|
||||
}
|
||||
@ -73,7 +77,7 @@ public class WebLoadUtils {
|
||||
while(response == null)
|
||||
Thread.sleep(10);
|
||||
|
||||
} catch (NullMusicManager | NotConnectedException | InterruptedException nullMusicManager) {
|
||||
} catch (InterruptedException nullMusicManager) {
|
||||
nullMusicManager.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user