'use strict'; /* App Controllers */ function ConnectCtl($scope, StropheSrv, $log, $rootScope) { $rootScope.username = $scope.username; $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); }; $scope.disconnect = function() { StropheSrv.disconnect(); }; } ConnectCtl.$inject = ['$scope', 'StropheSrv', '$log', '$rootScope']; function RosterCtl($scope, StropheSrv, $log) { $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()); }; 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)){ if (type !== 'error') { if (type === 'unavailable') { $log.log(jid +' is offline'); $scope.contacts[jid]['status'] = 'offline'; }else{ var show = $(presence).find('show').text(); if (show === '') { $scope.contacts[jid]['status'] = 'online'; }else{ $log.log(jid +' is away'); $scope.contacts[jid]['status'] = 'away'; } } } } } $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); }; } RosterCtl.$inject = ['$scope','StropheSrv','$log'];