summaryrefslogtreecommitdiff
path: root/web-ui/app/js/services/model
diff options
context:
space:
mode:
authorOla Bini <ola.bini@gmail.com>2014-07-31 19:29:33 -0300
committerOla Bini <ola.bini@gmail.com>2014-07-31 19:29:33 -0300
commit04cf441c5ae18400c6b4865b0b37a71718dc9d46 (patch)
treedd0b0d049ec00389e2d4561b226c46eb1682b997 /web-ui/app/js/services/model
parent639a663a4c37020003586438fdcd7ac529a00f10 (diff)
Add web-ui based on previous code
Diffstat (limited to 'web-ui/app/js/services/model')
-rw-r--r--web-ui/app/js/services/model/mail.js147
1 files changed, 147 insertions, 0 deletions
diff --git a/web-ui/app/js/services/model/mail.js b/web-ui/app/js/services/model/mail.js
new file mode 100644
index 00000000..6f99465e
--- /dev/null
+++ b/web-ui/app/js/services/model/mail.js
@@ -0,0 +1,147 @@
+/*global _ */
+'use strict';
+
+define(['helpers/contenttype'],
+ function (contentType) {
+
+ var asMail = (function () {
+
+ function isSentMail() {
+ return _.contains(this.tags, 'sent');
+ }
+
+ function isDraftMail() {
+ return _.contains(this.tags, 'drafts');
+ }
+
+ function normalize(recipients) {
+ return _.chain([recipients])
+ .flatten()
+ .filter(function (r) {
+ return !_.isUndefined(r) && !_.isEmpty(r);
+ })
+ .value();
+ }
+
+ function isInTrash() {
+ return _.contains(this.tags, 'trash');
+ }
+
+ function setDraftReplyFor(ident) {
+ this.draft_reply_for = ident;
+ }
+
+ function recipients(){
+ return {
+ to: normalize(this.header.to),
+ cc: normalize(this.header.cc)
+ };
+ }
+
+ function replyToAddress() {
+ var recipients;
+
+ if (this.isSentMail()) {
+ recipients = this.recipients();
+ } else {
+ recipients = {
+ to: normalize(this.header.reply_to || this.header.from),
+ cc: []
+ };
+ }
+
+ return recipients;
+ }
+
+ function replyToAllAddress() {
+ return {
+ to: normalize([this.header.reply_to, this.header.from, this.header.to]),
+ cc: normalize(this.header.cc)
+ };
+ }
+
+ function getHeadersFromMailPart (rawBody) {
+ var lines, headerLines, endOfHeaders, headers;
+
+ lines = rawBody.split('\n');
+ endOfHeaders = _.indexOf(lines, '');
+ headerLines = lines.slice(0, endOfHeaders);
+
+ headers = _.map(headerLines, function (headerLine) {
+ return headerLine.split(': ');
+ });
+
+ return _.object(headers);
+ }
+
+ function getBodyFromMailPart (rawBody) {
+ var lines, endOfHeaders;
+
+ lines = rawBody.split('\n');
+ endOfHeaders = _.indexOf(lines, '');
+
+ return lines.slice(endOfHeaders + 1).join('\n');
+ }
+
+ function parseWithHeaders(rawBody) {
+ return {headers: getHeadersFromMailPart(rawBody), body: getBodyFromMailPart(rawBody)};
+ }
+
+ function getMailMultiParts () {
+ var mediaType = this.getMailMediaType();
+ var boundary = '--' + mediaType.params.boundary + '\n';
+ var finalBoundary = '--' + mediaType.params.boundary + '--';
+
+ var bodyParts = this.body.split(finalBoundary)[0].split(boundary);
+
+ bodyParts = _.reject(bodyParts, function(bodyPart) { return _.isEmpty(bodyPart.trim()); });
+
+ return _.map(bodyParts, parseWithHeaders);
+ };
+
+ function getMailMediaType () {
+ return new contentType.MediaType(this.header.content_type);
+ }
+
+ function isMailMultipartAlternative () {
+ return this.getMailMediaType().type === 'multipart/alternative';
+ }
+
+ function availableBodyPartsContentType () {
+ var bodyParts = this.getMailMultiParts();
+
+ return _.pluck(_.pluck(bodyParts, 'headers'), 'Content-Type');
+ }
+
+ function getMailPartByContentType (contentType) {
+ var bodyParts = this.getMailMultiParts();
+
+ return _.findWhere(bodyParts, {headers: {'Content-Type': contentType}});
+ }
+
+ return function () {
+ this.isSentMail = isSentMail;
+ this.isDraftMail = isDraftMail;
+ this.isInTrash = isInTrash;
+ this.setDraftReplyFor = setDraftReplyFor;
+ this.replyToAddress = replyToAddress;
+ this.replyToAllAddress = replyToAllAddress;
+ this.recipients = recipients;
+ this.getMailMediaType = getMailMediaType;
+ this.isMailMultipartAlternative = isMailMultipartAlternative;
+ this.getMailMultiParts = getMailMultiParts;
+ this.availableBodyPartsContentType = availableBodyPartsContentType;
+ this.getMailPartByContentType = getMailPartByContentType;
+ return this;
+ };
+ }());
+
+ return {
+ create: function (mail) {
+ if (mail) {
+ asMail.apply(mail);
+ }
+ return mail;
+ }
+ };
+});