# all files / keystone/fields/types/email/ EmailType.js

100% Statements 37/37
82.61% Branches 19/23
100% Functions 5/5
100% Lines 37/37

    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
    931×
    1×
    1×
    1×
    1×

    1×
    2×
    2×
    2×
    2×

    1×

    1×

    1×
    5×
    5×
    1×

    4×

    1×
    16×
    16×
    16×
    13×

    16×

    1×

    1×
    8×
    8×
    4×

    4×

    1×
    3×
    3×
    3×

    3×
    3×

    3×

    1×
     var crypto = require('crypto');
    var FieldType = require('../Type');
    var TextType = require('../text/TextType');
    var util = require('util');
    var utils = require('keystone-utils');

    /**
     * Email FieldType Constructor
     * @extends Field
     * @api public
     */
    function email (list, path, options) {
    	this._nativeType = String;
    	this._underscoreMethods = ['gravatarUrl'];
    	this.typeDescription = 'email address';
    	email.super_.call(this, list, path, options);
    }
    util.inherits(email, FieldType);

    /* Inherit from TextType prototype */
    email.prototype.addFilterToQuery = TextType.prototype.addFilterToQuery;

    /**
     * Generate a gravatar image request url
     */
    email.prototype.gravatarUrl = function (item, size, defaultImage, rating) {
    	var value = item.get(this.path);
    	if (typeof value !== 'string') {
    		return '';
    	}
    	return [
    		// base url protocol-less for both http/https
    		'//www.gravatar.com/avatar/',
    		// md5 hash the trimmed lowercase email
    		crypto.createHash('md5').update(value.toLowerCase().trim()).digest('hex'),
    		// size of images ranging from 1 to 2048 pixels, square
    		'?s=' + (/^(?:[1-9][0-9]{0,2}|1[0-9]{3}|20[0-3][0-9]|204[0-8])$/.test(size) ? size : 80),
    		// default image url encoded href or one of the built in options: 404, mm, identicon, monsterid, wavatar, retro, blank
    		'&d=' + (defaultImage ? encodeURIComponent(defaultImage) : 'identicon'),
    		// rating, g, pg, r or x
    		'&r=' + (/^(?:g|pg|r|x)$/i.test(rating) ? rating.toLowerCase() : 'g'),
    	].join('');
    };

    /**
     * Asynchronously confirms that the provided email is valid
     */
    email.prototype.validateInput = function (data, callback) {
    	var input = this.getValueFromData(data);
    	var result = true;
    	if (input) {
    		result = utils.isEmail(input);
    	}
    	utils.defer(callback, result);
    };

    /**
     * Asynchronously confirms that required input is present
     */
    email.prototype.validateRequiredInput = TextType.prototype.validateRequiredInput;

    /**
     * Validates that a valid email has been provided in a data object
     *
     * Deprecated
     */
    email.prototype.inputIsValid = function (data, required, item) {
    	var value = this.getValueFromData(data);
    	if (value) {
    		return utils.isEmail(value);
    	} else {
    		return (!required || (item && item.get(this.path))) ? true : false;
    	}
    };

    /**
     * Updates the value for this field in the item from a data object
     * Ensures that the email address is lowercase
     */
    email.prototype.updateItem = function (item, data, callback) {
    	var newValue = this.getValueFromData(data);
    	Eif (typeof newValue === 'string') {
    		newValue = newValue.toLowerCase();
    	}
    	Eif (newValue !== undefined && newValue !== item.get(this.path)) {
    		item.set(this.path, newValue);
    	}
    	process.nextTick(callback);
    };

    /* Export Field Type */
    module.exports = email;