Skip to content

Commit

Permalink
fix(twitch.tv): disconnect twitch.tv profile (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Mar 22, 2021
1 parent b73ec35 commit dcec891
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/players/services/players.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ describe('PlayersService', () => {
});
});

describe('#removeTwitchTvProfile()', () => {
beforeEach(async () => {
const twitchTvUser = {
userId: 'FAKE_TWITCH_TV_USER_ID',
login: 'FAKE_TWITCH_TV_LOGIN',
};
await service.registerTwitchAccount(mockPlayer.id, twitchTvUser);
});

it('should remove the twitchTvProfile', async () => {
const ret = await service.removeTwitchTvProfile(mockPlayer.id);
expect(ret.twitchTvUser).toBe(undefined);
expect((await playerModel.findById(mockPlayer.id)).twitchTvUser).toBe(undefined);
});
});

describe('#getUsersWithTwitchTvAccount()', () => {
beforeEach(async () => {
const player = await playerModel.findOne();
Expand Down
9 changes: 8 additions & 1 deletion src/players/services/players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { classToPlain, plainToClass } from 'class-transformer';
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { OnlinePlayersService } from './online-players.service';
import { WebsocketEvent } from '@/websocket-event';
import { UpdateQuery } from 'mongoose';

type ForceCreatePlayerOptions = Pick<Player, 'steamId' | 'name'>;

Expand Down Expand Up @@ -144,15 +145,21 @@ export class PlayersService implements OnModuleInit {
return player;
}

// FIXME Move this method to TwitchModule
async registerTwitchAccount(playerId: string, twitchTvUser: TwitchTvUser): Promise<Player> {
return this.updatePlayer(playerId, { twitchTvUser });
}

// FIXME Move this method to TwitchModule
async removeTwitchTvProfile(playerId: string) {
return this.updatePlayer(playerId, { $unset: { twitchTvUser: 1 } });
}

async getUsersWithTwitchTvAccount(): Promise<Player[]> {
return plainToClass(Player, await this.playerModel.find({ twitchTvUser: { $exists: true } }).lean().exec());
}

async updatePlayer(playerId: string, update: Partial<Player>, adminId?: string): Promise<Player> {
async updatePlayer(playerId: string, update: UpdateQuery<Player>, adminId?: string): Promise<Player> {
const oldPlayer = await this.getById(playerId);
const newPlayer = plainToClass(Player, await this.playerModel.findOneAndUpdate({ _id: playerId }, update, { new: true }).lean().exec());
this.events.playerUpdates.next({ oldPlayer, newPlayer, adminId });
Expand Down
10 changes: 10 additions & 0 deletions src/streams/twitch/controllers/twitch.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PlayersService } from '@/players/services/players.service';
import { AuthService } from '@/auth/services/auth.service';
import { JsonWebTokenError } from 'jsonwebtoken';
import { BadRequestException } from '@nestjs/common';
import { Player } from '@/players/models/player';

class TwitchServiceStub {
streams = [
Expand Down Expand Up @@ -43,6 +44,7 @@ class TwitchAuthServiceStub {

class PlayersServiceStub {
registerTwitchAccount(playerId: string, twitchUserId: string) { return Promise.resolve(); }
removeTwitchTvProfile = jest.fn().mockResolvedValue({ id: 'FAKE_USER_ID' });
}

class AuthServiceStub {
Expand Down Expand Up @@ -109,6 +111,14 @@ describe('Twitch Controller', () => {
});
});

describe('#disconnect()', () => {
it('should remove twitch.tv profile from the user\'s account', async () => {
const ret = await controller.disconnect({ id: 'FAKE_USER_ID' } as Player);
expect(playersService.removeTwitchTvProfile).toHaveBeenCalledWith('FAKE_USER_ID');
expect(ret).toEqual({ id: 'FAKE_USER_ID' });
});
});

describe('#getStreams()', () => {
it('should return streams', () => {
expect(controller.getStreams()).toEqual(twitchService.streams);
Expand Down
12 changes: 10 additions & 2 deletions src/streams/twitch/controllers/twitch.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Controller, Get, Redirect, Query, BadRequestException, Logger } from '@nestjs/common';
import { Controller, Get, Redirect, Query, BadRequestException, Logger, Put } from '@nestjs/common';
import { TwitchService } from '../services/twitch.service';
import { TwitchAuthService } from '../services/twitch-auth.service';
import { PlayersService } from '@/players/services/players.service';
import { AuthService } from '@/auth/services/auth.service';
import { JsonWebTokenError } from 'jsonwebtoken';
import { TwitchTvUser } from '@/players/models/twitch-tv-user';
import { KeyName } from '@/auth/key-name';
import { JwtTokenPurpose } from '@/auth/jwt-token-purpose';
import { Auth } from '@/auth/decorators/auth.decorator';
import { User } from '@/auth/decorators/user.decorator';
import { Player } from '@/players/models/player';

@Controller('twitch')
export class TwitchController {
Expand Down Expand Up @@ -52,6 +54,12 @@ export class TwitchController {
await this.playersService.registerTwitchAccount(id, twitchTvUser);
}

@Put('disconnect')
@Auth()
async disconnect(@User() user: Player) {
return this.playersService.removeTwitchTvProfile(user.id);
}

@Get('streams')
getStreams() {
return this.twitchService.streams;
Expand Down

0 comments on commit dcec891

Please sign in to comment.