2014-11-25 22:09:55 +01:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Message
|
|
|
|
* @constructor
|
|
|
|
* @param {string} accountId - the account ID
|
|
|
|
* @param {string} mailboxPath - an array of the mailbox path components
|
|
|
|
* @param {object} futureAddressBookData - either an object literal or a promise
|
2015-08-26 17:56:47 +02:00
|
|
|
* @param {bool} lazy - do "lazy loading" so we are very quick at initializing message instances
|
2014-11-25 22:09:55 +01:00
|
|
|
*/
|
2015-08-26 17:56:47 +02:00
|
|
|
function Message(accountId, mailbox, futureMessageData, lazy) {
|
2014-11-25 22:09:55 +01:00
|
|
|
this.accountId = accountId;
|
2014-12-16 21:47:34 +01:00
|
|
|
this.$mailbox = mailbox;
|
2015-03-18 18:41:14 +01:00
|
|
|
this.$hasUnsafeContent = false;
|
|
|
|
this.$loadUnsafeContent = false;
|
2015-08-21 19:37:18 +02:00
|
|
|
this.$showDetailedRecipients = false;
|
2015-04-18 14:50:06 +02:00
|
|
|
this.editable = {to: [], cc: [], bcc: []};
|
2015-08-26 17:56:47 +02:00
|
|
|
this.selected = false;
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
// Data is immediately available
|
|
|
|
if (typeof futureMessageData.then !== 'function') {
|
|
|
|
//console.debug(JSON.stringify(futureMessageData, undefined, 2));
|
2015-08-26 17:56:47 +02:00
|
|
|
if (angular.isDefined(lazy) && lazy) {
|
|
|
|
this.uid = futureMessageData.uid;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
angular.extend(this, futureMessageData);
|
|
|
|
this.$formatFullAddresses();
|
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The promise will be unwrapped first
|
|
|
|
this.$unwrap(futureMessageData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Message
|
|
|
|
* @desc The factory we'll use to register with Angular
|
|
|
|
* @returns the Message constructor
|
|
|
|
*/
|
2015-09-30 17:28:22 +02:00
|
|
|
Message.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Gravatar', 'Resource', 'Preferences', function($q, $timeout, $log, Settings, Gravatar, Resource, Preferences) {
|
2014-11-25 22:09:55 +01:00
|
|
|
angular.extend(Message, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
|
|
|
$log: $log,
|
2015-08-04 19:52:31 +02:00
|
|
|
$gravatar: Gravatar,
|
2015-08-03 17:53:54 +02:00
|
|
|
$$resource: new Resource(Settings.activeUser('folderURL') + 'Mail', Settings.activeUser())
|
|
|
|
});
|
|
|
|
// Initialize tags form user's defaults
|
|
|
|
Preferences.ready().then(function() {
|
|
|
|
if (Preferences.defaults.SOGoMailLabelsColors) {
|
|
|
|
Message.$tags = Preferences.defaults.SOGoMailLabelsColors;
|
|
|
|
}
|
2015-09-04 22:43:01 +02:00
|
|
|
if (Preferences.defaults.SOGoMailDisplayRemoteInlineImages &&
|
|
|
|
Preferences.defaults.SOGoMailDisplayRemoteInlineImages == 'always') {
|
|
|
|
Message.$displayRemoteInlineImages = true;
|
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
});
|
2015-05-05 21:22:52 +02:00
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
return Message; // return constructor
|
|
|
|
}];
|
|
|
|
|
2015-07-29 21:16:43 +02:00
|
|
|
/**
|
|
|
|
* @module SOGo.MailerUI
|
|
|
|
* @desc Factory registration of Message in Angular module.
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
angular.module('SOGo.MailerUI');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
angular.module('SOGo.MailerUI', ['SOGo.Common']);
|
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
angular.module('SOGo.MailerUI')
|
2015-05-06 04:06:13 +02:00
|
|
|
.factory('Message', Message.$factory);
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2015-05-04 19:56:29 +02:00
|
|
|
/**
|
|
|
|
* @function filterTags
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Search for tags (ie., mail labels) matching some criterias
|
|
|
|
* @param {string} search - the search string to match
|
|
|
|
* @returns a collection of strings
|
|
|
|
*/
|
|
|
|
Message.filterTags = function(query) {
|
2015-09-10 17:44:09 +02:00
|
|
|
var re = new RegExp(query, 'i'),
|
|
|
|
results = [];
|
|
|
|
|
|
|
|
_.forEach(_.keys(Message.$tags), function(tag) {
|
|
|
|
var pair = Message.$tags[tag];
|
|
|
|
if (pair[0].search(re) != -1) {
|
|
|
|
results.push({ name: tag, description: pair[0], color: pair[1] });
|
|
|
|
}
|
2015-05-04 19:56:29 +02:00
|
|
|
});
|
2015-09-10 17:44:09 +02:00
|
|
|
return results;
|
2015-05-04 19:56:29 +02:00
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
/**
|
|
|
|
* @function $absolutePath
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Build the path of the message
|
|
|
|
* @returns a string representing the path relative to the mail module
|
|
|
|
*/
|
2014-12-11 15:15:33 +01:00
|
|
|
Message.prototype.$absolutePath = function(options) {
|
2015-09-03 20:09:13 +02:00
|
|
|
if (angular.isUndefined(this.id) || options) {
|
2015-08-26 17:56:47 +02:00
|
|
|
var path;
|
|
|
|
path = _.map(this.$mailbox.path.split('/'), function(component) {
|
|
|
|
return 'folder' + component.asCSSIdentifier();
|
|
|
|
});
|
|
|
|
path.splice(0, 0, this.accountId); // insert account ID
|
|
|
|
if (options && options.asDraft && this.draftId) {
|
|
|
|
path.push(this.draftId); // add draft ID
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
path.push(this.uid); // add message UID
|
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2015-08-26 17:56:47 +02:00
|
|
|
this.id = path.join('/');
|
2014-12-11 15:15:33 +01:00
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2015-08-26 17:56:47 +02:00
|
|
|
return this.id;
|
2014-11-25 22:09:55 +01:00
|
|
|
};
|
|
|
|
|
2014-12-16 21:47:34 +01:00
|
|
|
/**
|
|
|
|
* @function $setUID
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Change the UID of the message. This happens when saving a draft.
|
|
|
|
* @param {number} uid - the new message UID
|
|
|
|
*/
|
|
|
|
Message.prototype.$setUID = function(uid) {
|
2015-12-01 17:19:18 +01:00
|
|
|
var oldUID = (this.uid || -1);
|
2014-12-16 21:47:34 +01:00
|
|
|
|
2015-12-01 17:19:18 +01:00
|
|
|
if (oldUID != parseInt(uid)) {
|
2014-12-16 21:47:34 +01:00
|
|
|
this.uid = uid;
|
2015-12-01 17:19:18 +01:00
|
|
|
if (oldUID > -1) {
|
|
|
|
oldUID = oldUID.toString();
|
|
|
|
if (angular.isDefined(this.$mailbox.uidsMap[oldUID])) {
|
|
|
|
this.$mailbox.uidsMap[uid] = this.$mailbox.uidsMap[oldUID];
|
|
|
|
delete this.$mailbox.uidsMap[oldUID];
|
|
|
|
}
|
2014-12-16 21:47:34 +01:00
|
|
|
}
|
2015-12-01 22:26:30 +01:00
|
|
|
else {
|
|
|
|
// Refresh selected folder if it's the drafts mailbox
|
|
|
|
if (this.$mailbox.constructor.selectedFolder.type == 'draft') {
|
|
|
|
this.$mailbox.constructor.selectedFolder.$filter();
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 21:47:34 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-11 15:15:33 +01:00
|
|
|
/**
|
|
|
|
* @function $formatFullAddresses
|
|
|
|
* @memberof Message.prototype
|
2014-12-12 03:52:46 +01:00
|
|
|
* @desc Format all sender and recipients addresses with a complete description (name <email>).
|
2015-08-21 19:37:18 +02:00
|
|
|
* This function also generates a gravatar for each email address, and a short name
|
2014-12-11 15:15:33 +01:00
|
|
|
*/
|
|
|
|
Message.prototype.$formatFullAddresses = function() {
|
|
|
|
var _this = this;
|
2015-08-21 19:37:18 +02:00
|
|
|
var identities = _.pluck(_this.$mailbox.$account.identities, 'email');
|
2014-12-11 15:15:33 +01:00
|
|
|
|
|
|
|
// Build long representation of email addresses
|
|
|
|
_.each(['from', 'to', 'cc', 'bcc', 'reply-to'], function(type) {
|
|
|
|
_.each(_this[type], function(data, i) {
|
2015-08-21 19:37:18 +02:00
|
|
|
if (data.name && data.name != data.email) {
|
2014-12-11 15:15:33 +01:00
|
|
|
data.full = data.name + ' <' + data.email + '>';
|
2015-08-21 19:37:18 +02:00
|
|
|
|
|
|
|
// If we have "Alice Foo" as name, we grab "Alice"
|
|
|
|
if (data.name.split(' ').length)
|
|
|
|
data.shortname = data.name.split(' ')[0].replace('\'','');
|
|
|
|
}
|
|
|
|
else {
|
2014-12-11 15:15:33 +01:00
|
|
|
data.full = '<' + data.email + '>';
|
2015-08-21 19:37:18 +02:00
|
|
|
data.shortname = data.email.split('@')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the gravatar
|
|
|
|
data.image = Message.$gravatar(data.email, 32);
|
|
|
|
|
|
|
|
// If the current user is the recepient, overwrite
|
|
|
|
// the short name with 'me'
|
|
|
|
if (_.indexOf(identities, data.email) >= 0)
|
2015-08-22 13:51:13 +02:00
|
|
|
data.shortname = l('me');
|
2015-08-21 19:37:18 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $shortRecipients
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Format all recipients into a very compact string
|
|
|
|
* @returns a compacted string of all recipients
|
|
|
|
*/
|
|
|
|
Message.prototype.$shortRecipients = function() {
|
|
|
|
var _this = this;
|
|
|
|
var result = [];
|
|
|
|
|
|
|
|
// Build long representation of email addresses
|
|
|
|
_.each(['to', 'cc', 'bcc'], function(type) {
|
|
|
|
_.each(_this[type], function(data, i) {
|
|
|
|
result.push(data.shortname);
|
2014-12-11 15:15:33 +01:00
|
|
|
});
|
|
|
|
});
|
2015-08-21 19:37:18 +02:00
|
|
|
|
|
|
|
return result.join(', ');
|
2014-12-11 15:15:33 +01:00
|
|
|
};
|
|
|
|
|
2014-12-06 04:49:08 +01:00
|
|
|
/**
|
|
|
|
* @function $shortAddress
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Format the first address of a specific type with a short description.
|
|
|
|
* @returns a string of the name or the email of the envelope address type
|
|
|
|
*/
|
|
|
|
Message.prototype.$shortAddress = function(type) {
|
|
|
|
var address = '';
|
|
|
|
if (this[type] && this[type].length > 0) {
|
|
|
|
address = this[type][0].name || this[type][0].email || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
|
|
|
};
|
|
|
|
|
2015-11-24 22:30:36 +01:00
|
|
|
/**
|
|
|
|
* @function allowReplyAll
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Check if 'Reply to All' is an appropriate action on the message.
|
|
|
|
* @returns true if the message is not a draft and has more than one recipient
|
|
|
|
*/
|
|
|
|
Message.prototype.allowReplyAll = function() {
|
|
|
|
var recipientsCount = 0;
|
|
|
|
recipientsCount = _.reduce(['to', 'cc'], function(count, type) {
|
|
|
|
if (this[type])
|
|
|
|
return count + this[type].length;
|
|
|
|
else
|
|
|
|
return count;
|
|
|
|
}, recipientsCount, this);
|
|
|
|
|
|
|
|
return !this.isDraft && recipientsCount > 1;
|
|
|
|
};
|
|
|
|
|
2015-03-18 18:41:14 +01:00
|
|
|
/**
|
|
|
|
* @function loadUnsafeContent
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Mark the message to load unsafe resources when calling $content().
|
|
|
|
*/
|
|
|
|
Message.prototype.loadUnsafeContent = function() {
|
|
|
|
this.$loadUnsafeContent = true;
|
|
|
|
};
|
|
|
|
|
2015-08-21 19:37:18 +02:00
|
|
|
/**
|
|
|
|
* @function showDetailedRecipients
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Mark the message to show all to/cc recipients.
|
|
|
|
*/
|
|
|
|
Message.prototype.showDetailedRecipients = function() {
|
|
|
|
this.$showDetailedRecipients = true;
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
/**
|
|
|
|
* @function $content
|
|
|
|
* @memberof Message.prototype
|
2014-12-11 17:24:22 +01:00
|
|
|
* @desc Get the message body as accepted by SCE (Angular Strict Contextual Escaping).
|
|
|
|
* @returns the HTML representation of the body
|
2014-11-25 22:09:55 +01:00
|
|
|
*/
|
|
|
|
Message.prototype.$content = function() {
|
2015-05-13 04:37:58 +02:00
|
|
|
var _this = this,
|
|
|
|
parts = [],
|
|
|
|
_visit = function(part) {
|
2015-08-08 02:38:26 +02:00
|
|
|
part.msgclass = 'msg-attachment-other';
|
2015-05-13 16:36:34 +02:00
|
|
|
if (part.type == 'UIxMailPartAlternativeViewer') {
|
2015-05-13 04:37:58 +02:00
|
|
|
_visit(_.find(part.content, function(alternatePart) {
|
|
|
|
return part.preferredPart == alternatePart.contentType;
|
|
|
|
}));
|
2015-03-18 18:41:14 +01:00
|
|
|
}
|
2015-07-29 21:37:55 +02:00
|
|
|
// Can be used for UIxMailPartMixedViewer and UIxMailPartMessageViewer
|
2015-05-13 04:37:58 +02:00
|
|
|
else if (angular.isArray(part.content)) {
|
|
|
|
_.each(part.content, function(mixedPart) {
|
|
|
|
_visit(mixedPart);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (angular.isUndefined(part.safeContent)) {
|
|
|
|
// Keep a copy of the original content
|
|
|
|
part.safeContent = part.content;
|
2015-09-04 22:43:01 +02:00
|
|
|
_this.$hasUnsafeContent |= (part.safeContent.indexOf(' unsafe-') > -1);
|
2015-05-13 04:37:58 +02:00
|
|
|
}
|
2015-05-13 16:36:34 +02:00
|
|
|
if (part.type == 'UIxMailPartHTMLViewer') {
|
|
|
|
part.html = true;
|
2015-09-04 22:43:01 +02:00
|
|
|
if (_this.$loadUnsafeContent || Message.$displayRemoteInlineImages) {
|
2015-05-13 04:37:58 +02:00
|
|
|
if (angular.isUndefined(part.unsafeContent)) {
|
|
|
|
part.unsafeContent = document.createElement('div');
|
|
|
|
part.unsafeContent.innerHTML = part.safeContent;
|
|
|
|
angular.forEach(['src', 'data', 'classid', 'background', 'style'], function(suffix) {
|
|
|
|
var elements = part.unsafeContent.querySelectorAll('[unsafe-' + suffix + ']'),
|
|
|
|
element,
|
|
|
|
value,
|
|
|
|
i;
|
|
|
|
for (i = 0; i < elements.length; i++) {
|
|
|
|
element = angular.element(elements[i]);
|
|
|
|
value = element.attr('unsafe-' + suffix);
|
|
|
|
element.attr(suffix, value);
|
|
|
|
element.removeAttr('unsafe-' + suffix);
|
|
|
|
}
|
|
|
|
});
|
2015-09-04 22:43:01 +02:00
|
|
|
_this.$hasUnsafeContent = false;
|
2015-05-13 04:37:58 +02:00
|
|
|
}
|
2015-09-30 17:28:22 +02:00
|
|
|
part.content = part.unsafeContent.innerHTML;
|
2015-05-13 04:37:58 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-09-30 17:28:22 +02:00
|
|
|
part.content = part.safeContent;
|
2015-05-13 04:37:58 +02:00
|
|
|
}
|
|
|
|
parts.push(part);
|
|
|
|
}
|
2015-05-13 16:36:34 +02:00
|
|
|
else if (part.type == 'UIxMailPartICalViewer' ||
|
2015-05-29 16:49:40 +02:00
|
|
|
part.type == 'UIxMailPartImageViewer' ||
|
2015-05-13 16:36:34 +02:00
|
|
|
part.type == 'UIxMailPartLinkViewer') {
|
2015-08-04 19:52:31 +02:00
|
|
|
|
|
|
|
// UIxMailPartICalViewer injects 'participants'
|
|
|
|
if (part.participants) {
|
|
|
|
_.each(part.participants, function(participant) {
|
|
|
|
participant.image = Message.$gravatar(participant.email, 32);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-08 02:38:26 +02:00
|
|
|
if (part.type == 'UIxMailPartImageViewer')
|
|
|
|
part.msgclass = 'msg-attachment-image';
|
2015-12-02 20:03:10 +01:00
|
|
|
else if (part.type == 'UIxMailPartLinkViewer')
|
|
|
|
part.msgclass = 'msg-attachment-link';
|
2015-08-08 02:38:26 +02:00
|
|
|
|
2015-05-13 16:36:34 +02:00
|
|
|
// Trusted content that can be compiled (Angularly-speaking)
|
|
|
|
part.compile = true;
|
|
|
|
parts.push(part);
|
|
|
|
}
|
2015-05-13 04:37:58 +02:00
|
|
|
else {
|
2015-05-13 16:36:34 +02:00
|
|
|
part.html = true;
|
2015-09-30 17:28:22 +02:00
|
|
|
part.content = part.safeContent;
|
2015-05-13 04:37:58 +02:00
|
|
|
parts.push(part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_visit(this.parts);
|
|
|
|
|
|
|
|
return parts;
|
2014-12-11 17:24:22 +01:00
|
|
|
};
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2014-12-12 03:52:46 +01:00
|
|
|
/**
|
|
|
|
* @function $editableContent
|
|
|
|
* @memberof Message.prototype
|
2014-12-16 21:47:34 +01:00
|
|
|
* @desc First, fetch the draft ID that corresponds to the temporary draft object on the SOGo server.
|
|
|
|
* Secondly, fetch the editable message body along with other metadata such as the recipients.
|
2014-12-12 03:52:46 +01:00
|
|
|
* @returns the HTML representation of the body
|
|
|
|
*/
|
|
|
|
Message.prototype.$editableContent = function() {
|
2015-08-04 16:56:55 +02:00
|
|
|
var _this = this;
|
2014-12-12 03:52:46 +01:00
|
|
|
|
2015-08-26 17:56:47 +02:00
|
|
|
return Message.$$resource.fetch(this.$absolutePath(), 'edit').then(function(data) {
|
2014-12-16 21:47:34 +01:00
|
|
|
angular.extend(_this, data);
|
2015-08-04 16:56:55 +02:00
|
|
|
return Message.$$resource.fetch(_this.$absolutePath({asDraft: true}), 'edit').then(function(data) {
|
2014-12-16 21:47:34 +01:00
|
|
|
Message.$log.debug('editable = ' + JSON.stringify(data, undefined, 2));
|
2015-04-18 14:50:06 +02:00
|
|
|
angular.extend(_this.editable, data);
|
2015-08-04 16:56:55 +02:00
|
|
|
return data.text;
|
|
|
|
});
|
|
|
|
});
|
2014-12-12 03:52:46 +01:00
|
|
|
};
|
|
|
|
|
2015-05-13 21:20:22 +02:00
|
|
|
/**
|
|
|
|
* @function addTag
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Add a mail tag on the current message.
|
|
|
|
* @param {string} tag - the tag name
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Message.prototype.addTag = function(tag) {
|
|
|
|
return this.$addOrRemoveTag('add', tag);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function removeTag
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Remove a mail tag from the current message.
|
|
|
|
* @param {string} tag - the tag name
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Message.prototype.removeTag = function(tag) {
|
|
|
|
return this.$addOrRemoveTag('remove', tag);
|
|
|
|
};
|
|
|
|
|
2015-05-05 19:03:59 +02:00
|
|
|
/**
|
|
|
|
* @function $addOrRemoveTag
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Add or remove a mail tag on the current message.
|
2015-05-13 21:20:22 +02:00
|
|
|
* @param {string} operation - the operation name to perform
|
|
|
|
* @param {string} tag - the tag name
|
2015-05-05 19:03:59 +02:00
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2015-05-04 19:56:29 +02:00
|
|
|
Message.prototype.$addOrRemoveTag = function(operation, tag) {
|
2015-05-13 21:20:22 +02:00
|
|
|
var data = {
|
|
|
|
operation: operation,
|
|
|
|
msgUIDs: [this.uid],
|
|
|
|
flags: tag
|
|
|
|
};
|
|
|
|
|
|
|
|
if (tag)
|
|
|
|
return Message.$$resource.post(this.$mailbox.$id(), 'addOrRemoveLabel', data);
|
|
|
|
};
|
2015-05-04 19:56:29 +02:00
|
|
|
|
2015-06-04 01:42:30 +02:00
|
|
|
/**
|
|
|
|
* @function $imipAction
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Perform IMIP actions on the current message.
|
|
|
|
* @param {string} path - the path of the IMIP calendar part
|
|
|
|
* @param {string} action - the the IMIP action to perform
|
|
|
|
* @param {object} data - the delegation info
|
|
|
|
*/
|
|
|
|
Message.prototype.$imipAction = function(path, action, data) {
|
|
|
|
var _this = this;
|
2015-08-26 17:56:47 +02:00
|
|
|
Message.$$resource.post([this.$absolutePath(), path].join('/'), action, data).then(function(data) {
|
2015-06-04 01:42:30 +02:00
|
|
|
Message.$timeout(function() {
|
|
|
|
_this.$reload();
|
|
|
|
}, function() {
|
|
|
|
// TODO: show toast
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-08 19:19:16 +02:00
|
|
|
/**
|
|
|
|
* @function $sendMDN
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Send MDN response for current email message
|
|
|
|
*/
|
|
|
|
Message.prototype.$sendMDN = function() {
|
|
|
|
this.shouldAskReceipt = 0;
|
2015-08-26 17:56:47 +02:00
|
|
|
return Message.$$resource.post(this.$absolutePath(), 'sendMDN');
|
2015-07-24 22:14:53 +02:00
|
|
|
};
|
2015-07-08 19:19:16 +02:00
|
|
|
|
2015-06-08 21:49:27 +02:00
|
|
|
/**
|
|
|
|
* @function $deleteAttachment
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Delete an attachment from a message being composed
|
|
|
|
* @param {string} filename - the filename of the attachment to delete
|
|
|
|
*/
|
|
|
|
Message.prototype.$deleteAttachment = function(filename) {
|
|
|
|
var action = 'deleteAttachment?filename=' + filename;
|
|
|
|
var _this = this;
|
|
|
|
Message.$$resource.post(this.$absolutePath({asDraft: true}), action).then(function(data) {
|
|
|
|
Message.$timeout(function() {
|
|
|
|
_this.editable.attachmentAttrs = _.filter(_this.editable.attachmentAttrs, function(attachment) {
|
2015-07-24 22:14:53 +02:00
|
|
|
return attachment.filename != filename;
|
|
|
|
});
|
2015-06-08 21:49:27 +02:00
|
|
|
}, function() {
|
|
|
|
// TODO: show toast
|
|
|
|
});
|
|
|
|
});
|
2015-07-24 22:14:53 +02:00
|
|
|
};
|
2015-06-08 21:49:27 +02:00
|
|
|
|
2015-05-05 19:03:59 +02:00
|
|
|
/**
|
|
|
|
* @function $markAsFlaggedOrUnflagged
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Add or remove a the \\Flagged flag on the current message.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2015-05-13 21:20:22 +02:00
|
|
|
Message.prototype.toggleFlag = function() {
|
|
|
|
var _this = this,
|
|
|
|
action = 'markMessageFlagged';
|
2015-05-05 19:03:59 +02:00
|
|
|
|
2015-05-13 21:20:22 +02:00
|
|
|
if (this.isflagged)
|
|
|
|
action = 'markMessageUnflagged';
|
2015-05-05 19:03:59 +02:00
|
|
|
|
2015-08-26 17:56:47 +02:00
|
|
|
return Message.$$resource.post(this.$absolutePath(), action).then(function(data) {
|
2015-05-13 21:20:22 +02:00
|
|
|
Message.$timeout(function() {
|
|
|
|
_this.isflagged = !_this.isflagged;
|
|
|
|
});
|
|
|
|
});
|
2015-07-24 22:14:53 +02:00
|
|
|
};
|
2015-05-05 19:03:59 +02:00
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
/**
|
2014-12-17 20:08:43 +01:00
|
|
|
* @function $reload
|
2014-12-11 17:24:22 +01:00
|
|
|
* @memberof Message.prototype
|
2014-12-12 03:52:46 +01:00
|
|
|
* @desc Fetch the viewable message body along with other metadata such as the list of attachments.
|
2014-12-11 17:24:22 +01:00
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2015-09-08 18:22:37 +02:00
|
|
|
Message.prototype.$reload = function(options) {
|
2014-12-11 17:24:22 +01:00
|
|
|
var futureMessageData;
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2015-09-08 18:22:37 +02:00
|
|
|
futureMessageData = Message.$$resource.fetch(this.$absolutePath(options), 'view');
|
2014-11-25 22:09:55 +01:00
|
|
|
|
|
|
|
return this.$unwrap(futureMessageData);
|
|
|
|
};
|
|
|
|
|
2015-01-06 04:34:12 +01:00
|
|
|
/**
|
|
|
|
* @function $reply
|
|
|
|
* @memberof Message.prototype
|
2015-01-06 18:09:14 +01:00
|
|
|
* @desc Prepare a new Message object as a reply to the sender.
|
2015-01-06 04:34:12 +01:00
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Message.prototype.$reply = function() {
|
2015-01-06 18:09:14 +01:00
|
|
|
return this.$newDraft('reply');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $replyAll
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Prepare a new Message object as a reply to the sender and all recipients.
|
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Message.prototype.$replyAll = function() {
|
|
|
|
return this.$newDraft('replyall');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $forward
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Prepare a new Message object as a forward.
|
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Message.prototype.$forward = function() {
|
|
|
|
return this.$newDraft('forward');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $newDraft
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Prepare a new Message object as a reply or a forward of the current message and associated
|
|
|
|
* to the draft mailbox.
|
|
|
|
* @see {@link Account.$newMessage}
|
|
|
|
* @see {@link Message.$editableContent}
|
|
|
|
* @see {@link Message.$reply}
|
|
|
|
* @see {@link Message.$replyAll}
|
|
|
|
* @see {@link Message.$forwad}
|
2015-03-18 18:41:14 +01:00
|
|
|
* @param {string} action - the HTTP action to perform on the message
|
2015-01-06 18:09:14 +01:00
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Message.prototype.$newDraft = function(action) {
|
2015-07-13 21:22:58 +02:00
|
|
|
var _this = this;
|
2015-01-06 04:34:12 +01:00
|
|
|
|
|
|
|
// Query server for draft folder and draft UID
|
2015-08-26 17:56:47 +02:00
|
|
|
return Message.$$resource.fetch(this.$absolutePath(), action).then(function(data) {
|
2015-07-13 21:22:58 +02:00
|
|
|
var mailbox, message;
|
2015-01-06 18:09:14 +01:00
|
|
|
Message.$log.debug('New ' + action + ': ' + JSON.stringify(data, undefined, 2));
|
2015-01-06 04:34:12 +01:00
|
|
|
mailbox = _this.$mailbox.$account.$getMailboxByPath(data.mailboxPath);
|
|
|
|
message = new Message(data.accountId, mailbox, data);
|
|
|
|
// Fetch draft initial data
|
2015-07-13 21:22:58 +02:00
|
|
|
return Message.$$resource.fetch(message.$absolutePath({asDraft: true}), 'edit').then(function(data) {
|
2015-08-28 21:50:46 +02:00
|
|
|
Message.$log.debug('New ' + action + ': ' + JSON.stringify(data, undefined, 2) + ' original UID: ' + _this.uid);
|
2015-04-18 14:50:06 +02:00
|
|
|
angular.extend(message.editable, data);
|
2015-08-28 21:50:46 +02:00
|
|
|
|
|
|
|
// We keep a reference to our original message in order to update the flags
|
|
|
|
message.origin = {message: _this, action: action};
|
2015-07-13 21:22:58 +02:00
|
|
|
return message;
|
2015-01-06 04:34:12 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-12-12 03:52:46 +01:00
|
|
|
/**
|
|
|
|
* @function $save
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Save the message to the server.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2014-12-11 17:24:22 +01:00
|
|
|
Message.prototype.$save = function() {
|
2014-12-16 21:47:34 +01:00
|
|
|
var _this = this,
|
|
|
|
data = this.editable;
|
2014-12-12 03:52:46 +01:00
|
|
|
|
|
|
|
Message.$log.debug('save = ' + JSON.stringify(data, undefined, 2));
|
2014-12-11 17:24:22 +01:00
|
|
|
|
2014-12-16 21:47:34 +01:00
|
|
|
return Message.$$resource.save(this.$absolutePath({asDraft: true}), data).then(function(response) {
|
|
|
|
Message.$log.debug('save = ' + JSON.stringify(response, undefined, 2));
|
|
|
|
_this.$setUID(response.uid);
|
2015-09-08 18:22:37 +02:00
|
|
|
_this.$reload({asDraft: false}); // fetch a new viewable version of the message
|
2014-12-16 21:47:34 +01:00
|
|
|
});
|
2014-12-11 17:24:22 +01:00
|
|
|
};
|
|
|
|
|
2014-12-12 03:52:46 +01:00
|
|
|
/**
|
|
|
|
* @function $send
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Send the message.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2014-12-11 17:24:22 +01:00
|
|
|
Message.prototype.$send = function() {
|
2015-08-28 21:50:46 +02:00
|
|
|
var _this = this,
|
|
|
|
data = angular.copy(this.editable),
|
2014-12-11 17:24:22 +01:00
|
|
|
deferred = Message.$q.defer();
|
|
|
|
|
2014-12-12 03:52:46 +01:00
|
|
|
Message.$log.debug('send = ' + JSON.stringify(data, undefined, 2));
|
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
Message.$$resource.post(this.$absolutePath({asDraft: true}), 'send', data).then(function(data) {
|
|
|
|
if (data.status == 'success') {
|
|
|
|
deferred.resolve(data);
|
2015-08-28 21:50:46 +02:00
|
|
|
if (angular.isDefined(_this.origin)) {
|
|
|
|
if (_this.origin.action.startsWith('reply'))
|
|
|
|
_this.origin.message.isanswered = true;
|
|
|
|
else if (_this.origin.action == 'forward')
|
|
|
|
_this.origin.message.isforwarded = true;
|
|
|
|
}
|
2014-12-11 17:24:22 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferred.reject(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
/**
|
|
|
|
* @function $unwrap
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Unwrap a promise.
|
|
|
|
* @param {promise} futureMessageData - a promise of some of the Message's data
|
|
|
|
*/
|
|
|
|
Message.prototype.$unwrap = function(futureMessageData) {
|
2015-08-28 21:15:40 +02:00
|
|
|
var _this = this;
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2015-08-28 21:15:40 +02:00
|
|
|
// Resolve and expose the promise
|
|
|
|
this.$futureMessageData = futureMessageData.then(function(data) {
|
2014-11-25 22:09:55 +01:00
|
|
|
// Calling $timeout will force Angular to refresh the view
|
2015-08-28 21:15:40 +02:00
|
|
|
if (_this.isread === 0) {
|
2015-08-26 17:56:47 +02:00
|
|
|
Message.$$resource.fetch(_this.$absolutePath(), 'markMessageRead').then(function() {
|
2014-12-17 20:08:43 +01:00
|
|
|
Message.$timeout(function() {
|
|
|
|
_this.isread = true;
|
2015-08-04 22:08:17 +02:00
|
|
|
_this.$mailbox.unseenCount--;
|
2014-12-17 20:08:43 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-08-28 21:15:40 +02:00
|
|
|
return Message.$timeout(function() {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
_this.$formatFullAddresses();
|
|
|
|
_this.$loadUnsafeContent = false;
|
|
|
|
return _this;
|
|
|
|
});
|
2014-11-25 22:09:55 +01:00
|
|
|
});
|
|
|
|
|
2015-08-28 21:15:40 +02:00
|
|
|
return this.$futureMessageData;
|
2014-11-25 22:09:55 +01:00
|
|
|
};
|
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
/**
|
|
|
|
* @function $omit
|
|
|
|
* @memberof Message.prototype
|
|
|
|
* @desc Return a sanitized object used to send to the server.
|
|
|
|
* @return an object literal copy of the Message instance
|
|
|
|
*/
|
|
|
|
Message.prototype.$omit = function() {
|
|
|
|
var message = {};
|
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' && key[0] != '$') {
|
|
|
|
message[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Format addresses as arrays
|
|
|
|
_.each(['from', 'to', 'cc', 'bcc', 'reply-to'], function(type) {
|
|
|
|
if (message[type])
|
|
|
|
message[type] = _.invoke(message[type].split(','), 'trim');
|
|
|
|
});
|
|
|
|
|
|
|
|
//Message.$log.debug(JSON.stringify(message, undefined, 2));
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
})();
|