-
Notifications
You must be signed in to change notification settings - Fork 4
/
library.js
109 lines (94 loc) · 2.79 KB
/
library.js
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
'use strict';
const db = require.main.require('./src/database');
const meta = require.main.require('./src/meta');
const user = require.main.require('./src/user');
const routerHelpers = require.main.require('./src/routes/helpers');
const mapbox = require('mapbox');
const osmMap = {
_settings: {},
_defaults: {
mapboxAccessToken: '',
},
};
async function renderMap(req, res) {
const uids = await db.getSetMembers('osmMap.users');
let users = await user.getUsersWithFields(uids, [
'username', 'userslug', 'picture', 'locationLon', 'locationLat', 'status',
], req.uid);
users = users.filter(user => user.locationLon && user.locationLat);
res.render('map', { settings: osmMap._settings, users, title: '[[osm-map:map]]' });
}
osmMap.init = function (params, callback) {
const { router } = params;
routerHelpers.setupAdminPageRoute(router, '/admin/plugins/osm-map', (req, res) => {
res.render('admin/plugins/osm-map', {
title: 'Mapbox',
});
});
routerHelpers.setupPageRoute(router, '/map', renderMap);
meta.settings.get('osm-map', (err, settings) => {
if (err) {
console.log(`osm-map: failed to retrieve settings${err.message}`);
}
Object.assign(osmMap._settings, osmMap._defaults, settings);
});
callback();
};
osmMap.addAdminNavigation = function (header, callback) {
header.plugins.push({
route: '/plugins/osm-map',
icon: 'fa-map',
name: '[[osm-map:osm-map]]',
});
callback(null, header);
};
osmMap.getNavigation = function (core, callback) {
core.push({
route: '/map',
title: '\\[\\[osm-map:map\\]\\]',
enabled: false,
iconClass: 'fa-map',
textClass: 'visible-xs-inline',
text: '\\[\\[osm-map:map\\]\\]',
properties: { },
core: false,
});
callback(null, core);
};
osmMap.whitelistFields = function (hookData, callback) {
hookData.whitelist.push('locationLon');
hookData.whitelist.push('locationLat');
callback(null, hookData);
};
osmMap.addCoordinates = function (profile, callback) {
let lon = '';
let lat = '';
async function setLonLat() {
profile.data.locationLon = lon;
profile.data.locationLat = lat;
profile.fields.push('locationLon');
profile.fields.push('locationLat');
await (!!lat && !!lon ? db.setAdd : db.setRemove)('osmMap.users', profile.data.uid);
}
if (profile.data.location && profile.data.location !== '') {
const mapboxClient = new mapbox(osmMap._settings.mapboxAccessToken);
mapboxClient.geocodeForward(profile.data.location, { limit: 1 })
.then((res) => {
const data = res.entity;
if (data.features) {
lon = String(data.features[0].center[0]);
lat = String(data.features[0].center[1]);
}
setLonLat();
callback(null, profile);
})
.catch((err) => {
console.log(`catch: ${err}`);
callback(null, profile);
});
} else {
setLonLat();
callback(null, profile);
}
};
module.exports = osmMap;