1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-15 14:04:59 -05:00
kaiwa/clientapp/models/muc.js

137 lines
3.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;
}
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,
2014-12-10 05:25:19 -05:00
onclick: _.bind(app.navigate, app, '/groupchat/' + encodeURIComponent(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);
}
});