-
Notifications
You must be signed in to change notification settings - Fork 5
/
broadcast.js
129 lines (119 loc) · 4.36 KB
/
broadcast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
const logger = require('winston');
const contentful = require('../contentful');
const helpers = require('../helpers');
/**
* @return {Array}
*/
function getContentTypes() {
const configs = Object.values(helpers.contentfulEntry.getContentTypeConfigs());
return configs.filter(config => config.broadcastable).map(config => config.type);
}
/**
* Fetches broadcast Contentful entries and parses them as broadcasts.
*
* @param {Object} query
* @return {Promise}
*/
async function fetch(query = {}) {
const contentfulRes = await contentful
.fetchByContentTypes(module.exports.getContentTypes(), query);
const broadcasts = await Promise
.all(contentfulRes.data.map(module.exports.parseBroadcastFromContentfulEntry));
return Object.assign(contentfulRes, { data: broadcasts });
}
/**
* Fetches a Contentful entry and parses as a broadcast.
*
* @param {String} broadcastId
* @return {Promise}
*/
function fetchById(broadcastId) {
return contentful.fetchByContentfulId(broadcastId)
.then(module.exports.parseBroadcastFromContentfulEntry)
.then(broadcast => helpers.cache.broadcasts.set(broadcastId, broadcast));
}
/**
* @param {String} broadcastId
* @param {Boolean} resetCache
* @return {Promise}
*/
function getById(broadcastId, resetCache = false) {
if (resetCache === true) {
logger.debug('Broadcast cache reset', { broadcastId });
return module.exports.fetchById(broadcastId);
}
return helpers.cache.broadcasts.get(broadcastId)
.then((data) => {
if (data) {
logger.debug('Broadcast cache hit', { broadcastId });
return data;
}
logger.debug('Broadcast cache miss', { broadcastId });
return module.exports.fetchById(broadcastId);
});
}
/**
* @param {Object} contentfulEntry
* @return {Promise}
*/
async function parseBroadcastFromContentfulEntry(contentfulEntry) {
const data = helpers.contentfulEntry.getSummaryFromContentfulEntry(contentfulEntry);
// A nice to have TODO would be migrating all legacy broadcast entries into their new respective
// type to avoid this check, but it likely is not possible to change type of an existing entry.
if (helpers.contentfulEntry.isLegacyBroadcast(contentfulEntry)) {
return Object.assign(data, module.exports
.getLegacyBroadcastDataFromContentfulEntry(contentfulEntry));
}
const [message, templates] = await Promise.all([
// Parse the outbound broadcast message to send.
helpers.contentfulEntry
.getMessageTemplate(contentfulEntry, data.type),
// Parse topic templates if they exist.
helpers.contentfulEntry.getTopicTemplates(contentfulEntry),
]);
return Object.assign(data, { message, templates });
}
/**
* The 'broadcast' content type was used to send various types of broadcasts, which will soon be
* deprecated by new content types per broadcast type.
*
* @param {Object} contentfulEntry
* @return {Object}
*/
function getLegacyBroadcastDataFromContentfulEntry(contentfulEntry) {
const data = {
message: {
text: contentfulEntry.fields.message,
attachments: contentful.getAttachmentsFromContentfulEntry(contentfulEntry),
},
};
// This content type was used to send all types of broadcasts and has since been deprecated.
const hardcodedTopic = contentfulEntry.fields.topic;
if (hardcodedTopic) {
// Another note: the new broadcast content types will reference a topic Contentful entry, not
// a campaign Contentful entry -- eventually topic will be returned as a nested object.
data.campaignId = null;
// TODO: Return this as an object once Conversations is updated to check if topic is an object
// first before checking it as a string. This will be removed when we're no longer dependent on
// the legacy broadcast content type.
data.topic = hardcodedTopic;
data.message.template = 'rivescript';
} else {
const campaignConfigEntry = contentfulEntry.fields.campaign;
data.campaignId = Number(contentful.getCampaignIdFromContentfulEntry(campaignConfigEntry));
data.topic = null;
// Ancient legacy broadcasts didn't require the template field, because at the time, we only
// supported askSignup broadcasts.
data.message.template = contentfulEntry.fields.template || 'askSignup';
}
return data;
}
module.exports = {
fetch,
fetchById,
getById,
getContentTypes,
getLegacyBroadcastDataFromContentfulEntry,
parseBroadcastFromContentfulEntry,
};