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=';
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
const isUrlEmpty = url => !url || url.length === 0;
|
|
|
|
|
|
|
|
const isUrlValid = (url) => {
|
|
|
|
const regexp = RegExp('^(https?://)?(www.)?(youtube.com|youtu.?be)/.+$');
|
|
|
|
return !isUrlEmpty(url) && url.match(regexp);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getUrlFromVideoId = id => (id ? `${YOUTUBE_PREFIX}${id}` : '');
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-02-07 21:17:05 +08:00
|
|
|
// https://stackoverflow.com/questions/3452546/how-do-i-get-the-youtube-video-id-from-a-url
|
2018-11-30 01:24:02 +08:00
|
|
|
const videoIdFromUrl = (url) => {
|
2019-05-02 04:09:49 +08:00
|
|
|
const regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#]*).*/;
|
2019-02-07 21:17:05 +08:00
|
|
|
const match = url.match(regExp);
|
2019-05-02 04:09:49 +08:00
|
|
|
return (match && match[1].length === 11) ? 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,
|
|
|
|
};
|