Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / serializers / rest / user-profile-strategy.js
blobf2f84e6d1066435aaf6458be511ee94337d45748
1 'use strict';
3 var _ = require('lodash');
4 var BackendMuxer = require('gitter-web-backend-muxer');
6 function UserProfileStrategy(/* options */) {
7 var user;
8 var profileResults;
10 this.preload = function(users) {
11 var length = users.size();
12 if (length === 0) return;
13 if (length !== 1) {
14 throw new Error('User profile serializer can only load a single profile at a time');
17 user = users.first();
18 var backendMuxer = new BackendMuxer(user);
19 return backendMuxer.findProfiles(profileResults).then(function(profiles) {
20 profileResults = profiles;
21 // cache the profiles so we can get them out later.
22 // (is this the best variable name?)
24 // A hash would probably we better for this
25 user.profiles = profiles;
26 });
29 this.map = function(_user) {
30 if (user !== _user) return;
32 var profile = {
33 id: user.id,
34 username: user.username,
35 displayName: user.displayName,
36 removed: user.state === 'REMOVED' || undefined, // isRemoved?
37 has_gitter_login: true // by definition
40 // Provider is just the one that matched and we prefer the avatar in the
41 // database over what's coming from the API so that it is easier to reuse
42 // gravatarVersion for github users.
43 _.extend(profile, _.omit(user.profiles[0], ['provider', 'gravatarImageUrl']));
45 if (user.gravatarVersion) {
46 // github
47 profile.gv = user.gravatarVersion;
48 } else {
49 // non-github
50 profile.gravatarImageUrl = user.gravatarImageUrl;
53 return profile;
57 UserProfileStrategy.prototype = {
58 name: 'UserProfileStrategy'
61 module.exports = UserProfileStrategy;