Browse Source

misc: Use chalk + add role write right to users and admins

style/lint-staged
Dale 2 months ago
parent
commit
aafea37211
Signed by: Deiru GPG Key ID: AA250C0277B927E1
  1. 22
      src/server/db/generate-roles.ts
  2. 31
      src/server/db/setup-db.ts
  3. 4
      src/server/server.ts

22
src/server/db/generate-roles.ts

@ -3,6 +3,7 @@ import * as rdb from "rethinkdb";
import config from "miracle-tv-server/config";
import { AccessUnit, Role } from "miracle-tv-shared/graphql";
import { green, red, yellow } from "chalk";
const defaultAdminRole: Role = {
id: "admin",
@ -13,6 +14,7 @@ const defaultAdminRole: Role = {
channels: AccessUnit.Write,
streamKeys: AccessUnit.Write,
users: AccessUnit.Write,
roles: AccessUnit.Write,
},
actions: {
user: {
@ -42,9 +44,9 @@ const defaultModeratorRole: Role = {
},
};
const defaultVolounteerRole: Role = {
id: "volounteer",
name: "Volounteer",
const defaultVolunteerRole: Role = {
id: "volunteer",
name: "Volunteer",
parentId: "user",
access: {
rights: {},
@ -65,6 +67,7 @@ const defaultUserRole: Role = {
channels: AccessUnit.Self,
users: AccessUnit.Self,
activities: AccessUnit.Read,
roles: AccessUnit.Read,
},
actions: {
user: {
@ -78,7 +81,7 @@ const defaultUserRole: Role = {
const defaultRoles = [
defaultUserRole,
defaultVolounteerRole,
defaultVolunteerRole,
defaultModeratorRole,
defaultAdminRole,
];
@ -91,17 +94,24 @@ export const generateRoles = async () => {
const table = rdb.db(config.database?.db || "miracle-tv").table("roles");
const roles = await table.filter({}).coerceTo("array").run(conn);
const roleIds = roles.map((role) => role.id);
console.info("");
console.info(green`[Checking role setup]`);
return await Promise.all(
defaultRoles.map(async (dr) => {
console.info(yellow`- Checking role [${dr.id}] ${dr.name}`);
if (!roleIds.includes(dr.id)) {
const res = await table.insert(dr).run(conn);
if (res.errors > 0) {
console.info(`Error with ${dr.id}`, res.first_error);
console.info(
red`-- Error with [${dr.id}] ${dr.name}`,
res.first_error
);
} else {
console.info(`${dr.id} Ok!`);
console.info(green`-- Created role [${dr.id}] ${dr.name}`);
}
return res;
}
console.info(green`-- Role [${dr.id}] ${dr.name} Ok!`);
})
);
};

31
src/server/db/setup-db.ts

@ -1,6 +1,7 @@
import * as rdb from "rethinkdb";
import config from "miracle-tv-server/config";
import { generateRoles } from "./generate-roles";
import { green, yellow } from "chalk";
const tables: string[] = [
"users",
@ -19,44 +20,36 @@ export const connection = rdb.connect({
export const setupDB = async () => {
const con = await connection;
console.info("\x1b[33m", "[Checking Database Setup]", "\x1b[0m");
console.info("");
console.info(green`[Checking Database Setup]`);
console.info(
"\x1b[33m",
`- Checking database \`${config.database?.db || "miracle-tv"}\`...`,
"\x1b[0m"
yellow`- Checking database \`${config.database?.db || "miracle-tv"}\`...`
);
const dbList = await rdb.dbList().run(con);
if (!dbList.includes(config.database?.db || "miracle-tv")) {
console.info(
"\x1b[33m",
`-- Creating database \`${config.database?.db || "miracle-tv"}\`...`,
"\x1b[0m"
yellow`-- Creating database \`${config.database?.db || "miracle-tv"}\`...`
);
await rdb.dbCreate(config.database?.db || "miracle-tv").run(con);
}
console.info(
"\x1b[32m",
`-- ✔ Database \`${config.database?.db || "miracle-tv"}\` ok!`,
"\x1b[0m"
green`-- ✔ Database \`${config.database?.db || "miracle-tv"}\` ok!`
);
const tableList = await rdb.db(config.database.db).tableList().run(con);
await Promise.all(
tables.map((table) => {
console.info("\x1b[33m", `- Checking \`${table}\` Table`, "\x1b[0m");
console.info(yellow`- Checking \`${table}\` Table`);
if (!tableList.includes(table)) {
console.info(
"\x1b[33m",
`-- Creating \`${table}\` table...`,
"\x1b[0m"
);
console.info(yellow`-- Creating \`${table}\` table...`);
return rdb.db(config.database.db).tableCreate(table).run(con);
} else {
console.info("\x1b[32m", `-- Table \`${table}\` ok!`, "\x1b[0m");
console.info(green`-- Table \`${table}\` ok!`);
}
})
);
console.info("\x1b[32m", "- ✔ Table Check Complete!", "\x1b[0m");
console.info("\x1b[32m", "✔ Database Check Complete!", "\x1b[0m");
console.info(green`- ✔ Table Check Complete!`);
console.info(green`✔ Database Check Complete!`);
const res = await generateRoles();
console.info("");
return res;
};

4
src/server/server.ts

@ -7,10 +7,6 @@ import { green } from "chalk";
import webhooks from "miracle-tv-server/webhooks";
const { pathPrefix, dataDir } = config;
// app.get("/", (_, res) => res.send("FUCK!"));
// app.use(graphqlUploadExpress());
// graphqlEndpoint.applyMiddleware({ app });
const main = async () => {
await setupDB();

Loading…
Cancel
Save