use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class SearchCommand method listLocalList.
private void listLocalList() {
if (getCommandInput().isBlank()) {
Session session = getContext().getSession();
List<Playlist> playlists = getQueryBuilderFactory().find(Playlist.class).build(session).getResultList();
EmbedBuilder embedBuilder = new EmbedBuilder();
if (playlists.isEmpty()) {
embedBuilder.setDescription("No playlists");
} else {
EmbedTable table = new EmbedTable(embedBuilder);
table.addColumn("Playlist", playlists, Playlist::getName);
table.addColumn("Duration", playlists, playlist -> Util.normalizeMillis(playlist.getDuration()));
table.addColumn("Items", playlists, playlist -> String.valueOf(playlist.getSize()));
table.build();
}
sendMessage(embedBuilder);
} else {
Playlist playlist = SearchEngine.searchLocalList(getContext().getSession(), getCommandInput());
if (playlist == null) {
throw new NoResultsFoundException(String.format("No local list found for '%s'", getCommandInput()));
}
String createdUserId = playlist.getCreatedUserId();
String createdUser;
if (createdUserId.equals("system")) {
createdUser = playlist.getCreatedUser();
} else {
ShardManager shardManager = Aiode.get().getShardManager();
User userById;
try {
userById = shardManager.retrieveUserById(createdUserId).complete();
} catch (ErrorResponseException e) {
if (e.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
userById = null;
} else {
throw e;
}
}
createdUser = userById != null ? userById.getName() : playlist.getCreatedUser();
}
SpringPropertiesConfig springPropertiesConfig = Aiode.get().getSpringPropertiesConfig();
String baseUri = springPropertiesConfig.requireApplicationProperty("aiode.server.base_uri");
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.addField("Name", playlist.getName(), true);
embedBuilder.addField("Duration", Util.normalizeMillis(playlist.getDuration()), true);
embedBuilder.addField("Created by", createdUser, true);
embedBuilder.addField("Tracks", String.valueOf(playlist.getSize()), true);
embedBuilder.addBlankField(false);
String url = baseUri + String.format("/list?name=%s&guildId=%s", URLEncoder.encode(playlist.getName(), StandardCharsets.UTF_8), playlist.getGuildId());
embedBuilder.addField("First tracks:", "[Full list](" + url + ")", false);
List<PlaylistItem> items = playlist.getItemsSorted();
Util.appendEmbedList(embedBuilder, items.size() > 5 ? items.subList(0, 5) : items, item -> item.display() + " - " + Util.normalizeMillis(item.getDuration()), "Track - Duration");
sendWithLogo(embedBuilder);
}
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class EmptyCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
Playlist playlist = SearchEngine.searchLocalList(session, getCommandInput());
if (playlist == null) {
throw new NoResultsFoundException(String.format("No aiode playlist found for '%s'", getCommandInput()));
}
if (playlist.isEmpty()) {
throw new InvalidCommandException("Playlist is already empty");
}
invoke(() -> {
for (PlaylistItem item : playlist.getItems()) {
session.delete(item);
}
});
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class MoveCommand method moveIndexRange.
private void moveIndexRange(int start, int end, int targetIndex, Playlist playlist) {
List<PlaylistItem> itemsSorted = playlist.getItemsSorted();
invoke(() -> {
boolean movedDown = start < targetIndex;
int range = end - start + 1;
if (movedDown) {
for (int i = start; i <= targetIndex; i++) {
PlaylistItem item = itemsSorted.get(i);
if (i <= end) {
// set items are within the defined start - end range to the desired index
// note that the end item ends up at the targetIndex because of how the items are moved up
// e.g moving 4 - 6 to 10
// before after
// 3. track 3 3. track 3
// 4. to move 1 4. track 7
// 5. to move 2 5. track 8
// 6. to move 3 6. track 9
// 7. track 7 7. track 10
// 8. track 8 8. to move 1
// 9. track 9 9. to move 2
// 10. track 10 10. to move 3
// 11. track 11 11. track 11
item.setIndex(targetIndex - (end - i));
} else {
item.setIndex(item.getIndex() - range);
}
}
String message = String.format("Moved items %d through %d behind item '%s'", start + 1, end + 1, itemsSorted.get(targetIndex).display());
successMessageBuilder.append(message);
} else {
// 17. track 17 17. track 17
for (int i = end; i >= targetIndex; i--) {
PlaylistItem item = itemsSorted.get(i);
if (i >= start) {
item.setIndex(targetIndex + i - start);
} else {
item.setIndex(item.getIndex() + range);
}
}
String message = String.format("Moved items %d through %d ahead of item '%s'", start + 1, end + 1, itemsSorted.get(targetIndex).display());
successMessageBuilder.append(message);
}
});
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class MoveCommand method moveSingleIndex.
private void moveSingleIndex(int index, int targetIndex, Playlist playlist) {
List<PlaylistItem> itemsSorted = playlist.getItemsSorted();
PlaylistItem itemToMove = itemsSorted.get(index);
invoke(() -> {
if (index < targetIndex) {
// move up items that were after the moving item but are now before
for (int i = index + 1; i <= targetIndex; i++) {
PlaylistItem item = itemsSorted.get(i);
item.setIndex(item.getIndex() - 1);
}
} else {
// move down items that were before the moving item but are now after
for (int i = targetIndex; i < index; i++) {
PlaylistItem item = itemsSorted.get(i);
item.setIndex(item.getIndex() + 1);
}
}
itemToMove.setIndex(targetIndex);
successMessageBuilder.append("Moved item '").append(itemToMove.display()).append("' to index ").append(targetIndex + 1);
});
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class RemoveCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
String playlistName = getArgumentValue("from");
Playlist playlist = SearchEngine.searchLocalList(session, playlistName);
if (playlist == null) {
throw new NoResultsFoundException(String.format("No local list found for '%s'", playlistName));
} else if (playlist.isEmpty()) {
throw new InvalidCommandException("Playlist is empty");
}
if (argumentSet("index")) {
StringList indices = StringList.createWithRegex(getCommandInput(), "-");
if (indices.size() == 1 || indices.size() == 2) {
indices.applyForEach(String::trim);
if (indices.size() == 1) {
int index = parse(indices.get(0));
checkIndex(index, playlist);
invoke(() -> session.delete(playlist.getItemsSorted().get(index - 1)));
} else {
int start = parse(indices.get(0));
int end = parse(indices.get(1));
checkIndex(start, playlist);
checkIndex(end, playlist);
if (end <= start) {
throw new InvalidCommandException("End index needs to be greater than start.");
}
invoke(() -> playlist.getItemsSorted().subList(start - 1, end).forEach(session::delete));
}
} else {
throw new InvalidCommandException("Expected one or two indices but found " + indices.size());
}
} else {
List<PlaylistItem> playlistItems = SearchEngine.searchPlaylistItems(playlist, getCommandInput());
if (playlistItems.size() == 1) {
invoke(() -> session.delete(playlistItems.get(0)));
} else if (playlistItems.isEmpty()) {
throw new NoResultsFoundException(String.format("No tracks found for '%s' on list '%s'", getCommandInput(), playlistName));
} else {
askQuestion(playlistItems, PlaylistItem::display, item -> valueOf(item.getIndex() + 1));
}
}
}
Aggregations