aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/friends.js
blob: 9a2c5c06e6687c7f4a2ebb4e73223ff5b2d0529a (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict'

const async = require('async')
const config = require('config')
const fs = require('fs')
const request = require('request')

const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
const peertubeCrypto = require('../helpers/peertubeCrypto')
const Pods = require('../models/pods')
const requestsScheduler = require('../lib/requestsScheduler')
const requests = require('../helpers/requests')
const Videos = require('../models/videos')

const http = config.get('webserver.https') ? 'https' : 'http'
const host = config.get('webserver.host')
const port = config.get('webserver.port')

const pods = {
  addVideoToFriends: addVideoToFriends,
  hasFriends: hasFriends,
  makeFriends: makeFriends,
  quitFriends: quitFriends,
  removeVideoToFriends: removeVideoToFriends
}

function addVideoToFriends (video) {
  // To avoid duplicates
  const id = video.name + video.magnetUri
  // ensure namePath is null
  video.namePath = null
  requestsScheduler.addRequest(id, 'add', video)
}

function hasFriends (callback) {
  Pods.count(function (err, count) {
    if (err) return callback(err)

    const has_friends = (count !== 0)
    callback(null, has_friends)
  })
}

function makeFriends (callback) {
  const pods_score = {}

  logger.info('Make friends!')
  fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
    if (err) {
      logger.error('Cannot read public cert.')
      return callback(err)
    }

    const urls = config.get('network.friends')

    async.each(urls, function (url, callback) {
      computeForeignPodsList(url, pods_score, callback)
    }, function (err) {
      if (err) return callback(err)

      logger.debug('Pods scores computed.', { pods_score: pods_score })
      const pods_list = computeWinningPods(urls, pods_score)
      logger.debug('Pods that we keep.', { pods_to_keep: pods_list })

      makeRequestsToWinningPods(cert, pods_list, callback)
    })
  })
}

function quitFriends (callback) {
  // Stop pool requests
  requestsScheduler.deactivate()
  // Flush pool requests
  requestsScheduler.forceSend()

  Pods.list(function (err, pods) {
    if (err) return callback(err)

    const request = {
      method: 'POST',
      path: '/api/' + constants.API_VERSION + '/pods/remove',
      sign: true,
      encrypt: true,
      data: {
        url: 'me' // Fake data
      }
    }

    // Announce we quit them
    requests.makeMultipleRetryRequest(request, pods, function () {
      Pods.removeAll(function (err) {
        requestsScheduler.activate()

        if (err) return callback(err)

        logger.info('Broke friends, so sad :(')

        Videos.removeAllRemotes(function (err) {
          if (err) return callback(err)

          logger.info('Removed all remote videos.')
          callback(null)
        })
      })
    })
  })
}

function removeVideoToFriends (video) {
  // To avoid duplicates
  const id = video.name + video.magnetUri
  requestsScheduler.addRequest(id, 'remove', video)
}

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

module.exports = pods

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

function computeForeignPodsList (url, pods_score, callback) {
  // Let's give 1 point to the pod we ask the friends list
  pods_score[url] = 1

  getForeignPodsList(url, function (err, foreign_pods_list) {
    if (err) return callback(err)
    if (foreign_pods_list.length === 0) return callback()

    async.each(foreign_pods_list, function (foreign_pod, callback_each) {
      const foreign_url = foreign_pod.url

      if (pods_score[foreign_url]) pods_score[foreign_url]++
      else pods_score[foreign_url] = 1

      callback_each()
    }, function () {
      callback()
    })
  })
}

function computeWinningPods (urls, pods_score) {
  // Build the list of pods to add
  // Only add a pod if it exists in more than a half base pods
  const pods_list = []
  const base_score = urls.length / 2
  Object.keys(pods_score).forEach(function (pod) {
    if (pods_score[pod] > base_score) pods_list.push({ url: pod })
  })

  return pods_list
}

function getForeignPodsList (url, callback) {
  const path = '/api/' + constants.API_VERSION + '/pods'

  request.get(url + path, function (err, response, body) {
    if (err) return callback(err)

    callback(null, JSON.parse(body))
  })
}

function makeRequestsToWinningPods (cert, pods_list, callback) {
  // Stop pool requests
  requestsScheduler.deactivate()
  // Flush pool requests
  requestsScheduler.forceSend()

  // Get the list of our videos to send to our new friends
  Videos.listOwned(function (err, videos_list) {
    if (err) {
      logger.error('Cannot get the list of videos we own.')
      return callback(err)
    }

    const data = {
      url: http + '://' + host + ':' + port,
      publicKey: cert,
      videos: videos_list
    }

    requests.makeMultipleRetryRequest(
      { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },

      pods_list,

      function eachRequest (err, response, body, url, pod, callback_each_request) {
        // We add the pod if it responded correctly with its public certificate
        if (!err && response.statusCode === 200) {
          Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
            if (err) {
              logger.error('Error with adding %s pod.', pod.url, { error: err })
              return callback_each_request()
            }

            Videos.addRemotes(body.videos, function (err) {
              if (err) {
                logger.error('Error with adding videos of pod.', pod.url, { error: err })
                return callback_each_request()
              }

              logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
              return callback_each_request()
            })
          })
        } else {
          logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
          return callback_each_request()
        }
      },

      function endRequests (err) {
        // Now we made new friends, we can re activate the pool of requests
        requestsScheduler.activate()

        if (err) {
          logger.error('There was some errors when we wanted to make friends.')
          return callback(err)
        }

        logger.debug('makeRequestsToWinningPods finished.')
        return callback(null)
      }
    )
  })
}