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
|
|
|
|
|
|
|
const videoIdFromUrl = (url) => {
|
2019-01-16 04:44:41 +08:00
|
|
|
const urlObj = new URL(url);
|
|
|
|
const params = new URLSearchParams(urlObj.search);
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
return params.get('v');
|
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,
|
|
|
|
};
|