2014-11-25 22:09:55 +01:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Account
|
|
|
|
* @constructor
|
|
|
|
* @param {object} futureAccountData
|
|
|
|
*/
|
|
|
|
function Account(futureAccountData) {
|
|
|
|
// Data is immediately available
|
|
|
|
if (typeof futureAccountData.then !== 'function') {
|
|
|
|
angular.extend(this, futureAccountData);
|
2014-12-11 17:24:22 +01:00
|
|
|
_.each(this.identities, function(identity) {
|
|
|
|
if (identity.fullName)
|
|
|
|
identity.full = identity.fullName + ' <' + identity.email + '>';
|
|
|
|
else
|
|
|
|
identity.full = '<' + identity.email + '>';
|
|
|
|
});
|
|
|
|
Account.$log.debug('Account: ' + JSON.stringify(futureAccountData, undefined, 2));
|
2014-11-25 22:09:55 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The promise will be unwrapped first
|
|
|
|
//this.$unwrap(futureAccountData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Account
|
|
|
|
* @desc The factory we'll use to register with Angular
|
|
|
|
* @returns the Account constructor
|
|
|
|
*/
|
2015-05-06 04:06:13 +02:00
|
|
|
Account.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Resource', 'Mailbox', 'Message', function($q, $timeout, $log, Settings, Resource, Mailbox, Message) {
|
2014-11-25 22:09:55 +01:00
|
|
|
angular.extend(Account, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
|
|
|
$log: $log,
|
|
|
|
$$resource: new Resource(Settings.baseURL, Settings.activeUser),
|
2014-12-11 17:24:22 +01:00
|
|
|
$Mailbox: Mailbox,
|
|
|
|
$Message: Message
|
2014-11-25 22:09:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return Account; // return constructor
|
|
|
|
}];
|
|
|
|
|
|
|
|
/* Factory registration in Angular module */
|
|
|
|
angular.module('SOGo.MailerUI')
|
2015-05-06 04:06:13 +02:00
|
|
|
.factory('Account', Account.$factory);
|
2014-11-25 22:09:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Account
|
|
|
|
* @desc Set the list of accounts and instanciate a new Account object for each item.
|
|
|
|
* @param {array} [data] - the metadata of the accounts
|
|
|
|
* @returns the list of accounts
|
|
|
|
*/
|
|
|
|
Account.$findAll = function(data) {
|
|
|
|
var collection = [];
|
|
|
|
if (data) {
|
|
|
|
// Each entry is spun up as an Account instance
|
|
|
|
angular.forEach(data, function(o, i) {
|
|
|
|
o.id = i;
|
|
|
|
collection[i] = new Account(o);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return collection;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $getMailboxes
|
|
|
|
* @memberof Account.prototype
|
|
|
|
* @desc Fetch the list of mailboxes for the current account.
|
2015-05-06 04:06:13 +02:00
|
|
|
* @param {object} [options] - force a reload by setting 'reload' to true
|
2014-11-25 22:09:55 +01:00
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2014-12-18 21:22:29 +01:00
|
|
|
Account.prototype.$getMailboxes = function(options) {
|
2014-12-11 17:24:22 +01:00
|
|
|
var _this = this,
|
|
|
|
deferred = Account.$q.defer();
|
|
|
|
|
2014-12-18 21:22:29 +01:00
|
|
|
if (this.$mailboxes && !(options && options.reload)) {
|
2014-12-11 17:24:22 +01:00
|
|
|
deferred.resolve(this.$mailboxes);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Account.$Mailbox.$find(this).then(function(data) {
|
|
|
|
_this.$mailboxes = data;
|
2015-04-29 17:23:21 +02:00
|
|
|
_this.$flattenMailboxes({reload: true});
|
2014-12-11 17:24:22 +01:00
|
|
|
deferred.resolve(_this.$mailboxes);
|
|
|
|
});
|
|
|
|
}
|
2014-11-30 14:50:26 +01:00
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2015-04-29 17:23:21 +02:00
|
|
|
/**
|
|
|
|
* @function $flattenMailboxes
|
|
|
|
* @memberof Account.prototype
|
|
|
|
* @desc Get a flatten array of the mailboxes.
|
|
|
|
* @param {object} [options] - force a reload
|
|
|
|
* @returns an array of Mailbox instances
|
|
|
|
*/
|
|
|
|
Account.prototype.$flattenMailboxes = function(options) {
|
2014-12-11 17:24:22 +01:00
|
|
|
var _this = this,
|
|
|
|
allMailboxes = [],
|
2015-04-29 17:23:21 +02:00
|
|
|
_visit = function(mailboxes) {
|
2014-12-11 17:24:22 +01:00
|
|
|
_.each(mailboxes, function(o) {
|
2015-04-29 17:23:21 +02:00
|
|
|
allMailboxes.push(o);
|
2014-12-11 17:24:22 +01:00
|
|
|
if (o.children && o.children.length > 0) {
|
2015-04-29 17:23:21 +02:00
|
|
|
_visit(o.children);
|
2014-12-11 17:24:22 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-29 17:23:21 +02:00
|
|
|
if (this.$$flattenMailboxes && !(options && options.reload)) {
|
2014-12-11 17:24:22 +01:00
|
|
|
allMailboxes = this.$$flattenMailboxes;
|
|
|
|
}
|
|
|
|
else {
|
2015-04-29 17:23:21 +02:00
|
|
|
_visit(this.$mailboxes);
|
2014-12-11 17:24:22 +01:00
|
|
|
_this.$$flattenMailboxes = allMailboxes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allMailboxes;
|
|
|
|
};
|
|
|
|
|
|
|
|
Account.prototype.$getMailboxByType = function(type) {
|
|
|
|
var mailbox,
|
|
|
|
// Recursive find function
|
|
|
|
_find = function(mailboxes) {
|
|
|
|
var mailbox = _.find(mailboxes, function(o) {
|
|
|
|
return o.type == type;
|
|
|
|
});
|
|
|
|
if (!mailbox) {
|
|
|
|
angular.forEach(mailboxes, function(o) {
|
|
|
|
if (!mailbox && o.children && o.children.length > 0) {
|
|
|
|
mailbox = _find(o.children);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return mailbox;
|
|
|
|
};
|
2014-12-16 21:47:34 +01:00
|
|
|
mailbox = _find(this.$mailboxes);
|
2014-12-11 17:24:22 +01:00
|
|
|
|
|
|
|
console.debug(mailbox);
|
|
|
|
console.debug(this.specialMailboxes);
|
|
|
|
};
|
|
|
|
|
2014-12-16 21:47:34 +01:00
|
|
|
/**
|
|
|
|
* @function $getMailboxByPath
|
|
|
|
* @memberof Account.prototype
|
|
|
|
* @desc Recursively find a mailbox using its path
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Account.prototype.$getMailboxByPath = function(path) {
|
|
|
|
var mailbox = null,
|
|
|
|
// Recursive find function
|
|
|
|
_find = function(mailboxes) {
|
|
|
|
var mailbox = _.find(mailboxes, function(o) {
|
|
|
|
return o.path == path;
|
|
|
|
});
|
|
|
|
if (!mailbox) {
|
|
|
|
angular.forEach(mailboxes, function(o) {
|
|
|
|
if (!mailbox && o.children && o.children.length > 0) {
|
|
|
|
mailbox = _find(o.children);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return mailbox;
|
|
|
|
};
|
|
|
|
mailbox = _find(this.$mailboxes);
|
|
|
|
|
|
|
|
return mailbox;
|
|
|
|
};
|
|
|
|
|
2014-12-18 21:22:29 +01:00
|
|
|
/**
|
|
|
|
* @function $newMailbox
|
|
|
|
* @memberof Account.prototype
|
|
|
|
* @desc Create a new mailbox on the server and refresh the list of mailboxes.
|
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Account.prototype.$newMailbox = function(path, name) {
|
|
|
|
var _this = this,
|
|
|
|
deferred = Account.$q.defer();
|
|
|
|
|
|
|
|
Account.$$resource.post(path, 'createFolder', {name: name}).then(function() {
|
|
|
|
_this.$getMailboxes({reload: true});
|
|
|
|
deferred.resolve();
|
|
|
|
}, function(response) {
|
|
|
|
deferred.reject(response.error);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
/**
|
|
|
|
* @function $newMessage
|
|
|
|
* @memberof Account.prototype
|
|
|
|
* @desc Prepare a new Message object associated to the appropriate mailbox.
|
|
|
|
* @returns a promise of the HTTP operations
|
|
|
|
*/
|
|
|
|
Account.prototype.$newMessage = function() {
|
|
|
|
var _this = this,
|
|
|
|
deferred = Account.$q.defer(),
|
|
|
|
message;
|
|
|
|
|
|
|
|
// Query account for draft folder and draft UID
|
2014-12-16 21:47:34 +01:00
|
|
|
Account.$$resource.fetch(this.id.toString(), 'compose').then(function(data) {
|
|
|
|
Account.$log.debug('New message: ' + JSON.stringify(data, undefined, 2));
|
|
|
|
message = new Account.$Message(data.accountId, _this.$getMailboxByPath(data.mailboxPath), data);
|
2014-12-11 17:24:22 +01:00
|
|
|
// Fetch draft initial data
|
2014-12-16 21:47:34 +01:00
|
|
|
Account.$$resource.fetch(message.$absolutePath({asDraft: true}), 'edit').then(function(data) {
|
2014-12-11 17:24:22 +01:00
|
|
|
Account.$log.debug('New message: ' + JSON.stringify(data, undefined, 2));
|
2015-04-18 14:50:06 +02:00
|
|
|
angular.extend(message.editable, data);
|
2014-12-11 17:24:22 +01:00
|
|
|
deferred.resolve(message);
|
|
|
|
}, function(data) {
|
|
|
|
deferred.reject(data);
|
|
|
|
});
|
|
|
|
}, function(data) {
|
|
|
|
deferred.reject(data);
|
2014-11-30 14:50:26 +01:00
|
|
|
});
|
2014-11-25 22:09:55 +01:00
|
|
|
|
2014-12-11 17:24:22 +01:00
|
|
|
return deferred.promise;
|
2014-11-25 22:09:55 +01:00
|
|
|
};
|
|
|
|
|
2015-07-04 02:40:06 +02:00
|
|
|
/**
|
|
|
|
* @function $addDelegate
|
|
|
|
* @memberof Account.prototype
|
|
|
|
* @param {Object} user - a User object with minimal set of attributes (uid, isGroup, cn, c_email)
|
|
|
|
* @desc Remove a user from the account's delegates
|
|
|
|
* @see {@link User.$filter}
|
|
|
|
*/
|
|
|
|
Account.prototype.$addDelegate = function(user) {
|
|
|
|
var _this = this,
|
|
|
|
deferred = Account.$q.defer(),
|
|
|
|
param = {uid: user.uid};
|
|
|
|
if (!user.uid || _.indexOf(_.pluck(this.delegates, 'uid'), user.uid) > -1) {
|
|
|
|
// No UID specified or user already in delegates
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Account.$$resource.fetch(this.id.toString(), 'addDelegate', param).then(function() {
|
|
|
|
_this.delegates.push(user);
|
|
|
|
deferred.resolve(_this.users);
|
|
|
|
}, function(data, status) {
|
|
|
|
deferred.reject(l('An error occured please try again.'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $removeDelegate
|
|
|
|
* @memberof Account.prototype
|
|
|
|
* @param {Object} user - a User object with minimal set of attributes (uid, isGroup, cn, c_email)
|
|
|
|
* @desc Remove a user from the account's delegates
|
|
|
|
* @return a promise of the server call to remove the user from the account's delegates
|
|
|
|
*/
|
|
|
|
Account.prototype.$removeDelegate = function(uid) {
|
|
|
|
var _this = this,
|
|
|
|
param = {uid: uid};
|
|
|
|
return Account.$$resource.fetch(this.id.toString(), 'removeDelegate', param).then(function() {
|
|
|
|
var i = _.indexOf(_.pluck(_this.delegates, 'uid'), uid);
|
|
|
|
if (i >= 0) {
|
|
|
|
_this.delegates.splice(i, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-11-25 22:09:55 +01:00
|
|
|
})();
|