| 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 |
2×
2×
2×
2×
2×
2×
18×
17×
13×
2×
2×
7×
7×
3×
3×
4×
2×
2×
8×
8×
8×
8×
8×
8×
8×
8×
8×
8×
2×
2×
8×
8×
8×
8×
1×
8×
8×
2×
2×
2×
2×
1×
1×
6×
8×
8×
2×
8×
5×
5×
3×
2×
1×
1×
11×
11×
13×
9×
9×
5×
9×
6×
3×
3×
4×
13×
2×
9×
9×
9×
5×
1×
1×
4×
1×
12×
1×
11×
2×
4×
3×
3×
2×
2×
1×
1×
3×
1×
8×
8×
8×
8×
1×
12×
15×
1×
14×
14×
| 'use strict';
const R = require('ramda');
const config = require('config');
let getLastUpdated = require("./getLastUpdated");
let tz = require('timezone/loaded');
const emptyOrNull = R.either(R.isNil, R.isEmpty);
const resolveTags = R.compose(
R.pluck('name'),
R.map(R.last),
R.filter(R.pathEq([0, 'type'], 'tag')),
R.defaultTo([])
);
const removeTag = tag => R.compose(R.reject(R.equals(tag)), R.defaultTo([]));
const prependTag = tag => R.compose(R.prepend(tag), R.defaultTo([]));
const evolveTags = primaryTag => R.compose(
R.reject(R.isNil),
prependTag(primaryTag),
removeTag(primaryTag),
resolveTags
);
const resolveSearchTags = R.compose(
R.map(R.last),
R.filter(R.pathEq([0, 'type'], 'tag')),
R.defaultTo([])
);
const formatSearchTags = tag => {
const tag_id = R.path(['tag_id'], tag);
if (tag_id) {
const id = tag_id.split(".")[1];
return R.assoc('tag_id',parseInt(id), tag);
} else {
return tag;
}
};
const evolveSearchTags = R.compose(
R.map(formatSearchTags),
resolveSearchTags
);
module.exports = arrayOfObjects => {
const articleTuple = R.head(arrayOfObjects);
let article = articleTuple[1];
const leadMedia = R.path(["data", "attributes", "leadMedia"], article);
const tail = R.pipe(
// put any tail transformations here
formatGalleries(config)
)(R.reject(R.isNil, R.tail(arrayOfObjects)));
const lastModifiedDate = R.path(["data", "attributes", "lastModifiedDate"], article) != null
? tz(getLastUpdated(article.data.attributes.lastModifiedDate, tail), '%Y-%m-%dT%H:%M:%S.%3N%^z', 'UTC')
: void 0;
const ownerUrl = emptyOrNull(R.path(["data", "links", "ownerUrl"], article)) ? void 0 : article.data.links.ownerUrl;
const socialHeadline = emptyOrNull(R.path(["data", "attributes", "socialHeadline"], article)) ? null : article.data.attributes.socialHeadline;
// Inline images must be formated at last moment to preserve leadmedia crop behaviour
let included = createIncludedList(article, formatInlineImages(tail));
const urlTransform = R.curry((section, filename, id) => {
return `${section.url}${filename}/embedded-webview/${section.publication.name}-${id}`;
});
// super hacky solution to solve the HTML Embed problem:
// we needed in scope section, publication and filename of the article
// to build up the embed url and could not resolve it from the relational model
// Resolving these dependencies from the relational model
// implies a large number of joins, which leads to a very slow query.
included = R.map(
item => {
Iif(R.path(['data','attributes','subType'], item) === 'interactiveHTML'){
return R.assocPath(
['data', 'links','url'],
urlTransform(articleTuple[0].section, articleTuple[0].file_name, item.data.id),
item
);
}
return item;
}
, included);
const primaryTag = R.path(['data', 'attributes', 'primaryTag'], article);
const evolved = R.evolve({
data: {
attributes: R.merge(R.__, {
socialHeadline,
lastModifiedDate,
checksum: lastModifiedDate, // to be removed
leadMedia: resolveLeadMedia(leadMedia, tail),
authors: deduplicateAuthors(tail.filter(x => x[0].type === 'author').map(R.last)),
webview: isWebviewArticle(article, included),
tags: evolveTags(primaryTag)(tail),
searchTags: evolveSearchTags(tail)
}),
links: R.merge(R.__, {ownerUrl})
},
included: R.always(included)
}, article);
return evolved;
};
function resolveLeadMedia(leadMedia, tail) {
let processedLeadMedia;
if (!R.isNil(leadMedia) && leadMedia.noContent === true) {
const staticContent = leadMedia.static_content || {};
const findLeadMedia = R.find(tuple => tuple[0].id === leadMedia.id, tail);
processedLeadMedia = findLeadMedia ? findLeadMedia[1] : null;
if (!R.isNil(processedLeadMedia) && !R.isNil(R.path(['data', 'attributes'], processedLeadMedia))) {
const attributes = R.merge(processedLeadMedia.data.attributes, staticContent);
processedLeadMedia = R.assocPath(['data', 'attributes'], attributes, processedLeadMedia);
}
} else {
processedLeadMedia = leadMedia;
}
Iif (!R.isNil(processedLeadMedia) && R.path(['data', 'type'], processedLeadMedia) === 'video') {
return R.assocPath(['data', 'attributes', 'leadMedia'], resolveLeadMedia(R.defaultTo(null, processedLeadMedia.data.attributes.leadMedia), tail), processedLeadMedia);
}
return R.isNil(processedLeadMedia) ? processedLeadMedia : cropLeadImages(config, processedLeadMedia);
}
const cropLeadImages = R.curry(function(config, leadMedia) {
if (R.path(['data', 'type'], leadMedia) === 'image') {
const filteredLeadMedia = R.evolve({data: {attributes: {alternates: filterAlternates('leadMedia')}}}, leadMedia);
return R.evolve({
data: {
attributes: R.flip(R.merge)(config.leadImageDimensions),
links: {
url: R.replace(/ALTERNATES\/s\d{3}b?/, `ALTERNATES/${config.leadImageDimensions.code}`),
thumbnailUrl: R.replace(/ALTERNATES\/s\d{3}b?/, `ALTERNATES/${config.thumbnailDimensions.code}`)
}
}
}, filteredLeadMedia);
} else if (!R.isNil(R.path(['data', 'attributes', 'leadMedia'], leadMedia))) {
return R.evolve({
data: {
attributes: {
leadMedia: cropLeadImages(config)
}
}
}, leadMedia);
}
return leadMedia;
});
function createIncludedList(article, tail) {
Iif (R.path(['data', 'type'], article) === 'live-event') {
article.included = R.append({
data: {
type: 'liveEventEntries',
links: {
url: article.data.links.liveEventEntriesUrl
}
}
}, article.included);
}
return article.included.map(article_content => {
if (article_content.noContent === true) {
const staticContent = article_content.static_content || {};
const tupleToSub = R.find(tuple => {
return tuple[0].id === article_content.id;
}, tail);
if(!tupleToSub) {
return null;
}
Eif (!R.isNil(R.path(['data', 'attributes'], tupleToSub[1]))) {
return R.assocPath(['data', 'attributes'], R.merge(tupleToSub[1].data.attributes, staticContent), tupleToSub[1]);
}
return tupleToSub[1];
}
return article_content;
}).filter(x => !R.isNil(x));
}
const formatGalleries = R.curry(function(config, tail) {
return R.map(tuple => {
const type = tuple[0].type;
if (type === 'gallery') {
return [tuple[0], R.evolve({
data: {
attributes: {
leadMedia: cropLeadImages(config)
}
},
included: R.map(galleryImage => {
const filteredImage = R.evolve({data: {attributes: {alternates: filterAlternates('gallery')}}}, galleryImage);
return R.evolve({
data: {
attributes: R.flip(R.merge)(config.galleryImageDimensions),
links: {
url: R.replace(/ALTERNATES\/s\d{3}b?/, `ALTERNATES/${config.galleryImageDimensions.code}`)
}
}
}, filteredImage);
})
}, tuple[1])];
}
return tuple;
}, tail);
});
function isWebviewArticle(article, included) {
if (R.path(['data', 'attributes', 'webview'], article) === true) {
return true;
}
return R.any(R.anyPass([
R.pathEq(['data', 'type'], 'quiz'),
R.pathEq(['data', 'type'], 'interactiveHtml')
]), included);
}
const filterAlternates = R.curry((context, alternates) => {
if (R.isNil(alternates)) return null;
let crops = [];
switch(context) {
case 'leadMedia':
crops = R.filter(R.propSatisfies(R.test(/^.+[^b]$/), 'crop'), alternates);
break;
case 'gallery':
crops = R.filter(R.propSatisfies(R.test(/^s1227b$/), 'crop'), alternates);
break;
case 'image':
crops = R.filter(R.propSatisfies(R.test(/^.+b$/), 'crop'), alternates);
break;
default:
return alternates;
}
return R.isEmpty(crops) ? null : crops;
});
function formatInlineImages(tail) {
return R.map(tuple => {
const type = tuple[0].type;
Iif (type === 'image') {
return [tuple[0], R.evolve({
data: {
attributes: {
alternates: filterAlternates('image')
}
}
}, tuple[1])];
}
return tuple;
}, tail);
}
function deduplicateAuthors(authorsList) {
return R.reduce((mem, author) => {
if (R.isNil(author.id) && R.filter(R.propEq('name', author.name), authorsList).length > 1) {
return mem;
}
mem.push(author);
return mem;
}, [], authorsList);
} |