cordis/server/scripts/setup-test-servers.ts
2021-11-01 23:29:24 -05:00

116 lines
8.7 KiB
TypeScript

import * as fs from 'fs/promises';
import * as path from 'path';
import * as pg from 'pg';
import { ExitCodes, ExitCode } from '../../exit-codes/exit-codes';
import Logger from '../../logger/logger';
const LOG = Logger.create(__filename);
// TODO: remove db in favor of DB.js
const db = new pg.Client({
host: 'localhost',
user: 'cordis',
password: 'cordis_pass',
database: 'cordis',
});
process.on('unhandledRejection', async (reason, promise) => {
LOG.error('unhandled promise rejection:', reason);
ExitCodes.exit(ExitCode.GENERAL_ERROR);
});
(async () => {
await db.connect();
try {
await db.query('BEGIN');
// Note: Order matters
await db.query('DELETE FROM "messages"');
await db.query('DELETE FROM "channels"');
await db.query('DELETE FROM "members"');
await db.query('DELETE FROM "servers_meta"');
await db.query('DELETE FROM "resources"');
await db.query('DELETE FROM "servers"');
let icon1Buff = await fs.readFile(path.join(__dirname, 'resources/icon-no-chicoms.png'));
let icon2Buff = await fs.readFile(path.join(__dirname, 'resources/icon-literally-heaven.png'));
let server1Result = await db.query('INSERT INTO "servers" ("id") VALUES (DEFAULT) RETURNING "id"');
let server1Id = server1Result.rows[0].id;
let icon1Result = await db.query('INSERT INTO "resources" ("server_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"', [ server1Id, icon1Buff ]);
let icon1ResourceId = icon1Result.rows[0].id;
await db.query('INSERT INTO "servers_meta" ("id", "name", "icon_resource_id") VALUES ($1, $2, $3)', [ server1Id, 'no chicoms', icon1ResourceId ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 1, 'general', 'Some testing flavor text' ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 2, 'vocabulary', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 3, 'gear', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 4, 'memes', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 5, 'apocalypse', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 6, 'wisdom', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 7, 'footage', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 8, 'burger-chicken', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 9, 'handles', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 10, 'handles-link', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 11, 'workout', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 12, 'montage', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 13, 'epic-gamers', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 14, 'scams', null ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server1Id, 15, 'bot-commands', null ]);
// Some dummy members
let avatar1Buff = await fs.readFile(path.join(__dirname, '/resources/avatar-1.png'));
let avatar1Result = await db.query('INSERT INTO "resources" ("server_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"', [ server1Id, avatar1Buff ]);
let avatar1Id = avatar1Result.rows[0].id;
await db.query('INSERT INTO "members" ("server_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)', [ server1Id, 'dummy public key 1', 'AndrewSfeir', avatar1Id ]);
let avatar2Buff = await fs.readFile(path.join(__dirname, '/resources/avatar-2.png'));
let avatar2Result = await db.query('INSERT INTO "resources" ("server_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"', [ server1Id, avatar2Buff ]);
let avatar2Id = avatar2Result.rows[0].id;
await db.query('INSERT INTO "members" ("server_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)', [ server1Id, 'dummy public key 2', 'Danbot', avatar2Id ]);
let avatar3Buff = await fs.readFile(path.join(__dirname, '/resources/avatar-3.png'));
let avatar3Result = await db.query('INSERT INTO "resources" ("server_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"', [ server1Id, avatar3Buff ]);
let avatar3Id = avatar3Result.rows[0].id;
await db.query('INSERT INTO "members" ("server_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)', [ server1Id, 'dummy public key 3', 'gamer321', avatar3Id ]);
// An admin role
await db.query('INSERT INTO roles (server_id, name, color) VALUES ($1, \'admin\', \'#ff0000\')', [ server1Id ]);
// A lot of dummy members (25) with the same avatar
// let avatarDefaultBuff = await fs.readFile('./avatar-default.png');
// let avatarDefaultResult = await db.query('INSERT INTO "resources" ("server_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"', [ server1Id, avatarDefaultBuff ]);
// let avatarDefaultId = avatarDefaultResult.rows[0].id;
// for (let i = 0; i < 25; ++i) {
// await db.query('INSERT INTO "members" ("server_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)', [ server1Id, 'dummy default public key ' + i, 'Dummy Member #' + i, avatarDefaultId ]);
// }
let server2Result = await db.query('INSERT INTO "servers" ("id") VALUES (DEFAULT) RETURNING "id"');
let server2Id = server2Result.rows[0].id;
let icon2Result = await db.query('INSERT INTO "resources" ("server_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"', [ server2Id, icon2Buff ]);
let icon2ResourceId = icon2Result.rows[0].id;
await db.query('INSERT INTO "servers_meta" ("id", "name", "icon_resource_id") VALUES ($1, $2, $3)', [ server2Id, 'Literally Heaven', icon2ResourceId ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server2Id, 1, 'general', 'The Ghost of Christmas Past.' ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server2Id, 2, 'art', 'Green is not a creative color.' ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server2Id, 3, 'quote-wall', 'Like a slowly updating history book.' ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server2Id, 4, 'tbd', 'This channel is to be determined.' ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server2Id, 5, 'robbies-corner', 'The signed original.' ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server2Id, 6, 'zanes-wing', 'Still Literally, Zane the Man.' ]);
await db.query('INSERT INTO "channels" ("server_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)', [ server2Id, 7, 'the-voiceless', 'For voiceless people who can\'t use or don\'t have a mic.' ]);
await db.query('COMMIT');
LOG.info(`server 1 initialized as s#${server1Id}`);
LOG.info(`server 2 initialized as s#${server2Id}`);
} catch (e) {
await db.query('ROLLBACK');
LOG.error('error setting up test database', e);
}
await db.end();
})()
;