2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2013-11-18 13:53:31 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
//
|
|
|
|
// Controller
|
|
|
|
//
|
2014-02-20 13:20:24 -05:00
|
|
|
|
2015-01-15 04:23:15 -05:00
|
|
|
var ReadCtrl = function($scope, $location, $q, email, invitation, outbox, pgp, keychain, appConfig, download, auth, dialog, status) {
|
2013-12-03 07:15:10 -05:00
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
var str = appConfig.string;
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
//
|
|
|
|
// scope state
|
|
|
|
//
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.state.read = {
|
|
|
|
open: false,
|
|
|
|
toggle: function(to) {
|
|
|
|
this.open = to;
|
|
|
|
}
|
|
|
|
};
|
2013-12-04 10:35:53 -05:00
|
|
|
|
2014-12-04 10:24:42 -05:00
|
|
|
$scope.$on('read', function(e, state) {
|
|
|
|
$scope.state.read.toggle(state);
|
|
|
|
});
|
|
|
|
|
|
|
|
// set default value so that the popover height is correct on init
|
|
|
|
$scope.keyId = 'No key found.';
|
|
|
|
|
2014-12-04 06:46:55 -05:00
|
|
|
//
|
|
|
|
// url/history handling
|
|
|
|
//
|
|
|
|
|
|
|
|
// read state url watcher
|
|
|
|
$scope.loc = $location;
|
|
|
|
$scope.$watch('(loc.search()).uid', function(uid) {
|
|
|
|
// synchronize the url to the scope state
|
|
|
|
$scope.state.read.toggle(!!uid);
|
|
|
|
});
|
|
|
|
$scope.$watch('state.read.open', function(value) {
|
|
|
|
// close read mode by navigating to folder view
|
|
|
|
if (!value) {
|
|
|
|
$location.search('uid', null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// scope functions
|
|
|
|
//
|
|
|
|
|
2015-01-15 04:23:15 -05:00
|
|
|
/**
|
|
|
|
* Close read mode and return to mail-list
|
|
|
|
*/
|
|
|
|
$scope.close = function() {
|
|
|
|
status.setReading(false);
|
|
|
|
};
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.getKeyId = function(address) {
|
2014-12-04 06:46:55 -05:00
|
|
|
if ($location.search().dev || !address) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
$scope.keyId = 'Searching...';
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
}).then(function() {
|
|
|
|
return keychain.getReceiverPublicKey(address);
|
2013-12-04 10:42:17 -05:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function(pubkey) {
|
2014-10-02 16:05:44 -04:00
|
|
|
if (!pubkey) {
|
|
|
|
$scope.keyId = 'User has no key. Click to invite.';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fpr = pgp.getFingerprint(pubkey.publicKey);
|
|
|
|
var formatted = fpr.slice(32);
|
|
|
|
$scope.keyId = 'PGP key: ' + formatted;
|
2014-12-17 12:18:26 -05:00
|
|
|
|
|
|
|
}).catch(dialog.error);
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2013-12-04 10:35:53 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.$watch('state.mailList.selected', function(mail) {
|
2014-12-04 06:46:55 -05:00
|
|
|
if ($location.search().dev || !mail) {
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-09-22 11:33:06 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// display sender security status
|
|
|
|
mail.from.forEach(checkPublicKey);
|
|
|
|
// display recipient security status
|
|
|
|
mail.to.forEach(checkPublicKey);
|
|
|
|
// display recipient security status
|
|
|
|
Array.isArray(mail.cc) && mail.cc.forEach(checkPublicKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
function checkPublicKey(user) {
|
|
|
|
user.secure = undefined;
|
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
resolve();
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function() {
|
|
|
|
return keychain.getReceiverPublicKey(user.address);
|
2014-09-22 11:33:06 -04:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function(pubkey) {
|
2014-12-18 10:50:11 -05:00
|
|
|
if (pubkey && pubkey.publicKey) {
|
2014-10-02 16:05:44 -04:00
|
|
|
user.secure = true;
|
|
|
|
} else {
|
|
|
|
user.secure = false;
|
|
|
|
}
|
2013-12-04 10:35:53 -05:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).catch(dialog.error);
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$scope.download = function(attachment) {
|
|
|
|
// download file to disk if content is available
|
|
|
|
if (attachment.content) {
|
|
|
|
download.createDownload({
|
|
|
|
content: attachment.content,
|
|
|
|
filename: attachment.filename,
|
|
|
|
contentType: attachment.mimeType
|
2013-12-04 10:35:53 -05:00
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
2013-12-04 10:35:53 -05:00
|
|
|
}
|
2014-01-16 09:37:08 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var folder = $scope.state.nav.currentFolder;
|
2014-11-20 09:14:39 -05:00
|
|
|
var message = $scope.state.mailList.selected;
|
2014-12-17 12:18:26 -05:00
|
|
|
|
|
|
|
return $q(function(resolve) {
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
}).then(function() {
|
|
|
|
return email.getAttachment({
|
|
|
|
folder: folder,
|
|
|
|
uid: message.uid,
|
|
|
|
attachment: attachment
|
|
|
|
});
|
|
|
|
|
|
|
|
}).catch(dialog.error);
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-01-16 09:37:08 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.invite = function(user) {
|
|
|
|
// only invite non-pgp users
|
|
|
|
if (user.secure) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-20 09:14:39 -05:00
|
|
|
var sender = auth.emailAddress,
|
2014-10-02 16:05:44 -04:00
|
|
|
recipient = user.address;
|
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
$scope.keyId = 'Sending invitation...';
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
}).then(function() {
|
|
|
|
return invitation.invite({
|
|
|
|
recipient: recipient,
|
|
|
|
sender: sender
|
|
|
|
});
|
2014-02-27 12:14:38 -05:00
|
|
|
|
2014-12-17 12:18:26 -05:00
|
|
|
}).then(function() {
|
2014-10-02 16:05:44 -04:00
|
|
|
var invitationMail = {
|
|
|
|
from: [{
|
|
|
|
address: sender
|
|
|
|
}],
|
|
|
|
to: [{
|
|
|
|
address: recipient
|
|
|
|
}],
|
|
|
|
cc: [],
|
|
|
|
bcc: [],
|
|
|
|
subject: str.invitationSubject,
|
|
|
|
body: str.invitationMessage
|
|
|
|
};
|
|
|
|
// send invitation mail
|
2014-12-17 12:18:26 -05:00
|
|
|
return outbox.put(invitationMail);
|
|
|
|
|
|
|
|
}).catch(dialog.error);
|
2013-11-08 16:05:08 -05:00
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2013-10-05 08:16:04 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = ReadCtrl;
|