use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.
the class SubsonicRESTController method getNowPlaying.
@RequestMapping({ "/getNowPlaying", "/getNowPlaying.view" })
public void getNowPlaying(HttpServletRequest req, HttpServletResponse response) {
HttpServletRequest request = wrapRequest(req);
NowPlaying result = new NowPlaying();
for (PlayStatus status : statusService.getPlayStatuses()) {
Player player = status.getPlayer();
MediaFile mediaFile = status.getMediaFile();
String username = player.getUsername();
if (username == null) {
continue;
}
UserSettings userSettings = securityService.getUserSettings(username);
if (!userSettings.isNowPlayingAllowed()) {
continue;
}
long minutesAgo = status.getMinutesAgo();
if (minutesAgo < LIMIT_OF_HISTORY_TO_BE_PRESENTED) {
NowPlayingEntry entry = new NowPlayingEntry();
entry.setUsername(username);
entry.setPlayerId(player.getId());
entry.setPlayerName(player.getName());
entry.setMinutesAgo((int) minutesAgo);
result.getEntry().add(createJaxbChild(entry, player, mediaFile, username));
}
}
Response res = createResponse();
res.setNowPlaying(result);
jaxbWriter.writeResponse(request, response, res);
}
use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.
the class StatusService method getPlayStatuses.
// (Date, PlayStatus) Not reusable
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public List<PlayStatus> getPlayStatuses() {
synchronized (STREAM_LOCK) {
synchronized (REMOTE_LOCK) {
Map<Integer, PlayStatus> result = new LinkedHashMap<>();
for (PlayStatus remotePlay : remotePlays) {
if (!remotePlay.isExpired()) {
result.put(remotePlay.getPlayer().getId(), remotePlay);
}
}
List<TransferStatus> statuses = new ArrayList<>();
statuses.addAll(inactiveStreamStatuses.values());
statuses.addAll(streamStatuses);
for (TransferStatus streamStatus : statuses) {
Player player = streamStatus.getPlayer();
File file = streamStatus.getFile();
if (file == null) {
continue;
}
MediaFile mediaFile = mediaFileService.getMediaFile(file);
if (player == null || mediaFile == null) {
continue;
}
Date time = new Date(System.currentTimeMillis() - streamStatus.getMillisSinceLastUpdate());
result.put(player.getId(), new PlayStatus(mediaFile, player, time));
}
return unmodifiableList(new ArrayList<>(result.values()));
}
}
}
use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.
the class NowPlayingServiceTest method setup.
@BeforeEach
public void setup() {
StatusService statusService = mock(StatusService.class);
MediaFile file = new MediaFile();
file.setId(0);
Player player = new Player();
player.setUsername(ServiceMockUtils.ADMIN_NAME);
PlayStatus playStatus = new PlayStatus(file, player, new Date());
Mockito.when(statusService.getPlayStatuses()).thenReturn(Arrays.asList(playStatus));
nowPlayingService = new NowPlayingService(mock(SecurityService.class), mock(PlayerService.class), statusService, mock(MediaScannerService.class), mock(AvatarService.class), AjaxMockUtils.mock(AjaxHelper.class));
}
use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.
the class SubsonicRESTController method scrobble.
@RequestMapping({ "/scrobble", "/scrobble.view" })
public void scrobble(HttpServletRequest req, HttpServletResponse response) throws ServletRequestBindingException {
HttpServletRequest request = wrapRequest(req);
int[] ids = ServletRequestUtils.getRequiredIntParameters(request, Attributes.Request.ID.value());
long[] times = ServletRequestUtils.getLongParameters(request, "time");
if (times.length > 0 && (int) times.length != (int) ids.length) {
writeError(request, response, ErrorCode.GENERIC, "Wrong number of timestamps: " + times.length);
return;
}
Player player = playerService.getPlayer(request, response);
boolean submission = ServletRequestUtils.getBooleanParameter(request, Attributes.Request.SUBMISSION.value(), true);
for (int i = 0; i < ids.length; i++) {
int id = ids[i];
MediaFile file = mediaFileService.getMediaFile(id);
if (file == null) {
if (LOG.isWarnEnabled()) {
LOG.warn("File to scrobble not found: " + id);
}
continue;
}
Date time = times.length == 0 ? new Date() : new Date(times[i]);
statusService.addRemotePlay(new PlayStatus(file, player, time));
mediaFileService.incrementPlayCount(file);
if (securityService.getUserSettings(player.getUsername()).isLastFmEnabled()) {
audioScrobblerService.register(file, player.getUsername(), submission, time);
}
}
writeEmptyResponse(request, response);
}
use of com.tesshu.jpsonic.domain.PlayStatus in project jpsonic by tesshucom.
the class NowPlayingService method convert.
// (NowPlayingInfo) Not reusable
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private List<NowPlayingInfo> convert(List<PlayStatus> playStatuses) {
HttpServletRequest request = ajaxHelper.getHttpServletRequest();
String url = NetworkUtils.getBaseUrl(request);
List<NowPlayingInfo> result = new ArrayList<>();
final StringBuilder builder = new StringBuilder();
for (PlayStatus status : playStatuses) {
Player player = status.getPlayer();
MediaFile mediaFile = status.getMediaFile();
String username = player.getUsername();
if (username == null) {
continue;
}
UserSettings userSettings = securityService.getUserSettings(username);
if (!userSettings.isNowPlayingAllowed()) {
continue;
}
String artist = mediaFile.getArtist();
String title = mediaFile.getTitle();
String streamUrl = url + "stream?player=" + player.getId() + "&id=" + mediaFile.getId();
String albumUrl = url + ViewName.MAIN.value() + "?id=" + mediaFile.getId();
String lyricsUrl = null;
if (!mediaFile.isVideo()) {
lyricsUrl = url + ViewName.LYRICS.value() + "?artistUtf8Hex=" + StringUtil.utf8HexEncode(artist) + "&songUtf8Hex=" + StringUtil.utf8HexEncode(title);
}
String coverArtUrl = url + ViewName.COVER_ART.value() + "?size=60&id=" + mediaFile.getId();
String avatarUrl = avatarService.createAvatarUrl(url, userSettings);
String tooltip = StringEscapeUtils.escapeHtml(artist) + " – " + StringEscapeUtils.escapeHtml(title);
artist = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(artist, 25));
title = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(title, 25));
if (StringUtils.isNotBlank(player.getName())) {
builder.setLength(0);
username = builder.append(username).append('@').append(player.getName()).toString();
}
username = StringEscapeUtils.escapeHtml(StringUtils.abbreviate(username, 25));
long minutesAgo = status.getMinutesAgo();
if (minutesAgo < LIMIT_OF_HISTORY_TO_BE_PRESENTED) {
result.add(new NowPlayingInfo(player.getId(), username, artist, title, tooltip, streamUrl, albumUrl, lyricsUrl, coverArtUrl, avatarUrl, (int) minutesAgo));
}
}
return result;
}
Aggregations