2018-11-30 01:24:02 +08:00
|
|
|
import Meetings from '/imports/api/meetings';
|
|
|
|
import Auth from '/imports/ui/services/auth';
|
|
|
|
import ExternalVideoStreamer from '/imports/api/external-videos';
|
|
|
|
|
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
const YOUTUBE_PREFIX = 'https://youtube.com/watch?v=';
|
2019-05-29 01:24:42 +08:00
|
|
|
const YOUTUBE_REGEX = /(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\/?\?(?:\S*?&?v\=))|youtu\.be\/)([a-zA-Z0-9_-]{6,11})/g;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
const isUrlEmpty = url => !url || url.length === 0;
|
|
|
|
|
2019-05-30 23:02:51 +08:00
|
|
|
const isUrlValid = url => !isUrlEmpty(url) && url.match(YOUTUBE_REGEX);
|
2019-01-16 04:44:41 +08:00
|
|
|
|
|
|
|
const getUrlFromVideoId = id => (id ? `${YOUTUBE_PREFIX}${id}` : '');
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
const videoIdFromUrl = (url) => {
|
2019-05-29 01:24:42 +08:00
|
|
|
const match = YOUTUBE_REGEX.exec(url);
|
|
|
|
return match ? match[1] : false;
|
2019-01-16 04:44:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const startWatching = (url) => {
|
|
|
|
const externalVideoUrl = videoIdFromUrl(url);
|
|
|
|
makeCall('startWatchingExternalVideo', { externalVideoUrl });
|
|
|
|
};
|
|
|
|
|
|
|
|
const stopWatching = () => {
|
|
|
|
makeCall('stopWatchingExternalVideo');
|
|
|
|
};
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
const sendMessage = (event, data) => {
|
2019-01-16 04:44:41 +08:00
|
|
|
ExternalVideoStreamer.emit(event, {
|
|
|
|
...data,
|
2019-01-21 13:49:01 +08:00
|
|
|
meetingId: Auth.meetingID,
|
|
|
|
userId: Auth.userID,
|
2019-01-16 04:44:41 +08:00
|
|
|
});
|
2018-11-30 01:24:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const onMessage = (message, func) => {
|
|
|
|
ExternalVideoStreamer.on(message, func);
|
|
|
|
};
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
const getVideoId = () => {
|
2019-01-21 13:49:01 +08:00
|
|
|
const meetingId = Auth.meetingID;
|
2019-01-16 04:44:41 +08:00
|
|
|
const meeting = Meetings.findOne({ meetingId });
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
return meeting && meeting.externalVideoUrl;
|
2019-01-16 04:44:41 +08:00
|
|
|
};
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
export {
|
|
|
|
sendMessage,
|
|
|
|
onMessage,
|
2019-01-16 04:44:41 +08:00
|
|
|
getVideoId,
|
|
|
|
getUrlFromVideoId,
|
|
|
|
isUrlValid,
|
2018-11-30 01:24:02 +08:00
|
|
|
startWatching,
|
|
|
|
stopWatching,
|
|
|
|
};
|