mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 20:35:02 +00:00
Dashboard work and related
This commit is contained in:
parent
7bda2b1763
commit
b230a73a6f
44 changed files with 637 additions and 272 deletions
34
src/api/archives.ts
Normal file
34
src/api/archives.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import express, { Request, Response } from "express";
|
||||
import { GuildArchives } from "../data/GuildArchives";
|
||||
import { notFound } from "./responses";
|
||||
import moment from "moment-timezone";
|
||||
|
||||
export function initArchives(app: express.Express) {
|
||||
const archives = new GuildArchives(null);
|
||||
|
||||
// Legacy redirect
|
||||
app.get("/spam-logs/:id", (req: Request, res: Response) => {
|
||||
res.redirect("/archives/" + req.params.id);
|
||||
});
|
||||
|
||||
app.get("/archives/:id", async (req: Request, res: Response) => {
|
||||
const archive = await archives.find(req.params.id);
|
||||
if (!archive) return notFound(res);
|
||||
|
||||
let body = archive.body;
|
||||
|
||||
// Add some metadata at the end of the log file (but only if it doesn't already have it directly in the body)
|
||||
if (archive.body.indexOf("Log file generated on") === -1) {
|
||||
const createdAt = moment(archive.created_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
||||
body += `\n\nLog file generated on ${createdAt}`;
|
||||
|
||||
if (archive.expires_at !== null) {
|
||||
const expiresAt = moment(archive.expires_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
||||
body += `\nExpires at ${expiresAt}`;
|
||||
}
|
||||
}
|
||||
|
||||
res.setHeader("Content-Type", "text/plain; charset=UTF-8");
|
||||
res.end(body);
|
||||
});
|
||||
}
|
|
@ -2,9 +2,11 @@ import express, { Request, Response } from "express";
|
|||
import passport from "passport";
|
||||
import OAuth2Strategy from "passport-oauth2";
|
||||
import CustomStrategy from "passport-custom";
|
||||
import { DashboardLogins, DashboardLoginUserData } from "../data/DashboardLogins";
|
||||
import { ApiLogins } from "../data/ApiLogins";
|
||||
import pick from "lodash.pick";
|
||||
import https from "https";
|
||||
import { ApiUserInfo } from "../data/ApiUserInfo";
|
||||
import { ApiUserInfoData } from "../data/entities/ApiUserInfo";
|
||||
|
||||
const DISCORD_API_URL = "https://discordapp.com/api";
|
||||
|
||||
|
@ -53,7 +55,8 @@ export function initAuth(app: express.Express) {
|
|||
passport.serializeUser((user, done) => done(null, user));
|
||||
passport.deserializeUser((user, done) => done(null, user));
|
||||
|
||||
const dashboardLogins = new DashboardLogins();
|
||||
const apiLogins = new ApiLogins();
|
||||
const apiUserInfo = new ApiUserInfo();
|
||||
|
||||
// Initialize API tokens
|
||||
passport.use(
|
||||
|
@ -62,7 +65,7 @@ export function initAuth(app: express.Express) {
|
|||
const apiKey = req.header("X-Api-Key");
|
||||
if (!apiKey) return cb();
|
||||
|
||||
const userId = await dashboardLogins.getUserIdByApiKey(apiKey);
|
||||
const userId = await apiLogins.getUserIdByApiKey(apiKey);
|
||||
if (userId) {
|
||||
return cb(null, { userId });
|
||||
}
|
||||
|
@ -72,6 +75,7 @@ export function initAuth(app: express.Express) {
|
|||
);
|
||||
|
||||
// Initialize OAuth2 for Discord login
|
||||
// When the user logs in through OAuth2, we create them a "login" (= api token) and update their user info in the DB
|
||||
passport.use(
|
||||
new OAuth2Strategy(
|
||||
{
|
||||
|
@ -84,10 +88,10 @@ export function initAuth(app: express.Express) {
|
|||
},
|
||||
async (accessToken, refreshToken, profile, cb) => {
|
||||
const user = await simpleDiscordAPIRequest(accessToken, "users/@me");
|
||||
const userData = pick(user, ["username", "discriminator", "avatar"]) as DashboardLoginUserData;
|
||||
const apiKey = await dashboardLogins.addLogin(user.id, userData);
|
||||
const apiKey = await apiLogins.addLogin(user.id);
|
||||
const userData = pick(user, ["username", "discriminator", "avatar"]) as ApiUserInfoData;
|
||||
await apiUserInfo.update(user.id, userData);
|
||||
// TODO: Revoke access token, we don't need it anymore
|
||||
console.log("done, calling cb with", apiKey);
|
||||
cb(null, { apiKey });
|
||||
},
|
||||
),
|
||||
|
@ -108,7 +112,7 @@ export function initAuth(app: express.Express) {
|
|||
return res.status(400).json({ error: "No key supplied" });
|
||||
}
|
||||
|
||||
const userId = await dashboardLogins.getUserIdByApiKey(key);
|
||||
const userId = await apiLogins.getUserIdByApiKey(key);
|
||||
if (!userId) {
|
||||
return res.json({ valid: false });
|
||||
}
|
||||
|
|
|
@ -2,35 +2,35 @@ import express from "express";
|
|||
import passport from "passport";
|
||||
import { AllowedGuilds } from "../data/AllowedGuilds";
|
||||
import { requireAPIToken } from "./auth";
|
||||
import { DashboardUsers } from "../data/DashboardUsers";
|
||||
import { ApiPermissions } from "../data/ApiPermissions";
|
||||
import { clientError, ok, unauthorized } from "./responses";
|
||||
import { Configs } from "../data/Configs";
|
||||
import { DashboardRoles } from "../data/DashboardRoles";
|
||||
import { ApiRoles } from "../data/ApiRoles";
|
||||
|
||||
export function initGuildsAPI(app: express.Express) {
|
||||
const guildAPIRouter = express.Router();
|
||||
requireAPIToken(guildAPIRouter);
|
||||
|
||||
const allowedGuilds = new AllowedGuilds();
|
||||
const dashboardUsers = new DashboardUsers();
|
||||
const apiPermissions = new ApiPermissions();
|
||||
const configs = new Configs();
|
||||
|
||||
guildAPIRouter.get("/guilds/available", async (req, res) => {
|
||||
const guilds = await allowedGuilds.getForDashboardUser(req.user.userId);
|
||||
const guilds = await allowedGuilds.getForApiUser(req.user.userId);
|
||||
res.json(guilds);
|
||||
});
|
||||
|
||||
guildAPIRouter.get("/guilds/:guildId/config", async (req, res) => {
|
||||
const dbUser = await dashboardUsers.getByGuildAndUserId(req.params.guildId, req.user.userId);
|
||||
if (!dbUser) return unauthorized(res);
|
||||
const permissions = await apiPermissions.getByGuildAndUserId(req.params.guildId, req.user.userId);
|
||||
if (!permissions) return unauthorized(res);
|
||||
|
||||
const config = await configs.getActiveByKey(`guild-${req.params.guildId}`);
|
||||
res.json({ config: config ? config.config : "" });
|
||||
});
|
||||
|
||||
guildAPIRouter.post("/guilds/:guildId/config", async (req, res) => {
|
||||
const dbUser = await dashboardUsers.getByGuildAndUserId(req.params.guildId, req.user.userId);
|
||||
if (!dbUser || DashboardRoles[dbUser.role] < DashboardRoles.Editor) return unauthorized(res);
|
||||
const permissions = await apiPermissions.getByGuildAndUserId(req.params.guildId, req.user.userId);
|
||||
if (!permissions || ApiRoles[permissions.role] < ApiRoles.Editor) return unauthorized(res);
|
||||
|
||||
const config = req.body.config;
|
||||
if (config == null) return clientError(res, "No config supplied");
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
import { error, notFound } from "./responses";
|
||||
|
||||
require("dotenv").config();
|
||||
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
import { initAuth } from "./auth";
|
||||
import { initGuildsAPI } from "./guilds";
|
||||
import { initArchives } from "./archives";
|
||||
import { connect } from "../data/db";
|
||||
|
||||
console.log("Connecting to database...");
|
||||
|
@ -19,16 +22,23 @@ connect().then(() => {
|
|||
|
||||
initAuth(app);
|
||||
initGuildsAPI(app);
|
||||
initArchives(app);
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
res.status(err.status || 500);
|
||||
res.json({ error: err.message });
|
||||
});
|
||||
|
||||
// Default route
|
||||
app.get("/", (req, res) => {
|
||||
res.end({ status: "cookies" });
|
||||
});
|
||||
|
||||
// Error response
|
||||
app.use((err, req, res, next) => {
|
||||
error(res, err.message, err.status || 500);
|
||||
});
|
||||
|
||||
// 404 response
|
||||
app.use((req, res, next) => {
|
||||
return notFound(res);
|
||||
});
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => console.log(`API server listening on port ${port}`));
|
||||
});
|
||||
|
|
|
@ -4,8 +4,20 @@ export function unauthorized(res: Response) {
|
|||
res.status(403).json({ error: "Unauthorized" });
|
||||
}
|
||||
|
||||
export function error(res: Response, message: string, statusCode: number = 500) {
|
||||
res.status(statusCode).json({ error: message });
|
||||
}
|
||||
|
||||
export function serverError(res: Response, message: string) {
|
||||
error(res, message, 500);
|
||||
}
|
||||
|
||||
export function clientError(res: Response, message: string) {
|
||||
res.status(400).json({ error: message });
|
||||
error(res, message, 400);
|
||||
}
|
||||
|
||||
export function notFound(res: Response) {
|
||||
res.status(404).json({ error: "Not found" });
|
||||
}
|
||||
|
||||
export function ok(res: Response) {
|
||||
|
|
|
@ -21,21 +21,25 @@ export class AllowedGuilds extends BaseRepository {
|
|||
async isAllowed(guildId) {
|
||||
const count = await this.allowedGuilds.count({
|
||||
where: {
|
||||
guild_id: guildId,
|
||||
id: guildId,
|
||||
},
|
||||
});
|
||||
return count !== 0;
|
||||
}
|
||||
|
||||
getForDashboardUser(userId) {
|
||||
getForApiUser(userId) {
|
||||
return this.allowedGuilds
|
||||
.createQueryBuilder("allowed_guilds")
|
||||
.innerJoin(
|
||||
"dashboard_users",
|
||||
"dashboard_users",
|
||||
"dashboard_users.guild_id = allowed_guilds.guild_id AND dashboard_users.user_id = :userId",
|
||||
"api_permissions",
|
||||
"api_permissions",
|
||||
"api_permissions.guild_id = allowed_guilds.id AND api_permissions.user_id = :userId",
|
||||
{ userId },
|
||||
)
|
||||
.getMany();
|
||||
}
|
||||
|
||||
updateInfo(id, name, icon) {
|
||||
return this.allowedGuilds.update({ id }, { name, icon });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { getRepository, Repository } from "typeorm";
|
||||
import { DashboardLogin } from "./entities/DashboardLogin";
|
||||
import { ApiLogin } from "./entities/ApiLogin";
|
||||
import { BaseRepository } from "./BaseRepository";
|
||||
import crypto from "crypto";
|
||||
import moment from "moment-timezone";
|
||||
|
@ -9,18 +9,12 @@ import uuidv4 from "uuid/v4";
|
|||
import { DBDateFormat } from "../utils";
|
||||
import { log } from "util";
|
||||
|
||||
export interface DashboardLoginUserData {
|
||||
username: string;
|
||||
discriminator: string;
|
||||
avatar: string;
|
||||
}
|
||||
|
||||
export class DashboardLogins extends BaseRepository {
|
||||
private dashboardLogins: Repository<DashboardLogin>;
|
||||
export class ApiLogins extends BaseRepository {
|
||||
private apiLogins: Repository<ApiLogin>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.dashboardLogins = getRepository(DashboardLogin);
|
||||
this.apiLogins = getRepository(ApiLogin);
|
||||
}
|
||||
|
||||
async getUserIdByApiKey(apiKey: string): Promise<string | null> {
|
||||
|
@ -29,7 +23,7 @@ export class DashboardLogins extends BaseRepository {
|
|||
return null;
|
||||
}
|
||||
|
||||
const login = await this.dashboardLogins
|
||||
const login = await this.apiLogins
|
||||
.createQueryBuilder()
|
||||
.where("id = :id", { id: loginId })
|
||||
.andWhere("expires_at > NOW()")
|
||||
|
@ -49,12 +43,12 @@ export class DashboardLogins extends BaseRepository {
|
|||
return login.user_id;
|
||||
}
|
||||
|
||||
async addLogin(userId: string, userData: DashboardLoginUserData): Promise<string> {
|
||||
async addLogin(userId: string): Promise<string> {
|
||||
// Generate random login id
|
||||
let loginId;
|
||||
while (true) {
|
||||
loginId = uuidv4();
|
||||
const existing = await this.dashboardLogins.findOne({
|
||||
const existing = await this.apiLogins.findOne({
|
||||
where: {
|
||||
id: loginId,
|
||||
},
|
||||
|
@ -69,11 +63,10 @@ export class DashboardLogins extends BaseRepository {
|
|||
const hashedToken = hash.digest("hex");
|
||||
|
||||
// Save this to the DB
|
||||
await this.dashboardLogins.insert({
|
||||
await this.apiLogins.insert({
|
||||
id: loginId,
|
||||
token: hashedToken,
|
||||
user_id: userId,
|
||||
user_data: userData,
|
||||
logged_in_at: moment().format(DBDateFormat),
|
||||
expires_at: moment()
|
||||
.add(1, "day")
|
|
@ -1,17 +1,17 @@
|
|||
import { getRepository, Repository } from "typeorm";
|
||||
import { DashboardUser } from "./entities/DashboardUser";
|
||||
import { ApiPermission } from "./entities/ApiPermission";
|
||||
import { BaseRepository } from "./BaseRepository";
|
||||
|
||||
export class DashboardUsers extends BaseRepository {
|
||||
private dashboardUsers: Repository<DashboardUser>;
|
||||
export class ApiPermissions extends BaseRepository {
|
||||
private apiPermissions: Repository<ApiPermission>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.dashboardUsers = getRepository(DashboardUser);
|
||||
this.apiPermissions = getRepository(ApiPermission);
|
||||
}
|
||||
|
||||
getByGuildAndUserId(guildId, userId) {
|
||||
return this.dashboardUsers.findOne({
|
||||
return this.apiPermissions.findOne({
|
||||
where: {
|
||||
guild_id: guildId,
|
||||
user_id: userId,
|
|
@ -1,4 +1,4 @@
|
|||
export enum DashboardRoles {
|
||||
export enum ApiRoles {
|
||||
Viewer = 1,
|
||||
Editor,
|
||||
Manager,
|
38
src/data/ApiUserInfo.ts
Normal file
38
src/data/ApiUserInfo.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { getRepository, Repository } from "typeorm";
|
||||
import { ApiUserInfo as ApiUserInfoEntity, ApiUserInfoData } from "./entities/ApiUserInfo";
|
||||
import { BaseRepository } from "./BaseRepository";
|
||||
import { connection } from "./db";
|
||||
import moment from "moment-timezone";
|
||||
import { DBDateFormat } from "../utils";
|
||||
|
||||
export class ApiUserInfo extends BaseRepository {
|
||||
private apiUserInfo: Repository<ApiUserInfoEntity>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.apiUserInfo = getRepository(ApiUserInfoEntity);
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.apiUserInfo.findOne({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
update(id, data: ApiUserInfoData) {
|
||||
return connection.transaction(async entityManager => {
|
||||
const repo = entityManager.getRepository(ApiUserInfoEntity);
|
||||
|
||||
const existingInfo = await repo.findOne({ where: { id } });
|
||||
const updatedAt = moment().format(DBDateFormat);
|
||||
|
||||
if (existingInfo) {
|
||||
await repo.update({ id }, { data, updated_at: updatedAt });
|
||||
} else {
|
||||
await repo.insert({ id, data, updated_at: updatedAt });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -32,6 +32,18 @@ export class Configs extends BaseRepository {
|
|||
return (await this.getActiveByKey(key)) != null;
|
||||
}
|
||||
|
||||
getRevisions(key, num = 10) {
|
||||
return this.configs.find({
|
||||
relations: this.getRelations(),
|
||||
where: { key },
|
||||
select: ["id", "key", "is_active", "edited_by", "edited_at"],
|
||||
order: {
|
||||
edited_at: "DESC",
|
||||
},
|
||||
take: num,
|
||||
});
|
||||
}
|
||||
|
||||
async saveNewRevision(key, config, editedBy) {
|
||||
return connection.transaction(async entityManager => {
|
||||
const repo = entityManager.getRepository(Config);
|
||||
|
|
|
@ -4,7 +4,7 @@ import { Entity, Column, PrimaryColumn, CreateDateColumn } from "typeorm";
|
|||
export class AllowedGuild {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
|
25
src/data/entities/ApiLogin.ts
Normal file
25
src/data/entities/ApiLogin.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { Entity, Column, PrimaryColumn, OneToOne, ManyToOne, JoinColumn } from "typeorm";
|
||||
import { ApiUserInfo } from "./ApiUserInfo";
|
||||
|
||||
@Entity("api_logins")
|
||||
export class ApiLogin {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
token: string;
|
||||
|
||||
@Column()
|
||||
user_id: string;
|
||||
|
||||
@Column()
|
||||
logged_in_at: string;
|
||||
|
||||
@Column()
|
||||
expires_at: string;
|
||||
|
||||
@ManyToOne(type => ApiUserInfo, userInfo => userInfo.logins)
|
||||
@JoinColumn({ name: "user_id" })
|
||||
userInfo: ApiUserInfo;
|
||||
}
|
20
src/data/entities/ApiPermission.ts
Normal file
20
src/data/entities/ApiPermission.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { Entity, Column, PrimaryColumn, ManyToOne, JoinColumn } from "typeorm";
|
||||
import { ApiUserInfo } from "./ApiUserInfo";
|
||||
|
||||
@Entity("api_permissions")
|
||||
export class ApiPermission {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
user_id: string;
|
||||
|
||||
@Column()
|
||||
role: string;
|
||||
|
||||
@ManyToOne(type => ApiUserInfo, userInfo => userInfo.permissions)
|
||||
@JoinColumn({ name: "user_id" })
|
||||
userInfo: ApiUserInfo;
|
||||
}
|
28
src/data/entities/ApiUserInfo.ts
Normal file
28
src/data/entities/ApiUserInfo.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { Entity, Column, PrimaryColumn, OneToMany } from "typeorm";
|
||||
import { ApiLogin } from "./ApiLogin";
|
||||
import { ApiPermission } from "./ApiPermission";
|
||||
|
||||
export interface ApiUserInfoData {
|
||||
username: string;
|
||||
discriminator: string;
|
||||
avatar: string;
|
||||
}
|
||||
|
||||
@Entity("api_user_info")
|
||||
export class ApiUserInfo {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
||||
@Column("simple-json")
|
||||
data: ApiUserInfoData;
|
||||
|
||||
@Column()
|
||||
updated_at: string;
|
||||
|
||||
@OneToMany(type => ApiLogin, login => login.userInfo)
|
||||
logins: ApiLogin[];
|
||||
|
||||
@OneToMany(type => ApiPermission, perm => perm.userInfo)
|
||||
permissions: ApiPermission[];
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import { Entity, Column, PrimaryColumn, CreateDateColumn } from "typeorm";
|
||||
import { Entity, Column, PrimaryColumn, CreateDateColumn, ManyToOne, JoinColumn } from "typeorm";
|
||||
import { ApiUserInfo } from "./ApiUserInfo";
|
||||
|
||||
@Entity("configs")
|
||||
export class Config {
|
||||
|
@ -20,4 +21,8 @@ export class Config {
|
|||
|
||||
@Column()
|
||||
edited_at: string;
|
||||
|
||||
@ManyToOne(type => ApiUserInfo)
|
||||
@JoinColumn({ name: "edited_by" })
|
||||
userInfo: ApiUserInfo;
|
||||
}
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
import { DashboardLoginUserData } from "../DashboardLogins";
|
||||
|
||||
@Entity("dashboard_logins")
|
||||
export class DashboardLogin {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
token: string;
|
||||
|
||||
@Column()
|
||||
user_id: string;
|
||||
|
||||
@Column("simple-json")
|
||||
user_data: DashboardLoginUserData;
|
||||
|
||||
@Column()
|
||||
logged_in_at: string;
|
||||
|
||||
@Column()
|
||||
expires_at: string;
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity("dashboard_users")
|
||||
export class DashboardUser {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
user_id: string;
|
||||
|
||||
@Column()
|
||||
username: string;
|
||||
|
||||
@Column()
|
||||
role: string;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class RenameBackendDashboardStuffToAPI1561282151982 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query(`ALTER TABLE dashboard_users RENAME api_users`);
|
||||
await queryRunner.query(`ALTER TABLE dashboard_logins RENAME api_logins`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query(`ALTER TABLE api_users RENAME dashboard_users`);
|
||||
await queryRunner.query(`ALTER TABLE api_logins RENAME dashboard_logins`);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class RenameAllowedGuildGuildIdToId1561282552734 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query("ALTER TABLE `allowed_guilds` CHANGE COLUMN `guild_id` `id` BIGINT(20) NOT NULL FIRST;");
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query("ALTER TABLE `allowed_guilds` CHANGE COLUMN `id` `guild_id` BIGINT(20) NOT NULL FIRST;");
|
||||
}
|
||||
}
|
31
src/migrations/1561282950483-CreateApiUserInfoTable.ts
Normal file
31
src/migrations/1561282950483-CreateApiUserInfoTable.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
import { MigrationInterface, QueryRunner, Table } from "typeorm";
|
||||
|
||||
export class CreateApiUserInfoTable1561282950483 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: "api_user_info",
|
||||
columns: [
|
||||
{
|
||||
name: "id",
|
||||
type: "bigint",
|
||||
isPrimary: true,
|
||||
},
|
||||
{
|
||||
name: "data",
|
||||
type: "text",
|
||||
},
|
||||
{
|
||||
name: "updated_at",
|
||||
type: "datetime",
|
||||
default: "now()",
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.dropTable("api_user_info", true);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class RenameApiUsersToApiPermissions1561283165823 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query(`ALTER TABLE api_users RENAME api_permissions`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query(`ALTER TABLE api_permissions RENAME api_users`);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class DropUserDataFromLoginsAndPermissions1561283405201 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query("ALTER TABLE `api_logins` DROP COLUMN `user_data`");
|
||||
await queryRunner.query("ALTER TABLE `api_permissions` DROP COLUMN `username`");
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query(
|
||||
"ALTER TABLE `api_logins` ADD COLUMN `user_data` TEXT NOT NULL COLLATE 'utf8mb4_swedish_ci' AFTER `user_id`",
|
||||
);
|
||||
await queryRunner.query(
|
||||
"ALTER TABLE `api_permissions` ADD COLUMN `username` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_swedish_ci' AFTER `user_id`",
|
||||
);
|
||||
}
|
||||
}
|
24
src/plugins/GuildInfoSaver.ts
Normal file
24
src/plugins/GuildInfoSaver.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
||||
import { AllowedGuilds } from "../data/AllowedGuilds";
|
||||
import { MINUTES } from "../utils";
|
||||
|
||||
export class GuildInfoSaverPlugin extends ZeppelinPlugin {
|
||||
public static pluginName = "guild_info_saver";
|
||||
protected allowedGuilds: AllowedGuilds;
|
||||
private updateInterval;
|
||||
|
||||
onLoad() {
|
||||
this.allowedGuilds = new AllowedGuilds();
|
||||
|
||||
this.updateGuildInfo();
|
||||
this.updateInterval = setInterval(() => this.updateGuildInfo(), 60 * MINUTES);
|
||||
}
|
||||
|
||||
onUnload() {
|
||||
clearInterval(this.updateInterval);
|
||||
}
|
||||
|
||||
protected updateGuildInfo() {
|
||||
this.allowedGuilds.updateInfo(this.guildId, this.guild.name, this.guild.iconURL);
|
||||
}
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
import http, { ServerResponse } from "http";
|
||||
import { GlobalPlugin, IPluginOptions, logger } from "knub";
|
||||
import { GuildArchives } from "../data/GuildArchives";
|
||||
import { sleep } from "../utils";
|
||||
import moment from "moment-timezone";
|
||||
|
||||
const DEFAULT_PORT = 9920;
|
||||
const archivesRegex = /^\/(spam-logs|archives)\/([a-z0-9\-]+)\/?$/i;
|
||||
|
||||
function notFound(res: ServerResponse) {
|
||||
res.statusCode = 404;
|
||||
res.end("Not Found");
|
||||
}
|
||||
|
||||
interface ILogServerPluginConfig {
|
||||
port: number;
|
||||
}
|
||||
|
||||
export class LogServerPlugin extends GlobalPlugin<ILogServerPluginConfig> {
|
||||
public static pluginName = "log_server";
|
||||
|
||||
protected archives: GuildArchives;
|
||||
protected server: http.Server;
|
||||
|
||||
protected getDefaultOptions(): IPluginOptions<ILogServerPluginConfig> {
|
||||
return {
|
||||
config: {
|
||||
port: DEFAULT_PORT,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async onLoad() {
|
||||
this.archives = new GuildArchives(null);
|
||||
|
||||
this.server = http.createServer(async (req, res) => {
|
||||
const pathMatch = req.url.match(archivesRegex);
|
||||
if (!pathMatch) return notFound(res);
|
||||
|
||||
const logId = pathMatch[2];
|
||||
|
||||
if (pathMatch[1] === "spam-logs") {
|
||||
res.statusCode = 301;
|
||||
res.setHeader("Location", `/archives/${logId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathMatch) {
|
||||
const log = await this.archives.find(logId);
|
||||
if (!log) return notFound(res);
|
||||
|
||||
let body = log.body;
|
||||
|
||||
// Add some metadata at the end of the log file (but only if it doesn't already have it directly in the body)
|
||||
if (log.body.indexOf("Log file generated on") === -1) {
|
||||
const createdAt = moment(log.created_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
||||
body += `\n\nLog file generated on ${createdAt}`;
|
||||
|
||||
if (log.expires_at !== null) {
|
||||
const expiresAt = moment(log.expires_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
||||
body += `\nExpires at ${expiresAt}`;
|
||||
}
|
||||
}
|
||||
|
||||
res.setHeader("Content-Type", "text/plain; charset=UTF-8");
|
||||
res.end(body);
|
||||
}
|
||||
});
|
||||
|
||||
const port = this.getConfig().port;
|
||||
let retried = false;
|
||||
|
||||
this.server.on("error", async (err: any) => {
|
||||
if (err.code === "EADDRINUSE" && !retried) {
|
||||
logger.info("Got EADDRINUSE, retrying in 2 sec...");
|
||||
retried = true;
|
||||
await sleep(2000);
|
||||
this.server.listen(port);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
this.server.listen(port);
|
||||
}
|
||||
|
||||
async onUnload() {
|
||||
return new Promise(resolve => {
|
||||
this.server.close(() => resolve());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -19,9 +19,9 @@ import { SelfGrantableRolesPlugin } from "./SelfGrantableRolesPlugin";
|
|||
import { RemindersPlugin } from "./Reminders";
|
||||
import { WelcomeMessagePlugin } from "./WelcomeMessage";
|
||||
import { BotControlPlugin } from "./BotControl";
|
||||
import { LogServerPlugin } from "./LogServer";
|
||||
import { UsernameSaver } from "./UsernameSaver";
|
||||
import { CustomEventsPlugin } from "./CustomEvents";
|
||||
import { GuildInfoSaverPlugin } from "./GuildInfoSaver";
|
||||
|
||||
/**
|
||||
* Plugins available to be loaded for individual guilds
|
||||
|
@ -48,12 +48,14 @@ export const availablePlugins = [
|
|||
RemindersPlugin,
|
||||
WelcomeMessagePlugin,
|
||||
CustomEventsPlugin,
|
||||
GuildInfoSaverPlugin,
|
||||
];
|
||||
|
||||
/**
|
||||
* Plugins that are always loaded (subset of the names of the plugins in availablePlugins)
|
||||
*/
|
||||
export const basePlugins = [
|
||||
GuildInfoSaverPlugin.pluginName,
|
||||
MessageSaverPlugin.pluginName,
|
||||
NameHistoryPlugin.pluginName,
|
||||
CasesPlugin.pluginName,
|
||||
|
@ -63,4 +65,4 @@ export const basePlugins = [
|
|||
/**
|
||||
* Available global plugins (can't be loaded per-guild, only globally)
|
||||
*/
|
||||
export const availableGlobalPlugins = [BotControlPlugin, LogServerPlugin, UsernameSaver];
|
||||
export const availableGlobalPlugins = [BotControlPlugin, UsernameSaver];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue