You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.5 KiB

  1. 'use strict';
  2. import * as fs from 'fs';
  3. import { Uri } from 'vscode';
  4. import { GravatarDefaultStyle } from './config';
  5. import { Strings } from './system';
  6. import { ContactPresenceStatus } from './vsls/vsls';
  7. import { Container } from './container';
  8. const gravatarCache = new Map<string, Uri>();
  9. const missingGravatarHash = '00000000000000000000000000000000';
  10. const presenceCache = new Map<ContactPresenceStatus, string>();
  11. export function clearGravatarCache() {
  12. gravatarCache.clear();
  13. }
  14. export function getGravatarUri(email: string | undefined, fallback: GravatarDefaultStyle, size: number = 16): Uri {
  15. const hash =
  16. email != null && email.length !== 0 ? Strings.md5(email.trim().toLowerCase(), 'hex') : missingGravatarHash;
  17. const key = `${hash}:${size}`;
  18. let gravatar = gravatarCache.get(key);
  19. if (gravatar !== undefined) return gravatar;
  20. gravatar = Uri.parse(`https://www.gravatar.com/avatar/${hash}.jpg?s=${size}&d=${fallback}`);
  21. gravatarCache.set(key, gravatar);
  22. return gravatar;
  23. }
  24. export function getPresenceDataUri(status: ContactPresenceStatus) {
  25. let dataUri = presenceCache.get(status);
  26. if (dataUri === undefined) {
  27. const contents = fs
  28. .readFileSync(Container.context.asAbsolutePath(`images/dark/icon-presence-${status}.svg`))
  29. .toString('base64');
  30. dataUri = encodeURI(`data:image/svg+xml;base64,${contents}`);
  31. presenceCache.set(status, dataUri);
  32. }
  33. return dataUri;
  34. }