UNPKG

2.08 kBJavaScriptView Raw
1// Copyright Michael Rhodes. 2017,2018. All Rights Reserved.
2// This file is licensed under the MIT License.
3// License text available at https://opensource.org/licenses/MIT
4
5var uuid = require('uuid');
6var async = require('async');
7var VError = require('verror').VError;
8var WError = require('verror').WError;
9var _ = require('lodash');
10
11module.exports.fixIfBehindProxy = function fixIfBehindProxy(app, url) {
12 if (process.env.BEHIND_PROXY === 'true') {
13 var rx = new RegExp('^' + app.config.publicHost);
14 if (url.match(rx)) {
15 url = url.replace(app.config.publicHost, 'http://localhost:' + app.config.port);
16 }
17 }
18 return url;
19};
20
21module.exports.refresh = function refresh(app, socket, cb) {
22 async.series([
23 function (done) {
24 if (!_.has(socket, 'antisocial.user.id')) {
25 return async.setImmediate(function () {
26 done();
27 });
28 }
29 app.db.getInstances('users', [{
30 'property': 'id',
31 'value': socket.antisocial.user.id
32 }], function (err, userInstances) {
33 if (err) {
34 return done(new VError(err, 'user not found'));
35 }
36
37 if (userInstances.length > 1) {
38 return done(new VError('more than one user matching id'));
39 }
40
41 if (!userInstances.length) {
42 return done(new VError('user not found'));
43 }
44
45 socket.antisocial.user = userInstances[0];
46 done();
47 });
48 },
49 function (done) {
50 if (!_.has(socket, 'antisocial.friend.id')) {
51 return async.setImmediate(function () {
52 done();
53 });
54 }
55 app.db.getInstances('friends', [{
56 'property': 'id',
57 'value': socket.antisocial.friend.id
58 }], function (err, friendInstances) {
59 if (err) {
60 return done(new VError(err, 'friend not found'));
61 }
62
63 if (friendInstances.length > 1) {
64 return done(new VError('more than one friend matching id'));
65 }
66
67 if (!friendInstances.length) {
68 return done(new VError('friend not found'));
69 }
70
71 socket.antisocial.friend = friendInstances[0];
72 done();
73 });
74 }
75 ], function (err) {
76 if (err) {
77 var e = new WError(err, 'refresh failed');
78 return cb(e);
79 }
80 cb();
81 });
82};