all files / src/socket.io/user/ picture.js

19.61% Statements 10/51
0% Branches 0/36
6.25% Functions 1/16
19.61% Lines 10/51
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                                                                                                                                                                                                                                 
'use strict';
 
var async = require('async');
var winston = require('winston');
var path = require('path');
 
var user = require('../../user');
var plugins = require('../../plugins');
 
module.exports = function(SocketUser) {
 
	SocketUser.changePicture = function(socket, data, callback) {
		if (!socket.uid) {
			return callback('[[error:invalid-uid]]');
		}
 
		if (!data) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		var type = data.type;
 
		async.waterfall([
			function (next) {
				user.isAdminOrSelf(socket.uid, data.uid, next);
			},
			function (next) {
				switch(type) {
					case 'default':
						next(null, '');
						break;
					case 'uploaded':
						user.getUserField(data.uid, 'uploadedpicture', next);
						break;
					default:
						plugins.fireHook('filter:user.getPicture', {
							uid: socket.uid,
							type: type,
							picture: undefined
						}, function(err, returnData) {
							next(null, returnData.picture || '');
						});
						break;
				}
			},
			function (picture, next) {
				user.setUserField(data.uid, 'picture', picture, next);
			}
		], callback);
	};
 
	SocketUser.uploadProfileImageFromUrl = function(socket, data, callback) {
		if (!socket.uid || !data.url || !data.uid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		user.isAdminOrSelf(socket.uid, data.uid, function(err) {
			if (err) {
				return callback(err);
			}
			user.uploadFromUrl(data.uid, data.url, function(err, uploadedImage) {
				callback(err, uploadedImage ? uploadedImage.url : null);
			});
		});
	};
 
	SocketUser.removeUploadedPicture = function(socket, data, callback) {
		if (!socket.uid || !data.uid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		async.waterfall([
			function (next) {
				user.isAdminOrSelf(socket.uid, data.uid, next);
			},
			function (next) {
				user.getUserFields(data.uid, ['uploadedpicture', 'picture'], next);
			},
			function(userData, next) {
				if (!userData.uploadedpicture.startsWith('http')) {
					require('fs').unlink(path.join(__dirname, '../../../public', userData.uploadedpicture), function(err) {
						if (err) {
							winston.error(err);
						}
					});
				}
 
				user.setUserFields(data.uid, {
					uploadedpicture: '',
					picture: userData.uploadedpicture === userData.picture ? '' : userData.picture	// if current picture is uploaded picture, reset to user icon
				}, next);
			}
		], callback);
	};
 
	SocketUser.getProfilePictures = function(socket, data, callback) {
		if (!data || !data.uid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		async.parallel({
			list: async.apply(plugins.fireHook, 'filter:user.listPictures', {
				uid: data.uid,
				pictures: []
			}),
			uploaded: async.apply(user.getUserField, data.uid, 'uploadedpicture')
		}, function(err, data) {
			if (err) {
				return callback(err);
			}
 
			if (data.uploaded) {
				data.list.pictures.push({
					type: 'uploaded',
					url: data.uploaded,
					text: '[[user:uploaded_picture]]'
				});
			}
 
			callback(null, data.list.pictures);
		})
	};
};