198b205c10
* Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
194 lines
4.2 KiB
JavaScript
194 lines
4.2 KiB
JavaScript
'use strict'
|
|
|
|
const values = require('lodash/values')
|
|
|
|
const modelUtils = require('./utils')
|
|
const constants = require('../initializers/constants')
|
|
const peertubeCrypto = require('../helpers/peertube-crypto')
|
|
const customUsersValidators = require('../helpers/custom-validators').users
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
const User = sequelize.define('User',
|
|
{
|
|
password: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
validate: {
|
|
passwordValid: function (value) {
|
|
const res = customUsersValidators.isUserPasswordValid(value)
|
|
if (res === false) throw new Error('Password not valid.')
|
|
}
|
|
}
|
|
},
|
|
username: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
validate: {
|
|
usernameValid: function (value) {
|
|
const res = customUsersValidators.isUserUsernameValid(value)
|
|
if (res === false) throw new Error('Username not valid.')
|
|
}
|
|
}
|
|
},
|
|
email: {
|
|
type: DataTypes.STRING(400),
|
|
allowNull: false,
|
|
validate: {
|
|
isEmail: true
|
|
}
|
|
},
|
|
displayNSFW: {
|
|
type: DataTypes.BOOLEAN,
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
validate: {
|
|
nsfwValid: function (value) {
|
|
const res = customUsersValidators.isUserDisplayNSFWValid(value)
|
|
if (res === false) throw new Error('Display NSFW is not valid.')
|
|
}
|
|
}
|
|
},
|
|
role: {
|
|
type: DataTypes.ENUM(values(constants.USER_ROLES)),
|
|
allowNull: false
|
|
}
|
|
},
|
|
{
|
|
indexes: [
|
|
{
|
|
fields: [ 'username' ],
|
|
unique: true
|
|
},
|
|
{
|
|
fields: [ 'email' ],
|
|
unique: true
|
|
}
|
|
],
|
|
classMethods: {
|
|
associate,
|
|
|
|
countTotal,
|
|
getByUsername,
|
|
list,
|
|
listForApi,
|
|
loadById,
|
|
loadByUsername,
|
|
loadByUsernameOrEmail
|
|
},
|
|
instanceMethods: {
|
|
isPasswordMatch,
|
|
toFormatedJSON,
|
|
isAdmin
|
|
},
|
|
hooks: {
|
|
beforeCreate: beforeCreateOrUpdate,
|
|
beforeUpdate: beforeCreateOrUpdate
|
|
}
|
|
}
|
|
)
|
|
|
|
return User
|
|
}
|
|
|
|
function beforeCreateOrUpdate (user, options, next) {
|
|
peertubeCrypto.cryptPassword(user.password, function (err, hash) {
|
|
if (err) return next(err)
|
|
|
|
user.password = hash
|
|
|
|
return next()
|
|
})
|
|
}
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
function isPasswordMatch (password, callback) {
|
|
return peertubeCrypto.comparePassword(password, this.password, callback)
|
|
}
|
|
|
|
function toFormatedJSON () {
|
|
return {
|
|
id: this.id,
|
|
username: this.username,
|
|
email: this.email,
|
|
displayNSFW: this.displayNSFW,
|
|
role: this.role,
|
|
createdAt: this.createdAt
|
|
}
|
|
}
|
|
|
|
function isAdmin () {
|
|
return this.role === constants.USER_ROLES.ADMIN
|
|
}
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
|
|
|
function associate (models) {
|
|
this.hasOne(models.Author, {
|
|
foreignKey: 'userId',
|
|
onDelete: 'cascade'
|
|
})
|
|
|
|
this.hasMany(models.OAuthToken, {
|
|
foreignKey: 'userId',
|
|
onDelete: 'cascade'
|
|
})
|
|
}
|
|
|
|
function countTotal (callback) {
|
|
return this.count().asCallback(callback)
|
|
}
|
|
|
|
function getByUsername (username) {
|
|
const query = {
|
|
where: {
|
|
username: username
|
|
}
|
|
}
|
|
|
|
return this.findOne(query)
|
|
}
|
|
|
|
function list (callback) {
|
|
return this.find().asCallback(callback)
|
|
}
|
|
|
|
function listForApi (start, count, sort, callback) {
|
|
const query = {
|
|
offset: start,
|
|
limit: count,
|
|
order: [ modelUtils.getSort(sort) ]
|
|
}
|
|
|
|
return this.findAndCountAll(query).asCallback(function (err, result) {
|
|
if (err) return callback(err)
|
|
|
|
return callback(null, result.rows, result.count)
|
|
})
|
|
}
|
|
|
|
function loadById (id, callback) {
|
|
return this.findById(id).asCallback(callback)
|
|
}
|
|
|
|
function loadByUsername (username, callback) {
|
|
const query = {
|
|
where: {
|
|
username: username
|
|
}
|
|
}
|
|
|
|
return this.findOne(query).asCallback(callback)
|
|
}
|
|
|
|
function loadByUsernameOrEmail (username, email, callback) {
|
|
const query = {
|
|
where: {
|
|
$or: [ { username }, { email } ]
|
|
}
|
|
}
|
|
|
|
return this.findOne(query).asCallback(callback)
|
|
}
|