'use strict' // external modules const crypto = require('crypto') const Chance = require('chance') const config = require('./config') // core exports.generateAvatar = function (name) { // use darker colors for better contrast const color = new Chance(name).color({ max_red: 150, max_green: 150, max_blue: 150 }) const letter = name.substring(0, 1).toUpperCase() let svg = '' svg += '' svg += '' svg += '' svg += '' svg += '' + letter + '' svg += '' svg += '' svg += '' return svg } exports.generateAvatarURL = function (name, email = '', big = true) { let photo if (typeof email !== 'string') { email = '' + name + '@example.com' } name = encodeURIComponent(name) const hash = crypto.createHash('md5') hash.update(email.toLowerCase()) const hexDigest = hash.digest('hex') if (email !== '' && config.allowGravatar) { photo = `https://cdn.libravatar.org/avatar/${hexDigest}?default=identicon` if (big) { photo += '&s=400' } else { photo += '&s=96' } } else { photo = config.serverURL + '/user/' + (name || email.substring(0, email.lastIndexOf('@')) || hexDigest) + '/avatar.svg' } return photo }