-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
129 lines (111 loc) · 3.34 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";
var user = module.parent.require('./user'),
meta = module.parent.require('./meta'),
db = module.parent.require('./database'),
winston = module.parent.require('winston'),
async = module.parent.require('async'),
crypto = require('crypto'),
controllers = require('./lib/controllers'),
plugin = {};
plugin.init = function(params, callback) {
var router = params.router,
hostMiddleware = params.middleware,
hostControllers = params.controllers;
router.get('/admin/plugins/knuddels', hostMiddleware.admin.buildHeader, controllers.renderAdminPage);
router.get('/api/admin/plugins/knuddels', controllers.renderAdminPage);
meta.settings.get('knuddels', function(err, settings) {
if (err) {
winston.error('[plugin/knuddels] Could not retrieve plugin settings! Using defaults.');
plugin.settings = {
default: false,
force: false
};
return;
}
plugin.settings = settings;
});
callback();
};
plugin.addAdminNavigation = function(header, callback) {
header.plugins.push({
route: '/plugins/knuddels',
icon: 'fa-picture',
name: 'Knuddels'
});
callback(null, header);
};
plugin.list = function(data, callback) {
user.getUserFields(data.uid, ['username'], function(err, userData) {
data.pictures.push({
type: 'knuddels',
url: getProfilePicture(userData.username),
text: 'Knuddels'
});
callback(null, data);
});
};
plugin.get = function(data, callback) {
if (data.type === 'knuddels') {
user.getUserFields(data.uid, ['username'], function(err, userData) {
data.picture = getProfilePicture(userData.username);
callback(null, data);
});
} else {
callback(null, data);
}
};
plugin.updateUser = function(data, callback) {
if (plugin.settings.default === 'on') {
winston.verbose('[plugin/gravatar] Updating uid ' + data.user.uid + ' to use gravatar');
data.user.picture = getProfilePicture(data.user.username);
callback(null, data);
} else {
// No transformation
callback(null, data);
}
};
plugin.onForceEnabled = function(users, callback) {
if (plugin.hasOwnProperty('settings') && plugin.settings.force === 'on') {
async.map(users, function(userObj, next) {
if (!userObj) {
return next(null, userObj);
}
if (!userObj.email) {
db.getObjectField('user:' + userObj.uid, 'email', function(err, email) {
userObj.picture = getProfilePicture(userObj.username);
next(null, userObj);
});
} else {
userObj.picture = getProfilePicture(userObj.username);
next(null, userObj);
}
}, callback);
} else if (plugin.hasOwnProperty('settings') && plugin.settings.default === 'on') {
async.map(users, function(userObj, next) {
if (!userObj) {
return next(null, userObj);
}
if (userObj.picture === '') {
if (!userObj.email) {
db.getObjectField('user:' + userObj.uid, 'email', function(err, email) {
userObj.picture = getProfilePicture(userObj.username);
next(null, userObj);
});
} else {
userObj.picture = getProfilePicture(userObj.username);
next(null, userObj);
}
} else {
setImmediate(next, null, userObj);
}
}, callback);
} else {
// No transformation
callback(null, users);
}
}
function getProfilePicture(username) {
var size = parseInt(meta.config.profileImageDimension, 10) || 128;
return 'http://chat.knuddels.de/pics/fotos/knuddels.de?n=' + encodeURIComponent(username);
};
module.exports = plugin;