import { Source } from './source'; import { AvatarSource } from './avatar-source.enum'; function isRetina() { var mediaQuery; if (typeof window !== "undefined" && window !== null) { mediaQuery = "(-webkit-min-device-pixel-ratio: 1.25), (min--moz-device-pixel-ratio: 1.25), (-o-min-device-pixel-ratio: 5/4), (min-resolution: 1.25dppx)"; if (window.devicePixelRatio > 1.25) { return true; } if (window.matchMedia && window.matchMedia(mediaQuery).matches) { return true; } } return false; } function hashStr(str) { return str .split("") .reduce( (prevHash, currVal) => ((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0, 0 ); } /** * Gravatar source implementation. * Fetch avatar source based on gravatar email */ export class Gravatar implements Source { readonly sourceType: AvatarSource = AvatarSource.GRAVATAR; public sourceId: string; constructor(public value: string) { this.sourceId = value.match('^[a-f0-9]{32}$') ? value : hashStr(value).toString(); } public getAvatar(size: number): string { const avatarSize = isRetina() ? size * 2 : size; return `https://secure.gravatar.com/avatar/${ this.sourceId }?s=${avatarSize}&d=404`; } }