kaiwa/clientapp/models/muc.js

125 lines
3.3 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;
}
},
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],
2013-09-16 19:12:00 -04:00
joined: ['bool', true, false]
},
derived: {
displayName: {
deps: ['name', 'jid'],
fn: function () {
return this.name || this.jid;
}
},
displayUnreadCount: {
deps: ['unreadCount'],
fn: function () {
if (this.unreadCount > 0) {
return this.unreadCount.toString();
}
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;
}
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++;
if (message.mentions) {
app.notifications.create(this.displayName, {
body: message.body,
icon: this.avatar,
tag: this.id,
onclick: _.bind(app.navigate, app, '/groupchat/' + this.jid)
});
}
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
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();
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);
}
});