1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-15 05:55:01 -05:00
kaiwa/clientapp/models/message.js

192 lines
5.7 KiB
JavaScript
Raw Normal View History

/*global app, me*/
2013-08-29 23:38:28 -04:00
"use strict";
var HumanModel = require('human-model');
2013-09-25 23:38:00 -04:00
var templates = require('../templates');
2013-09-26 15:34:31 -04:00
var htmlify = require('../helpers/htmlify');
2013-08-20 13:45:06 -04:00
2013-09-05 19:53:23 -04:00
module.exports = HumanModel.define({
2013-08-29 23:38:28 -04:00
initialize: function (attrs) {
2013-10-08 11:03:58 -04:00
this._created = new Date(Date.now());
2013-08-20 13:45:06 -04:00
},
type: 'message',
props: {
owner: 'string',
id: ['string', true, ''],
2013-09-13 02:47:44 -04:00
to: ['object', true],
from: ['object', true],
2013-08-20 13:45:06 -04:00
body: ['string', true, ''],
type: ['string', true, 'normal'],
acked: ['bool', true, false],
archivedId: ['string', true, '']
2013-08-20 13:45:06 -04:00
},
derived: {
mine: {
2013-12-13 19:16:40 -05:00
deps: ['from', '_mucMine'],
2013-08-20 13:45:06 -04:00
fn: function () {
2013-12-13 19:16:40 -05:00
return this._mucMine || me.isMe(this.from);
2013-08-20 13:45:06 -04:00
}
},
2013-09-25 23:38:00 -04:00
sender: {
deps: ['from', 'mine'],
fn: function () {
if (this.mine) {
return me;
} else {
return me.getContact(this.from);
}
}
},
2013-08-20 13:45:06 -04:00
delayed: {
deps: ['delay'],
fn: function () {
return !!this.delay;
}
},
created: {
deps: ['delay', '_created'],
fn: function () {
if (this.delay && this.delay.stamp) {
return this.delay.stamp;
}
return this._created;
}
},
formattedTime: {
deps: ['created'],
fn: function () {
2013-08-29 23:38:28 -04:00
if (this.created) {
2013-12-17 01:07:09 -05:00
var month = this.created.getMonth() + 1;
2013-08-30 01:37:24 -04:00
var day = this.created.getDate();
var hour = this.created.getHours();
var minutes = this.created.getMinutes();
var m = (hour >= 12) ? 'p' : 'a';
var strDay = (day < 10) ? '0' + day : day;
var strHour = (hour < 10) ? '0' + hour : hour;
var strMin = (minutes < 10) ? '0' + minutes: minutes;
return '' + month + '/' + strDay + ' ' + strHour + ':' + strMin + m;
2013-08-29 23:38:28 -04:00
}
return undefined;
2013-08-20 13:45:06 -04:00
}
},
pending: {
deps: ['acked'],
fn: function () {
return !this.acked;
}
2013-09-24 16:24:35 -04:00
},
nick: {
deps: ['mine', 'type'],
fn: function () {
if (this.type === 'groupchat') {
return this.from.resource;
}
2013-12-13 19:16:40 -05:00
if (this.mine) {
return 'me';
}
2013-09-24 16:24:35 -04:00
return me.getContact(this.from.bare).displayName;
}
2013-09-25 23:38:00 -04:00
},
2013-09-26 15:34:31 -04:00
processedBody: {
deps: ['body', 'meAction', 'mentions'],
2013-09-26 15:34:31 -04:00
fn: function () {
2013-12-16 17:28:13 -05:00
var body = this.body;
if (this.meAction) {
body = body.substr(4);
}
body = htmlify.toHTML(body);
if (this.mentions) {
var existing = htmlify.toHTML(this.mentions);
body = body.replace(existing, '<span class="mention">' + existing + '</span>');
}
return body;
2013-09-26 15:34:31 -04:00
}
},
2013-09-25 23:38:00 -04:00
partialTemplateHtml: {
deps: ['edited', 'pending', 'body'],
cache: false,
fn: function () {
2013-12-13 19:16:40 -05:00
if (this.type === 'groupchat') {
return templates.includes.mucBareMessage({message: this});
} else {
return templates.includes.bareMessage({message: this});
}
2013-09-25 23:38:00 -04:00
}
},
templateHtml: {
fn: function () {
2013-12-13 19:16:40 -05:00
if (this.type === 'groupchat') {
return templates.includes.mucWrappedMessage({message: this});
} else {
return templates.includes.wrappedMessage({message: this});
}
2013-09-25 23:38:00 -04:00
}
},
classList: {
cache: false,
fn: function () {
var res = [];
if (this.mine) res.push('mine');
if (this.pending) res.push('pending');
if (this.delayed) res.push('delayed');
if (this.edited) res.push('edited');
2013-12-16 17:28:13 -05:00
if (this.meAction) res.push('meAction');
2013-09-25 23:38:00 -04:00
return res.join(' ');
}
2013-12-16 17:28:13 -05:00
},
meAction: {
deps: ['body'],
fn: function () {
return this.body.indexOf('/me') === 0;
}
2013-08-20 13:45:06 -04:00
}
},
session: {
_created: 'date',
2013-12-13 19:16:40 -05:00
_mucMine: 'bool',
2013-08-20 13:45:06 -04:00
receiptReceived: ['bool', true, false],
edited: ['bool', true, false],
delay: 'object',
mentions: ['string', false, '']
2013-08-20 13:45:06 -04:00
},
correct: function (msg) {
if (this.from.full !== msg.from.full) return false;
2013-08-20 13:45:06 -04:00
delete msg.id;
2013-08-20 13:45:06 -04:00
this.set(msg);
2013-10-08 11:03:58 -04:00
this._created = new Date(Date.now());
2013-08-20 13:45:06 -04:00
this.edited = true;
this.save();
return true;
},
save: function () {
var data = {
archivedId: this.archivedId,
owner: this.owner,
to: this.to,
from: this.from,
created: this.created,
body: this.body,
type: this.type,
delay: this.delay,
edited: this.edited
};
app.storage.archive.add(data);
2013-09-25 23:38:00 -04:00
},
shouldGroupWith: function (previous) {
2013-12-13 19:16:40 -05:00
if (this.type === 'groupchat') {
return previous && previous.from.full === this.from.full;
} else {
return previous && previous.from.bare === this.from.bare;
}
2013-08-20 13:45:06 -04:00
}
});