2014-08-21 22:40:19 +08:00
# Convert a color `value` as integer to a hex color (e.g. 255 to #0000ff)
@colourToHex = (value) ->
hex = parseInt ( value ) . toString ( 16 )
hex = " 0 " + hex while hex . length < 6
" # #{ hex } "
2014-08-09 00:04:04 +08:00
# retrieve account for selected user
@getCurrentUserFromSession = ->
2014-10-10 01:43:22 +08:00
Meteor . Users . findOne ( " _id " : getInSession ( " userId " ) )
2014-08-09 00:04:04 +08:00
@getInSession = (k) -> SessionAmplify . get k
@getMeetingName = ->
meetName = getInSession ( " meetingName " ) # check if we actually have one in the session
if meetName ? then meetName # great return it, no database query
else # we need it from the database
meet = Meteor . Meetings . findOne ( { } )
if meet ? . meetingName
setInSession " meetingName " , meet ? . meetingName # store in session for fast access next time
meet ? . meetingName
2014-10-08 00:12:53 +08:00
else " your meeting "
2014-08-09 00:04:04 +08:00
@getTime = -> # returns epoch in ms
( new Date ) . valueOf ( )
2014-09-25 03:55:55 +08:00
@getTimeOfJoining = ->
2014-10-04 01:26:01 +08:00
Meteor . Users . findOne ( _id: getInSession " DBID " ) ? . user ? . time_of_joining
2014-09-25 03:55:55 +08:00
2014-08-09 00:04:04 +08:00
@getUsersName = ->
name = getInSession ( " userName " ) # check if we actually have one in the session
if name ? then name # great return it, no database query
else # we need it from the database
2014-10-10 01:43:22 +08:00
user = Meteor . Users . findOne ( { ' _id ' : getInSession ( " DBID " ) } )
2014-08-09 00:04:04 +08:00
if user ? . user ? . name
setInSession " userName " , user . user . name # store in session for fast access next time
user . user . name
else null
2014-09-23 06:11:24 +08:00
@getPresentationFilename = ->
currentPresentation = Meteor . Presentations . findOne ( { " presentation.current " : true } )
currentPresentation ? . presentation ? . name
2014-08-21 22:40:19 +08:00
Handlebars . registerHelper " colourToHex " , (value) =>
@ window . colourToHex ( value )
2014-07-09 21:15:50 +08:00
Handlebars . registerHelper ' equals ' , (a, b) -> # equals operator was dropped in Meteor's migration from Handlebars to Spacebars
2014-07-04 01:54:41 +08:00
a is b
2014-09-05 00:32:24 +08:00
Handlebars . registerHelper " getCurrentMeeting " , ->
Meteor . Meetings . findOne ( )
2014-08-18 22:29:02 +08:00
Handlebars . registerHelper " getCurrentSlide " , ->
currentPresentation = Meteor . Presentations . findOne ( { " presentation.current " : true } )
presentationId = currentPresentation ? . presentation ? . id
Meteor . Slides . find ( { " presentationId " : presentationId , " slide.current " : true } )
2014-08-09 00:04:04 +08:00
# retrieve account for selected user
Handlebars . registerHelper " getCurrentUser " , =>
@ window . getCurrentUserFromSession ( )
2014-09-17 03:29:17 +08:00
Handlebars . registerHelper " getHandRaiseStats " , ->
2014-10-09 21:58:48 +08:00
numRaised = Meteor . Users . find ( { ' user.raise_hand ' : true } ) . count ( )
total = Meteor . Users . find ( ) . count ( )
2014-09-17 03:29:17 +08:00
percentageRaised = numRaised / total
if numRaised > 0
" #{ numRaised } Hands Raised ( #{ percentageRaised } % of Attendees) "
else false
2014-06-14 02:19:50 +08:00
# Allow access through all templates
2014-07-22 21:31:32 +08:00
Handlebars . registerHelper " getInSession " , (k) -> SessionAmplify . get k
2014-06-14 02:19:50 +08:00
2014-08-09 00:04:04 +08:00
Handlebars . registerHelper " getMeetingName " , ->
window . getMeetingName ( )
2014-09-23 06:11:24 +08:00
Handlebars . registerHelper " getWhiteboardTitle " , ->
" Whiteboard: " + getPresentationFilename ( )
2014-06-14 02:19:50 +08:00
2014-08-18 22:29:02 +08:00
Handlebars . registerHelper " getShapesForSlide " , ->
2014-09-16 00:46:02 +08:00
currentSlide = getCurrentSlideDoc ( )
2014-08-18 22:29:02 +08:00
# try to reuse the lines above
Meteor . Shapes . find ( { whiteboardId: currentSlide ? . slide ? . id } )
2014-08-09 00:04:04 +08:00
# retrieves all users in the meeting
Handlebars . registerHelper " getUsersInMeeting " , ->
Meteor . Users . find ( { } )
2014-10-08 00:35:45 +08:00
Handlebars . registerHelper " isCurrentUser " , (_id) ->
_id is getInSession ( " DBID " )
2014-08-09 00:04:04 +08:00
2014-08-15 01:26:15 +08:00
Handlebars . registerHelper " meetingIsRecording " , ->
Meteor . Meetings . findOne ( ) ? . recorded # Should only ever have one meeting, so we dont need any filter and can trust result #1
2014-09-04 04:02:00 +08:00
Handlebars . registerHelper " isCurrentUserMuted " , ->
getInSession " isMuted "
Handlebars . registerHelper " isCurrentUserRaisingHand " , ->
2014-10-08 03:28:54 +08:00
user = Meteor . Users . findOne ( { _id : getInSession ( " DBID " ) } )
2014-09-04 21:45:17 +08:00
user ? . user ? . raise_hand
2014-09-04 04:02:00 +08:00
2014-09-04 03:22:08 +08:00
Handlebars . registerHelper " isCurrentUserSharingAudio " , ->
2014-10-10 00:48:27 +08:00
user = Meteor . Users . findOne ( { _id : getInSession ( " DBID " ) } )
2014-09-04 21:45:17 +08:00
user ? . user ? . voiceUser ? . joined
2014-09-04 03:22:08 +08:00
Handlebars . registerHelper " isCurrentUserSharingVideo " , ->
2014-10-10 00:48:27 +08:00
user = Meteor . Users . findOne ( { _id : getInSession ( " DBID " ) } )
2014-09-04 21:45:17 +08:00
user ? . user ? . webcam_stream ? . length isnt 0
2014-09-04 04:11:56 +08:00
Handlebars . registerHelper " isCurrentUserTalking " , ->
2014-10-10 00:48:27 +08:00
user = Meteor . Users . findOne ( { _id : getInSession ( " DBID " ) } )
2014-09-04 21:45:17 +08:00
user ? . user ? . voiceUser ? . talking
2014-09-04 03:22:08 +08:00
2014-08-09 00:04:04 +08:00
Handlebars . registerHelper " isUserSharingAudio " , (u) ->
2014-08-13 00:25:15 +08:00
if u ?
2014-10-10 00:48:27 +08:00
user = Meteor . Users . findOne ( { _id : u . _id } )
2014-09-04 21:45:17 +08:00
user ? . user ? . voiceUser ? . joined
2014-08-09 00:04:04 +08:00
else return false
2014-09-20 05:23:58 +08:00
Handlebars . registerHelper " isUserListenOnly " , (u) ->
if u ?
2014-10-10 00:48:27 +08:00
user = Meteor . Users . findOne ( { _id : u . _id } )
2014-09-20 05:23:58 +08:00
user ? . user ? . listenOnly
else return false
2014-08-09 00:04:04 +08:00
Handlebars . registerHelper " isUserSharingVideo " , (u) ->
2014-09-04 04:11:56 +08:00
u . webcam_stream ? . length isnt 0
2014-08-09 00:04:04 +08:00
Handlebars . registerHelper " isUserTalking " , (u) ->
2014-08-13 00:25:15 +08:00
if u ?
2014-10-10 00:48:27 +08:00
user = Meteor . Users . findOne ( { _id : u . _id } )
2014-09-04 04:11:56 +08:00
user . user ? . voiceUser ? . talking
2014-08-13 00:25:15 +08:00
else return false
Handlebars . registerHelper " isUserMuted " , (u) ->
if u ?
2014-10-10 01:43:22 +08:00
user = Meteor . Users . findOne ( { _id : u . _id } )
2014-08-13 00:25:15 +08:00
user . user . voiceUser ? . muted
2014-08-09 00:04:04 +08:00
else return false
2014-08-16 01:17:51 +08:00
Handlebars . registerHelper " messageFontSize " , ->
style: " font-size: #{ getInSession ( " messageFontSize " ) } px; "
2014-08-18 22:29:02 +08:00
Handlebars . registerHelper " pointerLocation " , ->
currentPresentation = Meteor . Presentations . findOne ( { " presentation.current " : true } )
2014-09-30 21:56:10 +08:00
currentPresentation ? . pointer
2014-08-18 22:29:02 +08:00
2014-08-09 00:04:04 +08:00
Handlebars . registerHelper " setInSession " , (k, v) -> SessionAmplify . set k , v
Handlebars . registerHelper " visibility " , (section) ->
if getInSession " display_ #{ section } "
style: ' display:block '
else
style: ' display:none '
2014-09-26 01:24:42 +08:00
Handlebars . registerHelper " getChatbarTabs1 " , ->
chatTabs . find ( ) . fetch ( )
2014-08-18 22:29:02 +08:00
# transform plain text links into HTML tags compatible with Flash client
@linkify = (str) ->
www = /(^|[^\/])(www\.[\S]+($|\b))/img
http = /\b(https?:\/\/[0-9a-z+|.,:;\/&?_~%#=@!-]*[0-9a-z+|\/&_~%#=@-])/img
str = str . replace http , " <a href= ' event:$1 ' ><u>$1</u></a> "
str = str . replace www , " $1<a href= ' event:http://$2 ' ><u>$2</u></a> "
2014-08-09 00:04:04 +08:00
@setInSession = (k, v) -> SessionAmplify . set k , v
Meteor . methods
sendMeetingInfoToClient: (meetingId, userId) ->
setInSession ( " userId " , userId )
setInSession ( " meetingId " , meetingId )
setInSession ( " currentChatId " , meetingId )
setInSession ( " meetingName " , null )
2014-10-10 01:38:06 +08:00
setInSession ( " userName " , null )
2014-06-14 02:19:50 +08:00
2014-07-14 22:46:02 +08:00
@toggleCam = (event) ->
2014-08-09 00:04:04 +08:00
# Meteor.Users.update {_id: context._id} , {$set:{"user.sharingVideo": !context.sharingVideo}}
2014-06-27 01:13:19 +08:00
# Meteor.call('userToggleCam', context._id, !context.sharingVideo)
2014-06-14 02:19:50 +08:00
2014-08-09 00:04:04 +08:00
@toggleChatbar = ->
setInSession " display_chatbar " , ! getInSession " display_chatbar "
@toggleMic = (event) ->
if getInSession " isSharingAudio " # only allow muting/unmuting if they are in the call
2014-10-10 01:27:44 +08:00
u = Meteor . Users . findOne ( { _id : getInSession ( " DBID " ) } )
2014-08-09 00:04:04 +08:00
if u ?
2014-10-10 01:27:44 +08:00
# publishMuteRequest: (meetingId, mutee_id, requesterUserId, requester_id, mutedBoolean) ->
Meteor . call ( ' publishMuteRequest ' , getInSession ( " meetingId " ) , u . _id , getInSession ( " userId " ) , u . _id , not u . user . voiceUser . muted )
2014-09-04 04:02:00 +08:00
setInSession " isMuted " , not u . user . voiceUser . muted
2014-08-09 00:04:04 +08:00
@toggleNavbar = ->
setInSession " display_navbar " , ! getInSession " display_navbar "
# toggle state of session variable
@toggleUsersList = ->
setInSession " display_usersList " , ! getInSession " display_usersList "
2014-07-29 03:24:07 +08:00
@toggleVoiceCall = (event) ->
2014-08-16 00:26:17 +08:00
if getInSession " isSharingAudio "
hangupCallback = ->
console . log " left voice conference "
# sometimes we can hangup before the message that the user stopped talking is received so lets set it manually, otherwise they might leave the audio call but still be registered as talking
2014-10-10 00:39:58 +08:00
# userStopAudio: (meetingId, userId, user_id, requesterUserId, requester_id) ->
Meteor . call ( " userStopAudio " , getInSession ( " meetingId " ) , getInSession ( " userId " ) , getInSession ( " DBID " ) , getInSession ( " userId " ) , getInSession ( " DBID " ) )
2014-08-16 00:26:17 +08:00
setInSession " isSharingAudio " , false # update to no longer sharing
webrtc_hangup hangupCallback # sign out of call
else
# create voice call params
2014-10-10 00:39:58 +08:00
username = " #{ getInSession ( " DBID " ) } -bbbID- #{ getUsersName ( ) } "
voiceBridge = Meteor . Meetings . findOne ( { } ) . voiceConf
2014-08-16 00:26:17 +08:00
server = null
joinCallback = (message) ->
2014-10-10 00:39:58 +08:00
# userShareAudio: (meetingId, userId, user_id) ->
Meteor . call ( " userShareAudio " , getInSession ( " meetingId " ) , getInSession ( " userId " ) , getInSession ( " DBID " ) )
2014-08-16 00:26:17 +08:00
setInSession " isSharingAudio " , true
webrtc_call ( username , voiceBridge , server , joinCallback ) # make the call
return false
2014-06-14 02:19:50 +08:00
2014-08-01 21:51:45 +08:00
@toggleWhiteBoard = ->
setInSession " display_whiteboard " , ! getInSession " display_whiteboard "
2014-06-19 22:45:58 +08:00
2014-07-29 22:20:51 +08:00
# Starts the entire logout procedure.
2014-07-25 01:56:31 +08:00
# meeting: the meeting the user is in
# the user's userId
2014-07-28 23:00:07 +08:00
@userLogout = (meeting, user) ->
2014-07-25 01:56:31 +08:00
Meteor . call ( " userLogout " , meeting , user )
2014-07-28 23:00:07 +08:00
# Clear the local user session and redirect them away
2014-07-25 01:56:31 +08:00
setInSession ( " userId " , null )
setInSession ( " meetingId " , null )
setInSession ( " currentChatId " , null )
setInSession ( " meetingName " , null )
setInSession ( " bbbServerVersion " , null )
setInSession ( " userName " , null )
setInSession " display_navbar " , false # needed to hide navbar when the layout template renders
2014-07-28 23:00:07 +08:00
Router . go ( ' logout ' ) # navigate to logout
2014-08-22 03:36:50 +08:00
# color can be a number (a hex converted to int) or a string (e.g. "#ffff00")
@formatColor = (color) ->
color ? = " 0 " # default value
if ! color . toString ( ) . match ( /\#.*/ )
color = colourToHex ( color )
color
# thickness can be a number (e.g. "2") or a string (e.g. "2px")
@formatThickness = (thickness) ->
thickness ? = " 1 " # default value
if ! thickness . toString ( ) . match ( /.*px$/ )
" # " + thickness + " px " # leading "#" - to be compatible with Firefox
thickness
2014-09-16 00:46:02 +08:00
2014-09-13 04:26:25 +08:00
# applies zooming to the stroke thickness
@zoomStroke = (thickness) ->
2014-09-16 00:46:02 +08:00
currentSlide = @ getCurrentSlideDoc ( )
2014-09-13 04:26:25 +08:00
ratio = ( currentSlide ? . slide . width_ratio + currentSlide ? . slide . height_ratio ) / 2
thickness * 100 / ratio
2014-09-16 00:46:02 +08:00
@getCurrentSlideDoc = -> # returns only one document
currentPresentation = Meteor . Presentations . findOne ( { " presentation.current " : true } )
presentationId = currentPresentation ? . presentation ? . id
2014-09-19 23:35:08 +08:00
currentSlide = Meteor . Slides . findOne ( { " presentationId " : presentationId , " slide.current " : true } )
2014-09-26 01:24:42 +08:00
#start a clientside-only collection keeping track of the chat tabs
@chatTabs = new Meteor . Collection ( null )
2014-09-26 02:58:32 +08:00
#insert the basic tabs
2014-09-26 02:17:39 +08:00
@ chatTabs . insert ( { userId: " PUBLIC_CHAT " , name: " Public " , gotMail: false , class : " publicChatTab " } )
@ chatTabs . insert ( { userId: " OPTIONS " , name: " Options " , gotMail: false , class : " optionsChatTab " } )
2014-09-26 01:57:58 +08:00
2014-09-26 02:58:32 +08:00
#check the chat history of the user and add tabs for the private chats
2014-09-26 01:57:58 +08:00
@populateChatTabs = ->
2014-10-08 07:08:48 +08:00
mydbid = getInSession " DBID "
2014-09-26 01:24:42 +08:00
users = Meteor . Users . find ( ) . fetch ( )
2014-10-08 07:08:48 +08:00
# assuming that I only have access only to private messages where I am the sender or the recipient
myPrivateChats = Meteor . Chat . find ( { ' message.chat_type ' : ' PRIVATE_CHAT ' } ) . fetch ( )
2014-09-26 01:24:42 +08:00
uniqueArray = [ ]
for chat in myPrivateChats
2014-10-08 07:08:48 +08:00
if chat . message . to_userid is mydbid
2014-09-26 01:24:42 +08:00
uniqueArray . push ( { userId: chat . message . from_userid , username: chat . message . from_username } )
2014-10-08 07:08:48 +08:00
if chat . message . from_userid is mydbid
2014-09-26 01:24:42 +08:00
uniqueArray . push ( { userId: chat . message . to_userid , username: chat . message . to_username } )
2014-09-26 02:58:32 +08:00
#keep unique entries only
2014-09-26 01:24:42 +08:00
uniqueArray = uniqueArray . filter ( (itm, i, a) ->
i is a . indexOf ( itm )
)
2014-09-26 02:58:32 +08:00
#insert the unique entries in the collection
2014-09-26 01:24:42 +08:00
for u in uniqueArray
2014-09-26 01:57:58 +08:00
unless chatTabs . findOne ( { userId: u . userId } ) ?
chatTabs . insert ( { userId: u . userId , name: u . username , gotMail: false , class : " privateChatTab " } )
2014-09-26 01:24:42 +08:00
2014-09-26 01:57:58 +08:00
Handlebars . registerHelper " grabChatTabs " , ->
2014-09-26 01:24:42 +08:00
chatTabs . find ( ) . fetch ( )