File size: 3,147 Bytes
b0798af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
  Controller,
  Get,
  Put,
  Post,
  Body,
  UseGuards,
  Req,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { PlayerService } from './player.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@ApiTags('Player & Playback State')
@Controller('me/player')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class PlayerController {
  constructor(private readonly playerService: PlayerService) {}

  @Get()
  @ApiOperation({ summary: 'Get current playback state (Spotify-Connect)' })
  getPlayerState(@Req() req: any) {
    return this.playerService.getPlayerState(req.user.userId);
  }

  @Put('play')
  @ApiOperation({
    summary: 'Start/resume playback (enforces tier server-side)',
  })
  play(
    @Req() req: any,
    @Body() body: { context_uri?: string; track_id?: string; offset?: number },
  ) {
    return this.playerService.play(req.user.userId, body);
  }

  @Put('pause')
  @ApiOperation({ summary: 'Pause playback' })
  pause(@Req() req: any) {
    return this.playerService.pause(req.user.userId);
  }

  @Post('next')
  @ApiOperation({ summary: 'Skip to next track in queue' })
  next(@Req() req: any) {
    return this.playerService.next(req.user.userId);
  }

  @Post('previous')
  @ApiOperation({ summary: 'Go to previous track' })
  previous(@Req() req: any) {
    return this.playerService.previous(req.user.userId);
  }

  @Put('seek')
  @ApiOperation({ summary: 'Seek position_ms in active track' })
  seek(@Req() req: any, @Body() body: { position_ms: number }) {
    return this.playerService.seek(req.user.userId, body.position_ms);
  }

  @Put('volume')
  @ApiOperation({ summary: 'Set volume percent for active device' })
  setVolume(@Req() req: any, @Body() body: { volume: number }) {
    return this.playerService.setVolume(req.user.userId, body.volume);
  }

  @Put('shuffle')
  @ApiOperation({ summary: 'Toggle shuffle mode' })
  toggleShuffle(@Req() req: any) {
    return this.playerService.toggleShuffle(req.user.userId);
  }

  @Put('repeat')
  @ApiOperation({ summary: 'Set repeat mode (off / context / track)' })
  setRepeat(
    @Req() req: any,
    @Body() body: { mode: 'off' | 'track' | 'context' },
  ) {
    return this.playerService.setRepeat(req.user.userId, body.mode);
  }

  @Get('queue')
  @ApiOperation({ summary: 'Get upcoming queue' })
  getQueue(@Req() req: any) {
    return this.playerService.getQueue(req.user.userId);
  }

  @Post('queue')
  @ApiOperation({ summary: 'Add track to end of queue' })
  addToQueue(@Req() req: any, @Body() body: { track_id: string }) {
    return this.playerService.addToQueue(req.user.userId, body.track_id);
  }

  @Get('devices')
  @ApiOperation({
    summary: 'List active devices for Spotify-Connect switching',
  })
  getDevices(@Req() req: any) {
    return this.playerService.getDevices(req.user.userId);
  }

  @Put('transfer')
  @ApiOperation({ summary: 'Transfer playback to another registered device' })
  transferPlayback(@Req() req: any, @Body() body: { device_id: string }) {
    return this.playerService.transferPlayback(req.user.userId, body.device_id);
  }
}