playlist websockets
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import {Body, Controller, Get, Post} from '@nestjs/common';
|
import {Body, Controller, Get, Post} from '@nestjs/common';
|
||||||
import {PlaylistService} from "./playlist.service";
|
import {PlaylistService} from "./playlist.service";
|
||||||
import {PlaylistModel} from "./playlist.model";
|
import {PlaylistModel} from "./playlist.model";
|
||||||
import {PlaylistEntity} from "./playlist.entity";
|
|
||||||
|
|
||||||
@Controller('playlist')
|
@Controller('playlist')
|
||||||
export class PlaylistController {
|
export class PlaylistController {
|
||||||
@@ -17,7 +16,7 @@ export class PlaylistController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async create(@Body() playlist: PlaylistModel): Promise<PlaylistEntity> {
|
async create(@Body() playlist: PlaylistModel): Promise<void> {
|
||||||
return await this.service.create(playlist);
|
await this.service.create(playlist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,12 +3,17 @@ import { InjectRepository } from '@nestjs/typeorm';
|
|||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import {PlaylistEntity} from "./playlist.entity";
|
import {PlaylistEntity} from "./playlist.entity";
|
||||||
import {TrackService} from "../track/track.service";
|
import {TrackService} from "../track/track.service";
|
||||||
|
import {WebSocketGateway, WebSocketServer} from "@nestjs/websockets";
|
||||||
|
import {Server} from "socket.io";
|
||||||
const fetch = require('isomorphic-unfetch');
|
const fetch = require('isomorphic-unfetch');
|
||||||
const { getData, getPreview, getTracks, getDetails } = require('spotify-url-info')(fetch);
|
const { getData, getPreview, getTracks, getDetails } = require('spotify-url-info')(fetch);
|
||||||
|
|
||||||
|
@WebSocketGateway()
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PlaylistService {
|
export class PlaylistService {
|
||||||
|
|
||||||
|
@WebSocketServer() io: Server;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(PlaylistEntity)
|
@InjectRepository(PlaylistEntity)
|
||||||
private repository: Repository<PlaylistEntity>,
|
private repository: Repository<PlaylistEntity>,
|
||||||
@@ -27,9 +32,10 @@ export class PlaylistService {
|
|||||||
await this.repository.delete(id);
|
await this.repository.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(playlist: PlaylistEntity): Promise<PlaylistEntity> {
|
async create(playlist: PlaylistEntity): Promise<void> {
|
||||||
const details = await getDetails(playlist.spotifyUrl);
|
const details = await getDetails(playlist.spotifyUrl);
|
||||||
const savedPlaylist = await this.repository.save({...playlist, name: details.preview.title});
|
const savedPlaylist = await this.repository.save({...playlist, name: details.preview.title});
|
||||||
|
this.io.emit('playlistNew', savedPlaylist);
|
||||||
for(let track of details.tracks) {
|
for(let track of details.tracks) {
|
||||||
await this.trackService.create({
|
await this.trackService.create({
|
||||||
artist: track.artist,
|
artist: track.artist,
|
||||||
@@ -37,10 +43,10 @@ export class PlaylistService {
|
|||||||
spotifyUrl: track.previewUrl,
|
spotifyUrl: track.previewUrl,
|
||||||
}, savedPlaylist);
|
}, savedPlaylist);
|
||||||
}
|
}
|
||||||
return savedPlaylist;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: number, track: PlaylistEntity): Promise<void> {
|
async update(id: number, playlist: PlaylistEntity): Promise<void> {
|
||||||
await this.repository.update(id, track);
|
await this.repository.update(id, playlist);
|
||||||
|
this.io.emit('trackPlaylist', playlist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ export class TrackService {
|
|||||||
|
|
||||||
async update(id: number, track: TrackEntity): Promise<void> {
|
async update(id: number, track: TrackEntity): Promise<void> {
|
||||||
await this.repository.update(id, track);
|
await this.repository.update(id, track);
|
||||||
this.io.emit('track', track);
|
this.io.emit('trackUpdate', track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Interval(1000)
|
@Interval(1000)
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import {
|
|||||||
} from "@ngneat/elf-entities";
|
} from "@ngneat/elf-entities";
|
||||||
import {joinRequestResult, trackRequestResult} from "@ngneat/elf-requests";
|
import {joinRequestResult, trackRequestResult} from "@ngneat/elf-requests";
|
||||||
import {map, tap} from "rxjs";
|
import {map, tap} from "rxjs";
|
||||||
import {TrackService} from "./track.service";
|
import {Track, TrackService} from "./track.service";
|
||||||
import {PlaylistEntity} from "spooty-be/dist/playlist/playlist.entity";
|
import {Socket} from "ngx-socket-io";
|
||||||
|
|
||||||
const STORE_NAME = 'playlist';
|
const STORE_NAME = 'playlist';
|
||||||
const ENDPOINT = '/api/playlist';
|
const ENDPOINT = '/api/playlist';
|
||||||
@@ -49,8 +49,16 @@ export class PlaylistService {
|
|||||||
createLoading$ = this.store.pipe(joinRequestResult([CREATE_LOADING], { initialStatus: 'idle' }));
|
createLoading$ = this.store.pipe(joinRequestResult([CREATE_LOADING], { initialStatus: 'idle' }));
|
||||||
|
|
||||||
constructor(private readonly http: HttpClient,
|
constructor(private readonly http: HttpClient,
|
||||||
|
private readonly socket: Socket,
|
||||||
private readonly trackService: TrackService,
|
private readonly trackService: TrackService,
|
||||||
) {
|
) {
|
||||||
|
this.socket.on('playlistUpdate', (playlist: Playlist) => this.store.update(upsertEntities(playlist)));
|
||||||
|
this.socket.on('playlistNew', (playlist: Playlist) =>
|
||||||
|
this.store.update(
|
||||||
|
upsertEntities(playlist),
|
||||||
|
upsertEntities({id: playlist.id, collapsed: false}, {ref: UIEntitiesRef})
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch(): void {
|
fetch(): void {
|
||||||
@@ -67,13 +75,7 @@ export class PlaylistService {
|
|||||||
create(spotifyUrl: string): void {
|
create(spotifyUrl: string): void {
|
||||||
this.http.post(ENDPOINT, {spotifyUrl}).pipe(
|
this.http.post(ENDPOINT, {spotifyUrl}).pipe(
|
||||||
trackRequestResult([CREATE_LOADING], { skipCache: true })
|
trackRequestResult([CREATE_LOADING], { skipCache: true })
|
||||||
).subscribe((playlist: Partial<Playlist>) => {
|
).subscribe();
|
||||||
this.store.update(upsertEntities(playlist));
|
|
||||||
this.store.update(
|
|
||||||
upsertEntities(playlist),
|
|
||||||
upsertEntities({id: playlist.id, collapsed: false}, {ref: UIEntitiesRef})
|
|
||||||
)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleCollapsed(id: number): void {
|
toggleCollapsed(id: number): void {
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ export class TrackService {
|
|||||||
private readonly http: HttpClient,
|
private readonly http: HttpClient,
|
||||||
private readonly socket: Socket,
|
private readonly socket: Socket,
|
||||||
) {
|
) {
|
||||||
this.socket.on('track', (track: Track) => this.store.update(upsertEntities(track)));
|
this.socket.on('trackUpdate', (track: Track) => this.store.update(upsertEntities(track)));
|
||||||
this.socket.on('trackNew', ({track, playlistId}: {track: Track, playlistId: number}) =>
|
this.socket.on('trackNew', ({track, playlistId}: {track: Track, playlistId: number}) =>
|
||||||
this.store.update(upsertEntities([{...track, playlistId}]))
|
this.store.update(upsertEntities([{...track, playlistId}]))
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user