UNPKG

react-cimpress-comment

Version:

Visualizes comment(s) for a particular platform resource

62 lines (49 loc) 2.62 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPrincipalMemoized = exports.fetchMatchingMentions = undefined; var _coamClient = require('coam-client'); var _debouncePromise = require('debounce-promise'); var _debouncePromise2 = _interopRequireDefault(_debouncePromise); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var mentionsUserCache = {}; var _fetchMatchingMentions = function _fetchMatchingMentions(accessToken, query) { return (0, _coamClient.findPrincipals)(accessToken, query).then(function (principals) { return principals.reduce(function (result, p) { var profile = p.profiles[0] || {}; if (!p.canonical_principal.endsWith('@clients')) { result.push({ id: profile.user_id || p.canonical_principal, display: profile.name || profile.email || p.canonical_principal, email: profile.email ? profile.email || p.canonical_principal : null }); } return result; }, []); }).catch(function (err) { return Promise.reject(new Error('Unable to fetch principals for query: ' + query)); }); }; var fetchMatchingMentions = (0, _debouncePromise2.default)(_fetchMatchingMentions, 300); var getPrincipalMemoized = function getPrincipalMemoized(accessToken, userId) { if (mentionsUserCache[userId]) { return mentionsUserCache[userId]; } var promiseToGetUser = (0, _coamClient.getPrincipal)(accessToken, userId).then(function (responseJson) { // At time of writing, users invited to the platform have no "profile.name" field // and instead have their name information stored in user metadata. This behaviour // is filed in COAM as a bug and the field may eventually be backpopulated. // Backpopulating it here in the meantime. if (responseJson && responseJson.profile && !responseJson.profile.name && responseJson.profile.user_metadata && (responseJson.profile.user_metadata.first_name || responseJson.profile.user_metadata.last_name)) { responseJson.profile.name = responseJson.profile.user_metadata.first_name + ' ' + responseJson.profile.user_metadata.last_name; } return responseJson; }).catch(function (err) { mentionsUserCache[userId] = null; throw err; }); mentionsUserCache[userId] = promiseToGetUser; return promiseToGetUser; }; exports.fetchMatchingMentions = fetchMatchingMentions; exports.getPrincipalMemoized = getPrincipalMemoized;