{"size": 1000, "ext": "js", "max_stars_repo_path": "routes/user.js", "max_stars_repo_name": "Devoxin/AutomaCord", "max_stars_repo_stars_event_min_datetime": "2019-05-04T08:54:22.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-16T22:08:51.000Z", "max_issues_repo_path": "routes/user.js", "max_issues_repo_name": "Jonfirexbox/AutomaCord", "max_issues_repo_issues_event_min_datetime": "2019-05-27T22:21:01.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-14T21:08:08.000Z", "max_forks_repo_path": "routes/user.js", "max_forks_repo_name": "Jonfirexbox/AutomaCord", "max_forks_repo_forks_event_min_datetime": "2019-05-27T18:59:28.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-28T07:45:32.000Z"}
{"max_stars_count": 10, "max_issues_count": 3, "max_forks_count": 4, "avg_line_length": 31.25, "max_line_length": 147, "alphanum_fraction": 0.646}
const config = require('../config');
const db = require('../utils/db');
const express = require('express');

class Route {
  static async ensureUserExists (bot, req, res, next) {
    const member = bot.listGuild.members.get(req.params.id);

    if (!member) {
      return res.render('error', { error: 'No users found with that ID. Note that you can only view profiles of users in the AutomaCord server' });
    }

    const profile = await db.table('users').get(req.params.id).default({});
    const bots = await db.getBotsOwnedBy(req.params.id);
    const isWebAdmin = member && member.roles.some(id => id === config.management.websiteAdminRole);

    req.userProfile = { ...member, ...profile, isWebAdmin, bots };
    next();
  }

  static configure (server, bot) {
    const router = express.Router();
    server.use('/user', router);

    router.get('/:id', this.ensureUserExists.bind(this, bot), (req, res) => {
      res.render('user', req.userProfile);
    });
  }
}

module.exports = Route;
