aboutsummaryrefslogtreecommitdiffstats
path: root/alias-angular/app/js/controllers.js
blob: 979a4aa0385e34adda2b991e98e2a8c977722328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
/* App Controllers */

function ConnectCtl($scope, StropheSrv, $log, $rootScope) {
    $rootScope.self = {jid: "",
                       status: 'offline'
                      };
    $rootScope.is_connected = function () {
        return $rootScope.status == Strophe.Status.CONNECTED;
    };
    function connect_callback(status){
        $rootScope.status = status;
        switch(status) {
        case Strophe.Status.CONNECTING:
            $log.log('Strophe is connecting.');
            break;
        case  Strophe.Status.CONNFAIL:
            $log.log('Strophe failed to connect.');
            break;
        case Strophe.Status.DISCONNECTING:
            $log.log('Strophe is disconnecting.');
            break;
        case Strophe.Status.DISCONNECTED:
            $log.log('Strophe is disconnected.');
            break;
        case Strophe.Status.CONNECTED:
            $log.log('Strophe is connected.');
            $rootScope.$broadcast('connected', true);
        }
    };
    $scope.login = function () {
        StropheSrv.login($scope.username, $scope.password, connect_callback);
        $rootScope.self["jid"] = $scope.username;
    };
    $scope.disconnect = function() {
        StropheSrv.disconnect();
    };
}

ConnectCtl.$inject = ['$scope', 'StropheSrv', '$log', '$rootScope'];

function RosterCtl($scope, StropheSrv, $log, $rootScope) {
    $scope.contacts = {};
    $scope.get_contacts = function() {
        return _.toArray($scope.contacts);
    };

    function onRoster(iq) {
        var elems = iq.getElementsByTagName('query');
        var query  = elems[0];
        Strophe.forEachChild(query, 'item', function(item){
            var jid = item.getAttribute('jid');
            var name = item.getAttribute('name')||'';
            //contacts are offline by default
            $scope.contacts[jid] = {jid: jid, name: name, status: 'offline'};
        });
        //make sure the roster is populated before sending presence request
        StropheSrv.send($pres());
        $rootScope.self["status"] = "chat";
    };

    function onPresence(presence) {
        var who = $(presence).attr('from');
        var jid = Strophe.getBareJidFromJid(who);
        var type = $(presence).attr('type');
        // if (jid in $scope.contacts) {
        if(_.has($scope.contacts, jid)){
            var contact = $scope.contacts[jid];
            if (type !== 'error') {
                if (type === 'unavailable') {
                    contact['status'] = 'offline';
                } else {
                    var show = $(presence).find('show');
                    if (show.length) {
                        contact['status'] = show.text();
                    } else {
                        contact['status'] = 'chat';
                    }
                }
            }
        }
    }

    $scope.$on('connected', function() {
        StropheSrv.addHandler(onPresence, 'presence');
        $scope.getRoster();
    });

    $scope.getRoster = function() {
        var query = $iq({type : 'get'}).c('query', {xmlns : Strophe.NS.ROSTER});
        StropheSrv.sendIQ(query, onRoster);
    };

    $scope.chatWith = function(contact) {
        $rootScope.$broadcast('msgrequest', contact.name||contact.jid);
    };

    $scope.setStatus = function(status) {
        var pres = $pres().c("show", {}, status);
        StropheSrv.send(pres);
        $rootScope.self["status"] = status;
    };

 }

RosterCtl.$inject = ['$scope','StropheSrv','$log', '$rootScope'];

function MsgCtl($scope, $log, StropheSrv, $rootScope) {

    $scope.conversations = {};

    $scope.activeConversation = '';

    function addTab(contact) {
        if (!_.has($scope.conversations, contact)) {
            $scope.conversations[contact] = [];
        }
    };

    function addMessage(conv,from,body) {
        $scope.conversations[conv].push({from:from, body:body});
    };

    function onMessage(message) {
        var full_jid = $(message).attr('from');
        var jid = Strophe.getBareJidFromJid(full_jid);
        addTab(jid);
        var body = $(message).find("html > body");

        if (body.length === 0) {
            body = $(message).find('body');
            if (body.length > 0) {
                body = body.text();
            } else {
                body = null;
            }
        } else {
            body = body.contents();
            var span = $("<span></span>");
            body.each(function () {
                if (document.importNode) {
                    $(document.importNode(this, true)).appendTo(span);
                } else {
                    // IE workaround
                    span.append(this.xml);
                }
            });
            body = span;
        }
        if (body) {
            addMessage(jid,jid,body);
        }
    };

    $scope.sendMessage = function(message,to) {
        var msg = $msg({to: to, "type": "chat"}).c('body').t(message);
        addMessage(to,$rootScope.self.jid,message);
        StropheSrv.send(msg);
    };

    $scope.$on('connected', function() {
        StropheSrv.addHandler(onMessage, 'message', 'chat');
    });

    $scope.$on('msgrequest', function(event, contact) {
        addTab(contact);
        $scope.activeConversation = contact;
    });

    $scope.delete = function(conversation) {
        delete $scope.conversations[conversation];
    };

    $scope.activate = function(conversation) {
        $scope.activeConversation = conversation;
    };

    $scope.isActive = function(conversation) {
        return $scope.activeConversation == conversation ? 'active' : '';
    };
}

MsgCtl.$inject = ['$scope','$log','StropheSrv', '$rootScope'];