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.

168 lines
3.5 KiB
JavaScript

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