Created
July 29, 2016 15:45
-
-
Save ndelitski/1e4b7bba3cf4da0e1243fbb538ca94c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_sendToServer: function (data) { | |
var quotedPart = '', | |
user = getCurrentUser(), | |
shouldRemoveParticipantIfFail, | |
lastMessage; | |
if (this.emailModel.get('messages').length ) { | |
// сообщения отсортированы в обратном порядке, самый последний – в начале | |
lastMessage = this.emailModel.get('messages').find(function (m) { | |
return !!m.id; | |
}); | |
if (lastMessage) { | |
lastMessage = lastMessage.toJSON(); | |
} | |
if (lastMessage && lastMessage.mime.htmlBody) { | |
var date = moment.utc(lastMessage.mime.date).user().format("ddd, MMM, YYYY [at] h:mm"), | |
author = lastMessage.mime.from.name | |
? lastMessage.mime.from.name + '<' + lastMessage.mime.from.address + '>' | |
: lastMessage.mime.from.address, | |
signature = '<p> On ' + date + ', ' + author + ' wrote:</p>'; | |
quotedPart += signature + '<blockquote>' + lastMessage.mime.htmlBody + '</blockquote>' | |
} | |
} | |
var eventData = { | |
type: 'Task::AddComment', | |
createdAt: moment.user.now(), | |
creator: _.extend({}, user, {email: user.emailBox || user.email}), | |
// пометка персональности сохраняется только для входящих сообщений адресованных мне | |
// recipient: data.recipient, | |
text: data.text, | |
state: 'pending', | |
files: data.files | |
}, | |
requestData = { | |
htmlBody: data.text + quotedPart, | |
replyToMessageId: lastMessage && lastMessage.messageId, | |
attachments: data.files | |
}; | |
if (data.recipient) { | |
requestData.toEmails = [{ | |
name: data.recipient.fullName || data.recipient.name, | |
address: data.recipient.email | |
}]; | |
shouldRemoveParticipantIfFail = !this.emailModel.get('participants').findWhere({email: data.recipient.email}); | |
// todo такто нужно добавлять только в случае успеха | |
this.emailModel.get('participants').add(data.recipient); | |
} else { // берем автора последнего сообщения | |
requestData.toEmails = [lastMessage.mime.from]; | |
} | |
var participants = this.emailModel.get('participants').toJSON(); | |
requestData.ccEmails = participants.filter(function(p) { | |
return !_.findWhere(requestData.toEmails, {address: p.email}); | |
}).map(function(p) { | |
return { | |
name: p.fullName || p.name, | |
address: p.email | |
} | |
}); | |
return io.uuid(4).done(function(uuid) { | |
requestData.tag = eventData.tag = uuid; | |
var eventModel = this.model.addEvent(eventData, {parse: false, wasRead: true, appendTo: 'head'}); | |
// при отправке сообщения сразу скролим в конец | |
this._scrollTo({to: 'end'}); | |
PendingMessagesStore.put(uuid, { | |
tag: uuid, | |
sentAt: moment.utc().toDate(), | |
thread: { | |
id: this.emailModel.id, | |
threadId: this.emailModel.get('threadId') | |
}, | |
message: _.extend({}, requestData, {htmlBody: data.text}) // override text w/o quotation part | |
}); | |
return this.postComment(requestData) | |
.done(function () { | |
PendingMessagesStore.removeByTag(uuid); | |
}.bind(this)) | |
.fail(function () { | |
shouldRemoveParticipantIfFail && this.emailModel.get('participants').remove(data.recipient.email); | |
eventModel.set('state', 'failed'); | |
}.bind(this)); | |
}.bind(this)); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment