🎨 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 { ConfigModule } from '@nestjs/config';
import { EventEmitterModule } from '@nestjs/event-emitter'; import { EventEmitterModule } from '@nestjs/event-emitter';
import { AppController } from './app.controller'; import { DiscordConfigService } from './clients/discord/discord.config.service';
import { AppService } from './app.service';
import { DiscordClientModule } from './clients/discord/discord.module'; import { DiscordClientModule } from './clients/discord/discord.module';
import { JellyfinClientModule } from './clients/jellyfin/jellyfin.module'; import { JellyfinClientModule } from './clients/jellyfin/jellyfin.module';
import { CommandModule } from './commands/command.module'; import { CommandModule } from './commands/command.module';
import { DiscordConfigService } from './clients/discord/discord.config.service';
import { PlaybackModule } from './playback/playback.module'; import { PlaybackModule } from './playback/playback.module';
@Module({ @Module({
@ -33,7 +31,7 @@ import { PlaybackModule } from './playback/playback.module';
JellyfinClientModule, JellyfinClientModule,
PlaybackModule, PlaybackModule,
], ],
controllers: [AppController], controllers: [],
providers: [AppService], providers: [],
}) })
export class AppModule {} 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 { export class JellyfinClientModule implements OnModuleInit, OnModuleDestroy {
constructor( constructor(private jellyfinService: JellyfinService) {}
private jellyfinService: JellyfinService,
private readonly jellyfinWebsocketService: JellyinWebsocketService,
) {}
onModuleDestroy() { onModuleDestroy() {
this.jellyfinService.destroy(); this.jellyfinService.destroy();
@ -32,9 +29,5 @@ export class JellyfinClientModule implements OnModuleInit, OnModuleDestroy {
onModuleInit() { onModuleInit() {
this.jellyfinService.init(); this.jellyfinService.init();
this.jellyfinService.authenticate(); 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 { Constants } from '../../utils/constants';
import { SystemApi } from '@jellyfin/sdk/lib/generated-client/api/system-api'; import { SystemApi } from '@jellyfin/sdk/lib/generated-client/api/system-api';
import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api'; import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api';
import { EventEmitter2 } from '@nestjs/event-emitter';
@Injectable() @Injectable()
export class JellyfinService { export class JellyfinService {
@ -13,6 +14,8 @@ export class JellyfinService {
private systemApi: SystemApi; private systemApi: SystemApi;
private userId: string; private userId: string;
constructor(private readonly eventEmitter: EventEmitter2) {}
init() { init() {
this.jellyfin = new Jellyfin({ this.jellyfin = new Jellyfin({
clientInfo: { clientInfo: {
@ -20,8 +23,8 @@ export class JellyfinService {
version: Constants.Metadata.Version, version: Constants.Metadata.Version,
}, },
deviceInfo: { deviceInfo: {
id: 'test', id: 'jellyfin-discord-bot',
name: 'test', name: 'Jellyfin Discord Bot',
}, },
}); });
@ -49,6 +52,8 @@ export class JellyfinService {
this.userId = response.data.SessionInfo.UserId; this.userId = response.data.SessionInfo.UserId;
this.systemApi = getSystemApi(this.api); this.systemApi = getSystemApi(this.api);
this.eventEmitter.emit('clients.jellyfin.ready');
}) })
.catch((test) => { .catch((test) => {
this.logger.error(test); this.logger.error(test);

View File

@ -16,14 +16,20 @@ export class JellyfinStreamBuilderService {
const accessToken = this.jellyfinService.getApi().accessToken; const accessToken = this.jellyfinService.getApi().accessToken;
const url = encodeURI( const uri = new URL(api.basePath);
`${ uri.pathname = `/Audio/${jellyfinItemId}/universal`;
api.basePath uri.searchParams.set('UserId', this.jellyfinService.getUserId());
}/Audio/${jellyfinItemId}/universal?UserId=${this.jellyfinService.getUserId()}&DeviceId=${ uri.searchParams.set(
this.jellyfinService.getJellyfin().clientInfo.name 'DeviceId',
}&MaxStreamingBitrate=${bitrate}&Container=ogg,opus&AudioCodec=opus&TranscodingContainer=ts&TranscodingProtocol=hls&api_key=${accessToken}`, 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 { JellyfinService } from './jellyfin.service';
import { getPlaystateApi } from '@jellyfin/sdk/lib/utils/api/playstate-api'; import { getPlaystateApi } from '@jellyfin/sdk/lib/utils/api/playstate-api';
import { OnEvent } from '@nestjs/event-emitter';
@Injectable() @Injectable()
export class JellyinWebsocketService { export class JellyinWebsocketService {
constructor(private readonly jellyfinClientManager: JellyfinService) {} 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()); const systemApi = getPlaystateApi(this.jellyfinClientManager.getApi());
// TODO: Write socket playstate api to report playback progress // TODO: Write socket playstate api to report playback progress

View File

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

View File

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

View File

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

View File

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