Skip to content

Commit

Permalink
Add manage PSA toggle, fix blank sequence triggering PSA, and clear s…
Browse files Browse the repository at this point in the history
…equencesPlayed when clearing queue/votes (#97)
  • Loading branch information
whitesoup12 authored Nov 20, 2023
1 parent 0919054 commit f427067
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.remotefalcon.api.controller;

import com.remotefalcon.api.aop.RequiresPluginAccess;
import com.remotefalcon.api.aop.RequiresViewerAccess;
import com.remotefalcon.api.entity.ViewerPageMeta;
import com.remotefalcon.api.request.*;
import com.remotefalcon.api.response.HighestVotedPlaylistResponse;
import com.remotefalcon.api.response.NextPlaylistResponse;
Expand All @@ -11,13 +9,9 @@
import com.remotefalcon.api.service.PluginService;
import com.remotefalcon.api.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class PluginController {
private final PluginService pluginService;
Expand Down Expand Up @@ -107,4 +101,10 @@ public ResponseEntity<PluginResponse> toggleViewerControl() {
public ResponseEntity<PluginResponse> updateViewerControl(@RequestBody ViewerControlRequest request) {
return this.pluginService.updateViewerControl(request);
}

@PostMapping(value = "/updateManagedPsa")
@RequiresPluginAccess
public ResponseEntity<PluginResponse> updateManagedPsa(@RequestBody ManagedPSARequest request) {
return this.pluginService.updateManagedPsa(request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.remotefalcon.api.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ManagedPSARequest {
private String managedPsaEnabled;
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ public ResponseEntity<?> purgeQueue() {
});
this.playlistGroupRepository.saveAll(playlistGroups);

RemotePreference remotePreference = this.remotePreferenceRepository.findByRemoteToken(tokenDTO.getRemoteToken());
remotePreference.setSequencesPlayed(0);
this.remotePreferenceRepository.save(remotePreference);

return ResponseEntity.status(200).build();
}

Expand Down Expand Up @@ -393,6 +397,11 @@ public ResponseEntity<?> resetAllVotes() {
this.playlistGroupRepository.saveAll(playlistGroups.stream().toList());
List<RemoteViewerVote> remoteViewerVotes = this.remoteViewerVoteRepository.findAllByRemoteToken(tokenDTO.getRemoteToken());
this.remoteViewerVoteRepository.deleteAll(remoteViewerVotes.stream().toList());

RemotePreference remotePreference = this.remotePreferenceRepository.findByRemoteToken(tokenDTO.getRemoteToken());
remotePreference.setSequencesPlayed(0);
this.remotePreferenceRepository.save(remotePreference);

return ResponseEntity.status(200).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.remotefalcon.api.response.PluginResponse;
import com.remotefalcon.api.response.RemotePreferenceResponse;
import com.remotefalcon.api.util.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -21,6 +22,7 @@
import java.util.stream.Collectors;

@Service
@Slf4j
public class PluginService {
private final RemoteRepository remoteRepository;
private final RemoteJukeRepository remoteJukeRepository;
Expand Down Expand Up @@ -199,46 +201,46 @@ public ResponseEntity<PluginResponse> updateWhatsPlaying(UpdateWhatsPlayingReque
}else {
this.currentPlaylistRepository.save(CurrentPlaylist.builder().remoteToken(remoteToken).currentPlaylist(request.getPlaylist()).build());
}
}

this.updateSequenceHideCounts(remoteToken);
RemotePreference remotePreference = this.remotePreferenceRepository.findByRemoteToken(remoteToken);
remotePreference.setSequencesPlayed(remotePreference.getSequencesPlayed() + 1);

RemotePreference remotePreference = this.remotePreferenceRepository.findByRemoteToken(remoteToken);
remotePreference.setSequencesPlayed(remotePreference.getSequencesPlayed() + 1);

//PSA
if(remotePreference.getPsaEnabled() && remotePreference.getManagePsa()) {
int sequencesPlayed = remotePreference.getSequencesPlayed();
if(sequencesPlayed != 0 && sequencesPlayed % remotePreference.getPsaFrequency() == 0) {
Optional<PsaSequence> psaSequence = this.psaSequenceRepository.findFirstByRemoteTokenOrderByPsaSequenceLastPlayedAscPsaSequenceOrderAsc(remoteToken);
if(psaSequence.isPresent()) {
List<Playlist> playlists = this.playlistRepository.findAllByRemoteTokenAndIsSequenceActiveOrderBySequenceVotesDescSequenceVoteTimeAsc(remoteToken, true);
Optional<Playlist> psaPlaylist = playlists.stream().filter(playlist -> StringUtils.equalsIgnoreCase(psaSequence.get().getPsaSequenceName(), playlist.getSequenceName())).findFirst();
if(psaPlaylist.isPresent()) {
if(StringUtils.equalsIgnoreCase("VOTING", remotePreference.getViewerControlMode())) {
psaPlaylist.get().setSequenceVotes(11111);
this.playlistRepository.save(psaPlaylist.get());
}else {
this.remoteJukeRepository.save(RemoteJuke.builder()
.remoteToken(remoteToken)
.nextPlaylist(psaPlaylist.get().getSequenceName())
.ownerRequested(true)
.build());
}
//PSA
if(remotePreference.getPsaEnabled() && remotePreference.getManagePsa()) {
int sequencesPlayed = remotePreference.getSequencesPlayed();
if(sequencesPlayed != 0 && sequencesPlayed % remotePreference.getPsaFrequency() == 0) {
Optional<PsaSequence> psaSequence = this.psaSequenceRepository.findFirstByRemoteTokenOrderByPsaSequenceLastPlayedAscPsaSequenceOrderAsc(remoteToken);
if(psaSequence.isPresent()) {
List<Playlist> playlists = this.playlistRepository.findAllByRemoteTokenAndIsSequenceActiveOrderBySequenceVotesDescSequenceVoteTimeAsc(remoteToken, true);
Optional<Playlist> psaPlaylist = playlists.stream().filter(playlist -> StringUtils.equalsIgnoreCase(psaSequence.get().getPsaSequenceName(), playlist.getSequenceName())).findFirst();
if(psaPlaylist.isPresent()) {
if(StringUtils.equalsIgnoreCase("VOTING", remotePreference.getViewerControlMode())) {
psaPlaylist.get().setSequenceVotes(11111);
this.playlistRepository.save(psaPlaylist.get());
}else {
this.remoteJukeRepository.save(RemoteJuke.builder()
.remoteToken(remoteToken)
.nextPlaylist(psaPlaylist.get().getSequenceName())
.ownerRequested(true)
.build());
}

psaSequence.get().setPsaSequenceLastPlayed(ZonedDateTime.now());
this.psaSequenceRepository.save(psaSequence.get());
psaSequence.get().setPsaSequenceLastPlayed(ZonedDateTime.now());
this.psaSequenceRepository.save(psaSequence.get());
}
}
}
List<PsaSequence> psaSequences = this.psaSequenceRepository.findAllByRemoteToken(remoteToken);
boolean isPsaPlaying = psaSequences.stream().anyMatch(psaSequence -> StringUtils.equalsIgnoreCase(request.getPlaylist(), psaSequence.getPsaSequenceName()));
if(isPsaPlaying) {
remotePreference.setSequencesPlayed(remotePreference.getSequencesPlayed() - 1);
}
this.remotePreferenceRepository.save(remotePreference);
}
List<PsaSequence> psaSequences = this.psaSequenceRepository.findAllByRemoteToken(remoteToken);
boolean isPsaPlaying = psaSequences.stream().anyMatch(psaSequence -> StringUtils.equalsIgnoreCase(request.getPlaylist(), psaSequence.getPsaSequenceName()));
if(isPsaPlaying) {
remotePreference.setSequencesPlayed(remotePreference.getSequencesPlayed() - 1);
}
this.remotePreferenceRepository.save(remotePreference);
}

this.updateSequenceHideCounts(remoteToken);

return ResponseEntity.status(200).body(PluginResponse.builder().currentPlaylist(request.getPlaylist()).build());
}

Expand Down Expand Up @@ -519,6 +521,15 @@ public ResponseEntity<PluginResponse> updateViewerControl(ViewerControlRequest r
return ResponseEntity.status(200).body(PluginResponse.builder().viewerControlEnabled(StringUtils.equalsIgnoreCase("Y", request.getViewerControlEnabled())).build());
}

public ResponseEntity<PluginResponse> updateManagedPsa(ManagedPSARequest request) {
String remoteToken = this.authUtil.getRemoteTokenFromHeader();
RemotePreference remotePreference = this.remotePreferenceRepository.findByRemoteToken(remoteToken);
remotePreference.setManagePsa(StringUtils.equalsIgnoreCase("Y", request.getManagedPsaEnabled()));
remotePreference.setSequencesPlayed(0);
this.remotePreferenceRepository.save(remotePreference);
return ResponseEntity.status(200).body(PluginResponse.builder().viewerControlEnabled(StringUtils.equalsIgnoreCase("Y", request.getManagedPsaEnabled())).build());
}

private void updateQueue(Optional<RemoteJuke> currentSequence, Optional<RemoteJuke> nextSequence) {
if(currentSequence.isPresent()) {
if(nextSequence.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,10 @@ public void allJukeboxRequests_onlyOneSequence() {
@Test
public void purgeQueue() {
TokenDTO tokenDTO = Mocks.tokenDTO();
RemotePreference remotePreference = Mocks.remotePreference();

when(this.authUtil.getJwtPayload()).thenReturn(tokenDTO);
when(this.remotePreferenceRepository.findByRemoteToken(tokenDTO.getRemoteToken())).thenReturn(remotePreference);

ResponseEntity<?> response = this.controlPanelService.purgeQueue();
assertNotNull(response);
Expand Down Expand Up @@ -453,8 +455,10 @@ public void resetAllVotes() {
List<Playlist> sequences = Mocks.sequences();
List<RemoteViewerVote> remoteViewerVotes = Mocks.remoteViewerVotes();
List<PlaylistGroup> playlistGroupList = Mocks.playlistGroupList();
RemotePreference remotePreference = Mocks.remotePreference();

when(this.authUtil.getJwtPayload()).thenReturn(tokenDTO);
when(this.remotePreferenceRepository.findByRemoteToken(tokenDTO.getRemoteToken())).thenReturn(remotePreference);
when(this.playlistRepository.findAllByRemoteTokenAndIsSequenceActiveOrderBySequenceOrderAsc(eq(tokenDTO.getRemoteToken()), eq(true))).thenReturn(sequences);
when(this.remoteViewerVoteRepository.findAllByRemoteToken(eq(tokenDTO.getRemoteToken()))).thenReturn(remoteViewerVotes);
when(this.playlistGroupRepository.findAllByRemoteToken(eq(tokenDTO.getRemoteToken()))).thenReturn(playlistGroupList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,9 @@ public void updateWhatsPlaying_noCurrentPlaylist() {
public void updateWhatsPlaying_noPlaylist() {
String remoteToken = "abc123";
UpdateWhatsPlayingRequest updateWhatsPlayingRequest = Mocks.updateWhatsPlayingRequest();
RemotePreference remotePreference = Mocks.remotePreference();
updateWhatsPlayingRequest.setPlaylist(null);

when(this.authUtil.getRemoteTokenFromHeader()).thenReturn(remoteToken);
when(this.remotePreferenceRepository.findByRemoteToken(remoteToken)).thenReturn(remotePreference);

ResponseEntity<PluginResponse> response = this.pluginService.updateWhatsPlaying(updateWhatsPlayingRequest);
assertNotNull(response);
Expand Down

0 comments on commit f427067

Please sign in to comment.