/*
 * Copyright (c) Novedia Group 2012.
 *
 *     This file is part of Hubiquitus.
 *
 *     Hubiquitus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Hubiquitus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Hubiquitus.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Searches for the subscriptions of the <Sender> to channels that are
 * currently active.
 */

var status = require('../codes.js').hResultStatus;

//Events
var util = require('util');
var events = require('events').EventEmitter;

var hGetSubscriptions = function(){
    events.call(this);
};
util.inherits(hGetSubscriptions, events);

/**
 * Method executed each time an hCommand with cmd = 'hGetSubscriptions' is received.
 * Once the execution finishes we should emit a result.
 * @param hCommand - hCommand received with cmd = 'hGetSubscriptions'
 * @param context - Models from the database to store/search data. See lib/mongo.js
 * @emit result - {
 *    hCommand: hCommand //hCommand received
 *    status: //Constant from var status to indicate the result of the hCommand
 *    result: //An optional result object defined by the hCommand
 * };
 */
hGetSubscriptions.prototype.exec = function(hCommand, context){
    var self = this;

    context.models.user.findOne({jid: hCommand.sender}, function(err, doc){
        if(!err){
            log.info('Subscriptions for', hCommand.sender, 'recovered successfully');
            var subscriptions = doc && doc.subs ? doc.subs : [];
            self.emit('result', {hCommand: hCommand, status: status.OK, result: subscriptions});
        }else{
            var errMsg = JSON.stringify(err);
            log.info('Error retrieving subscriptions', errMsg);
            self.emit('result', {hCommand: hCommand, status: status.TECH_ERROR, result: errMsg});
        }
    });
};

/**
 * Create an instance of hGetSubscriptions and expose it
 */
var hCommand = new hGetSubscriptions();
exports.Command = hCommand;
@@ -1709,16 +1709,72 @@
  = this;
+%0A    var resultValue = null;%0A    var statusValue = null;
 %0A%0A    co
@@ -2012,16 +2012,17 @@
 s : %5B%5D;%0A
+%0A
         
@@ -2029,92 +2029,791 @@
     
-self.emit('result', %7BhCommand: hCommand, status: status.OK, result: subscriptions%7D);
+//Only return active channels%0A            if(subscriptions.length %3E 0)%7B%0A%0A                var channelsRegEx = '';%0A                for(var i = 0; i %3C subscriptions.length -1; i++)%0A                    channelsRegEx += subscriptions%5Bi%5D + '%7C';%0A                channelsRegEx += subscriptions%5Bsubscriptions.length -1%5D;%0A%0A                context.models.hChannel.find(%7Bchid: new RegExp(channelsRegEx), active: 'Y'%7D, function(err, docs)%7B%0A                    if(!err)%7B%0A                        resultValue = %5B%5D;%0A                        docs.forEach(function(doc)%7B resultValue.push(doc.chid)%7D);%0A%0A                    %7D else%7B%0A                        statusValue = status.TECH_ERROR;%0A                        resultValue = JSON.stringify(err);%0A                    %7D%0A%0A                %7D);%0A            %7D%0A
 %0A   
@@ -2840,18 +2840,19 @@
     
-var errMsg
+resultValue
  = J
@@ -2867,32 +2867,185 @@
 ngify(err);%0A    
+        statusValue = status.TECH_ERROR;%0A        %7D%0A%0A        statusValue = statusValue %7C%7C status.OK;%0A        resultValue = resultValue %7C%7C subscriptions;%0A%0A
         log.info
@@ -3050,13 +3050,16 @@
 fo('
-Error
+Finished
  ret
@@ -3083,24 +3083,50 @@
 ions
-', errMsg);%0A    
+ with status', statusValue, resultValue);%0A
     
@@ -3188,48 +3188,37 @@
 atus
-.TECH_ERROR, result: errMsg%7D);%0A        %7D
+Value, result: resultValue%7D);
 %0A   

Support for inactive channels