Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.12 KB | None | 0 0
  1. import moment from 'moment'
  2. import toastr from 'toastr'
  3.  
  4. Meteor.startup ->
  5.  
  6. if Meteor.isCordova
  7. return
  8.  
  9.  
  10. RocketChat.ToneGenerator = new PhoneTones(0, 0)
  11.  
  12. Tracker.autorun ->
  13. user = Meteor.user()
  14. if not user
  15. return
  16.  
  17. enabled = RocketChat.settings.get('Phone_Enabled')
  18. wss = RocketChat.settings.get('Phone_WSS')
  19. servers = RocketChat.settings.get("Phone_ICEServers")
  20. forceRelay = RocketChat.settings.get("Phone_ICEForceRelay")
  21.  
  22. iceServers = []
  23. if servers? and (servers?.trim() isnt '')
  24. servers = servers.replace /\s/g, ''
  25. servers = servers.split ','
  26. for server in servers
  27. server = server.split '@'
  28. serverConfig =
  29. urls: [server.pop()]
  30.  
  31. if server.length is 1
  32. server = server[0].split ':'
  33. serverConfig.username = decodeURIComponent(server[0])
  34. serverConfig.credential = decodeURIComponent(server[1])
  35.  
  36. iceServers.push serverConfig
  37. iceConfig = {forceRelay: forceRelay, iceServers: iceServers}
  38.  
  39. if not enabled or not wss
  40. console.log("Phone not enabled or missing server url") if window.rocketDebug
  41. return
  42.  
  43. plogin = user.phonelogin
  44. ppass = user.phonepassword
  45.  
  46. if not plogin or not ppass
  47. console.warn("Phone account data not set (yet?)")
  48. return
  49.  
  50. RocketChat.Phone.start(plogin, ppass, wss, iceConfig)
  51.  
  52.  
  53. Template.phone.events
  54. 'click #phone_settings': (e, instance) ->
  55. showSettings = instance.showSettings.get()
  56. instance.showSettings.set(!showSettings)
  57.  
  58. 'mousedown .button.dialkey': (e, instance) ->
  59. value = _.trim $(e.target).val()
  60. RocketChat.Phone.startDtmf(value)
  61.  
  62. 'mouseup .button.dialkey': (e, instance) ->
  63. value = _.trim $(e.target).val()
  64. display = instance.phoneDisplay.get()
  65. instance.phoneDisplay.set(display + value)
  66. instance.search(display + value)
  67. RocketChat.Phone.endDtmf(value)
  68.  
  69. 'paste #phone-display': _.debounce (e, instance) ->
  70. value = $(e.target).val()
  71. replaced_value = value.replace(/\D/g,'')
  72. if !replaced_value
  73. replaced_value = ""
  74. RocketChat.Phone.setSearchTerm(replaced_value)
  75. RocketChat.Phone.setSearchResult(replaced_value)
  76. instance.phoneDisplay.set(replaced_value)
  77. $('#phone-display').val(replaced_value)
  78. , 200
  79.  
  80. 'change #phone-display': (e, instance) ->
  81. value = _.trim $(e.target).val()
  82. instance.phoneDisplay.set(value)
  83.  
  84. 'keydown #phone-display': _.debounce (e, instance) ->
  85. if e.keyCode == 13
  86. number = instance.phoneDisplay.get()
  87. RocketChat.Phone.newCall(number)
  88.  
  89. else
  90. value = e.target.value.trim()
  91. if value is '' and RocketChat.Phone.getSearchTerm()
  92. RocketChat.Phone.setSearchTerm('')
  93. RocketChat.Phone.setSearchResult(undefined)
  94. return
  95. else if value is RocketChat.Phone.getSearchTerm()
  96. return
  97. instance.search()
  98. , 500
  99.  
  100. 'click #phone-dial': (e, instance)->
  101. number = instance.phoneDisplay.get()
  102. RocketChat.Phone.dialKey(number)
  103. instance.phoneDisplay.set('')
  104.  
  105. 'click #phone-video-dial': (e, instance)->
  106. number = instance.phoneDisplay.get()
  107. RocketChat.Phone.dialKey(number, true)
  108. instance.phoneDisplay.set('')
  109.  
  110. 'click #phone-hangup': (e, instance)->
  111. if window.rocketDebug
  112. console.log "hangup"
  113.  
  114. RocketChat.Phone.hangup()
  115. instance.phoneDisplay.set('')
  116.  
  117. 'click #phone-hold': (e, instance)->
  118. if window.rocketDebug
  119. console.log "toggle hold"
  120. RocketChat.Phone.toggleHold()
  121.  
  122. 'click #phone-mute': (e, instance)->
  123. if window.rocketDebug
  124. console.log "toggle mute"
  125. RocketChat.Phone.toggleMute()
  126.  
  127. 'click #phone-redial': (e, instance)->
  128. if window.rocketDebug
  129. console.log "redialing...."
  130.  
  131. lastCalled = RocketChat.Phone.getLastCalled()
  132. if lastCalled
  133. instance.phoneDisplay.set(lastCalled)
  134. RocketChat.Phone.redial()
  135.  
  136. 'click #phone-clear': (e, instance)->
  137. if window.rocketDebug
  138. console.log "clearing display"
  139.  
  140. instance.phoneDisplay.set('')
  141. RocketChat.Phone.setSearchTerm('')
  142. RocketChat.Phone.setSearchResult()
  143.  
  144. 'click #phone-transfer': (e, instance)->
  145. if window.rocketDebug
  146. console.log "transferring call..."
  147.  
  148. number = instance.phoneDisplay.get()
  149. if number
  150. RocketChat.Phone.transfer(number)
  151. else
  152. toastr.error TAPi18n.__('Empty_Number')
  153.  
  154. 'click #phone-fullscreen': (e, instance) ->
  155. i = document.getElementById("phonestream")
  156. if i.requestFullscreen
  157. i.requestFullscreen()
  158. else
  159. if i.webkitRequestFullscreen
  160. i.webkitRequestFullscreen()
  161. else
  162. if i.mozRequestFullScreen
  163. i.mozRequestFullScreen()
  164. else
  165. if i.msRequestFullscreen
  166. i.msRequestFullscreen()
  167.  
  168. 'click .phone-search-contact-number': (e, instance) ->
  169. number = _.trim $(e.target).text()
  170. instance.phoneDisplay.set(number)
  171. RocketChat.Phone.newCall(number)
  172.  
  173. 'click .phone-registry-record-number': (e, instance) ->
  174. number = _.trim $(e.target).text()
  175. instance.phoneDisplay.set(number)
  176. RocketChat.Phone.newCall(number)
  177.  
  178. 'click #phone_personal_registry': (e, instance) ->
  179. showRegistry = instance.showRegistry.get()
  180. if !showRegistry
  181. Meteor.call 'getPersonalRegistry', (error, results) =>
  182. if not results?
  183. instance.listRegistry.set([])
  184. else
  185. repr_res = []
  186. for record in results.calls
  187. convert_date = moment(record.start_time).format("DD-MM-YYYY H:mm:ss")
  188. convert_status = "icon-up"
  189. switch record.status
  190. when "out"
  191. convert_status = "icon-up"
  192. when "in"
  193. convert_status = "icon-down"
  194. when "missed"
  195. convert_status = "icon-forward"
  196. repr_res.push({'number': record.number, 'status': convert_status, 'start_time': convert_date, 'name': record.name})
  197. instance.listRegistry.set({'calls': repr_res})
  198. else
  199. instance.listRegistry.set([])
  200.  
  201. instance.showRegistry.set(!showRegistry)
  202.  
  203. Template.phone.helpers
  204. phoneDisplay: ->
  205. return Template.instance().phoneDisplay.get()
  206.  
  207. showSettings: ->
  208. return Template.instance().showSettings.get()
  209.  
  210. callIsActive: ->
  211. if RocketChat.Phone.getCallState() == 'active'
  212. return true
  213.  
  214. callIsRinging: ->
  215. if RocketChat.Phone.getCallState() == 'ringing'
  216. return true
  217.  
  218. callIsIdle: ->
  219. if RocketChat.Phone.getCallState()
  220. return false
  221. return true
  222.  
  223. callState: ->
  224. return RocketChat.Phone.getCallState()
  225.  
  226. callCidNameOrNum: ->
  227. cidname = RocketChat.Phone.getCallCidName()
  228. if cidname.trim() == ""
  229. return RocketChat.Phone.getCallCidNum()
  230. else
  231. return cidname
  232.  
  233. callCidName: ->
  234. return RocketChat.Phone.getCallCidName()
  235.  
  236. callCidNum: ->
  237. return RocketChat.Phone.getCallCidNum()
  238.  
  239. callOperation: ->
  240. return RocketChat.Phone.getCallOperation()
  241.  
  242. displayCallStatus: ->
  243. if RocketChat.Phone.getCallState() and (RocketChat.Phone.getCallCidName() or RocketChat.Phone.getCallCidNum())
  244. return true
  245. return false
  246.  
  247. onHold: ->
  248. if RocketChat.Phone.isOnHold()
  249. return 'phone-active-key'
  250. return ''
  251.  
  252. isMuted: ->
  253. if RocketChat.Phone.isMuted()
  254. return 'phone-active-key'
  255. return ''
  256.  
  257. searchTerm: ->
  258. return RocketChat.Phone.getSearchTerm()
  259.  
  260. searchResult: ->
  261. return RocketChat.Phone.getSearchResult()
  262.  
  263. showRegistry: ->
  264. return Template.instance().showRegistry.get()
  265.  
  266. listRegistry: ->
  267. return Template.instance().listRegistry.get()
  268.  
  269. videoEnabled: ->
  270. return RocketChat.Phone.getEnabledCamera()
  271.  
  272. callIsAnswered: ->
  273. return RocketChat.Phone.isAnswered()
  274.  
  275. callDuration: ->
  276. start = localStorage.getItem("VoiSmart::Phone::lastAnswered")
  277. if start
  278. now = Math.round(Chronos.now() / 1000)
  279. else
  280. start = now
  281. return moment().startOf('day').seconds(now - start).format('HH:mm:ss')
  282.  
  283. contactsLoading: ->
  284. RocketChat.Phone.getContactsLoading() > 0
  285.  
  286. Template.phone.onCreated ->
  287. @showSettings = new ReactiveVar false
  288. @phoneDisplay = new ReactiveVar ""
  289. @showRegistry = new ReactiveVar false
  290. @listRegistry = new ReactiveVar ""
  291.  
  292.  
  293. Template.phone.onDestroyed ->
  294. if window.rocketDebug
  295. console.log("Moving video tag out from containter")
  296.  
  297. RocketChat.Phone.removeVideo()
  298.  
  299.  
  300. Template.phone.onRendered ->
  301. @autorun ->
  302. if window.rocketDebug
  303. console.log("Moving video tag to its containter")
  304. Session.get('openedRoom')
  305. FlowRouter.watchPathChange()
  306. RocketChat.Phone.placeVideo()
  307.  
  308. @search = (searchvalue) =>
  309. if RocketChat.Phone.isOnCall()
  310. # do not start contact search while on call
  311. return
  312. if searchvalue
  313. current_search = searchvalue
  314. else
  315. current_search = @$('#phone-display').val()
  316.  
  317. RocketChat.Phone.setSearchTerm(current_search)
  318. RocketChat.Phone.increaseContactsLoadingMask()
  319. Meteor.call 'getContacts', current_search, (error, results) =>
  320. RocketChat.Phone.decreaseContactsLoadingMask()
  321. if not results?
  322. RocketChat.Phone.setSearchResult([])
  323. else
  324. RocketChat.Phone.setSearchResult(results)
  325.  
  326.  
  327. RocketChat.Phone = new class
  328. callState = new ReactiveVar null
  329. callCidName = new ReactiveVar ""
  330. callCidNum = new ReactiveVar ""
  331. callOperation = new ReactiveVar ""
  332. onHold = new ReactiveVar false
  333. muted = new ReactiveVar false
  334. searchTerm = new ReactiveVar ''
  335. searchResult = new ReactiveVar
  336. enabledCamera = new ReactiveVar false
  337. answered = new ReactiveVar false
  338. contactsLoading = new ReactiveVar 0
  339. language = localStorage.getItem('userLanguage') or window.navigator.language
  340. _started = false
  341. _login = undefined
  342. _password = undefined
  343. _vertoHandle = undefined
  344. _server = undefined
  345. _iceConfig = {forceRelay: false, iceServers: []}
  346. _videoTag = undefined
  347. _vertoEchoTimer = undefined
  348.  
  349. _audioInDevice = undefined
  350. _audioOutDevice = undefined
  351. _videoDevice = null
  352. _useDeskPhone = false
  353.  
  354. _curCall = null
  355. _dialogs = {}
  356. _callState = null
  357. _isVideoCall = false
  358.  
  359. _curResolutions = null
  360. _curVideoW = null
  361. _curVideoH = null
  362.  
  363. _tabBars = []
  364. constructor: ->
  365. if window.rocketDebug
  366. console.log("Starting a new Phone Handler")
  367. WebNotifications.registerCallbacks('phone', [
  368. {name: 'answer', callback: => answer(false)},
  369. {name: 'hangup', callback: => @hangup()}
  370. ])
  371.  
  372. commands = "#{@getCall(language)} *name": (name) ->
  373. Meteor.call 'getContacts', (name), (error, user) =>
  374. openTabBar()
  375. if user.contacts.length > 0
  376. return RocketChat.Phone.newCall user.contacts[0].telephoneNumber[0], false
  377. else
  378. msg = TAPi18n.__('User_not_found')
  379. toastr.error(msg)
  380.  
  381. answer = (useVideo) ->
  382. if window.rocketDebug
  383. console.log "Will answer call"
  384.  
  385. if useVideo
  386. useVideo = true
  387. _videoTag.css('display', 'block')
  388. else
  389. useVideo = false
  390. _videoTag.css('display', 'none')
  391.  
  392. _isVideoCall = useVideo
  393.  
  394. has_video = false
  395. if _videoDevice and (_videoDevice != "none") and useVideo
  396. has_video = true
  397.  
  398. _curCall?.answer({
  399. useVideo: has_video,
  400. useStereo: true,
  401. useCamera: _videoDevice,
  402. useSpeak: _audioOutDevice || "none",
  403. useMic: _audioInDevice || "none",
  404. }, {})
  405.  
  406.  
  407. onWSLogin = (verto, success) ->
  408. if window.rocketDebug
  409. console.log('onWSLogin', success)
  410.  
  411. if _vertoEchoTimer?
  412. Meteor.clearInterval(_vertoEchoTimer)
  413. _vertoEchoTimer = undefined
  414. _vertoEchoTimer = Meteor.setInterval(vertoPinger, 15000)
  415.  
  416. vertoPinger = () ->
  417. if _vertoHandle?
  418. _vertoHandle.sendMethod("echo", {alive: true})
  419.  
  420. onWSClose = (verto, success) ->
  421. if _vertoEchoTimer?
  422. Meteor.clearInterval(_vertoEchoTimer)
  423. _vertoEchoTimer = undefined
  424.  
  425. if window.rocketDebug
  426. console.log('onWSClose', success)
  427.  
  428. setCallState = (state) ->
  429. _callState = state
  430. callState.set(state)
  431.  
  432.  
  433. getCall: (language) ->
  434. if language == "it"
  435. return "chiama"
  436. else
  437. return "call"
  438.  
  439.  
  440. onDialogState = (d) ->
  441. if window.rocketDebug
  442. console.log('on rocket dialog ', d)
  443. console.log('current dialogs ', _dialogs)
  444.  
  445. _dialogs[d.callID] = d
  446.  
  447. if !_curCall?
  448. _curCall = d
  449.  
  450. if d.callID != _curCall.callID
  451. switch d.state.name
  452. when 'ringing'
  453. RocketChat.ToneGenerator.stop()
  454. console.log("refusing call")
  455. d.stopRinging()
  456. d.hangup({cause: "USER_BUSY", causeCode: 17})
  457. when 'hangup', 'destroy'
  458. RocketChat.ToneGenerator.stop()
  459. delete _dialogs[d.callID]
  460. WebNotifications.closeNotification 'phone'
  461. return
  462.  
  463. if window.rocketDebug
  464. console.log "Processing state RQ:" + d.state.name
  465.  
  466. switch d.state.name
  467. when 'trying'
  468. setCallState('active')
  469. RocketChat.TabBar.updateButton('phone', { class: 'phone-blinking' })
  470. RocketChat.ToneGenerator.startRingback()
  471.  
  472. when 'early'
  473. setCallState('active')
  474. RocketChat.TabBar.updateButton('phone', { class: 'phone-blinking' })
  475. RocketChat.ToneGenerator.stop()
  476.  
  477. when 'ringing'
  478. setCallState('ringing')
  479. RocketChat.TabBar.updateButton('phone', { class: 'phone-blinking' })
  480. Meteor.call 'phoneFindUserByQ', {phoneextension: d.params.caller_id_number}, (error, user) =>
  481. if error or !user
  482. username = d.params.caller_id_name
  483. else
  484. username = user.username
  485.  
  486. openTabBar()
  487.  
  488. msg = TAPi18n.__("Incoming_call_from")
  489. putNotification(msg, d.params.caller_id_number, d.params.caller_id_name)
  490. cid = d.params.caller_id_number + ' ' + d.params.caller_id_name
  491. title = TAPi18n.__ "Phone_Call"
  492. text = TAPi18n.__("Incoming_call_from") + '\n' + cid
  493. actions = [
  494. {action: 'answer', title: TAPi18n.__('Phone_Answer'), icon: 'images/answer.png'},
  495. {action: 'hangup', title: TAPi18n.__('Phone_Hangup'), icon: 'images/hangup.png'}
  496. ]
  497. notification =
  498. title: title
  499. text: text
  500. actions: actions
  501. prefix: 'phone'
  502. icon: 'images/call.png'
  503. requireInteraction: true
  504. payload:
  505. rid: Session.get('openedRoom')
  506. sender:
  507. name: d.params.caller_id_name
  508. username: username
  509.  
  510. WebNotifications.showNotification notification
  511.  
  512. when 'active'
  513. setCallState('active')
  514. RocketChat.ToneGenerator.stop()
  515. msg = TAPi18n.__("In_call_with")
  516. if !d.attach
  517. now = new Date().getTime()
  518. localStorage.setItem("VoiSmart::Phone::lastAnswered", Math.round(now / 1000))
  519. answered.set(true)
  520. if d.direction.name == 'outbound'
  521. putNotification(msg, d.params.destination_number)
  522. else
  523. d.stopRinging()
  524. putNotification(msg, d.params.caller_id_number, d.params.caller_id_name)
  525. WebNotifications.closeNotification 'phone'
  526. RocketChat.TabBar.updateButton('phone', { class: 'phone-red' })
  527.  
  528. when 'hangup'
  529. RocketChat.ToneGenerator.stop()
  530. answered.set(false)
  531. localStorage.removeItem("VoiSmart::Phone::lastAnswered")
  532. localStorage.removeItem("VoiSmart::Phone::sessionUUID")
  533. if _callState != 'transfer'
  534. if window.rocketDebug
  535. console.log("hangup call rq")
  536. _curCall.hangup()
  537.  
  538. setCallState('hangup')
  539. _curCall = null
  540. clearNotification()
  541. RocketChat.TabBar.updateButton('phone', { class: '' })
  542. if d.answered or d.gotAnswer or d.cause == 'ORIGINATOR_CANCEL' or d.cause == 'NORMAL CLEARING'
  543. toastr.success TAPi18n.__('Phone_end_call')
  544. else
  545. msg = TAPi18n.__('Phone_failed_call')
  546. toastr.error(msg + ": " + RocketChat.Phone.remap_hcause(d.cause))
  547. WebNotifications.closeNotification 'phone'
  548.  
  549. when 'destroy'
  550. RocketChat.ToneGenerator.stop()
  551. if _callState != 'transfer' and _callState != 'hangup'
  552. if window.rocketDebug
  553. console.log("destroy call rq")
  554. _curCall.hangup()
  555.  
  556. setCallState(null)
  557. _curCall = null
  558. clearNotification()
  559. delete _dialogs[d.callID]
  560. WebNotifications.closeNotification 'phone'
  561. $("#phonestream").css('display', 'none')
  562. Meteor.setTimeout ->
  563. closeTabBar()
  564. , 1000
  565.  
  566. closeTabBar = () ->
  567. for tabBar in _tabBars
  568. if tabBar? and tabBar.getTemplate() == "phone"
  569. tabBar.close()
  570.  
  571. openTabBar = () ->
  572. for tabBar in _tabBars
  573. if tabBar?
  574. tabBar.setTemplate "phone"
  575. tabBar.open()
  576.  
  577. remap_hcause: (cause) ->
  578. dflt = cause
  579. mapper =
  580. 'NORMAL CLEARING': 'Phone_end_call'
  581. 'ORIGINATOR_CANCEL': 'Phone_end_call'
  582. 'USER_BUSY': 'User_busy'
  583. 'NO_ANSWER': 'No_answer'
  584. 'NO_ROUTE_DESTINATION': 'No_route_destination'
  585. 'DESTINATION_OUT_OF_ORDER': 'Destination_out_of_order'
  586. 'NORMAL_TEMPORARY_FAILURE': 'Normal_temporary_failure'
  587. 'PICKED_OFF': 'Picked_off'
  588. 'LOSE_RACE': 'Lose_race'
  589. msg = mapper[cause]
  590. return TAPi18n.__(msg or dflt)
  591.  
  592. clearNotification = ->
  593. callCidName.set('')
  594. callCidNum.set('')
  595. callOperation.set('')
  596. onHold.set(false)
  597. muted.set(false)
  598.  
  599. putNotification = (msg, cidnum, cidname="") ->
  600. callCidNum.set(cidnum)
  601. callOperation.set(msg)
  602. Meteor.call 'phoneFindUserByQ', {phoneextension: cidnum}, (error, user) =>
  603. if error or !user
  604. callCidName.set(cidname)
  605. else
  606. callCidName.set(user.username)
  607.  
  608. refreshVideoResolution = (resolutions) ->
  609. _curResolutions = resolutions.validRes
  610. if window.rocketDebug
  611. console.log ">>>>>>>>>< RESOLUTIONS >>>>>>>>>>>>>>>>>>"
  612. console.log resolutions
  613. console.log ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  614.  
  615. bootstrap = (status) =>
  616. if window.rocketDebug
  617. console.log _started
  618. console.log _vertoHandle
  619. console.log _login
  620. console.log _password
  621. console.log _server
  622. console.log _iceConfig
  623.  
  624. old_session_uuid = localStorage.getItem("VoiSmart::Phone::sessionUUID")
  625. if old_session_uuid
  626. session_uuid = old_session_uuid
  627. else
  628. session_uuid = $.verto.genUUID()
  629. localStorage.setItem("VoiSmart::Phone::sessionUUID", session_uuid)
  630. _vertoHandle = new jQuery.verto({
  631. login: _login,
  632. passwd: _password
  633. socketUrl: _server,
  634. ringFile: 'sounds/bell_ring2.wav',
  635. iceServers: _iceConfig?.iceServers,
  636. forceRelay: _iceConfig?.forceRelay,
  637. tagRinger: "phoneringer",
  638. tag: "phonestream"
  639. audioParams: {
  640. googEchoCancellation: true,
  641. googNoiseSuppression: true,
  642. googHighpassFilter: true
  643. },
  644. sessid: session_uuid,
  645. deviceParams: {
  646. useCamera: _videoDevice,
  647. onResCheck: refreshVideoResolution,
  648. useSpeak: _audioOutDevice,
  649. useMic: _audioInDevice
  650. }
  651. }, {
  652. onWSLogin: onWSLogin,
  653. onWSClose: onWSClose,
  654. onDialogState: onDialogState
  655. })
  656. _started = true
  657.  
  658. setConfig = ->
  659. $.verto.refreshDevices(refreshDevices)
  660. conf = {
  661. audioInDevice: _audioInDevice
  662. audioOutDevice: _audioOutDevice
  663. videoDevice: _videoDevice
  664. useDeskPhone: _useDeskPhone
  665. }
  666. localStorage.setItem('MeteorPhoneConfig', $.toJSON(conf))
  667.  
  668. getConfig = ->
  669. cached = localStorage.getItem('MeteorPhoneConfig')
  670. conf = $.parseJSON(cached)
  671. if not conf
  672. setConfig()
  673. return
  674.  
  675. _audioInDevice = conf.audioInDevice
  676. _audioOutDevice = conf.audioOutDevice
  677. _videoDevice = conf.videoDevice
  678. _useDeskPhone = conf.useDeskPhone
  679. enabledCamera.set(conf.videoDevice)
  680.  
  681. refreshDevices = (what) ->
  682. if window.rocketDebug
  683. console.log ">>>>>>>>>> REFRESH DEVICES <<<<<<<<<<<<"
  684. console.log what
  685. console.log ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  686.  
  687. if _videoDevice
  688. $.FSRTC.getValidRes(_videoDevice, refreshVideoResolution)
  689.  
  690. getCallState: ->
  691. return callState.get()
  692.  
  693. getCallOperation: ->
  694. return callOperation.get()
  695.  
  696. getCallCidName: ->
  697. return callCidName.get()
  698.  
  699. getCallCidNum: ->
  700. return callCidNum.get()
  701.  
  702. getSearchTerm: ->
  703. return searchTerm.get()
  704.  
  705. getSearchResult: ->
  706. return searchResult.get()?.contacts
  707.  
  708. setSearchTerm: (term) ->
  709. searchTerm.set(term)
  710.  
  711. increaseContactsLoadingMask: ->
  712. contactsLoading.set(contactsLoading.get() + 1)
  713.  
  714. decreaseContactsLoadingMask: ->
  715. v = contactsLoading.get()
  716. if v > 0
  717. contactsLoading.set(v-1)
  718. else
  719. contactsLoading.set(0)
  720.  
  721. getContactsLoading: ->
  722. return contactsLoading.get()
  723.  
  724. setSearchResult: (results) ->
  725. searchResult.set(results)
  726.  
  727. getEnabledCamera: ->
  728. return enabledCamera.get()
  729.  
  730. removeVideo: ->
  731. _videoTag = $("#phonestream")
  732. _videoTag.appendTo($("body"))
  733. _videoTag.css('display', 'none')
  734. if _curCall and _callState is 'active'
  735. _videoTag[0].play()
  736.  
  737. placeVideo: ->
  738. _videoTag.appendTo($("#phone-video"))
  739. if _curCall and _callState is 'active' and _isVideoCall
  740. _videoTag.css('display', 'block')
  741. _videoTag[0].play()
  742. else if _curCall and _callState is 'active' and !_isVideoCall
  743. _videoTag.css('display', 'none')
  744. _videoTag[0].play()
  745. else
  746. _videoTag.css('display', 'none')
  747.  
  748. transfer: (number) ->
  749. if _curCall and _callState is 'active'
  750. setCallState('transfer')
  751. _curCall.transfer(number)
  752.  
  753. getLastCalled: ->
  754. return Session.get("VoiSmart::Phone::lastCalled")
  755.  
  756. redial: () ->
  757. if !_curCall? and _callState is null
  758. @newCall(Session.get("VoiSmart::Phone::lastCalled"),
  759. Session.get("VoiSmart::Phone::lastUseVideo"))
  760.  
  761. toggleMute: () ->
  762. if !_curCall?
  763. return
  764.  
  765. muted.set(!muted.get())
  766. _curCall.setMute('toggle')
  767. return muted.get()
  768.  
  769. isMuted: () ->
  770. return muted.get()
  771.  
  772. isAnswered: () ->
  773. return answered.get()
  774.  
  775. toggleHold: () ->
  776. if !_curCall?
  777. return
  778.  
  779. onHold.set(!onHold.get())
  780. _curCall.toggleHold()
  781. return onHold.get()
  782.  
  783. isOnHold: () ->
  784. return onHold.get()
  785.  
  786. isOnCall: () ->
  787. return _curCall?
  788.  
  789. startDtmf: (key) ->
  790. RocketChat.ToneGenerator.startDtmf(key)
  791.  
  792. endDtmf: (key) ->
  793. RocketChat.ToneGenerator.stop()
  794. if !_curCall?
  795. return
  796.  
  797. _curCall.dtmf(key)
  798.  
  799. hangup: ->
  800. if !_curCall?
  801. if window.rocketDebug
  802. console.log "No call to hangup"
  803. return
  804.  
  805. _curCall.hangup()
  806. _curCall = null
  807.  
  808. dialKey: (number, useVideo) ->
  809.  
  810. if !_curCall? and _callState is null
  811. if !number
  812. toastr.error TAPi18n.__('Empty_Number')
  813. return
  814. @newCall(number, useVideo)
  815. return
  816.  
  817. if _callState is 'ringing'
  818. answer(useVideo)
  819. return
  820.  
  821. console.log('What Im doing here: ', _callState, ' ', _curCall)
  822.  
  823. newCall: (destination, useVideo) ->
  824. @setSearchTerm('')
  825. @setSearchResult(undefined)
  826. if useVideo
  827. useVideo = true
  828. _videoTag.css('display', 'block')
  829. else
  830. useVideo = false
  831. _videoTag.css('display', 'none')
  832.  
  833. _isVideoCall = useVideo
  834. Session.set("VoiSmart::Phone::lastUseVideo", useVideo)
  835.  
  836.  
  837. if !destination or destination is ''
  838. console.log("No number provided") if window.rocketDebug
  839. return
  840.  
  841. if _curCall?
  842. console.log("Cannot call while in call") if window.rocketDebug
  843. return
  844.  
  845. if _useDeskPhone and !useVideo
  846. Meteor.call 'clickAndDial', destination, (error, results) =>
  847. if error
  848. console.error("Error in calling click and dial method", error)
  849. toastr.error error.reason
  850. return
  851.  
  852. has_mic = RocketChat.Phone.getAudioInDevice()
  853. has_speak = RocketChat.Phone.getAudioOutDevice()
  854. if !has_mic? or !has_speak? or has_mic is "none" or has_speak is "none"
  855. console.log("no mic and speaker defined, should refuse call?") if window.rocketDebug
  856. #return # firefox still has issues in device selection
  857.  
  858. if !has_mic? or has_mic is "none"
  859. # all browsers have a mic, so bail out if none
  860. toastr.error TAPi18n.__('Phone_invalid_devices')
  861. settings_button = $('#phone_settings')
  862. if settings_button
  863. settings_button.addClass('phone-settings-blinking')
  864. Meteor.setTimeout ->
  865. if settings_button.hasClass('phone-settings-blinking')
  866. settings_button.removeClass('phone-settings-blinking')
  867. , 10000
  868. return
  869.  
  870. has_video = false
  871. if _videoDevice and (_videoDevice != "none") and useVideo
  872. has_video = true
  873.  
  874. setCallState('trying')
  875. _curCall = _vertoHandle.newCall({
  876. destination_number: destination,
  877. caller_id_name: Meteor.user().name,
  878. caller_id_number: Meteor.user().phoneextension,
  879. useVideo: has_video,
  880. useStereo: true,
  881. useCamera: _videoDevice,
  882. useSpeak: _audioOutDevice || "any",
  883. useMic: _audioInDevice || "any"
  884. }, {
  885. onDialogState: onDialogState
  886. })
  887. Session.set("VoiSmart::Phone::lastCalled", destination)
  888. msg = TAPi18n.__("Outgoing_call_to")
  889. putNotification(msg, destination)
  890.  
  891. setVideoResolution: (idx) ->
  892. if idx is "0"
  893. _curVideoW = null
  894. _curVideoH = null
  895. delete _vertoHandle.videoParams.minWidth
  896. delete _vertoHandle.videoParams.maxWidth
  897. delete _vertoHandle.videoParams.minHeight
  898. delete _vertoHandle.videoParams.maxHeight
  899. else
  900. idx = idx - 1
  901. wxh = _curResolutions[idx]
  902. console.log(wxh) if window.rocketDebug
  903. _curVideoW = wxh[0]
  904. _curVideoH = wxh[1]
  905. _vertoHandle.videoParams({
  906. #width: _curVideoW,
  907. #height: _curVideoH
  908. minWidth: _curVideoW,
  909. minHeight: _curVideoH,
  910. maxWidth: _curVideoW,
  911. maxHeight: _curVideoH
  912. })
  913.  
  914. _vertoHandle.videoParams({
  915. minFrameRate: 5,
  916. vertoBestFrameRate: 30
  917. })
  918.  
  919. getResolutions: ->
  920. return _curResolutions
  921.  
  922. setAudioInDevice: (id) ->
  923. _audioInDevice = id
  924. setConfig()
  925.  
  926. getAudioInDevice: ->
  927. return _audioInDevice
  928.  
  929. setAudioOutDevice: (id) ->
  930. _audioOutDevice = id
  931. setConfig()
  932.  
  933. getAudioOutDevice: ->
  934. return _audioOutDevice
  935.  
  936. getUseDeskPhone: ->
  937. return _useDeskPhone
  938.  
  939. setUseDeskPhone: (value) ->
  940. value = parseInt(value)
  941. if value
  942. _useDeskPhone = true
  943. else
  944. _useDeskPhone = false
  945. setConfig()
  946.  
  947. setVideoDevice: (id) ->
  948. if id is 'none'
  949. id = null
  950. _videoDevice = id
  951. enabledCamera.set(id)
  952. setConfig()
  953.  
  954. getVideoDevice: ->
  955. return _videoDevice
  956.  
  957. start: (login, password, server, iceConfig) ->
  958. console.log("Starting verto....") if window.rocketDebug
  959.  
  960. if _started and (login != _login or _password != password or _server != server)
  961. _vertoHandle.logout()
  962. _vertoHandle = undefined
  963. _started = false
  964. console.log("Restarting an already started client") if window.rocketDebug
  965.  
  966. if !_started
  967. console.log("Activating video element") if window.rocketDebug
  968. Blaze.render(Template.phonevideo, document.body)
  969.  
  970. if _started and _vertoHandle
  971. console.log("Client already started, ignoring") if window.rocketDebug
  972. return
  973.  
  974. _videoTag = $("#phonestream")
  975.  
  976. _login = login
  977. _password = password
  978. _server = server
  979. _iceConfig = iceConfig
  980.  
  981. getConfig()
  982.  
  983. $.verto.init({}, bootstrap)
  984.  
  985. logout: ->
  986. if !_vertoHandle
  987. return
  988.  
  989. _vertoHandle.logout()
  990. _vertoHandle = undefined
  991. _started = false
  992.  
  993. setTabBar: (tabBar) ->
  994. _tabBars.push tabBar
  995.  
  996. console.log(commands)
  997. console.log(getCall(language))
  998. annyang.addCommands commands
  999. console.log(language)
  1000. if language == 'it'
  1001.  
  1002. annyang.setLanguage('it-IT')
  1003. else
  1004. annyang.setLanguage('eng-GB')
  1005. annyang.start()
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011. RocketChat.callbacks.add 'afterLogoutCleanUp', ->
  1012. RocketChat.Phone.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement