kaiwa/clientapp/models/muc.js

228 lines
6.8 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');
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
},
addMessage: function (message, notify) {
message.owner = me.jid.bare;
var mine = message.from.resource === this.nick;
if (mine) {
message._mucMine = true;
}
else {
var mentions = [];
if (message.body.toLowerCase().indexOf('@' + this.nick.toLowerCase()) >= 0) {
mentions.push('@' + this.nick);
}
if (message.body.toLowerCase().indexOf('@all') >= 0) {
mentions.push('@all');
}
message.mentions = mentions;
}
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++;
if (message.mentions.length) {
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
2015-02-22 17:43:49 -05:00
var existing = Message.idLookup(message.from['full'], message.mid);
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;
}
},
join: function () {
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
var self = this;
// 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.getHistory(filter, function (err, res) {
if (err) return;
var results = res.mamQuery.results || [];
results.forEach(function (result) {
var msg = result.mam.forwarded.message;
msg.mid = msg.id;
delete msg.id;
if (!msg.delay) {
msg.delay = result.mam.forwarded.delay;
}
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.mam.id;
message.acked = true;
self.addMessage(message, false);
});
2015-02-22 17:43:49 -05:00
if (allInterval) {
self.trigger('refresh');
if (results.length == 40)
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);
}
});