add redirect visit counting

merge-requests/6/merge
Matthew 2020-06-12 16:49:00 -04:00
parent fbf28dbdb2
commit 8da3363ea4
No known key found for this signature in database
GPG Key ID: F841AB9BF496C194
4 changed files with 10 additions and 3 deletions

View File

@ -10,11 +10,14 @@ export default class Root extends Route {
}
public bind() {
this.router.get('/', (_req, res) => res.redirect('https://www.libraryofcode.org/'));
this.router.get('/:key', async (req, res) => {
try {
const link: RedirectRaw = await this.server.client.db.Redirect.findOne({ key: req.params.key }).lean().exec();
if (!link) return res.status(404).json({ code: this.constants.codes.NOT_FOUND, message: this.constants.messages.NOT_FOUND });
return res.redirect(link.to);
res.redirect(link.to);
return await this.server.client.db.Redirect.updateOne({ key: req.params.key }, { $inc: { visitedCount: 1 } });
} catch (err) {
this.server.client.util.handleError(err);
return res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR });

View File

@ -27,6 +27,7 @@ export default class AddRedirect extends Command {
const redirect = new this.client.db.Redirect({
key: args[1].toLowerCase(),
to: args[0],
visitedCount: 0,
});
await redirect.save();
return this.success(message.channel, `Redirect https://loc.sh/${args[1].toLowerCase()} -> ${args[0]} is now active.`);

View File

@ -21,7 +21,7 @@ export default class DelRedirect extends Command {
const embed = new RichEmbed();
embed.setTitle('Redirect Information');
for (const redirect of redirects) {
embed.addField(redirect.key, redirect.to);
embed.addField(`${redirect.key} | visited ${redirect.visitedCount} times`, redirect.to);
}
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
@ -31,7 +31,7 @@ export default class DelRedirect extends Command {
if (!redirects) return this.error(message.channel, 'No redirect links found.');
const redirectArray: [{ name: string, value: string }?] = [];
for (const redirect of redirects) {
redirectArray.push({ name: redirect.key, value: redirect.to });
redirectArray.push({ name: `${redirect.key} | visited ${redirect.visitedCount} times`, value: redirect.to });
}
const splitRedirects = this.client.util.splitFields(redirectArray);
const cmdPages: RichEmbed[] = [];

View File

@ -3,16 +3,19 @@ import { Document, Schema, model } from 'mongoose';
export interface RedirectInterface extends Document {
key: string,
to: string,
visitedCount: number,
}
export interface RedirectRaw {
key: string,
to: string,
visitedCount: number,
}
const Redirect: Schema = new Schema({
key: String,
to: String,
visitedCount: Number,
});
export default model<RedirectInterface>('Redirect', Redirect);