Skip to content

Commit fa83d88

Browse files
Fix promise array building
1 parent 93a5eff commit fa83d88

File tree

2 files changed

+82
-90
lines changed

2 files changed

+82
-90
lines changed

server/routes/events.js

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -500,43 +500,6 @@ router.get('/:eventId', async (req, res, next) => {
500500
return res.status(404).send(`Event (_id: ${escape(eventId)}) not found`);
501501
}
502502

503-
const stages = await Promise.all(event.stages.map(stage => {
504-
return async () => {
505-
const streamKey = stage.streamInfo.streamKey;
506-
const rtmpServerURL = `http://localhost:${process.env.RTMP_SERVER_HTTP_PORT}/api/streams/live/${streamKey}`;
507-
const {data: {isLive}} = await axios.get(rtmpServerURL, {
508-
headers: {Authorization: rtmpServer.auth.header}
509-
});
510-
511-
let thumbnailURL;
512-
if (isLive) {
513-
try {
514-
thumbnailURL = await getThumbnail(streamKey);
515-
} catch (err) {
516-
LOGGER.info('An error occurred when getting thumbnail for stream (stream key: {}). ' +
517-
'Returning splash thumbnail. Error: {}', streamKey, err);
518-
thumbnailURL = stage.getSplashThumbnailURL();
519-
}
520-
} else {
521-
thumbnailURL = stage.getSplashThumbnailURL();
522-
}
523-
524-
return {
525-
_id: stage._id,
526-
isLive,
527-
stageName: stage.stageName,
528-
thumbnailURL,
529-
streamInfo: {
530-
streamKey: stage.streamInfo.streamKey,
531-
title: stage.streamInfo.title,
532-
genre: stage.streamInfo.genre,
533-
category: stage.streamInfo.category,
534-
viewCount: stage.streamInfo.viewCount
535-
}
536-
};
537-
};
538-
}));
539-
540503
const socketIOURL = (process.env.NODE_ENV === 'production' ? 'https' : 'http')
541504
+ `://${process.env.SERVER_HOST}:${process.env.SOCKET_IO_PORT}?eventId=${eventId}`;
542505

@@ -547,13 +510,47 @@ router.get('/:eventId', async (req, res, next) => {
547510
endTime: event.endTime,
548511
bannerPicURL: event.getBannerPicURL(),
549512
tags: event.tags,
550-
stages,
513+
stages: await Promise.all(event.stages.map(buildEventStage)),
551514
numOfSubscribers: event.subscribers.length,
552515
rtmpServerURL: RTMP_SERVER_URL,
553516
socketIOURL
554517
});
555518
});
556519

520+
async function buildEventStage(eventStage) {
521+
const streamKey = eventStage.streamInfo.streamKey;
522+
const {data: {isLive}} = await axios.get(`http://localhost:${process.env.RTMP_SERVER_HTTP_PORT}/api/streams/live/${streamKey}`, {
523+
headers: {Authorization: rtmpServer.auth.header}
524+
});
525+
526+
let thumbnailURL;
527+
if (isLive) {
528+
try {
529+
thumbnailURL = await getThumbnail(streamKey);
530+
} catch (err) {
531+
LOGGER.info('An error occurred when getting thumbnail for stream (stream key: {}). ' +
532+
'Returning splash thumbnail. Error: {}', streamKey, err);
533+
thumbnailURL = eventStage.getSplashThumbnailURL();
534+
}
535+
} else {
536+
thumbnailURL = eventStage.getSplashThumbnailURL();
537+
}
538+
539+
return {
540+
_id: eventStage._id,
541+
isLive,
542+
stageName: eventStage.stageName,
543+
thumbnailURL,
544+
streamInfo: {
545+
streamKey: eventStage.streamInfo.streamKey,
546+
title: eventStage.streamInfo.title,
547+
genre: eventStage.streamInfo.genre,
548+
category: eventStage.streamInfo.category,
549+
viewCount: eventStage.streamInfo.viewCount
550+
}
551+
};
552+
}
553+
557554
router.get('/:eventId/event-name', async (req, res, next) => {
558555
const eventId = sanitise(req.params.eventId);
559556

server/routes/livestreams.js

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,35 @@ router.get('/', async (req, res, next) => {
5353
return next(err);
5454
}
5555

56-
const streams = await Promise.all(result.docs.map(user => {
57-
return async () => {
58-
const streamKey = user.streamInfo.streamKey;
59-
let thumbnailURL;
60-
try {
61-
thumbnailURL = await getThumbnail(streamKey);
62-
} catch (err) {
63-
LOGGER.info('An error occurred when getting thumbnail for stream (stream key: {}). ' +
64-
'Returning default thumbnail. Error: {}', streamKey, err);
65-
thumbnailURL = config.defaultThumbnailURL;
66-
}
67-
return {
68-
username: user.username,
69-
displayName: user.displayName,
70-
profilePicURL: user.getProfilePicURL(),
71-
title: user.streamInfo.title,
72-
genre: user.streamInfo.genre,
73-
category: user.streamInfo.category,
74-
viewCount: user.streamInfo.viewCount,
75-
startTime: user.streamInfo.startTime,
76-
thumbnailURL
77-
};
78-
};
79-
}));
80-
8156
res.json({
82-
streams,
57+
streams: await Promise.all(result.docs.map(buildUserLivestream)),
8358
nextPage: result.nextPage
8459
});
8560
});
8661
});
8762

