Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tsa96 committed Oct 19, 2024
1 parent 434f8de commit 71f7089
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
3 changes: 3 additions & 0 deletions apps/backend/src/app/dto/queries/map-queries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ export class MapLeaderboardGetQueryDto
})
readonly filter?: MapRunsGetFilter;

// THIS WILL THROW FOR BIG NUMBERS, VALIDATE BETTER DICKHEAD
@IntCsvQueryProperty({ description: 'List of users to limit results to' })
readonly filterUserIDs?: number[];

// TODO: steamID versioN!!

@BooleanQueryProperty({
description: 'Whether to order by date or not (false for reverse)'
})
Expand Down
7 changes: 6 additions & 1 deletion apps/backend/src/app/modules/maps/maps.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,13 @@ export class MapsController {
description:
"When the filtering by 'friends', and the user doesn't have any Steam friends"
})
@ApiConflictResponse({
description:
"When filtering by 'friends', the user's friend list is private"
})
@ApiServiceUnavailableResponse({
description: "Steam fails to return the user's friends list (Tuesdays lol)"
description:
"When filtering by 'friends', and Steam fails to return the user's friends list (Tuesdays lol)"
})
getLeaderboards(
@Param('mapID', ParseIntSafePipe) mapID: number,
Expand Down
12 changes: 7 additions & 5 deletions apps/backend/src/app/modules/runs/leaderboard-runs.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
BadRequestException,
ForbiddenException,
forwardRef,
GoneException,
Inject,
Injectable,
InternalServerErrorException,
NotFoundException,
ServiceUnavailableException,
UnauthorizedException
Expand All @@ -23,6 +25,7 @@ import { EXTENDED_PRISMA_SERVICE } from '../database/db.constants';
import { ExtendedPrismaService } from '../database/prisma.extension';
import { MapsService } from '../maps/maps.service';
import { FileStoreService } from '../filestore/file-store.service';
import { AxiosError } from 'axios';

@Injectable()
export class LeaderboardRunsService {
Expand Down Expand Up @@ -125,11 +128,10 @@ export class LeaderboardRunsService {
} else if (query.filter?.[0] === 'friends') {
// Regular skip/take should work fine here.

const steamFriends = await this.steamService
.getSteamFriends(loggedInUserSteamID)
.catch(() => {
throw new ServiceUnavailableException();
});
// Fetch Steam friends, leave errors uncaugght, this function will throw
// an appropriate response.
const steamFriends =
await this.steamService.getSteamFriends(loggedInUserSteamID);

if (steamFriends.length === 0)
throw new GoneException('No friends detected :(');
Expand Down
35 changes: 23 additions & 12 deletions apps/backend/src/app/modules/steam/steam.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { HttpService } from '@nestjs/axios';
import {
ConflictException,
Injectable,
InternalServerErrorException,
Logger,
ServiceUnavailableException,
UnauthorizedException
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { catchError, lastValueFrom, map } from 'rxjs';
import * as AppTicket from 'steam-appticket';
import { SteamFriendData, SteamUserSummaryData } from './steam.interface';
import { AxiosError } from 'axios';

@Injectable()
export class SteamService {
Expand All @@ -25,6 +28,8 @@ export class SteamService {
private readonly steamApiKey: string;
private readonly steamTicketsSecretKey: string;

private readonly logger = new Logger('Steam Service');

/**
* Handler for ISteamUser/GetPlayerSummaries/v2/
*/
Expand Down Expand Up @@ -57,9 +62,11 @@ export class SteamService {

/**
* Handler for ISteamUser/GetFriendList/v1/
* Throws several different different errors for different failures, which can
* be left uncaught if you want to throw an error response if this fails.
*/
async getSteamFriends(steamID: bigint): Promise<SteamFriendData[]> {
const response = await lastValueFrom(
return lastValueFrom(
this.http
.get('https://api.steampowered.com/ISteamUser/GetFriendList/v1/', {
params: {
Expand All @@ -69,20 +76,24 @@ export class SteamService {
}
})
.pipe(
catchError((_) => {
throw new ServiceUnavailableException(
'Failed to retrieve friends list from steam'
);
map((res) => res?.data?.friendslist?.friends),
catchError((error: AxiosError) => {
if (error.response) {
if (error.response.status === 401) {
throw new ConflictException();
} else {
throw new InternalServerErrorException(
`ISteamUser/GetFriendList/v1/ returned ${error.status}, which we don't know how to handle!`
);
}
} else {
throw new ServiceUnavailableException(
'Steam friends list API did not respond to our request'
);
}
})
)
);

if (!response.data)
throw new InternalServerErrorException(
'Failed to get Steam friends list'
);

return response.data.friendslist.friends;
}

/**
Expand Down
1 change: 1 addition & 0 deletions map-list-approved.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions map-list-submission.json

Large diffs are not rendered by default.

0 comments on commit 71f7089

Please sign in to comment.