Reformat code
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
import { TableEntry } from "../models/TableEntry";
|
||||
import { TunakillUser } from "../models/TunakillUser";
|
||||
import {HttpException, HttpStatus, Injectable} from "@nestjs/common";
|
||||
import {TunakillLogin} from "../models/TunakillLogin";
|
||||
import { TableEntry } from '../models/TableEntry';
|
||||
import { TunakillUser } from '../models/TunakillUser';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { TunakillLogin } from '../models/TunakillLogin';
|
||||
import fetch from 'node-fetch';
|
||||
import logger from "../logger/Logger";
|
||||
import logger from '../logger/Logger';
|
||||
|
||||
@Injectable()
|
||||
export class SinusBotService {
|
||||
private host = process.env.HOST
|
||||
private host = process.env.HOST;
|
||||
private credentials = {
|
||||
username: process.env.SINUSBOT_USER,
|
||||
password: process.env.SINUSBOT_PASSWORD
|
||||
}
|
||||
password: process.env.SINUSBOT_PASSWORD,
|
||||
};
|
||||
|
||||
private botInfo = {
|
||||
id: undefined,
|
||||
instanceId: process.env.SINUSBOT_INSTANCEID
|
||||
}
|
||||
instanceId: process.env.SINUSBOT_INSTANCEID,
|
||||
};
|
||||
|
||||
private botIdURL = `${this.host}/api/v1/botId`;
|
||||
private tunaKillURL = `${this.host}/api/v1/bot/i/${this.botInfo.instanceId}/event/tunakill_rank_all_user`;
|
||||
@@ -34,23 +34,30 @@ export class SinusBotService {
|
||||
// if (this.bearer == null) {
|
||||
logger.info(`Hey! I'm trying to get my Bearer token!`);
|
||||
await this.login()
|
||||
.then(token => this.bearer = token)
|
||||
.then(token => (this.bearer = token))
|
||||
.then(() => {
|
||||
logger.debug(
|
||||
`Seems like I have my Bearer token!
|
||||
Looks like it's ${this.bearer == null ? 'undefined' : 'not undefined'}`
|
||||
Looks like it's ${
|
||||
this.bearer == null ? 'undefined' : 'not undefined'
|
||||
}`,
|
||||
);
|
||||
});
|
||||
// }
|
||||
|
||||
logger.info(`I try to fetch user data now! The URL is called ${this.tunaKillURL}`);
|
||||
logger.info(
|
||||
`I try to fetch user data now! The URL is called ${this.tunaKillURL}`,
|
||||
);
|
||||
return await fetch(this.tunaKillURL, this.requestConfig(null, this.bearer))
|
||||
.then(res => this.checkStatus(res))
|
||||
.then(res => res.json())
|
||||
.then(data => this.consumeTunakillResponse(data))
|
||||
.catch(error => {
|
||||
logger.error(`I couldn't fetch user data.`, error);
|
||||
throw this.createHttpException(HttpStatus.INTERNAL_SERVER_ERROR, error.message);
|
||||
throw this.createHttpException(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
error.message,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -61,12 +68,15 @@ export class SinusBotService {
|
||||
if (this.botInfo.id == null) {
|
||||
logger.debug(`I have to fetch a bot ID before I can continue!`);
|
||||
await this.fetchDefaultBotId()
|
||||
.then(defaultBotId => this.botInfo.id = defaultBotId)
|
||||
.then(defaultBotId => (this.botInfo.id = defaultBotId))
|
||||
.catch(error => {
|
||||
logger.warn(`I couldn't retrieve SinusBot bot information. Login is likely to fail!`, error);
|
||||
logger.warn(
|
||||
`I couldn't retrieve SinusBot bot information. Login is likely to fail!`,
|
||||
error,
|
||||
);
|
||||
throw this.createHttpException(
|
||||
HttpStatus.NOT_FOUND,
|
||||
`Could not fetch enough bot information for further requests.`
|
||||
`Could not fetch enough bot information for further requests.`,
|
||||
);
|
||||
});
|
||||
logger.info(`The bot ID now is ${this.botInfo.id}`);
|
||||
@@ -75,20 +85,23 @@ export class SinusBotService {
|
||||
const body: TunakillLogin = {
|
||||
username: this.credentials.username,
|
||||
password: this.credentials.password,
|
||||
botId: this.botInfo.id
|
||||
}
|
||||
botId: this.botInfo.id,
|
||||
};
|
||||
|
||||
logger.info(`Logging in for Bearer token!`)
|
||||
logger.info(`Logging in for Bearer token!`);
|
||||
return await fetch(this.loginURL, this.requestConfig(JSON.stringify(body)))
|
||||
.then(res => this.checkStatus(res))
|
||||
.then(res => res.json())
|
||||
.then(data => data.token)
|
||||
.catch(error => {
|
||||
logger.error(`Oh oh! Something went wrong while fetching Bearer token.`, error);
|
||||
logger.error(
|
||||
`Oh oh! Something went wrong while fetching Bearer token.`,
|
||||
error,
|
||||
);
|
||||
throw this.createHttpException(
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
`Fetching Bearer token for Sinusbot failed.
|
||||
Please refresh page or try again later!`
|
||||
Please refresh page or try again later!`,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -110,39 +123,56 @@ export class SinusBotService {
|
||||
throw Error('Response from SinusBot does not have any data to parse.');
|
||||
}
|
||||
|
||||
return response
|
||||
// TODO: Remove hardcoded username filter for bots.
|
||||
.filter((user: TunakillUser) => user.name !== "Server Query Admin" && user.name !== "DJ Inshalla")
|
||||
return (
|
||||
response
|
||||
// TODO: Remove hardcoded username filter for bots.
|
||||
.filter(
|
||||
(user: TunakillUser) =>
|
||||
user.name !== 'Server Query Admin' && user.name !== 'DJ Inshalla',
|
||||
)
|
||||
|
||||
.filter((user: TunakillUser) => user.time != null)
|
||||
.map((user: TunakillUser) => {
|
||||
return {
|
||||
name: user.name,
|
||||
rawTime: user.time,
|
||||
onlineTime: this.humanizeTime(user.time)
|
||||
};
|
||||
})
|
||||
.sort((a: any, b: any) => this.sortByDescendingTime(a.rawTime, b.rawTime));
|
||||
.filter((user: TunakillUser) => user.time != null)
|
||||
.map((user: TunakillUser) => {
|
||||
return {
|
||||
name: user.name,
|
||||
rawTime: user.time,
|
||||
onlineTime: this.humanizeTime(user.time),
|
||||
};
|
||||
})
|
||||
.sort((a: any, b: any) =>
|
||||
this.sortByDescendingTime(a.rawTime, b.rawTime),
|
||||
)
|
||||
);
|
||||
}
|
||||
private requestConfig = (body?: string, bearerToken?: string, requestType: string = 'POST'): RequestInit => {
|
||||
private requestConfig = (
|
||||
body?: string,
|
||||
bearerToken?: string,
|
||||
requestType: string = 'POST',
|
||||
): RequestInit => {
|
||||
return {
|
||||
method: requestType,
|
||||
body: body,
|
||||
headers: {
|
||||
'Accept': '*/*',
|
||||
Accept: '*/*',
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': 'HumeniusTSRankingBackend/0.0.2',
|
||||
'Authorization': `Bearer ${bearerToken}`
|
||||
}
|
||||
Authorization: `Bearer ${bearerToken}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
private createHttpException = (statusCode: HttpStatus, message?: string): HttpException => {
|
||||
return new HttpException({
|
||||
status: statusCode,
|
||||
error: message
|
||||
}, statusCode);
|
||||
}
|
||||
private createHttpException = (
|
||||
statusCode: HttpStatus,
|
||||
message?: string,
|
||||
): HttpException => {
|
||||
return new HttpException(
|
||||
{
|
||||
status: statusCode,
|
||||
error: message,
|
||||
},
|
||||
statusCode,
|
||||
);
|
||||
};
|
||||
|
||||
private checkStatus = response => {
|
||||
if (!response.ok) {
|
||||
@@ -151,7 +181,7 @@ export class SinusBotService {
|
||||
throw err;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
};
|
||||
|
||||
private sortByDescendingTime = (a: number, b: number) => {
|
||||
if (a < b) {
|
||||
@@ -163,16 +193,16 @@ export class SinusBotService {
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
private humanizeTime = (seconds: number) => {
|
||||
const d = Math.floor(seconds / (3600 * 24));
|
||||
const h = Math.floor(seconds % (3600 * 24) / 3600);
|
||||
const m = Math.floor(seconds % 3600 / 60);
|
||||
const h = Math.floor((seconds % (3600 * 24)) / 3600);
|
||||
const m = Math.floor((seconds % 3600) / 60);
|
||||
const s = Math.floor(seconds % 60);
|
||||
|
||||
return `${d}d ${h}h ${m}m ${s}s`;
|
||||
}
|
||||
};
|
||||
|
||||
private static logResponse(res: any) {
|
||||
logger.debug(res);
|
||||
|
||||
Reference in New Issue
Block a user