mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-03-02 02:00:58 +01:00
overwrite user model toJSON, make is_vip vitrual field
This commit is contained in:
@@ -23,8 +23,9 @@ exports.renderIndex = function (req, res) {
|
||||
passkey: req.user.passkey,
|
||||
vip_start_at: req.user.vip_start_at,
|
||||
vip_end_at: req.user.vip_end_at,
|
||||
is_vip: isVip(req.user),
|
||||
is_vip: req.user.is_vip || isVip(req.user),
|
||||
score: req.user.score,
|
||||
ratio: req.user.ratio,
|
||||
profileImageURL: req.user.profileImageURL,
|
||||
email: validator.escape(req.user.email),
|
||||
lastName: validator.escape(req.user.lastName),
|
||||
|
||||
@@ -10,7 +10,8 @@ var mongoose = require('mongoose'),
|
||||
crypto = require('crypto'),
|
||||
validator = require('validator'),
|
||||
generatePassword = require('generate-password'),
|
||||
owasp = require('owasp-password-strength-test');
|
||||
owasp = require('owasp-password-strength-test'),
|
||||
moment = require('moment');
|
||||
|
||||
owasp.config(config.shared.owasp);
|
||||
|
||||
@@ -141,6 +142,10 @@ var UserSchema = new Schema({
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
ratio: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
seeded: {
|
||||
type: Number,
|
||||
default: 0
|
||||
@@ -169,6 +174,24 @@ var UserSchema = new Schema({
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* overwrite toJSON
|
||||
*/
|
||||
UserSchema.methods.toJSON = function (options) {
|
||||
var document = this.toObject(options);
|
||||
document.is_vip = false;
|
||||
|
||||
if (!document.vip_start_at || !document.vip_end_at) {
|
||||
document.is_vip = false;
|
||||
} else if (moment(Date.now()) > moment(document.vip_end_at)) {
|
||||
document.is_vip = false;
|
||||
} else {
|
||||
document.is_vip = true;
|
||||
}
|
||||
|
||||
return document;
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook a pre save method to hash the password
|
||||
*/
|
||||
@@ -181,6 +204,17 @@ UserSchema.pre('save', function (next) {
|
||||
next();
|
||||
});
|
||||
|
||||
/**
|
||||
* Hook a post save method to set the ratio
|
||||
*/
|
||||
UserSchema.post('save', function (doc) {
|
||||
if (doc.uploaded === 0 || doc.downloaded === 0) {
|
||||
doc.ratio = 0;
|
||||
} else {
|
||||
doc.ratio = Math.round((doc.uploaded / doc.downloaded) * 100) / 100;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Hook a pre validate method to test the local password
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user