]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/pod.ts
First typescript iteration
[github/Chocobozzz/PeerTube.git] / server / models / pod.ts
similarity index 88%
rename from server/models/pod.js
rename to server/models/pod.ts
index 8e2d488e109dd4036287e839adbfee6a2f699324..0e0262978517649359cabbf765c1d39a87956b94 100644 (file)
@@ -1,12 +1,8 @@
-'use strict'
+import { each, waterfall } from 'async'
+import { map } from 'lodash'
 
-const each = require('async/each')
-const map = require('lodash/map')
-const waterfall = require('async/waterfall')
-
-const constants = require('../initializers/constants')
-const logger = require('../helpers/logger')
-const customPodsValidators = require('../helpers/custom-validators').pods
+import { FRIEND_SCORE, PODS_SCORE } from '../initializers'
+import { logger, isHostValid } from '../helpers'
 
 // ---------------------------------------------------------------------------
 
@@ -18,7 +14,7 @@ module.exports = function (sequelize, DataTypes) {
         allowNull: false,
         validate: {
           isHost: function (value) {
-            const res = customPodsValidators.isHostValid(value)
+            const res = isHostValid(value)
             if (res === false) throw new Error('Host not valid.')
           }
         }
@@ -29,11 +25,11 @@ module.exports = function (sequelize, DataTypes) {
       },
       score: {
         type: DataTypes.INTEGER,
-        defaultValue: constants.FRIEND_SCORE.BASE,
+        defaultValue: FRIEND_SCORE.BASE,
         allowNull: false,
         validate: {
           isInt: true,
-          max: constants.FRIEND_SCORE.MAX
+          max: FRIEND_SCORE.MAX
         }
       },
       email: {
@@ -106,7 +102,7 @@ function countAll (callback) {
 }
 
 function incrementScores (ids, value, callback) {
-  if (!callback) callback = function () {}
+  if (!callback) callback = function () { /* empty */ }
 
   const update = {
     score: this.sequelize.literal('score +' + value)
@@ -135,7 +131,7 @@ function listAllIds (transaction, callback) {
     transaction = null
   }
 
-  const query = {
+  const query: any = {
     attributes: [ 'id' ]
   }
 
@@ -223,13 +219,13 @@ function updatePodsScore (goodPods, badPods) {
   logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
 
   if (goodPods.length !== 0) {
-    this.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
+    this.incrementScores(goodPods, PODS_SCORE.BONUS, function (err) {
       if (err) logger.error('Cannot increment scores of good pods.', { error: err })
     })
   }
 
   if (badPods.length !== 0) {
-    this.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
+    this.incrementScores(badPods, PODS_SCORE.MALUS, function (err) {
       if (err) logger.error('Cannot decrement scores of bad pods.', { error: err })
       removeBadPods.call(self)
     })
@@ -255,7 +251,7 @@ function removeBadPods () {
     },
 
     function removeTheseBadPods (pods, callback) {
-      each(pods, function (pod, callbackEach) {
+      each(pods, function (pod: any, callbackEach) {
         pod.destroy().asCallback(callbackEach)
       }, function (err) {
         return callback(err, pods.length)