-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
197 lines (183 loc) · 6.34 KB
/
index.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
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
'use strict';
const request = require('request');
const maxRetries = 3;
const baseWaitMS = 500;
function retry (roomId, numTries, cb) {
setTimeout(function () {
getManifest(roomId, numTries + 1, cb);
}, Math.pow(2, numTries) * baseWaitMS * (0.9 + Math.random() * 0.1));
}
function getManifest (roomId, numTries, cb) {
request({
url: `https://www.stream.me/api-web/v1/chat/room/${roomId}`,
json: true
}, function (err, res, body) {
if (err) {
if (numTries < maxRetries) {
return retry(roomId, numTries, cb);
}
return cb(err);
}
if (res.statusCode !== 200) {
if (numTries < maxRetries) {
return retry(roomId, numTries, cb);
}
var errMessage = 'Unable to fetch parser manifest, status code: ' + res.statusCode;
if (body && body.reasons && body.reasons[0] && body.reasons[0].message) {
errMessage += ' reason: ' + body.reasons[0].message;
}
return cb(new Error(errMessage));
}
if (!body.parserManifests) {
if (numTries < maxRetries) {
return retry(roomId, numTries, cb);
}
return cb(new Error("Malformed response body: this parserManifest doesn't exist."));
}
cb(err, body.parserManifests);
});
}
module.exports = function (roomId, cb) {
getManifest(roomId, 0, function (err, manifest) {
if (err) {
return cb(err);
}
return cb(null, function parseMessage (msgData) {
if (!Array.isArray(msgData)) {
throw new Error("Could not parse message: raw Message isn't an array as expected");
}
// The message object we will populate
const message = {};
// Kick things off by parsing the top level manifest
// TODO: Check message parser version and fetch if not available
parseNestable(message, msgData, manifest.manifests['v2'], manifest.urlTemplates, manifest.urlTemplateTypes);
// Create replace index
message._index = indexReplaceableItems(message);
// Message is now a fully hydrated object
// and can be passed along to the stream
return message;
});
});
};
// Parse a single piece of the manifest, can recurse
function parseNestable (msg, part, manifest, urlTemplates, urlTemplateTypes) {
// Loop through the manifest keys, then based on their type
// process the part from in the chat message.
for (const i in manifest) {
switch (manifest[i].type) {
// Simple types are just assigned to their keys
case 'int':
case 'string':
case 'stringArray':
case 'intArray':
case 'url':
msg[i] = part[manifest[i].index];
break;
// urlTemplate's come in the form of `http://host.com/foo/{{n}}/{{n}}`,
// with a corresponding vars array. The vars are interpolated in order
// to the bracketed n's.
case 'urlTemplate':
// @NOTE this is horrible code, but it works, and hopefully the comments describe it well enough
// This line uses `part`, the chat message part, and `manifest`, the manifest section,
// to get the template from the `urlTemplates` and process it with the vars
// from the message part.
// manifest[i] is the part of the manifest we are working on
// part[manifest[i].index] is the corresponding part from the message
// urlTemplates[part[manifest[i].index][manifest[i].nestedItems.key.index]] accesses the template specified in the message part
// part[manifest[i].index][manifest[i].nestedItems.vars.index] accesses the variable for the template
const _i = manifest[i].nestedItems.hasMultipleTypes.index;
var _key = part[manifest[i].index][manifest[i].nestedItems.key.index];
var _vars = part[manifest[i].index][manifest[i].nestedItems.vars.index];
msg[i] = part[manifest[i].index][_i]
? Object.keys(urlTemplateTypes[_key])
.reduce(
function (prev, cur) {
prev[cur] = parseUrlTemplate(urlTemplateTypes[_key], cur, _vars);
return prev;
},
{}
)
: parseUrlTemplate(urlTemplates, _key, _vars);
break;
case 'urlTemplates':
msg[i] = [];
for (let k1 = 0, len1 = part[manifest[i].index].length; k1 < len1; k1++) {
const parsedUrlTemplate = parseUrlTemplate(urlTemplates, part[manifest[i].index][k1][manifest[i].nestedItems.key.index], msg[i][k1]);
if (parsedUrlTemplate) {
msg[i].push({
key: part[manifest[i].index][k1][manifest[i].nestedItems.key.index],
urlTemplate: parsedUrlTemplate
});
}
}
break;
// A nestedObject is a complex type that we need to
// parse from it's own level, so recurse in. Also,
// apparently it is nullable, so check that first.
case 'nestedObject':
if (!part[manifest[i].index]) {
msg[i] = null;
} else {
msg[i] = {};
parseNestable(msg[i], part[manifest[i].index], manifest[i].nestedItems, urlTemplates, urlTemplateTypes);
}
break;
// A nestedArray is also complex, and it's manifest definition is
// actually a definition of what each item in the array looks like,
// so recurse on each item instead of the array itself.
case 'nestedArray':
// @TODO nestedObject is nullable, is nestedArray?
// Haven't seen one, they just come back empty, but
// should explore to be sure.
msg[i] = [];
for (let k2 = 0, len2 = part[manifest[i].index].length; k2 < len2; k2++) {
msg[i][k2] = {};
parseNestable(msg[i][k2], part[manifest[i].index][k2], manifest[i].nestedItems, urlTemplates, urlTemplateTypes);
}
break;
}
}
}
// Some things for use in the helper methods below
const matchNumRegexp = /{{n}}/gi;
const toReplace = [
'mentions',
'emoticons',
'links',
'tags'
];
// Matches each `{{n}}` and replaces it with the var for that index
function parseUrlTemplate (urlTemplates, templateKey, vars) {
let matchNum = 0;
const urlTemplate = urlTemplates[templateKey];
if (!urlTemplate) {
return null;
}
return urlTemplates[templateKey].replace(matchNumRegexp, function () {
return vars[matchNum++];
});
}
// Generate an index map for replicable message parts starting positions
// Takes in a parsed message object, and returns an object where
// each "replaceble part" is indexed by the character position where
// it starts
function indexReplaceableItems (message) {
const map = {};
toReplace.forEach(function (key) {
if (!message[key]) {
return;
}
message[key].forEach(function (replaceItem) {
if (!replaceItem || !replaceItem.positions) {
return;
}
replaceItem.positions.forEach(function (start) {
map[start] = {
item: replaceItem,
type: key
};
});
});
});
return map;
}