🎨 Code review

This commit is contained in:
Manuel Ruwe 2022-12-18 19:21:33 +01:00
parent 3fd94e590d
commit a63a2c6dc5
12 changed files with 38 additions and 81 deletions

View File

@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

View File

@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

View File

@ -5,12 +5,10 @@ import { DiscordModule } from '@discord-nestjs/core';
import { ConfigModule } from '@nestjs/config';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DiscordConfigService } from './clients/discord/discord.config.service';
import { DiscordClientModule } from './clients/discord/discord.module';
import { JellyfinClientModule } from './clients/jellyfin/jellyfin.module';
import { CommandModule } from './commands/command.module';
import { DiscordConfigService } from './clients/discord/discord.config.service';
import { PlaybackModule } from './playback/playback.module';
@Module({
@ -33,7 +31,7 @@ import { PlaybackModule } from './playback/playback.module';
JellyfinClientModule,
PlaybackModule,
],
controllers: [AppController],
providers: [AppService],
controllers: [],
providers: [],
})
export class AppModule {}

View File

@ -1,9 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
constructor() {}
getHello(): string {
return 'Hello World!';
}
}

View File

@ -20,10 +20,7 @@ import { JellyinWebsocketService } from './jellyfin.websocket.service';
],
})
export class JellyfinClientModule implements OnModuleInit, OnModuleDestroy {
constructor(
private jellyfinService: JellyfinService,
private readonly jellyfinWebsocketService: JellyinWebsocketService,
) {}
constructor(private jellyfinService: JellyfinService) {}
onModuleDestroy() {
this.jellyfinService.destroy();
@ -32,9 +29,5 @@ export class JellyfinClientModule implements OnModuleInit, OnModuleDestroy {
onModuleInit() {
this.jellyfinService.init();
this.jellyfinService.authenticate();
setTimeout(() => {
this.jellyfinWebsocketService.openSocket();
}, 5000);
}
}

View File

@ -4,6 +4,7 @@ import { Api, Jellyfin } from '@jellyfin/sdk';
import { Constants } from '../../utils/constants';
import { SystemApi } from '@jellyfin/sdk/lib/generated-client/api/system-api';
import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api';
import { EventEmitter2 } from '@nestjs/event-emitter';
@Injectable()
export class JellyfinService {
@ -13,6 +14,8 @@ export class JellyfinService {
private systemApi: SystemApi;
private userId: string;
constructor(private readonly eventEmitter: EventEmitter2) {}
init() {
this.jellyfin = new Jellyfin({
clientInfo: {
@ -20,8 +23,8 @@ export class JellyfinService {
version: Constants.Metadata.Version,
},
deviceInfo: {
id: 'test',
name: 'test',
id: 'jellyfin-discord-bot',
name: 'Jellyfin Discord Bot',
},
});
@ -49,6 +52,8 @@ export class JellyfinService {
this.userId = response.data.SessionInfo.UserId;
this.systemApi = getSystemApi(this.api);
this.eventEmitter.emit('clients.jellyfin.ready');
})
.catch((test) => {
this.logger.error(test);

View File

@ -16,14 +16,20 @@ export class JellyfinStreamBuilderService {
const accessToken = this.jellyfinService.getApi().accessToken;
const url = encodeURI(
`${
api.basePath
}/Audio/${jellyfinItemId}/universal?UserId=${this.jellyfinService.getUserId()}&DeviceId=${
this.jellyfinService.getJellyfin().clientInfo.name
}&MaxStreamingBitrate=${bitrate}&Container=ogg,opus&AudioCodec=opus&TranscodingContainer=ts&TranscodingProtocol=hls&api_key=${accessToken}`,
const uri = new URL(api.basePath);
uri.pathname = `/Audio/${jellyfinItemId}/universal`;
uri.searchParams.set('UserId', this.jellyfinService.getUserId());
uri.searchParams.set(
'DeviceId',
this.jellyfinService.getJellyfin().clientInfo.name,
);
uri.searchParams.set('MaxStreamingBitrate', `${bitrate}`);
uri.searchParams.set('Container', 'ogg,opus');
uri.searchParams.set('AudioCodec', 'opus');
uri.searchParams.set('TranscodingContainer', 'ts');
uri.searchParams.set('TranscodingProtocol', 'hls');
uri.searchParams.set('api_key', accessToken);
return url;
return uri.toString();
}
}

View File

@ -2,12 +2,20 @@ import { Injectable } from '@nestjs/common';
import { JellyfinService } from './jellyfin.service';
import { getPlaystateApi } from '@jellyfin/sdk/lib/utils/api/playstate-api';
import { OnEvent } from '@nestjs/event-emitter';
@Injectable()
export class JellyinWebsocketService {
constructor(private readonly jellyfinClientManager: JellyfinService) {}
async openSocket() {
@OnEvent('clients.jellyfin.ready')
handleJellyfinBotReady() {
console.log('ready!');
this.openSocket();
}
private async openSocket() {
const systemApi = getPlaystateApi(this.jellyfinClientManager.getApi());
// TODO: Write socket playstate api to report playback progress

View File

@ -19,22 +19,12 @@ export class PausePlaybackCommand implements DiscordCommand {
handler(
commandInteraction: CommandInteraction,
): string | InteractionReplyOptions {
const newStatus = this.discordVoiceService.togglePaused();
if (newStatus) {
return {
embeds: [
this.discordMessageService.buildMessage({
title: 'Paused',
}),
],
};
}
const shouldBePaused = this.discordVoiceService.togglePaused();
return {
embeds: [
this.discordMessageService.buildMessage({
title: 'Unpaused',
title: shouldBePaused ? 'Paused' : 'Unpaused',
}),
],
};

View File

@ -245,7 +245,7 @@ export class PlayItemCommand
const milliseconds = jellyfinPlayable.RunTimeTicks / 10000;
return this.playbackService.eneuqueTrack({
return this.playbackService.enqueueTrack({
jellyfinId: jellyfinPlayable.Id,
name: jellyfinPlayable.Name,
durationInMilliseconds: milliseconds,

View File

@ -43,7 +43,7 @@ export class PlaylistCommand implements DiscordCommand {
let point = this.getListPoint(isCurrent, index);
point += `**${trimStringToFixedLength(track.track.name, 30)}**`;
if (isCurrent === true) {
if (isCurrent) {
point += ' :loud_sound:';
}

View File

@ -64,7 +64,7 @@ export class PlaybackService {
return true;
}
eneuqueTrack(track: Track) {
enqueueTrack(track: Track) {
const uuid = uuidv4();
const emptyBefore = this.playlist.tracks.length === 0;