mail/src/js/controller/app/read.js

173 lines
4.3 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2014-10-02 16:05:44 -04:00
//
// Controller
//
2014-12-17 12:18:26 -05:00
var ReadCtrl = function($scope, $location, $q, email, invitation, outbox, pgp, keychain, appConfig, download, auth, dialog) {
var str = appConfig.string;
2014-10-02 16:05:44 -04:00
//
// scope state
//
2014-10-02 16:05:44 -04:00
$scope.state.read = {
open: false,
toggle: function(to) {
this.open = to;
}
};
$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.';
//
// 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
//
2014-10-02 16:05:44 -04:00
$scope.getKeyId = function(address) {
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
};
2014-10-02 16:05:44 -04:00
$scope.$watch('state.mailList.selected', function(mail) {
if ($location.search().dev || !mail) {
2014-10-02 16:05:44 -04:00
return;
}
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-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;
}
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
});
2014-10-02 16:05:44 -04:00
return;
}
2014-10-02 16:05:44 -04:00
var folder = $scope.state.nav.currentFolder;
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-10-02 16:05:44 -04:00
$scope.invite = function(user) {
// only invite non-pgp users
if (user.secure) {
return;
}
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-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);
};
2014-10-02 16:05:44 -04:00
};
2014-10-08 06:34:34 -04:00
module.exports = ReadCtrl;