Built motion from commit 10af8726.|2.6.34
[motion2.git] / public / tvox / ring.controller.js
1 angular.module('RingApp', ['ngMaterial', 'ngCookies', 'ngSanitize'])
2   .controller("RingController", function($scope, $http, $cookies) {
3
4     $scope.motion_url = "";
5     $scope.logo = "";
6     $scope.error_message = "";
7     $scope.welcome_message = "";
8     $scope.notification_message = "";
9
10     this.$onInit = function() {
11       $scope.token = $cookies.get('motion.token');
12
13       if (_.isNil($scope.token))
14         $scope.error_message = "XCally motion token not valid, please connect to XCally Motion, then retry";
15       else {
16         var urlParams = new URLSearchParams(window.location.search);
17         var params = urlParams.get("params");
18         var arr = params.split("|");
19         if (arr) {
20           $scope.messageId = arr[0];
21           $scope.motionUrl = arr[1];
22           $scope.chatWebsiteId = arr[2];
23           $scope.interactionId = arr[3];
24           $scope.contactId = arr[4];
25           $scope.welcome_message = arr[5];
26           $scope.notification_message = arr[6];
27         }
28
29         window.addEventListener("message", function(e) {
30           if (e.data) {
31             if (e.data.data) {
32               if (e.data.data.state) {
33                 if (e.data.data.state == "IDLE") {
34                   var callId = e.data.data.callId;
35                   refuseInteraction(callId);
36                 }
37               }
38             }
39           }
40
41           let a = e.data;
42           null != a && null != a.namespace && "webclient.teleniasoftware.com" == a.namespace ? "callEvent" == a.event ? callMessageData || (callMessageData = a.data, null !== infoCb && infoCb(callMessageData)) : "allCallsStateEvent" == a.event ? (callsMap = new Map, a.data.forEach(function(e) {
43             callsMap.set(e.callId, e)
44           })) : "userStateEvent" == a.event && (console.info("userStateEvent received", a.data), userData = a.data) : console.info("Received Unknown message", a)
45
46
47         }, false);
48
49         tvoxSendReadyEvent();
50
51         $http.get($scope.motion_url + "/api/chat/websites/" + $scope.chatWebsiteId, {
52           headers: {
53             'Authorization': 'Bearer ' + $scope.token
54           }
55         }).
56         then(
57           function successCallback(response) {
58             $scope.logo = $scope.motion_url + "/api/chat/websites/" + $scope.chatWebsiteId + "/logo?token=";
59             //$scope.notification_message=response.data.notificationTemplate;
60           },
61           function errorCallback(response) {
62             console.log("Unable to perform get request", response);
63           }
64         );
65       }
66     }
67
68     function refuseInteraction(callId) {
69       var msg = {
70         id: $scope.interactionId,
71         closed: true,
72         closeReason: 'unmanaged',
73         disposition: 'unmanaged',
74         closedAt: new Date() // moment().format('YYYY-MM-DD HH:mm:ss'),
75       };
76
77       $http.put($scope.motion_url + "/api/chat/interactions/" + $scope.interactionId + "/custom_update", msg, {
78         headers: {
79           'Authorization': 'Bearer ' + $scope.token
80         }
81       }).
82       then(
83         function ret(response) {
84           tvoxCloseInteraction(callId);
85         }
86       );
87
88     }
89
90
91     $scope.accept = function(event) {
92       tvoxAnswerCall(tvoxGetCallHandle());
93     }
94     $scope.refuse = function(event) {
95       tvoxDropCall(tvoxGetCallHandle());
96     }
97   });