-
Notifications
You must be signed in to change notification settings - Fork 14
/
users.js
239 lines (221 loc) · 6.18 KB
/
users.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
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
229
230
231
232
233
234
235
236
237
238
239
"use strict";
const async = require('./async');
const fs = require('fs-extra');
const logger = require('./logger.js');
const path = require('path');
const ReadWriteLock = require('rwlock');
const utils = require('./utils.js');
const USERS_PATH = path.join(__dirname, 'users');
const USERS_TEMP_PATH = path.join(__dirname, 'users.tmp');
let users = [];
let nextSaveTimeout = null;
const saveInterval = 5 * 60 * 1000; // save users to disk every 5 minutes
const usersFolderLock = new ReadWriteLock();
function writeAllUsers(cb) {
function getTempFilePathForUser(user) {
return path.resolve(USERS_TEMP_PATH, user.username + '.json');
}
usersFolderLock.writeLock(function(releaseLock) {
async()
.add('removeTemp', function(cb) {
fs.remove(USERS_TEMP_PATH, cb);
})
.add('makeTemp', ['@removeTemp'], function(cb) {
fs.mkdir(USERS_TEMP_PATH, cb);
})
.add('writeUsers', ['@makeTemp'], function(cb) {
// TODO: change map to forEach (or promises?)
async.map(users.map(copyStateForSave), function(userCopy, cb) {
fs.writeFile(getTempFilePathForUser(userCopy), JSON.stringify(userCopy, null, 4), cb);
}, cb);
})
.add('removeOldUsersFolder', ['@writeUsers'], function(cb) {
fs.remove(USERS_PATH, cb);
})
.add(['@removeOldUsersFolder'], function(cb) {
fs.rename(USERS_TEMP_PATH, USERS_PATH, cb);
})
.run(function(err) {
releaseLock();
cb(err);
});
});
}
function refreshSaveTimeout() {
if (nextSaveTimeout) {
clearTimeout(nextSaveTimeout);
}
nextSaveTimeout = setTimeout(function() {
writeAllUsers(function(err) {
if (err) {
logger.error('Error saving users', err);
} else {
logger.info('User data saved');
}
refreshSaveTimeout();
});
}, saveInterval);
}
function readAllUsers(cb) {
// we use a write lock here too because this function sets the 'users' value
usersFolderLock.writeLock(function(releaseLock) {
async()
.add('userFiles', function(cb) {
fs.readdir(USERS_PATH, cb);
})
.add('rawUserData', ['userFiles'], function(userFiles, cb) {
async.map(userFiles, function(filename, cb) {
utils.readJsonFile(path.join(USERS_PATH, filename), cb);
}, cb);
})
.add('userData', ['rawUserData'], function(rawUserData) {
return rawUserData.map(function(userSpec) {
return parseUserSpec(userSpec);
});
})
.add(['userData'], function(userData) {
users = userData;
})
.run(function(err) {
releaseLock();
cb(err);
});
});
}
function initialize(cb) {
async()
.add('readAllUsers', function(cb) {
readAllUsers(cb);
})
.add(['@readAllUsers'], function() {
users.forEach(function(user) {
// TODO: if !user.defaultIdentity, show the welcome/settings screen
user.servers.forEach(function(server) {
// TODO: connect only to the servers that weren't disconnected by the user
if (server.host !== null) {
server.reconnect();
}
});
});
})
.add(function() {
// begin saving user data every 'saveInterval' time
refreshSaveTimeout();
})
.run(cb);
}
function copyWithoutPointers(user) {
function cloneExceptFields(src, exceptFields) {
const exceptFieldsSet = new Set(exceptFields);
const ret = {};
Object.keys(src).filter(function(k) {
return !exceptFieldsSet.has(k);
}).forEach(function(k) {
ret[k] = src[k];
});
return ret;
}
const userCopy = cloneExceptFields(user, [
'activeWebSockets',
'servers',
'entities'
]);
userCopy.servers = user.servers.map(function(server) {
const serverCopy = cloneExceptFields(server, [
'socket',
'user',
'server',
'channels',
'queries',
'pingInterval'
]);
serverCopy.channels = server.channels.map(function(channel) {
return cloneExceptFields(channel, [
'server'
]);
});
serverCopy.queries = server.queries.map(function(query) {
return cloneExceptFields(query, [
'server'
]);
});
return serverCopy;
});
return userCopy;
}
function copyStateForClient(user) {
const userCopy = copyWithoutPointers(user);
delete userCopy.loggedInSessions;
delete userCopy.password;
return userCopy;
}
function copyStateForSave(user) {
const userCopy = copyWithoutPointers(user);
delete userCopy.loggedInSessions;
userCopy.servers.forEach(function(server) {
delete server.type;
delete server.connected;
delete server.currentNickname;
server.channels.forEach(function(channel) {
delete channel.type;
delete channel.userlist;
delete channel.tempUserlist;
delete channel.inChannel;
delete channel.rejoining;
});
server.queries.forEach(function(query) {
delete query.type;
});
});
return userCopy;
}
function parseUserSpec(spec) {
const newUser = new User(spec);
if (Array.isArray(spec.servers)) {
spec.servers.forEach(function(serverSpec) {
const newServer = new Server(serverSpec, newUser.getNextEntityId.bind(newUser));
newUser.addServer(newServer);
if (Array.isArray(serverSpec.channels)) {
serverSpec.channels.forEach(function(channelSpec) {
const newChannel = new Channel(channelSpec, newUser.getNextEntityId);
newServer.addChannel(newChannel);
});
}
if (Array.isArray(serverSpec.queries)) {
serverSpec.queries.forEach(function(querySpec) {
const newQuery = new Query(querySpec, newUser.getNextEntityId);
newServer.addQuery(newQuery);
});
}
});
}
return newUser;
}
function saveAndShutdown() {
writeAllUsers(function(err) {
if (err) {
logger.error('Unable to save user data', err);
process.exit(1);
} else {
logger.info('User data save completed');
process.exit(0);
}
});
}
function getUserBySessionId(sessionId) {
return utils.findFirst(users, function(user) {
return user.loggedInSessions.indexOf(sessionId) !== -1;
});
}
function getUserByCredentials(username, password) {
return utils.findFirst(users, function(user) {
return user.username === username && user.password === password;
});
}
module.exports.initialize = initialize;
module.exports.copyWithoutPointers = copyWithoutPointers;
module.exports.copyStateForClient = copyStateForClient;
module.exports.copyStateForSave = copyStateForSave;
module.exports.saveAndShutdown = saveAndShutdown;
module.exports.getUserBySessionId = getUserBySessionId;
module.exports.getUserByCredentials = getUserByCredentials;