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: {
|
2014-01-05 07:19:46 -05:00
|
|
|
id: ['string', true],
|
2013-09-16 19:12:00 -04:00
|
|
|
name: 'string',
|
2014-01-05 07:19:46 -05:00
|
|
|
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',
|
2014-01-05 07:19:46 -05:00
|
|
|
activeContact: ['bool', false, false],
|
|
|
|
lastInteraction: 'date',
|
2013-09-16 19:12:00 -04:00
|
|
|
lastSentMessage: 'object',
|
2014-01-05 07:19:46 -05:00
|
|
|
unreadCount: ['number', false, 0],
|
2015-02-09 08:56:02 -05:00
|
|
|
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 () {
|
2015-02-09 08:56:02 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
2013-09-25 13:39:57 -04:00
|
|
|
displayUnreadCount: {
|
|
|
|
deps: ['unreadCount'],
|
|
|
|
fn: function () {
|
|
|
|
if (this.unreadCount > 0) {
|
2015-02-09 08:56:02 -05:00
|
|
|
if (this.unreadCount < 100)
|
|
|
|
return this.unreadCount.toString();
|
|
|
|
else
|
|
|
|
return '99+'
|
2013-09-25 13:39:57 -04:00
|
|
|
}
|
2015-02-09 10:20:28 -05:00
|
|
|
return '';
|
2013-09-25 13:39:57 -04:00
|
|
|
}
|
|
|
|
},
|
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;
|
|
|
|
|
2013-12-16 23:20:29 -05:00
|
|
|
var mine = message.from.resource === this.nick;
|
|
|
|
|
|
|
|
if (mine) {
|
|
|
|
message._mucMine = true;
|
|
|
|
}
|
|
|
|
if (!mine && message.body.toLowerCase().indexOf(this.nick.toLowerCase()) >= 0) {
|
|
|
|
message.mentions = this.nick;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notify && (!this.activeContact || (this.activeContact && !app.state.focused)) && !mine) {
|
2013-09-16 19:12:00 -04:00
|
|
|
this.unreadCount++;
|
2013-12-16 23:20:29 -05:00
|
|
|
if (message.mentions) {
|
|
|
|
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))
|
2013-12-16 23:20:29 -05:00
|
|
|
});
|
|
|
|
}
|
2013-09-16 19:12:00 -04:00
|
|
|
}
|
|
|
|
|
2013-12-13 19:16:40 -05:00
|
|
|
message.acked = true;
|
2013-12-18 16:31:22 -05:00
|
|
|
message.save();
|
2013-12-13 19:16:40 -05:00
|
|
|
|
2013-12-16 23:20:29 -05:00
|
|
|
if (mine) {
|
2013-12-13 19:16:40 -05:00
|
|
|
this.lastSentMessage = message;
|
|
|
|
}
|
2013-09-16 19:12:00 -04:00
|
|
|
|
2013-12-18 16:31:22 -05:00
|
|
|
this.messages.add(message);
|
|
|
|
|
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();
|
2015-02-09 08:56:02 -05:00
|
|
|
|
2013-09-16 19:12:00 -04:00
|
|
|
client.joinRoom(this.jid, this.nick, {
|
|
|
|
history: {
|
2013-10-08 11:03:58 -04:00
|
|
|
maxstanzas: 50
|
|
|
|
//since: this.lastInteraction
|
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);
|
|
|
|
}
|
|
|
|
});
|