63+
async function buildUserLivestream(user) {
64+
const streamKey = user.streamInfo.streamKey;
65+
let thumbnailURL;
66+
try {
67+
thumbnailURL = await getThumbnail(streamKey);
68+
} catch (err) {
69+
LOGGER.info('An error occurred when getting thumbnail for stream (stream key: {}). ' +
70+
'Returning default thumbnail. Error: {}', streamKey, err);
71+
thumbnailURL = config.defaultThumbnailURL;
72+
}
73+
return {
74+
username: user.username,
75+
displayName: user.displayName,
76+
profilePicURL: user.getProfilePicURL(),
77+
title: user.streamInfo.title,
78+
genre: user.streamInfo.genre,
79+
category: user.streamInfo.category,
80+
viewCount: user.streamInfo.viewCount,
81+
startTime: user.streamInfo.startTime,
82+
thumbnailURL
83+
};
84+
}
8885

8986
router.get('/event-stages', async (req, res, next) => {
9087
const streamKeys = await getLiveStreamKeys();
@@ -154,33 +151,8 @@ router.get('/event-stages', async (req, res, next) => {
154151
return next(err);
155152
}
156153

157-
const streams = await Promise.all(result.docs.map(eventStage => {
158-
return async () => {
159-
const streamKey = eventStage.streamInfo.streamKey;
160-
let thumbnailURL;
161-
try {
162-
thumbnailURL = await getThumbnail(streamKey);
163-
} catch (err) {
164-
LOGGER.info('An error occurred when getting thumbnail for stream (stream key: {}). ' +
165-
'Returning default thumbnail. Error: {}', streamKey, err);
166-
thumbnailURL = config.defaultThumbnailURL;
167-
}
168-
return {
169-
eventStageId: eventStage._id,
170-
stageName: eventStage.stageName,
171-
event: eventStage.event,
172-
title: eventStage.streamInfo.title,
173-
genre: eventStage.streamInfo.genre,
174-
category: eventStage.streamInfo.category,
175-
viewCount: eventStage.streamInfo.viewCount,
176-
startTime: eventStage.streamInfo.startTime,
177-
thumbnailURL
178-
};
179-
};
180-
}));
181-
182154
res.json({
183-
streams,
155+
streams: await Promise.all(result.docs.map(buildEventStageLivestream)),
184156
nextPage: result.nextPage
185157
});
186158
});
@@ -193,6 +165,29 @@ async function getLiveStreamKeys() {
193165
return data.live ? Object.getOwnPropertyNames(data.live) : [];
194166
}
195167

168+
async function buildEventStageLivestream(eventStage) {
169+
const streamKey = eventStage.streamInfo.streamKey;
170+
let thumbnailURL;
171+
try {
172+
thumbnailURL = await getThumbnail(streamKey);
173+
} catch (err) {
174+
LOGGER.info('An error occurred when getting thumbnail for stream (stream key: {}). ' +
175+
'Returning default thumbnail. Error: {}', streamKey, err);
176+
thumbnailURL = config.defaultThumbnailURL;
177+
}
178+
return {
179+
eventStageId: eventStage._id,
180+
stageName: eventStage.stageName,
181+
event: eventStage.event,
182+
title: eventStage.streamInfo.title,
183+
genre: eventStage.streamInfo.genre,
184+
category: eventStage.streamInfo.category,
185+
viewCount: eventStage.streamInfo.viewCount,
186+
startTime: eventStage.streamInfo.startTime,
187+
thumbnailURL
188+
};
189+
}
190+
196191
router.get('/:streamKey/thumbnail', async (req, res) => {
197192
const streamKey = sanitise(req.params.streamKey);
198193
try {

0 commit comments

Comments
 (0)