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.

154 lines
3.4 KiB
JavaScript

var user = require('../user'),
topics = require('../topics'),
SocketUser = {};
11 years ago
SocketUser.exists = function(socket, data, callback) {
if (data && data.username) {
user.exists(utils.slugify(data.username), function(exists) {
11 years ago
socket.emit('user.exists', {
exists: exists
});
});
}
};
11 years ago
SocketUser.count = function(socket, data, callback) {
user.count(callback);
};
11 years ago
SocketUser.emailExists = function(socket, data, callback) {
user.email.exists(data.email, callback);
};
// Password Reset
SocketUser.reset = {};
11 years ago
SocketUser.reset.send = function(socket, data, callback) {
user.reset.send(socket, data.email);
};
11 years ago
SocketUser.reset.valid = function(socket, data, callback) {
user.reset.validate(socket, data.code);
};
11 years ago
SocketUser.reset.commit = function(socket, data, callback) {
user.reset.commit(socket, data.code, data.password);
};
11 years ago
SocketUser.isOnline = function(socket, uid, callback) {
callback({
online: module.parent.exports.isUserOnline(uid),
uid: uid,
timestamp: Date.now()
});
};
11 years ago
SocketUser.changePassword = function(socket, data, callback) {
user.changePassword(socket.uid, data, callback);
};
11 years ago
SocketUser.updateProfile = function(socket, data, callback) {
user.updateProfile(socket.uid, data, callback);
};
11 years ago
SocketUser.changePicture = function(socket, data, callback) {
var type = data.type;
function updateHeader() {
11 years ago
user.getUserFields(socket.uid, ['picture'], function(err, fields) {
if (!err && fields) {
11 years ago
fields.uid = socket.uid;
socket.emit('meta.updateHeader', fields);
callback(true);
} else {
callback(false);
}
});
}
if (type === 'gravatar') {
11 years ago
user.getUserField(socket.uid, 'gravatarpicture', function(err, gravatar) {
user.setUserField(socket.uid, 'picture', gravatar);
updateHeader();
});
} else if (type === 'uploaded') {
11 years ago
user.getUserField(socket.uid, 'uploadedpicture', function(err, uploadedpicture) {
user.setUserField(socket.uid, 'picture', uploadedpicture);
updateHeader();
});
} else {
callback(false);
}
};
11 years ago
SocketUser.follow = function(socket, data, callback) {
if (socket.uid) {
user.follow(socket.uid, data.uid, callback);
}
};
11 years ago
SocketUser.unfollow = function(socket, data, callback) {
if (socket.uid) {
user.unfollow(socket.uid, data.uid, callback);
}
};
11 years ago
SocketUser.saveSettings = function(socket, data, callback) {
if (socket.uid) {
user.setUserFields(socket.uid, {
showemail: data.showemail
}, function(err, result) {
callback(err);
});
}
};
11 years ago
SocketUser.get_online_users = function(socket, data, callback) {
var returnData = [];
for (var i = 0; i < data.length; ++i) {
var uid = data[i];
11 years ago
if (module.parent.exports.isUserOnline(uid)) {
returnData.push(uid);
11 years ago
} else {
returnData.push(0);
11 years ago
}
}
callback(returnData);
};
11 years ago
SocketUser.getOnlineAnonCount = function(socket, data, callback) {
callback(module.parent.exports.getOnlineAnonCount());
};
11 years ago
SocketUser.getUnreadCount = function(socket, data, callback) {
topics.getUnreadTids(socket.uid, 0, 19, function(err, tids) {
callback(tids.length);
});
};
11 years ago
SocketUser.getActiveUsers = function(socket, data, callback) {
module.parent.exports.emitOnlineUserCount(callback);
};
11 years ago
SocketUser.loadMore = function(socket, data, callback) {
var start = data.after,
end = start + 19;
user.getUsers(data.set, start, end, function(err, data) {
if (err) {
winston.err(err);
} else {
callback({
users: data
});
}
});
};
/* Exports */
module.exports = SocketUser;