/* * Helper to load in all app plugins */ const loadPlugins = (app, lando) => Promise.resolve(app.plugins.registry) // Filter out .filter(plugin => _.has(plugin, 'app')) // LOADEM! .map(plugin => app.plugins.load(plugin, plugin.app, app, lando)) // Remove any naughty shit .map(plugin => _.pick(plugin.data, ['config', 'composeData', 'env', 'labels'])) // Merge minotaur .each(result => _.merge(app, result))
report(action = 'unknown', data = {}) { // Get Stuff const log = this.log; const id = this.id; // Attempt to sanitize merged data as much as possible const send = cleanseData(_.merge({}, this.data, data, {action, created: new Date().toJSON()})); // Log the attempt log.verbose('reporting %s action to', action, this.endpoints); log.debug('reported data', send); // Start the reporting chain return Promise.resolve(this.endpoints) // Filter out any inactive endpoints .filter(endpoint => endpoint.report) // Get the client and report .map(endpoint => { const agent = axios.create({baseURL: endpoint.url}); // Post the data return agent.post('/metrics/v2/' + id, send).catch(error => { const url = _.get(endpoint, 'url', 'unknown'); const status = _.get(error, 'response.status', 'unknown'); const reason = _.get(error, 'response.statusText', 'unknown'); const message = _.get(error, 'response.data.message', 'unknown'); log.debug('metrics post to %s failed with %s (%s) %s', url, status, reason, message); }); }); }
/* * Get list of sites */ getSites() { // Call to get user sites const pantheonUserSites = () => { const getSites = ['users', _.get(this.session, 'user_id'), 'memberships', 'sites']; return pantheonRequest(this.request, this.log, 'get', getSites, {params: {limit: MAX_SITES}}) .then(sites => _.map(sites, (site, id) => _.merge(site, site.site))); }; // Call to get org sites const pantheonOrgSites = () => { const getOrgs = ['users', _.get(this.session, 'user_id'), 'memberships', 'organizations']; return pantheonRequest(this.request, this.log, 'get', getOrgs) .map(org => { if (org.role !== 'unprivileged') { const getOrgsSites = ['organizations', org.id, 'memberships', 'sites']; return pantheonRequest(this.request, this.log, 'get', getOrgsSites, {params: {limit: MAX_SITES}}) .map(site => _.merge(site, site.site)); } }) .then(sites => _.flatten(sites)); }; // Run both requests return Promise.all([pantheonUserSites(), pantheonOrgSites()]) // Combine, cache and all the things .then(sites => _.compact(_.sortBy(_.uniqBy(_.flatten(sites), 'name'), 'name'))) // Filter out any BAAAAD BIZZZNIZZZ .filter(site => !site.frozen); }
removeFromList (author, listName, values, bypassPermissions) { return this.checkListChanges(author, listName, values, bypassPermissions) .then(() => this.roomState.removeFromList(listName, values)) .then(() => Promise.filter( values, val => this.hasRemoveChangedCurrentAccess(val, listName), { concurrency: asyncLimit })) }
async authenticate(accounts) { return Promise.filter(accounts, async acc => { try { await this.refreshAccountAuth(acc) } catch (error) { // TODO update Account status error. console.log('Problem refreshing account authentication:', error) return false } return true }) }
test('Promise.filter', function (t) { t.plan(4) twice(function () { var trans = ins.startTransaction() var arr = [resolved(1), resolved(2), resolved(3), resolved(4)] Promise.filter(arr, function (value) { return value > 2 }).then(function (result) { t.deepEqual(result, [3, 4]) t.strictEqual(ins.currentTransaction.id, trans.id) }) }) })
const fileSearch = async (files: Array<{path: string}>, language: string) => { // Caption uses iso6392B for language-codes, but thesubdb uses iso6391 const languageCode = convertLanguageCode(language); // Fetch available languages for files const subtitleReferences = files.map(async file => ({ languages: await SubDB.availLan(file.path), file })); // Only return files that are available in the given language return await Promise.all(subtitleReferences) .filter(({languages}) => languages.includes(languageCode)) .map(result => ({language: languageCode, file: result.file, source: "thesubdb"})); }
addToList (author, listName, values, bypassPermissions) { return this.checkListChanges(author, listName, values, bypassPermissions) .then(() => this.roomState.addToList( listName, values, this.listSizeLimit)) .then(() => Promise.filter( values, val => this.hasAddChangedCurrentAccess(val, listName), { concurrency: asyncLimit })) }
test('new Promise -> filter', function (t) { t.plan(4) twice(function () { var trans = ins.startTransaction() var arr = [resolved(1), resolved(2), resolved(3), resolved(4)] resolved(arr).filter(function (value) { return value > 2 }).then(function (result) { t.deepEqual(result, [3, 4]) t.strictEqual(ins.currentTransaction.id, trans.id) }) }) })