You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
8.4 KiB
JavaScript

'use strict';
var async = require('async');
var user = require('../user');
var topics = require('../topics');
var notifications = require('../notifications');
var messaging = require('../messaging');
var plugins = require('../plugins');
var meta = require('../meta');
var events = require('../events');
var emailer = require('../emailer');
var db = require('../database');
var apiController = require('../controllers/api');
var SocketUser = {};
require('./user/profile')(SocketUser);
require('./user/search')(SocketUser);
require('./user/status')(SocketUser);
require('./user/picture')(SocketUser);
9 years ago
require('./user/ban')(SocketUser);
11 years ago
SocketUser.exists = function(socket, data, callback) {
if (data && data.username) {
9 years ago
meta.userOrGroupExists(data.username, callback);
}
};
11 years ago
SocketUser.deleteAccount = function(socket, data, callback) {
10 years ago
if (!socket.uid) {
return;
}
async.waterfall([
function (next) {
user.isAdministrator(socket.uid, next);
},
function (isAdmin, next) {
if (isAdmin) {
return next(new Error('[[error:cant-delete-admin]]'));
10 years ago
}
user.deleteAccount(socket.uid, next);
},
function (next) {
socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'});
events.log({
type: 'user-delete',
uid: socket.uid,
targetUid: socket.uid,
ip: socket.ip
});
next();
}
], callback);
11 years ago
};
11 years ago
SocketUser.emailExists = function(socket, data, callback) {
10 years ago
if (data && data.email) {
user.email.exists(data.email, callback);
}
};
SocketUser.emailConfirm = function(socket, data, callback) {
if (socket.uid && parseInt(meta.config.requireEmailConfirmation, 10) === 1) {
user.getUserField(socket.uid, 'email', function(err, email) {
if (err) {
return callback(err);
}
if (!email) {
return;
}
10 years ago
user.email.sendValidationEmail(socket.uid, email, callback);
});
}
};
// Password Reset
SocketUser.reset = {};
SocketUser.reset.send = function(socket, email, callback) {
if (email) {
user.reset.send(email, callback);
}
};
11 years ago
SocketUser.reset.commit = function(socket, data, callback) {
if (!data || !data.code || !data.password) {
return callback(new Error('[[error:invalid-data]]'));
}
10 years ago
async.parallel({
uid: async.apply(db.getObjectField, 'reset:uid', data.code),
reset: async.apply(user.reset.commit, data.code, data.password)
}, function(err, results) {
if (err) {
return callback(err);
}
var uid = results.uid,
now = new Date(),
parsedDate = now.getFullYear() + '/' + (now.getMonth()+1) + '/' + now.getDate();
user.getUserField(uid, 'username', function(err, username) {
emailer.send('reset_notify', uid, {
username: username,
date: parsedDate,
site_title: meta.config.title || 'NodeBB',
subject: '[[email:reset.notify.subject]]'
});
});
events.log({
type: 'password-reset',
uid: uid,
ip: socket.ip
});
callback();
});
};
SocketUser.isFollowing = function(socket, data, callback) {
if (!socket.uid || !data.uid) {
return callback(null, false);
}
user.isFollowing(socket.uid, data.uid, callback);
};
11 years ago
SocketUser.follow = function(socket, data, callback) {
if (!socket.uid || !data) {
return;
}
var userData;
async.waterfall([
function (next) {
toggleFollow('follow', socket.uid, data.uid, next);
},
function (next) {
user.getUserFields(socket.uid, ['username', 'userslug'], next);
},
function (_userData, next) {
userData = _userData;
notifications.create({
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
nid: 'follow:' + data.uid + ':uid:' + socket.uid,
from: socket.uid,
9 years ago
path: '/user/' + userData.userslug,
mergeId: 'notifications:user_started_following_you'
}, next);
},
function (notification, next) {
if (!notification) {
return next();
}
notification.user = userData;
notifications.push(notification, [data.uid], next);
}
], callback);
};
11 years ago
SocketUser.unfollow = function(socket, data, callback) {
if (socket.uid && data) {
toggleFollow('unfollow', socket.uid, data.uid, callback);
}
};
function toggleFollow(method, uid, theiruid, callback) {
user[method](uid, theiruid, function(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:user.' + method, {
fromUid: uid,
toUid: theiruid
});
11 years ago
callback();
});
}
11 years ago
SocketUser.saveSettings = function(socket, data, callback) {
if (!socket.uid || !data) {
return callback(new Error('[[error:invalid-data]]'));
}
9 years ago
async.waterfall([
function(next) {
if (socket.uid === parseInt(data.uid, 10)) {
return next(null, true);
}
user.isAdminOrGlobalMod(socket.uid, next);
},
9 years ago
function(allowed, next) {
if (!allowed) {
return next(new Error('[[error:no-privileges]]'));
}
user.saveSettings(data.uid, data.settings, next);
}
9 years ago
], callback);
};
SocketUser.setTopicSort = function(socket, sort, callback) {
if (socket.uid) {
user.setSetting(socket.uid, 'topicPostSort', sort, callback);
}
};
10 years ago
SocketUser.setCategorySort = function(socket, sort, callback) {
if (socket.uid) {
user.setSetting(socket.uid, 'categoryTopicSort', sort, callback);
}
};
11 years ago
SocketUser.getUnreadCount = function(socket, data, callback) {
if (!socket.uid) {
return callback(null, 0);
}
11 years ago
topics.getTotalUnread(socket.uid, callback);
};
SocketUser.getUnreadChatCount = function(socket, data, callback) {
if (!socket.uid) {
return callback(null, 0);
}
messaging.getUnreadCount(socket.uid, callback);
};
SocketUser.getUnreadCounts = function(socket, data, callback) {
if (!socket.uid) {
return callback(null, {});
}
async.parallel({
unreadTopicCount: async.apply(topics.getTotalUnread, socket.uid),
unreadChatCount: async.apply(messaging.getUnreadCount, socket.uid),
unreadNotificationCount: async.apply(user.notifications.getUnreadCount, socket.uid)
}, callback);
};
11 years ago
SocketUser.loadMore = function(socket, data, callback) {
if (!data || !data.set || parseInt(data.after, 10) < 0) {
return callback(new Error('[[error:invalid-data]]'));
}
11 years ago
if (!socket.uid && !!parseInt(meta.config.privateUserInfo, 10)) {
return callback(new Error('[[error:no-privileges]]'));
11 years ago
}
var start = parseInt(data.after, 10);
var stop = start + 19;
11 years ago
async.parallel({
isAdmin: function(next) {
user.isAdministrator(socket.uid, next);
},
isGlobalMod: function(next) {
user.isGlobalModerator(socket.uid, next);
},
users: function(next) {
user.getUsersFromSet(data.set, socket.uid, start, stop, next);
}
}, function(err, results) {
10 years ago
if (err) {
return callback(err);
}
if (data.set === 'users:banned' && !results.isAdmin && !results.isGlobalMod) {
return callback(new Error('[[error:no-privileges]]'));
}
if (!results.isAdmin && data.set === 'users:online') {
results.users = results.users.filter(function(user) {
return user.status !== 'offline';
});
}
var result = {
users: results.users,
nextStart: stop + 1,
};
result['route_' + data.set] = true;
callback(null, result);
});
};
10 years ago
SocketUser.invite = function(socket, email, callback) {
if (!email || !socket.uid) {
return callback(new Error('[[error:invalid-data]]'));
10 years ago
}
var registrationType = meta.config.registrationType;
if (registrationType !== 'invite-only' && registrationType !== 'admin-invite-only') {
10 years ago
return callback(new Error('[[error:forum-not-invite-only]]'));
}
var max = meta.config.maximumInvites;
user.isAdministrator(socket.uid, function(err, admin) {
if (err) {
return callback(err);
}
if (registrationType === 'admin-invite-only' && !admin) {
return callback(new Error('[[error:no-privileges]]'));
}
if (max) {
async.waterfall([
function(next) {
user.getInvitesNumber(socket.uid, next);
},
function(invites, next) {
if (!admin && invites > max) {
return next(new Error('[[error:invite-maximum-met, ' + invites + ', ' + max + ']]'));
}
next();
},
function(next) {
user.sendInvitationEmail(socket.uid, email, next);
}
], callback);
} else {
user.sendInvitationEmail(socket.uid, email, callback);
}
});
10 years ago
};
SocketUser.getUserByUID = function(socket, uid, callback) {
apiController.getUserDataByUID(socket.uid, uid, callback);
};
SocketUser.getUserByUsername = function(socket, username, callback) {
apiController.getUserDataByUsername(socket.uid, username, callback);
};
SocketUser.getUserByEmail = function(socket, email, callback) {
apiController.getUserDataByEmail(socket.uid, email, callback);
};
module.exports = SocketUser;