notes backend

This commit is contained in:
dicedtomatoreal
2020-06-02 08:36:06 -07:00
parent 1fc264d08b
commit 1d4469a7e7
5 changed files with 36 additions and 3 deletions

View File

@@ -73,8 +73,13 @@ export class APIController {
if (!users[0]) return res.status(FORBIDDEN).json({ code: FORBIDDEN, message: "Unauthorized" })
if (req.headers['authorization'] !== users[0].token) return res.status(FORBIDDEN).json({ code: FORBIDDEN, message: "Unauthorized" })
const user = users[0];
const id = randomId(config.notes.idLength)
const note = await this.orm.repos.note.save(new Note().set({key:id,user:user.id,content:req.body.content,expiration:req.body.expiration?req.body.expiration:null}));
const id = randomId(config.note.idLength)
const note = await this.orm.repos.note.save(new Note().set({
key: id,
user: user.id,
content: req.body.content,
expiration: req.body.expiration ? req.body.expiration : null
}));
Logger.get('TypeX.Notes').info(`New note created ${note.id} and ${note.expriation ? `will expire in ${note.expriation},` : `will not expire,`} by ${user.username} (${user.id})`)
// if (config.discordWebhook.enabled) new DiscordWebhook(config.discordWebhook.url).sendShortenUpdate(user, shrt, ShortenUtil.parseURL(shrt.url), config);
return res.status(200).send(`${config.site.returnProtocol}://${req.headers['host']}${config.note.route}/${id}`)

View File

@@ -76,6 +76,13 @@ export class IndexController {
return res.redirect(shorten.origin)
}
@Get(`${config.note.route.slice(1)}/:id`)
private async getNote(req: Request, res: Response) {
const note = await this.orm.repos.note.findOne({ key: req.params.id });
if (!note) return res.render('404');
return res.send(note.content);
}
public set(orm: ORMHandler) {
this.orm = orm;
return this;

View File

@@ -17,12 +17,13 @@ export class Note {
@Column("bigint", { nullable: true, default: null })
expriation: number;
@Column("text", { default: 0 })
@Column("text")
content: string;
set(options: { user: number, key: string, content: string, expiration?: number }) {
this.user = options.user;
this.key = options.key;
this.content = options.content;
this.creation = Date.now();
this.expriation = options.expiration ? options.expiration : null;
return this;

View File

@@ -13,6 +13,7 @@ import { findFile } from "./util";
import { readFileSync } from 'fs';
import { Shorten } from "./entities/Shorten";
import { Note } from "./entities/Note";
import { notes } from "./interval";
if (!findFile('config.json', process.cwd())) {
Logger.get('FS').error(`No config.json exists in ${__dirname}, exiting...`)
@@ -59,4 +60,6 @@ Logger.get('TypeX').info(`Starting TypeX ${pk.version}`);
);
const server = new TypeXServer(orm);
server.start();
Logger.get('Interval').info('Starting Notes interval');
const notesInterval = notes(orm);
})();

17
src/interval.ts Normal file
View File

@@ -0,0 +1,17 @@
import { ORMHandler } from ".";
import Logger from "@ayanaware/logger";
export function notes(orm: ORMHandler) {
return setInterval(async () => {
const all = await orm.repos.note.find();
for (const note of all) {
if (note.expriation) {
const expiration = Number(note.creation) + Number(note.expriation);
if (Date.now() > expiration) {
orm.repos.note.delete({ id: note.id });
Logger.get('TypeX.Notes').info(`Note deleted ${note.id} and ${note.expriation ? `expired in ${note.expriation}` : `never expired`}`)
}
}
}
}, 5000)
}