aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/v1/pods.js20
-rw-r--r--server/models/pods.js28
-rw-r--r--server/tests/api/friends-basic.js13
3 files changed, 49 insertions, 12 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js
index 360575a0d..2bdfe0c92 100644
--- a/server/controllers/api/v1/pods.js
+++ b/server/controllers/api/v1/pods.js
@@ -17,7 +17,7 @@ const router = express.Router()
17const Pod = mongoose.model('Pod') 17const Pod = mongoose.model('Pod')
18const Video = mongoose.model('Video') 18const Video = mongoose.model('Video')
19 19
20router.get('/', listPodsUrl) 20router.get('/', listPods)
21router.post('/', validators.podsAdd, addPods) 21router.post('/', validators.podsAdd, addPods)
22router.post('/makefriends', 22router.post('/makefriends',
23 oAuth.authenticate, 23 oAuth.authenticate,
@@ -74,11 +74,11 @@ function addPods (req, res, next) {
74 }) 74 })
75} 75}
76 76
77function listPodsUrl (req, res, next) { 77function listPods (req, res, next) {
78 Pod.listOnlyUrls(function (err, podsUrlList) { 78 Pod.list(function (err, podsUrlList) {
79 if (err) return next(err) 79 if (err) return next(err)
80 80
81 res.json(podsUrlList) 81 res.json(getFormatedPods(podsUrlList))
82 }) 82 })
83} 83}
84 84
@@ -142,3 +142,15 @@ function quitFriends (req, res, next) {
142 res.type('json').status(204).end() 142 res.type('json').status(204).end()
143 }) 143 })
144} 144}
145
146// ---------------------------------------------------------------------------
147
148function getFormatedPods (pods) {
149 const formatedPods = []
150
151 pods.forEach(function (pod) {
152 formatedPods.push(pod.toFormatedJSON())
153 })
154
155 return formatedPods
156}
diff --git a/server/models/pods.js b/server/models/pods.js
index bf43d7b25..59de2d60c 100644
--- a/server/models/pods.js
+++ b/server/models/pods.js
@@ -11,7 +11,11 @@ const constants = require('../initializers/constants')
11const PodSchema = mongoose.Schema({ 11const PodSchema = mongoose.Schema({
12 url: String, 12 url: String,
13 publicKey: String, 13 publicKey: String,
14 score: { type: Number, max: constants.FRIEND_SCORE.MAX } 14 score: { type: Number, max: constants.FRIEND_SCORE.MAX },
15 createdDate: {
16 type: Date,
17 default: Date.now
18 }
15}) 19})
16 20
17// TODO: set options (TLD...) 21// TODO: set options (TLD...)
@@ -19,12 +23,15 @@ PodSchema.path('url').validate(validator.isURL)
19PodSchema.path('publicKey').required(true) 23PodSchema.path('publicKey').required(true)
20PodSchema.path('score').validate(function (value) { return !isNaN(value) }) 24PodSchema.path('score').validate(function (value) { return !isNaN(value) })
21 25
26PodSchema.methods = {
27 toFormatedJSON: toFormatedJSON
28}
29
22PodSchema.statics = { 30PodSchema.statics = {
23 countAll: countAll, 31 countAll: countAll,
24 incrementScores: incrementScores, 32 incrementScores: incrementScores,
25 list: list, 33 list: list,
26 listAllIds: listAllIds, 34 listAllIds: listAllIds,
27 listOnlyUrls: listOnlyUrls,
28 listBadPods: listBadPods, 35 listBadPods: listBadPods,
29 load: load, 36 load: load,
30 loadByUrl: loadByUrl, 37 loadByUrl: loadByUrl,
@@ -46,6 +53,19 @@ PodSchema.pre('save', function (next) {
46 53
47const Pod = mongoose.model('Pod', PodSchema) 54const Pod = mongoose.model('Pod', PodSchema)
48 55
56// ------------------------------ METHODS ------------------------------
57
58function toFormatedJSON () {
59 const json = {
60 id: this._id,
61 url: this.url,
62 score: this.score,
63 createdDate: this.createdDate
64 }
65
66 return json
67}
68
49// ------------------------------ Statics ------------------------------ 69// ------------------------------ Statics ------------------------------
50 70
51function countAll (callback) { 71function countAll (callback) {
@@ -69,10 +89,6 @@ function listAllIds (callback) {
69 }) 89 })
70} 90}
71 91
72function listOnlyUrls (callback) {
73 return this.find({}, { _id: 0, url: 1 }, callback)
74}
75
76function listBadPods (callback) { 92function listBadPods (callback) {
77 return this.find({ score: 0 }, callback) 93 return this.find({ score: 0 }, callback)
78} 94}
diff --git a/server/tests/api/friends-basic.js b/server/tests/api/friends-basic.js
index 2a6883acb..2f2f25e25 100644
--- a/server/tests/api/friends-basic.js
+++ b/server/tests/api/friends-basic.js
@@ -6,6 +6,7 @@ const expect = chai.expect
6const series = require('async/series') 6const series = require('async/series')
7 7
8const loginUtils = require('../utils/login') 8const loginUtils = require('../utils/login')
9const miscsUtils = require('../utils/miscs')
9const podsUtils = require('../utils/pods') 10const podsUtils = require('../utils/pods')
10const serversUtils = require('../utils/servers') 11const serversUtils = require('../utils/servers')
11 12
@@ -92,7 +93,11 @@ describe('Test basic friends', function () {
92 const result = res.body 93 const result = res.body
93 expect(result).to.be.an('array') 94 expect(result).to.be.an('array')
94 expect(result.length).to.equal(1) 95 expect(result.length).to.equal(1)
95 expect(result[0].url).to.be.equal(servers[2].url) 96
97 const pod = result[0]
98 expect(pod.url).to.equal(servers[2].url)
99 expect(pod.score).to.equal(20)
100 expect(miscsUtils.dateIsValid(pod.createdDate)).to.be.true
96 101
97 next() 102 next()
98 }) 103 })
@@ -105,7 +110,11 @@ describe('Test basic friends', function () {
105 const result = res.body 110 const result = res.body
106 expect(result).to.be.an('array') 111 expect(result).to.be.an('array')
107 expect(result.length).to.equal(1) 112 expect(result.length).to.equal(1)
108 expect(result[0].url).to.be.equal(servers[1].url) 113
114 const pod = result[0]
115 expect(pod.url).to.equal(servers[1].url)
116 expect(pod.score).to.equal(20)
117 expect(miscsUtils.dateIsValid(pod.createdDate)).to.be.true
109 118
110 next() 119 next()
111 }) 120 })