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 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;
|
|
|
|
|
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
|
|
|
|
|
}`;
|
|
|
|
|
Logger.debug(`[VRChat] Using user agent ${this.useragent}`);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Logger.info('Logging in to VRChat');
|
|
|
|
|
let res = await this.client.AuthenticationApi.getCurrentUser();
|
|
|
|
|
|
|
|
|
|
Logger.info('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()) {
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Removing user ' + key + ' from cache');
|
|
|
|
|
delete this.cache.Users[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (let key in this.cache.Worlds) {
|
|
|
|
|
if (this.cache.Worlds[key].expires < new Date()) {
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Removing world ' + key + ' from cache');
|
|
|
|
|
delete this.cache.Worlds[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, VRC_CACHE_DURATION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async getCachedUserById(id: string) {
|
|
|
|
|
if (typeof this.cache.Users[id] !== 'undefined') {
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Cache hit for user ' + id);
|
|
|
|
|
if (this.cache.Users[id].expires > new Date()) {
|
|
|
|
|
return this.cache.Users[id].user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Cache miss for ' + id);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Caching user ' + id);
|
|
|
|
|
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') {
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Cache hit for world ' + id);
|
|
|
|
|
if (this.cache.Worlds[id].expires > new Date()) {
|
|
|
|
|
return this.cache.Worlds[id].world;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Cache miss for ' + id);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.debug('[VRChat] [Cache] Caching world ' + id);
|
|
|
|
|
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();
|