1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-27 03:32:20 -05:00
kaiwa/clientapp/models/muc.js

304 lines
9.2 KiB
JavaScript
Raw Normal View History

2013-09-27 01:52:54 -04:00
/*global app, me, client*/
2013-09-16 19:12:00 -04:00
"use strict";
var _ = require('underscore');
var async = require('async');
var uuid = require('node-uuid');
2013-12-20 02:04:14 -05:00
var htmlify = require('../helpers/htmlify');
2013-09-16 19:12:00 -04:00
var HumanModel = require('human-model');
var Resources = require('./resources');
var Messages = require('./messages');
var Message = require('./message');
2015-03-20 15:36:40 -04:00
var avatarHandler = require('../helpers/avatarHandler');
2013-09-16 19:12:00 -04:00
module.exports = HumanModel.define({
initialize: function (attrs) {
if (attrs.jid) {
this.id = attrs.jid.full;
}
2015-02-09 10:20:28 -05:00
var self = this;
this.resources.bind("add remove reset", function(){
self.membersCount = self.resources.length;
});
2013-09-16 19:12:00 -04:00
},
type: 'muc',
props: {
id: ['string', true],
2013-09-16 19:12:00 -04:00
name: 'string',
autoJoin: ['bool', false, false],
2013-09-16 19:12:00 -04:00
nick: 'string',
jid: 'object'
},
session: {
2013-12-20 02:04:14 -05:00
subject: 'string',
activeContact: ['bool', false, false],
lastInteraction: 'date',
2013-09-16 19:12:00 -04:00
lastSentMessage: 'object',
unreadCount: ['number', false, 0],
persistent: ['bool', false, false],
2015-02-09 10:20:28 -05:00
joined: ['bool', true, false],
membersCount: ['number', false, 0],
2013-09-16 19:12:00 -04:00
},
derived: {
displayName: {
deps: ['name', 'jid'],
fn: function () {
var disp = this.name;
if (!disp) disp = this.jid.jid;
2014-11-21 11:11:05 -05:00
return disp.split('@')[0];
2013-09-16 19:12:00 -04:00
}
},
displayUnreadCount: {
deps: ['unreadCount'],
fn: function () {
if (this.unreadCount > 0) {
if (this.unreadCount < 100)
return this.unreadCount.toString();
else
return '99+';
}
2015-02-09 10:20:28 -05:00
return '';
}
},
2013-12-20 02:04:14 -05:00
displaySubject: {
deps: ['subject'],
fn: function () {
return htmlify.toHTML(this.subject);
}
},
2013-09-16 19:12:00 -04:00
hasUnread: {
deps: ['unreadCount'],
fn: function () {
return this.unreadCount > 0;
}
}
},
collections: {
resources: Resources,
messages: Messages
},
getName: function (jid) {
var nickname = jid.split('/')[1];
var name = nickname;
var xmppContact = me.getContact(nickname);
if (xmppContact) {
name = xmppContact.displayName;
}
return name !== '' ? name : nickname;
},
getNickname: function (jid) {
var nickname = jid.split('/')[1];
return nickname != this.getName(jid) ? nickname : '';
},
getAvatar: function (jid) {
2015-03-20 15:36:40 -04:00
var avatar = avatarHandler.getGravatar('').uri;
var xmppContact = me.getContact(jid.split('/')[1]);
if (xmppContact) {
avatar = xmppContact.avatar;
}
2015-03-16 08:43:20 -04:00
return avatar;
},
2013-09-16 19:12:00 -04:00
addMessage: function (message, notify) {
message.owner = me.jid.bare;
2015-04-07 05:04:53 -04:00
var self = this;
2015-04-04 09:19:34 -04:00
var mentions = [];
2015-04-07 05:04:53 -04:00
var toMe = false;
2015-04-04 09:19:34 -04:00
this.resources.forEach(function (resource) {
if (message.body.toLowerCase().indexOf('@' + resource.mucDisplayName) >= 0) {
mentions.push('@' + resource.mucDisplayName);
2015-04-07 05:04:53 -04:00
if (resource.mucDisplayName === self.nick)
toMe = true;
2015-04-04 09:19:34 -04:00
}
});
if (message.body.toLowerCase().indexOf('@all') >= 0) {
mentions.push('@all');
}
message.mentions = mentions;
var mine = message.from.resource === this.nick;
if (mine) {
message._mucMine = true;
}
2015-02-15 12:06:39 -05:00
if (notify && (!this.activeContact || (this.activeContact && !app.state.focused)) && !mine) {
2013-09-16 19:12:00 -04:00
this.unreadCount++;
2015-04-07 05:04:53 -04:00
if (toMe) {
app.notifications.create(this.displayName, {
body: message.body,
icon: this.avatar,
tag: this.id,
2014-12-10 05:25:19 -05:00
onclick: _.bind(app.navigate, app, '/groupchat/' + encodeURIComponent(this.jid))
});
2015-01-25 14:21:57 -05:00
if (me.soundEnabled)
app.soundManager.play('threetone-alert');
}
else
{
if (me.soundEnabled)
app.soundManager.play('ding');
}
2013-09-16 19:12:00 -04:00
}
2013-12-13 19:16:40 -05:00
message.acked = true;
if (mine) {
2013-12-13 19:16:40 -05:00
this.lastSentMessage = message;
}
2013-09-16 19:12:00 -04:00
var existing = Message.idLookup(message.from.full, message.mid);
2015-02-22 17:43:49 -05:00
if (existing) {
existing.set(message);
existing.save();
} else {
this.messages.add(message);
message.save();
}
2013-12-18 16:31:22 -05:00
2013-09-16 19:12:00 -04:00
var newInteraction = new Date(message.created);
if (!this.lastInteraction || this.lastInteraction < newInteraction) {
this.lastInteraction = newInteraction;
}
},
2015-02-27 13:39:48 -05:00
join: function (manual) {
var self = this;
2013-09-16 19:12:00 -04:00
if (!this.nick) {
this.nick = me.jid.local;
}
2014-01-01 20:42:36 -05:00
this.messages.reset();
this.resources.reset();
2013-09-16 19:12:00 -04:00
client.joinRoom(this.jid, this.nick, {
2015-02-08 18:21:00 -05:00
joinMuc: {
history: {
maxstanzas: 50
}
2013-09-16 19:12:00 -04:00
}
});
2015-02-22 17:43:49 -05:00
2015-02-27 13:39:48 -05:00
if (manual) {
var form = {
fields: [
{
type: 'hidden',
name: 'FORM_TYPE',
value: 'http://jabber.org/protocol/muc#roomconfig'
},
{
type: 'boolean',
name: 'muc#roomconfig_changesubject',
value: true
},
{
type: 'boolean',
name: 'muc#roomconfig_persistentroom',
value: true
},
]
};
client.configureRoom(this.jid, form, function(err, resp) {
if (err) return;
});
if (SERVER_CONFIG.domain && SERVER_CONFIG.admin) {
client.setRoomAffiliation(this.jid, SERVER_CONFIG.admin + '@' + SERVER_CONFIG.domain, 'owner', 'administration', function(err, resp) {
if (err) return;
client.setRoomAffiliation(self.jid, me.jid, 'none', 'administration');
});
}
}
2015-02-22 17:43:49 -05:00
// After a reconnection
client.on('muc:join', function (pres) {
if (self.messages.length) {
self.fetchHistory(true);
}
});
2013-09-16 19:12:00 -04:00
},
2015-02-22 17:43:49 -05:00
fetchHistory: function(allInterval) {
2015-02-08 18:21:00 -05:00
var self = this;
app.whenConnected(function () {
var filter = {
'to': self.jid,
rsm: {
max: 40,
2015-02-22 17:43:49 -05:00
before: !allInterval
2015-02-08 18:21:00 -05:00
}
};
2015-02-22 17:43:49 -05:00
if (allInterval) {
var lastMessage = self.messages.last();
if (lastMessage && lastMessage.created) {
var start = new Date(lastMessage.created);
filter.start = start.toISOString();
}
} else {
var firstMessage = self.messages.first();
if (firstMessage && firstMessage.created) {
var end = new Date(firstMessage.created);
filter.end = end.toISOString();
}
2015-02-08 18:21:00 -05:00
}
client.searchHistory(filter, function (err, res) {
2015-02-08 18:21:00 -05:00
if (err) return;
var results = res.mamResult.items || [];
if (filter.rsm.before) {
results.reverse();
}
2015-02-08 18:21:00 -05:00
results.forEach(function (result) {
var msg = result.forwarded.message;
2015-02-08 18:21:00 -05:00
msg.mid = msg.id;
delete msg.id;
if (!msg.delay) {
msg.delay = result.forwarded.delay;
2015-02-08 18:21:00 -05:00
}
if (msg.replace) {
var original = Message.idLookup(msg.from[msg.type == 'groupchat' ? 'full' : 'bare'], msg.replace);
// Drop the message if editing a previous, but
// keep it if it didn't actually change an
// existing message.
if (original && original.correct(msg)) return;
}
var message = new Message(msg);
message.archivedId = result.id;
2015-02-08 18:21:00 -05:00
message.acked = true;
self.addMessage(message, false);
});
2015-02-22 17:43:49 -05:00
if (allInterval) {
self.trigger('refresh');
if (results.length === filter.rsm.max)
2015-02-22 17:43:49 -05:00
self.fetchHistory(true);
}
2015-02-08 18:21:00 -05:00
});
});
},
2013-09-16 19:12:00 -04:00
leave: function () {
2014-01-01 20:42:36 -05:00
this.resources.reset();
2013-09-16 19:12:00 -04:00
client.leaveRoom(this.jid, this.nick);
2015-04-04 12:51:05 -04:00
},
destroy: function (cb) {
client.sendIq({
type: 'set',
to: this.jid,
mucOwner: {
destroy: {
jid: this.jid,
password: '',
reason: ''
} }
}, function (err, res) {
cb(err);
});
2013-09-16 19:12:00 -04:00
}
});