X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fpods.js;h=6ab018c1ccc0d107323d573f74960b00453abeb4;hb=2c49ca42d14087ce8e1695759435f796a290470b;hp=daeadeb078c6b2dd60de05967b1456a101e3a3e2;hpb=528a9efa8272532bbd0dafc35c3e05e57c50f61e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/pods.js b/server/models/pods.js index daeadeb07..6ab018c1c 100644 --- a/server/models/pods.js +++ b/server/models/pods.js @@ -1,103 +1,120 @@ 'use strict' +const each = require('async/each') const mongoose = require('mongoose') +const map = require('lodash/map') +const validator = require('express-validator').validator const constants = require('../initializers/constants') -const logger = require('../helpers/logger') + +const Video = mongoose.model('Video') // --------------------------------------------------------------------------- -const podsSchema = mongoose.Schema({ +const PodSchema = mongoose.Schema({ url: String, publicKey: String, - score: { type: Number, max: constants.FRIEND_BASE_SCORE } + score: { type: Number, max: constants.FRIEND_SCORE.MAX }, + createdDate: { + type: Date, + default: Date.now + } }) -const PodsDB = mongoose.model('pods', podsSchema) -// --------------------------------------------------------------------------- +// TODO: set options (TLD...) +PodSchema.path('url').validate(validator.isURL) +PodSchema.path('publicKey').required(true) +PodSchema.path('score').validate(function (value) { return !isNaN(value) }) -const Pods = { - add: add, - count: count, - findById: findById, - findByUrl: findByUrl, - findBadPods: findBadPods, - incrementScores: incrementScores, - list: list, - listAllIds: listAllIds, - listAllUrls: listAllUrls, - remove: remove, - removeAll: removeAll, - removeAllByIds: removeAllByIds +PodSchema.methods = { + toFormatedJSON } -// TODO: check if the pod is not already a friend -function add (data, callback) { - if (!callback) callback = function () {} - const params = { - url: data.url, - publicKey: data.publicKey, - score: constants.FRIEND_BASE_SCORE - } - - PodsDB.create(params, callback) +PodSchema.statics = { + countAll, + incrementScores, + list, + listAllIds, + listBadPods, + load, + loadByUrl, + removeAll } -function count (callback) { - return PodsDB.count(callback) -} +PodSchema.pre('save', function (next) { + const self = this -function findBadPods (callback) { - PodsDB.find({ score: 0 }, callback) -} + Pod.loadByUrl(this.url, function (err, pod) { + if (err) return next(err) + + if (pod) return next(new Error('Pod already exists.')) + + self.score = constants.FRIEND_SCORE.BASE + return next() + }) +}) + +PodSchema.pre('remove', function (next) { + // Remove the videos owned by this pod too + Video.listByUrl(this.url, function (err, videos) { + if (err) return next(err) + + each(videos, function (video, callbackEach) { + video.remove(callbackEach) + }, next) + }) +}) + +const Pod = mongoose.model('Pod', PodSchema) -function findById (id, callback) { - PodsDB.findById(id, callback) +// ------------------------------ METHODS ------------------------------ + +function toFormatedJSON () { + const json = { + id: this._id, + url: this.url, + score: this.score, + createdDate: this.createdDate + } + + return json } -function findByUrl (url, callback) { - PodsDB.findOne({ url: url }, callback) +// ------------------------------ Statics ------------------------------ + +function countAll (callback) { + return this.count(callback) } function incrementScores (ids, value, callback) { if (!callback) callback = function () {} - PodsDB.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) + return this.update({ _id: { $in: ids } }, { $inc: { score: value } }, { multi: true }, callback) } function list (callback) { - PodsDB.find(function (err, podsList) { - if (err) { - logger.error('Cannot get the list of the pods.') - return callback(err) - } - - return callback(null, podsList) - }) + return this.find(callback) } function listAllIds (callback) { - return PodsDB.find({}, { _id: 1 }, callback) -} + return this.find({}, { _id: 1 }, function (err, pods) { + if (err) return callback(err) -function listAllUrls (callback) { - return PodsDB.find({}, { _id: 0, url: 1 }, callback) + return callback(null, map(pods, '_id')) + }) } -function remove (url, callback) { - if (!callback) callback = function () {} - PodsDB.remove({ url: url }, callback) +function listBadPods (callback) { + return this.find({ score: 0 }, callback) } -function removeAll (callback) { - if (!callback) callback = function () {} - PodsDB.remove(callback) +function load (id, callback) { + return this.findById(id, callback) } -function removeAllByIds (ids, callback) { - if (!callback) callback = function () {} - PodsDB.remove({ _id: { $in: ids } }, callback) +function loadByUrl (url, callback) { + return this.findOne({ url: url }, callback) } -// --------------------------------------------------------------------------- - -module.exports = Pods +function removeAll (callback) { + return this.remove({}, callback) +}