Files
Yumi/src/providers/VRChatAPI.ts
T

207 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-04-23 14:23:20 +07:00
import * as VRChat from 'vrchat';
2023-04-23 13:13:20 +07:00
import Environment from './Environment';
import Logger from '../libs/Logger';
import App from './App';
2023-04-23 17:50:44 +07:00
const LOGGING_TAG = '[VRChatAPI]';
2023-04-23 14:23:20 +07:00
const VRC_CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
2023-04-23 13:13:20 +07:00
interface VRChatAPIs {
2023-04-23 14:23:20 +07:00
AuthenticationApi: VRChat.AuthenticationApi;
UsersApi: VRChat.UsersApi;
WorldsApi: VRChat.WorldsApi;
}
interface UserCache {
user: VRChat.User;
expires: Date;
}
interface WorldCache {
world: VRChat.World;
expires: Date;
}
interface Cache {
Users: { [key: string]: UserCache };
Worlds: { [key: string]: WorldCache };
2023-04-23 13:13:20 +07:00
}
class VRChatAPI {
public client: VRChatAPIs | null;
2023-04-23 14:23:20 +07:00
public configuration: VRChat.Configuration | null;
2023-04-23 17:50:44 +07:00
private ready = false;
2023-04-23 14:23:20 +07:00
private cache: Cache;
private useragent: string | null = null;
2023-04-23 13:13:20 +07:00
constructor() {
2023-04-23 14:23:20 +07:00
this.cache = {
Users: {},
Worlds: {}
};
2023-04-23 13:13:20 +07:00
this.client = null;
this.configuration = null;
}
public async init() {
2023-04-23 14:23:20 +07:00
this.useragent = `Yumi/${App.version.replaceAll('/', '').replaceAll(' ', '-').replaceAll('--', '-')} ${
Environment.get().VRC_CONTACT_EMAIL
}`;
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Using user agent ${this.useragent}`);
2023-04-23 14:23:20 +07:00
this.configuration = new VRChat.Configuration({
2023-04-23 13:13:20 +07:00
//username: Environment.get().VRC_USERNAME,
//;password: Environment.get().VRC_PASSWORD,
apiKey: Environment.get().VRC_API_KEY,
baseOptions: {
headers: {
2023-04-23 14:23:20 +07:00
'User-Agent': this.useragent,
2023-04-23 13:13:20 +07:00
'Content-Type': 'application/json',
Cookie: Environment.get().VRC_COOKIE || ''
}
}
});
this.client = {
2023-04-23 14:23:20 +07:00
AuthenticationApi: new VRChat.AuthenticationApi(this.configuration),
UsersApi: new VRChat.UsersApi(this.configuration),
WorldsApi: new VRChat.WorldsApi(this.configuration)
2023-04-23 13:13:20 +07:00
};
2023-04-23 17:50:44 +07:00
Logger.info(LOGGING_TAG, 'Logging in to VRChat');
2023-04-23 13:13:20 +07:00
let res;
try {
res = await this.client.AuthenticationApi.getCurrentUser();
} catch (err: any) {
if (err.response?.status === 401) {
2023-04-23 17:50:44 +07:00
Logger.error(LOGGING_TAG, 'Unable to log in, check your credentials');
return;
} else throw err;
}
2023-04-23 17:50:44 +07:00
if ((res.data as any).requiresTwoFactorAuth) {
Logger.error(
LOGGING_TAG,
'Two factor authentication is required, please authenticate',
(res.data as any).requiresTwoFactorAuth
);
Logger.info(LOGGING_TAG, `Auth Cookie: ${this.configuration.baseOptions.headers.Cookie}`);
return;
}
this.ready = true;
2023-04-23 17:50:44 +07:00
Logger.info(LOGGING_TAG, 'Logged in to VRChat as ' + res.data.displayName);
2023-04-23 14:23:20 +07:00
setInterval(() => {
for (let key in this.cache.Users) {
if (this.cache.Users[key].expires < new Date()) {
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Removing user ${key} from cache (expired)`);
2023-04-23 14:23:20 +07:00
delete this.cache.Users[key];
}
}
for (let key in this.cache.Worlds) {
if (this.cache.Worlds[key].expires < new Date()) {
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Removing world ${key} from cache (expired)`);
2023-04-23 14:23:20 +07:00
delete this.cache.Worlds[key];
}
}
}, VRC_CACHE_DURATION);
}
2023-04-23 17:51:25 +07:00
public async reInit() {
Logger.info(LOGGING_TAG, 'Re-initializing VRChat API');
this.ready = false;
this.client = null;
this.configuration = null;
await this.init();
}
public async loginEmailOtp(otp: string) {
if (this.ready) throw new Error('Already logged in');
let res;
try {
res = await this.client!.AuthenticationApi.verify2FAEmailCode({
code: otp
});
} catch (err: any) {
if (err.response?.status === 400) {
Logger.error(LOGGING_TAG, 'Unable to log in, invalid OTP');
throw new Error('Invalid OTP');
} else if (err.response?.status === 401) {
Logger.error(LOGGING_TAG, 'Unable to log in, invalid auth cookie');
throw new Error('Invalid auth cookie');
} else throw err;
}
}
public isReady(): boolean {
return this.ready;
}
2023-04-23 14:23:20 +07:00
public async getCachedUserById(id: string) {
if (typeof this.cache.Users[id] !== 'undefined') {
if (this.cache.Users[id].expires > new Date()) {
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Cache hit for user ${id}`);
2023-04-23 14:23:20 +07:00
return this.cache.Users[id].user;
}
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Cache hit for user ${id} but expired, refreshing`);
2023-04-23 14:23:20 +07:00
}
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Looking up user ${id} in VRChat API`);
2023-04-23 14:23:20 +07:00
let user;
try {
user = await this.client!.UsersApi.getUser(id);
} catch (err: any) {
if (err.response?.status === 404) return null;
throw err;
}
if (!user) {
return null;
}
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Caching user ${id}`);
2023-04-23 14:23:20 +07:00
this.cache.Users[id] = {
user: user.data,
expires: new Date(Date.now() + VRC_CACHE_DURATION)
};
return user.data;
}
public async getCachedWorldById(id: string) {
if (typeof this.cache.Worlds[id] !== 'undefined') {
if (this.cache.Worlds[id].expires > new Date()) {
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Cache hit for world ${id}`);
2023-04-23 14:23:20 +07:00
return this.cache.Worlds[id].world;
}
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Cache hit for world ${id} but expired, refreshing`);
2023-04-23 14:23:20 +07:00
}
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Looking up world ${id} in VRChat API`);
2023-04-23 14:23:20 +07:00
let world;
try {
world = await this.client!.WorldsApi.getWorld(id);
} catch (err: any) {
if (err.response?.status === 404) return null;
throw err;
}
if (!world) {
return null;
}
2023-04-23 17:50:44 +07:00
Logger.debug(LOGGING_TAG, `Caching world ${id}`);
2023-04-23 14:23:20 +07:00
this.cache.Worlds[id] = {
world: world.data,
expires: new Date(Date.now() + VRC_CACHE_DURATION)
};
return world.data;
2023-04-23 13:13:20 +07:00
}
public end(): void {}
}
export default new VRChatAPI();