Skip to content

Commit

Permalink
Merge branch 'master' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronschachter committed Oct 1, 2018
2 parents 6a45ece + 059d718 commit bc9db65
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 74 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ STATHAT_EZ_KEY=puppet.sloth@dosomething.org
##

GAMBIT_TIMEOUT_NUM_SECONDS=20
# Time To Live in seconds used when caching a campaign in the Redis DB

# Time To Live in seconds used when caching in Redis DB
GAMBIT_CAMPAIGNS_CACHE_TTL=3600
GAMBIT_PARSED_CONTENTFUL_ENTRIES_CACHE_TTL=3600

# See: https://github.com/winstonjs/winston#logging-levels
LOGGING_LEVEL=info
Expand Down
18 changes: 3 additions & 15 deletions config/lib/helpers/cache.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
'use strict';

module.exports = {
broadcasts: {
name: 'broadcasts',
ttl: parseInt(process.env.GAMBIT_BROADCASTS_CACHE_TTL, 10) || 3600,
},
campaignConfigs: {
name: 'campaignConfigs',
ttl: parseInt(process.env.GAMBIT_CAMPAIGN_CONFIGS_CACHE_TTL, 10) || 3600,
},
campaigns: {
name: 'campaigns',
ttl: parseInt(process.env.GAMBIT_CAMPAIGNS_CACHE_TTL, 10) || 3600,
},
defaultTopicTriggers: {
name: 'defaultTopicTriggers',
ttl: parseInt(process.env.GAMBIT_DEFAULT_TOPIC_TRIGGERS_CACHE_TTL, 10) || 3600,
},
topics: {
name: 'topics',
ttl: parseInt(process.env.GAMBIT_TOPICS_CACHE_TTL, 10) || 3600,
parsedContentfulEntries: {
name: 'parsedContentfulEntries',
ttl: parseInt(process.env.GAMBIT_PARSED_CONTENTFUL_ENTRIES_CACHE_TTL, 10) || 3600,
},
};
9 changes: 9 additions & 0 deletions config/lib/helpers/contentfulEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ module.exports = {
type: 'askSubscriptionStatus',
broadcastable: true,
},
askVotingPlanStatus: {
type: 'askVotingPlanStatus',
broadcastable: true,
templates: [
'invalidVotingPlanStatus',
'saidVoting',
'saidNotVoting',
],
},
askYesNo: {
type: 'askYesNo',
broadcastable: true,
Expand Down
2 changes: 1 addition & 1 deletion config/lib/helpers/defaultTopicTrigger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

module.exports = {
allDefaultTopicTriggersCacheKey: 'all',
allDefaultTopicTriggersCacheKey: 'allDefaultTopicTriggers',
};
2 changes: 1 addition & 1 deletion config/lib/helpers/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const photoPostDefaultText = {
};

module.exports = {
allTopicsCacheKey: 'all',
allTopicsCacheKey: 'allTopics',
defaultPostType: 'photo',
/*
* Maps each content type with a map of templateNames and its corresponding field name and
Expand Down
32 changes: 10 additions & 22 deletions lib/helpers/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@ const redisClient = require('../../config/redis')();
const config = require('../../config/lib/helpers/cache');

const redisEngine = new RedisEngine(redisClient);
const broadcastsCache = new Cacheman(config.broadcasts.name, {
ttl: config.broadcasts.ttl,
engine: redisEngine,
});
const campaignsCache = new Cacheman(config.campaigns.name, {
ttl: config.campaigns.ttl,
engine: redisEngine,
});
const campaignConfigsCache = new Cacheman(config.campaignConfigs.name, {
ttl: config.campaignConfigs.ttl,
engine: redisEngine,
});
const defaultTopicTriggersCache = new Cacheman(config.defaultTopicTriggers.name, {
ttl: config.defaultTopicTriggers.ttl,
engine: redisEngine,
});
const topicsCache = new Cacheman(config.topics.name, {
ttl: config.topics.ttl,
const parsedContentfulEntriesCache = new Cacheman(config.parsedContentfulEntries.name, {
ttl: config.parsedContentfulEntries.ttl,
engine: redisEngine,
});

Expand All @@ -34,18 +22,18 @@ function parseSetCacheResponse(res) {
module.exports = {
broadcasts: {
get: function get(id) {
return broadcastsCache.get(id);
return parsedContentfulEntriesCache.get(id);
},
set: function set(id, data) {
return broadcastsCache.set(id, data).then(res => parseSetCacheResponse(res));
return parsedContentfulEntriesCache.set(id, data).then(res => parseSetCacheResponse(res));
},
},
campaignConfigs: {
get: function get(id) {
return campaignConfigsCache.get(`${id}`);
return parsedContentfulEntriesCache.get(`${id}`);
},
set: function set(id, data) {
return campaignConfigsCache.set(`${id}`, data).then(res => parseSetCacheResponse(res));
return parsedContentfulEntriesCache.set(`${id}`, data).then(res => parseSetCacheResponse(res));
},
},
campaigns: {
Expand All @@ -58,18 +46,18 @@ module.exports = {
},
defaultTopicTriggers: {
get: function get(id) {
return defaultTopicTriggersCache.get(id);
return parsedContentfulEntriesCache.get(id);
},
set: function set(id, data) {
return defaultTopicTriggersCache.set(id, data).then(res => parseSetCacheResponse(res));
return parsedContentfulEntriesCache.set(id, data).then(res => parseSetCacheResponse(res));
},
},
topics: {
get: function get(id) {
return topicsCache.get(id);
return parsedContentfulEntriesCache.get(id);
},
set: function set(id, data) {
return topicsCache.set(id, data).then(res => parseSetCacheResponse(res));
return parsedContentfulEntriesCache.set(id, data).then(res => parseSetCacheResponse(res));
},
},
};
41 changes: 19 additions & 22 deletions lib/helpers/contentfulEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ function getSummaryFromContentfulEntry(contentfulEntry) {
* @return {Promise}
*/
async function getMessageTemplateFromContentfulEntryAndTemplateName(contentfulEntry, templateName) {
if (!contentfulEntry || !contentfulEntry.fields) {
return {};
}
const topicEntry = contentfulEntry.fields.topic;
const topic = topicEntry ? await helpers.topic
.getById(contentful.getContentfulIdFromContentfulEntry(topicEntry)) : {};
.getById(contentful.getContentfulIdFromContentfulEntry(contentfulEntry.fields.topic)) : {};
return {
text: contentful.getTextFromMessage(contentfulEntry),
attachments: contentful.getAttachmentsFromContentfulEntry(contentfulEntry),
Expand All @@ -120,39 +123,33 @@ async function getMessageTemplateFromContentfulEntryAndTemplateName(contentfulEn
}

/**
* @param {Object}
* @param {String}
* @param {Object} contentfulEntry
* @return {Promise}
*/
async function getTopicTemplates(contentfulEntry) {
const contentType = contentful.getContentTypeFromContentfulEntry(contentfulEntry);
const templateFieldNames = config.contentTypes[contentType].templates;
const result = {};
const templates = {};

if (!contentfulEntry || !templateFieldNames) {
return result;
return templates;
}

templateFieldNames.forEach((fieldName) => {
result[fieldName] = {
text: contentfulEntry.fields[fieldName],
topic: {},
const fields = contentfulEntry.fields;
/* eslint-disable */
// TODO: Disabling our linter probably isn't best way to handle this, find topics in parallel
// instead of serially per https://github.com/DoSomething/gambit-campaigns/pull/1088/files#r220969795
for (const fieldName of templateFieldNames) {
const templateTopicEntry = fields[`${fieldName}Topic`];
templates[fieldName] = {
text: fields[fieldName],
topic: templateTopicEntry ? await helpers.topic
.getById(contentful.getContentfulIdFromContentfulEntry(templateTopicEntry)) : {},
};
});

// The saidYes template should include the topic saved to the saidYesTopic field.
if (result.saidYes && contentfulEntry.fields.saidYesTopic) {
result.saidYes.topic = await helpers.topic
.getById(contentful.getContentfulIdFromContentfulEntry(contentfulEntry.fields.saidYesTopic));
}
/* eslint-enable */

// The saidNo template should include the topic saved to the saidNoTopic field.
if (result.saidNo && contentfulEntry.fields.saidNoTopic) {
result.saidNo.topic = await helpers.topic
.getById(contentful.getContentfulIdFromContentfulEntry(contentfulEntry.fields.saidNoTopic));
}

return result;
return templates;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gambit-campaigns",
"version": "5.14.0",
"version": "5.14.1",
"description": "The DoSomething.org chatbot service for campaigns and their activity.",
"license": "MIT",
"repository": {
Expand Down
Loading

0 comments on commit bc9db65

Please sign in to comment.