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
|
'use strict';
/* App Controllers */
function ConnectCtl($scope, StropheSrv, $log, $rootScope) {
function connect_callback(status){
if ( status == Strophe.Status.CONNECTING ) {
$log.log('Strophe is connecting.');
} else if ( status == Strophe.Status.CONNFAIL ) {
$log.log('Strophe failed to connect.');
} else if ( status == Strophe.Status.DISCONNECTING ) {
$log.log('Strophe is disconnecting.');
} else if ( status == Strophe.Status.DISCONNECTED ) {
$log.log('Strophe is disconnected.');
} else if ( status == Strophe.Status.CONNECTED ) {
$log.log('Strophe is connected.');
$rootScope.$broadcast('connected', true);
}
};
$scope.login = function () {
StropheSrv.login($scope.username, $scope.password, connect_callback);
};
}
ConnectCtl.$inject = ['$scope', 'StropheSrv', '$log', '$rootScope'];
function RosterCtl($scope, StropheSrv, $log) {
$scope.contacts = {};
function onRoster(iq) {
$log.log('im here');
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');
$scope.contacts[jid] = {name: name};
});
};
function onPresence(presence) {
var who = $(presence).attr('from');
var jid = Strophe.getBareJidFromJid(who);
var type = $(presence).attr('type');
if (type !== 'error') {
if (type === 'unavailable') {
$scope.contacts[jid]['status']='offline';
}
else {
var show = $(presence).find('show').text();
if (show === '') {
$scope.contacts[jid]['status'] = 'online';
}else{
$scope.contacts[jid]['status'] = 'away';
}
}
delete $scope.contacts[jid];
}
return true;
}
$scope.$on('connected', function() {
StropheSrv.addHandler(onPresence, 'presence');
$scope.getRoster();
StropheSrv.send($pres());
});
$scope.getRoster = function () {
var query = $iq({type : 'get'}).c('query', {xmlns : Strophe.NS.ROSTER});
StropheSrv.sendIQ(query, onRoster);
};
}
RosterCtl.$inject = ['$scope','StropheSrv','$log'];
|