aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pods.js
blob: 9502d92e407aacc3cea530ca9defebee10109ae0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'

const mongoose = require('mongoose')
const map = require('lodash/map')

const constants = require('../initializers/constants')
const logger = require('../helpers/logger')

// ---------------------------------------------------------------------------

const podsSchema = mongoose.Schema({
  url: String,
  publicKey: String,
  score: { type: Number, max: constants.FRIEND_BASE_SCORE }
})
const PodsDB = mongoose.model('pods', podsSchema)

// ---------------------------------------------------------------------------

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
}

// 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)
}

function count (callback) {
  return PodsDB.count(callback)
}

function findBadPods (callback) {
  PodsDB.find({ score: 0 }, callback)
}

function findById (id, callback) {
  PodsDB.findById(id, callback)
}

function findByUrl (url, callback) {
  PodsDB.findOne({ url: url }, callback)
}

function incrementScores (ids, value, callback) {
  if (!callback) callback = function () {}
  PodsDB.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)
  })
}

function listAllIds (callback) {
  return PodsDB.find({}, { _id: 1 }, function (err, pods) {
    if (err) return callback(err)

    return callback(null, map(pods, '_id'))
  })
}

function listAllUrls (callback) {
  return PodsDB.find({}, { _id: 0, url: 1 }, callback)
}

function remove (url, callback) {
  if (!callback) callback = function () {}
  PodsDB.remove({ url: url }, callback)
}

function removeAll (callback) {
  if (!callback) callback = function () {}
  PodsDB.remove(callback)
}

function removeAllByIds (ids, callback) {
  if (!callback) callback = function () {}
  PodsDB.remove({ _id: { $in: ids } }, callback)
}

// ---------------------------------------------------------------------------

module.exports = Pods