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
|
|
|
|
*/
|
2014-12-16 21:47:34 +01:00
|
|
|
function Message(accountId, mailbox, futureMessageData) {
|
2014-11-25 22:09:55 +01:00
|
|
|
this.accountId = accountId;
|
2014-12-16 21:47:34 +01:00
|
|
|
this.$mailbox = mailbox;
|
2014-11-25 22:09:55 +01:00
|
|
|
// Data is immediately available
|
|
|
|
if (typeof futureMessageData.then !== 'function') {
|
|
|
|
//console.debug(JSON.stringify(futureMessageData, undefined, 2));
|
|
|
|
angular.extend(this, futureMessageData);
|
|
|
|
this.id = this.$absolutePath();
|
2014-12-11 15:15:33 +01:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
Message.$factory = ['$q', '$timeout', '$log', '$sce', 'sgSettings', 'sgResource', function($q, $timeout, $log, $sce, Settings, Resource) {
|
|
|
|
angular.extend(Message, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
|
|
|
$log: $log,
|
|
|
|
$sce: $sce,
|
|
|
|
$$resource: new Resource(Settings.baseURL, Settings.activeUser)
|
|
|
|
});
|
|
|
|
|
|
|
|
return Message; // return constructor
|
|
|
|
}];
|
|
|
|
|
|
|
|
/* Factory registration in Angular module */
|
|
|
|
angular.module('SOGo.MailerUI')
|
|
|
|
.factory('sgMessage', Message.$factory);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2014-11-25 22:09:55 +01:00
|
|
|
var path;
|
|
|
|
|
2014-12-16 21:47:34 +01:00
|
|
|
path = _.map(this.$mailbox.path.split('/'), function(component) {
|
2014-11-25 22:09:55 +01:00
|
|
|
return 'folder' + component.asCSSIdentifier();
|
|
|
|
});
|
|
|
|
path.splice(0, 0, this.accountId); // insert account ID
|
2014-12-11 15:15:33 +01:00
|
|
|
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
|
|
|
|
|
|
|
return path.join('/');
|
|
|
|
};
|
|
|
|
|
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) {
|
|
|
|
var oldUID = this.uid || -1;
|
|
|
|
|
|
|
|
if (oldUID != uid) {
|
|
|
|
this.uid = uid;
|
|
|
|
this.id = this.$absolutePath();
|
|
|
|
if (oldUID > -1) {
|
2015-01-06 04:34:12 +01:00
|
|
|
// For new messages, $mailbox doesn't exist
|
2014-12-16 21:47:34 +01:00
|
|
|
this.$mailbox.uidsMap[uid] = this.$mailbox.uidsMap[oldUID];
|
|
|
|
this.$mailbox.uidsMap[oldUID] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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>).
|
2014-12-11 15:15:33 +01:00
|
|
|
*/
|
|
|
|
Message.prototype.$formatFullAddresses = function() {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
// Build long representation of email addresses
|
|
|
|
_.each(['from', 'to', 'cc', 'bcc', 'reply-to'], function(type) {
|
|
|
|
_.each(_this[type], function(data, i) {
|
|
|
|
if (data.name && data.name != data.email)
|
|
|
|
data.full = data.name + ' <' + data.email + '>';
|
|
|
|
else
|
|
|
|
data.full = '<' + data.email + '>';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
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-01-06 21:41:20 +01:00
|
|
|
this.hasUnsafeContent = (this.content.indexOf(' unsafe-') > -1);
|
2014-12-11 17:24:22 +01:00
|
|
|
return Message.$sce.trustAs('html', this.content);
|
|
|
|
};
|
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() {
|
|
|
|
var _this = this,
|
|
|
|
deferred = Message.$q.defer();
|
|
|
|
|
2014-12-16 21:47:34 +01:00
|
|
|
Message.$$resource.fetch(this.id, 'edit').then(function(data) {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
Message.$$resource.fetch(_this.$absolutePath({asDraft: true}), 'edit').then(function(data) {
|
|
|
|
Message.$log.debug('editable = ' + JSON.stringify(data, undefined, 2));
|
|
|
|
_this.editable = data;
|
|
|
|
deferred.resolve(data.text);
|
|
|
|
}, deferred.reject);
|
|
|
|
}, deferred.reject);
|
2014-12-12 03:52:46 +01:00
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
*/
|
2014-12-17 20:08:43 +01:00
|
|
|
Message.prototype.$reload = function() {
|
2014-12-11 17:24:22 +01:00
|
|
|
var futureMessageData;
|
2014-11-25 22:09:55 +01:00
|
|
|
|
|
|
|
futureMessageData = Message.$$resource.fetch(this.id, 'view');
|
|
|
|
|
|
|
|
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}
|
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Message.prototype.$newDraft = function(action) {
|
2015-01-06 04:34:12 +01:00
|
|
|
var _this = this,
|
|
|
|
deferred = Message.$q.defer(),
|
|
|
|
mailbox,
|
|
|
|
message;
|
|
|
|
|
|
|
|
// Query server for draft folder and draft UID
|
2015-01-06 18:09:14 +01:00
|
|
|
Message.$$resource.fetch(this.id, action).then(function(data) {
|
|
|
|
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
|
|
|
|
Message.$$resource.fetch(message.$absolutePath({asDraft: true}), 'edit').then(function(data) {
|
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
|
|
|
message.editable = data;
|
|
|
|
deferred.resolve(message);
|
|
|
|
}, function(data) {
|
|
|
|
deferred.reject(data);
|
|
|
|
});
|
|
|
|
}, function(data) {
|
|
|
|
deferred.reject(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
// Flatten recipient addresses
|
|
|
|
_.each(['to', 'cc', 'bcc', 'reply-to'], function(type) {
|
|
|
|
if (data[type]) {
|
|
|
|
data[type] = _.pluck(data[type], 'text');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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);
|
2014-12-17 20:08:43 +01:00
|
|
|
_this.$reload(); // 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() {
|
2014-12-12 03:52:46 +01:00
|
|
|
var 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
|
|
|
// Flatten recipient addresses
|
|
|
|
_.each(['to', 'cc', 'bcc', 'reply-to'], function(type) {
|
|
|
|
if (data[type]) {
|
|
|
|
data[type] = _.pluck(data[type], 'text');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
var _this = this,
|
|
|
|
deferred = Message.$q.defer();
|
|
|
|
|
|
|
|
// Expose the promise
|
|
|
|
this.$futureMessageData = futureMessageData;
|
|
|
|
|
|
|
|
// Resolve the promise
|
|
|
|
this.$futureMessageData.then(function(data) {
|
|
|
|
// Calling $timeout will force Angular to refresh the view
|
|
|
|
Message.$timeout(function() {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
_this.id = _this.$absolutePath();
|
2014-12-11 15:15:33 +01:00
|
|
|
_this.$formatFullAddresses();
|
|
|
|
deferred.resolve(_this);
|
2014-11-25 22:09:55 +01:00
|
|
|
});
|
2014-12-17 20:08:43 +01:00
|
|
|
if (!_this.isread) {
|
|
|
|
Message.$$resource.fetch(_this.id, 'markMessageRead').then(function() {
|
|
|
|
Message.$timeout(function() {
|
|
|
|
_this.isread = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-11-25 22:09:55 +01:00
|
|
|
}, function(data) {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
_this.isError = true;
|
|
|
|
Message.$log.error(_this.error);
|
|
|
|
deferred.reject();
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
})();
|