Skip to content

Commit

Permalink
Implement client target (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeKzor committed Aug 18, 2024
1 parent b793109 commit 760fe84
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 22 deletions.
2 changes: 2 additions & 0 deletions docker/volumes/initdb/_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ CREATE TABLE videos (
processed INT DEFAULT 0,
views INT NOT NULL DEFAULT 0,
visibility INT NOT NULL DEFAULT 0,
target_token_id BIGINT,
deleted_by BIGINT,
deleted_at TIMESTAMP,
deleted_reason VARCHAR(256),
Expand All @@ -169,6 +170,7 @@ CREATE TABLE videos (
FOREIGN KEY (map_id) REFERENCES maps(map_id),
FOREIGN KEY (rendered_by) REFERENCES users(user_id),
FOREIGN KEY (rendered_by_token) REFERENCES access_tokens(access_token_id),
FOREIGN KEY (target_token_id) REFERENCES access_tokens(access_token_id),
FOREIGN KEY (deleted_by) REFERENCES users(user_id),
FULLTEXT (title)
);
Expand Down
37 changes: 34 additions & 3 deletions src/bot/commands/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ const render = async (
);

try {
// FIXME: I wish board.portal2.sr would just return the filename
// in a Content-Disposition header :>
// TODO: Fix this via response header
const useManualRedirect = (new URL(url)).hostname === 'board.portal2.sr';

const maybeRedirect = await fetch(url, {
Expand Down Expand Up @@ -219,6 +218,12 @@ const render = async (
}
}

const clientTarget = args.find((arg) => arg.name === 'client')?.value as string | undefined;
if (clientTarget) {
const client = clientTarget.slice(clientTarget.lastIndexOf('-') + 1);
body.append('client', client);
}

const presetName = args.find((arg) => arg.name === 'preset')?.value as string | undefined;
if (presetName) {
const preset = await Presets.find(interaction.user.id, presetName);
Expand Down Expand Up @@ -379,7 +384,14 @@ const renderOptions: ApplicationCommandOption[] = [
},
{
name: 'preset',
description: 'Choose a custom preset.',
description: 'Choose custom preset.',
type: ApplicationCommandOptionTypes.String,
required: false,
autocomplete: true,
},
{
name: 'client',
description: 'Target specific render client.',
type: ApplicationCommandOptionTypes.String,
required: false,
autocomplete: true,
Expand Down Expand Up @@ -456,6 +468,7 @@ createCommand({
const args = [...(subCommand.options?.values() ?? [])];
const quality = args.find((arg) => arg.name === 'quality');
const preset = args.find((arg) => arg.name === 'preset');
const client = args.find((arg) => arg.name === 'client');

if (quality?.focused) {
await bot.helpers.sendInteractionResponse(
Expand Down Expand Up @@ -491,6 +504,24 @@ createCommand({
},
},
);
} else if (client?.focused) {
await bot.helpers.sendInteractionResponse(
interaction.id,
interaction.token,
{
type: InteractionResponseTypes.ApplicationCommandAutocompleteResult,
data: {
choices: [
...Server.clients.map((client) => {
return {
name: `${client.name} by @${client.username}`,
value: `${client.name.toLocaleLowerCase()}-${client.username}-${client.clientId}`,
};
}),
],
},
},
);
}
break;
}
Expand Down
12 changes: 10 additions & 2 deletions src/bot/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'dotenv/load.ts';

import { log } from './utils/logger.ts';
import { escapeMaskedLink, getPublicUrl, updateCommands } from './utils/helpers.ts';
import { BotDataType, BotMessages } from './protocol.ts';
import { BotDataType, BotMessages, WorkerDataType } from './protocol.ts';
import { bot } from './bot.ts';
import { Queue } from './services/queue.ts';
import { Server } from './services/server.ts';
Expand Down Expand Up @@ -102,6 +102,11 @@ worker.addEventListener('message', async (message) => {
}
case BotDataType.Config: {
Server.config = data;
worker.postMessage({ type: WorkerDataType.Clients });
break;
}
case BotDataType.Clients: {
Server.clients = data;
break;
}
default: {
Expand All @@ -114,7 +119,10 @@ worker.addEventListener('message', async (message) => {
}
});

setInterval(() => Queue.deleteOutdated(), 60 * 1_000);
setInterval(() => {
Queue.deleteOutdated();
worker.postMessage({ type: WorkerDataType.Clients });
}, 60 * 1_000);

log.info('Started bot');

Expand Down
26 changes: 24 additions & 2 deletions src/bot/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
* This defines the protocol between the server and the bot.
*/

import { Video } from '~/shared/models.ts';
import { RenderQuality, Video } from '~/shared/models.ts';

export enum BotDataType {
Upload = 'upload',
Error = 'error',
Config = 'config',
Clients = 'clients',
}

export type VideoUpload = Pick<
Expand All @@ -36,6 +37,13 @@ export interface ServerConfig {
maxDemoFileSize: number;
}

export interface RenderClient {
clientId: number;
name: string;
username: string;
maxRenderQuality: RenderQuality;
}

export type BotMessage<T extends BotDataType, P> = {
type: T;
data: P;
Expand All @@ -44,4 +52,18 @@ export type BotMessage<T extends BotDataType, P> = {
export type BotMessageUpload = BotMessage<BotDataType.Upload, VideoUpload>;
export type BotMessageError = BotMessage<BotDataType.Error, ErrorStatus>;
export type BotMessageConfig = BotMessage<BotDataType.Config, ServerConfig>;
export type BotMessages = BotMessageUpload | BotMessageError | BotMessageConfig;
export type BotMessageClients = BotMessage<BotDataType.Clients, RenderClient[]>;
export type BotMessages = BotMessageUpload | BotMessageError | BotMessageConfig | BotMessageClients;

export enum WorkerDataType {
Clients = 'clients',
}

export type WorkerMessage<T extends WorkerDataType, P> = {
type: T;
data: P;
};

export type WorkerMessageClients = WorkerMessage<WorkerDataType.Clients, undefined>;

export type WorkerMessages = WorkerMessageClients;
3 changes: 2 additions & 1 deletion src/bot/services/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* SPDX-License-Identifier: MIT
*/

import { ServerConfig } from '../protocol.ts';
import { RenderClient, ServerConfig } from '../protocol.ts';

export const Server = {
config: {
maxDemoFileSize: 0,
} satisfies ServerConfig,
clients: [] as RenderClient[],
};
17 changes: 17 additions & 0 deletions src/bot/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@
/// <reference lib="deno.worker" />

import { delay } from 'async/delay.ts';
import { WorkerDataType, WorkerMessages } from './protocol.ts';

const AUTORENDER_CONNECT_URI = Deno.env.get('AUTORENDER_CONNECT_URI')!;
const AUTORENDER_PROTOCOL = Deno.env.get('AUTORENDER_PROTOCOL')!;

self.addEventListener('message', (message: MessageEvent<WorkerMessages>) => {
try {
const { type } = message.data;

switch (type) {
case WorkerDataType.Clients: {
ws && ws.readyState === WebSocket.OPEN && ws.send(JSON.stringify({
type: 'clients',
}));
}
}
} catch (err) {
self.postMessage(err.toString());
}
});

self.postMessage('Running worker thread...');

let ws: WebSocket | null = null;
Expand Down
Loading

0 comments on commit 760fe84

Please sign in to comment.