1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-15 22:05:09 -05:00
kaiwa/clientapp/models/mucs.js

168 lines
4.7 KiB
JavaScript
Raw Normal View History

2013-09-16 19:12:00 -04:00
/*global app, client*/
"use strict";
var async = require('async');
var BaseCollection = require('./baseCollection');
var MUC = require('./muc');
module.exports = BaseCollection.extend({
type: 'mucs',
model: MUC,
comparator: function (model1, model2) {
var name1 = model1.displayName.toLowerCase();
var name2 = model2.displayName.toLowerCase();
if (name1 === name2) {
return 0;
}
if (name1 < name2) {
return -1;
}
return 1;
},
initialize: function (model, options) {
this.bind('change', this.sort, this);
},
loadBookmarks: function () {
2013-09-16 19:12:00 -04:00
var self = this;
app.whenConnected(function () {
client.getBookmarks(function (err, res) {
if (err) return;
var mucs = res.privateStorage.bookmarks.conferences;
mucs.forEach(function (muc) {
self.add(muc);
2013-12-17 12:36:47 -05:00
if (muc.autoJoin) {
self.get(muc.jid).join();
}
2013-09-16 19:12:00 -04:00
});
});
});
},
init: function () {
var self = this;
app.whenConnected(function () {
function _fetch(init) {
self.fetch(function() {
self.update();
if (init) self.trigger('loaded');
setTimeout(_fetch, 30000);
});
}
_fetch(true);
});
},
fetch: function(cb) {
var self = this;
while(app.mucInfos.length > 0) {
app.mucInfos.pop();
}
if (SERVER_CONFIG.muc) {
if (client.sessionStarted) {
2015-02-11 11:39:46 -05:00
var rooms = [];
client.getDiscoItems(SERVER_CONFIG.muc, '', function (err, res) {
if (err) return;
2015-02-11 11:39:46 -05:00
rooms = res.discoItems.items;
2015-02-27 13:25:13 -05:00
if (rooms === undefined)
rooms = [];
2015-02-11 11:39:46 -05:00
var roomNum = 0;
2015-02-27 13:25:13 -05:00
rooms.forEach (function (room) {
client.getDiscoInfo(room.jid, '', function (err, res) {
roomNum++;
if (err) return;
var features = res.discoInfo.features;
var persistent = features.indexOf("muc_persistent") > -1;
var mucInfo = {
id: room.jid.full,
name: room.name,
jid: room.jid,
nick: me.nick,
autoJoin: persistent,
persistent: persistent
};
app.mucInfos.push(mucInfo);
}).then(function() {
if (cb && roomNum == rooms.length) cb();
});
});
2015-02-11 11:39:46 -05:00
}).then(function() {
if (cb && !rooms.length) cb();
});
2015-02-11 11:39:46 -05:00
} else {
app.whenConnected(function () {
2015-02-11 11:39:46 -05:00
self.fetch(cb);
});
}
2015-02-11 11:39:46 -05:00
return;
}
2015-02-11 11:39:46 -05:00
if (cb) cb();
},
update: function () {
if (SERVER_CONFIG.muc) {
var toRemove = [];
for ( var i = 0; i < this.models.length; i++) {
toRemove.push(this.models[i]);
}
for (var i = 0; i < app.mucInfos.length; i++) {
var mucInfo = app.mucInfos[i];
var muc = this.get(mucInfo.jid);
if (muc) {
muc.name = mucInfo.name;
muc.autoJoin = mucInfo.autoJoin;
muc.persistent = mucInfo.persistent;
var index = toRemove.indexOf(muc);
if (index > -1) {
toRemove.splice(index, 1);
}
}
else {
this.add(mucInfo);
muc = this.get(mucInfo.jid);
}
if (muc.autoJoin && !muc.joined) {
muc.join();
}
}
this.remove(toRemove);
while(app.mucInfos.length > 0) {
app.mucInfos.pop();
}
}
},
2013-09-16 19:12:00 -04:00
save: function (cb) {
var self = this;
app.whenConnected(function () {
var models = [];
self.models.forEach(function (model) {
models.push({
name: model.name,
jid: model.jid,
nick: model.nick,
autoJoin: model.autoJoin
});
});
client.setBookmarks({conferences: models}, cb);
});
}
});