From fafac3b4128a0993b0de1c6e8ca3062bf1ccc14e Mon Sep 17 00:00:00 2001 From: Roald de Vries Date: Thu, 8 Dec 2016 16:59:09 +0100 Subject: Revert "[#801] Merge branch 'signup'" This reverts commit d10f607a4d40587510b0dc31b31fe4750bf4a3a3, reversing changes made to c28abba2f5b1186c671ebef508d40ffaae6d5bc5. --- web-ui/app/js/services/delete_service.js | 59 ++++++ web-ui/app/js/services/mail_service.js | 335 ++++++++++++++++++++++++++++++ web-ui/app/js/services/model/mail.js | 126 +++++++++++ web-ui/app/js/services/recover_service.js | 38 ++++ 4 files changed, 558 insertions(+) create mode 100644 web-ui/app/js/services/delete_service.js create mode 100644 web-ui/app/js/services/mail_service.js create mode 100644 web-ui/app/js/services/model/mail.js create mode 100644 web-ui/app/js/services/recover_service.js (limited to 'web-ui/app/js/services') diff --git a/web-ui/app/js/services/delete_service.js b/web-ui/app/js/services/delete_service.js new file mode 100644 index 00000000..0dfc1bdb --- /dev/null +++ b/web-ui/app/js/services/delete_service.js @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2014 ThoughtWorks, Inc. + * + * Pixelated is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Pixelated is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Pixelated. If not, see . + */ + +define(['flight/lib/component', 'page/events', 'views/i18n'], function (defineComponent, events, i18n) { + 'use strict'; + + return defineComponent(function() { + + this.successDeleteMessageFor = function(mail) { + return mail.isInTrash() ? + i18n.t('delete-single') : + i18n.t('trash-single'); + }; + + this.successDeleteManyMessageFor = function(mail) { + return mail.isInTrash() ? + i18n.t('delete-bulk') : + i18n.t('trash-bulk'); + }; + + this.deleteEmail = function (event, data) { + this.trigger(document, events.mail.delete, { + mail: data.mail, + successMessage: this.successDeleteMessageFor(data.mail) + }); + }; + + this.deleteManyEmails = function (event, data) { + var emails = _.values(data.checkedMails), + firstEmail = emails[_.first(_.keys(emails))]; + + this.trigger(document, events.mail.deleteMany, { + mails: emails, + successMessage: this.successDeleteManyMessageFor(firstEmail) + }); + + }; + + this.after('initialize', function () { + this.on(document, events.ui.mail.delete, this.deleteEmail); + this.on(document, events.ui.mail.deleteMany, this.deleteManyEmails); + }); + + }); +}); diff --git a/web-ui/app/js/services/mail_service.js b/web-ui/app/js/services/mail_service.js new file mode 100644 index 00000000..5e4bd4f3 --- /dev/null +++ b/web-ui/app/js/services/mail_service.js @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2014 ThoughtWorks, Inc. + * + * Pixelated is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Pixelated is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Pixelated. If not, see . + */ + +define( + [ + 'flight/lib/component', + 'views/i18n', + 'services/model/mail', + 'helpers/monitored_ajax', + 'page/events', + 'features', + 'mixins/with_auto_refresh', + 'page/router/url_params' + ], function (defineComponent, i18n, Mail, monitoredAjax, events, features, withAutoRefresh, urlParams) { + + 'use strict'; + + return defineComponent(mailService, withAutoRefresh('refreshMails')); + + function mailService() { + var that; + + this.defaultAttrs({ + mailsResource: '/mails', + singleMailResource: '/mail', + currentTag: '', + lastQuery: '', + currentPage: 1, + numPages: 1, + pageSize: 25 + }); + + this.errorMessage = function (msg) { + return function () { + that.trigger(document, events.ui.userAlerts.displayMessage, { message: msg }); + }; + }; + + this.updateTags = function (ev, data) { + var ident = data.ident; + + var success = function (data) { + this.refreshMails(); + $(document).trigger(events.mail.tags.updated, { ident: ident, tags: data.tags }); + $(document).trigger(events.dispatchers.tags.refreshTagList, { skipMailListRefresh: true }); + }; + + var failure = function (resp) { + var msg = i18n.t('failed-change-tags'); + if (resp.status === 403) { + msg = i18n.t('invalid-tag-name'); + } + this.trigger(document, events.ui.userAlerts.displayMessage, { message: msg }); + }; + + monitoredAjax(this, '/mail/' + ident + '/tags', { + type: 'POST', + contentType: 'application/json; charset=utf-8', + data: JSON.stringify({newtags: data.tags}) + }).done(success.bind(this)).fail(failure.bind(this)); + + }; + + this.readMail = function (ev, data) { + var mailIdents; + if (data.checkedMails) { + mailIdents = _.map(data.checkedMails, function (mail) { + return mail.ident; + }); + } else { + mailIdents = [data.ident]; + } + monitoredAjax(this, '/mails/read', { + type: 'POST', + data: JSON.stringify({idents: mailIdents}) + }).done(this.triggerMailsRead(data.checkedMails)); + }; + + this.unreadMail = function (ev, data) { + var mailIdents; + if (data.checkedMails) { + mailIdents = _.map(data.checkedMails, function (mail) { + return mail.ident; + }); + } else { + mailIdents = [data.ident]; + } + monitoredAjax(this, '/mails/unread', { + type: 'POST', + data: JSON.stringify({idents: mailIdents}) + }).done(this.triggerMailsRead(data.checkedMails)); + }; + + this.triggerMailsRead = function (mails) { + return _.bind(function () { + this.refreshMails(); + this.trigger(document, events.ui.mails.uncheckAll); + }, this); + }; + + this.triggerDeleted = function (dataToDelete) { + return _.bind(function () { + var mails = dataToDelete.mails || [dataToDelete.mail]; + + this.refreshMails(); + this.trigger(document, events.ui.userAlerts.displayMessage, { message: dataToDelete.successMessage}); + this.trigger(document, events.ui.mails.uncheckAll); + this.trigger(document, events.mail.deleted, { mails: mails }); + }, this); + }; + + this.triggerRecovered = function (dataToRecover) { + return _.bind(function () { + var mails = dataToRecover.mails || [dataToRecover.mail]; + + this.refreshMails(); + this.trigger(document, events.ui.userAlerts.displayMessage, { message: i18n.t(dataToRecover.successMessage)}); + this.trigger(document, events.ui.mails.uncheckAll); + }, this); + }; + + this.triggerArchived = function (dataToArchive) { + return _.bind(function (response) { + this.refreshMails(); + this.trigger(document, events.ui.userAlerts.displayMessage, { message: i18n.t(response.successMessage)}); + this.trigger(document, events.ui.mails.uncheckAll); + }, this); + }; + + this.archiveManyMails = function(event, dataToArchive) { + var mailIdents = _.map(dataToArchive.checkedMails, function (mail) { + return mail.ident; + }); + monitoredAjax(this, '/mails/archive', { + type: 'POST', + dataType: 'json', + contentType: 'application/json; charset=utf-8', + data: JSON.stringify({idents: mailIdents}) + }).done(this.triggerArchived(dataToArchive)) + .fail(this.errorMessage(i18n.t('failed-archive'))); + }; + + this.deleteMail = function (ev, data) { + monitoredAjax(this, '/mail/' + data.mail.ident, + {type: 'DELETE'}) + .done(this.triggerDeleted(data)) + .fail(this.errorMessage(i18n.t('failed-delete-single'))); + }; + + this.deleteManyMails = function (ev, data) { + var dataToDelete = data; + var mailIdents = _.map(data.mails, function (mail) { + return mail.ident; + }); + + monitoredAjax(this, '/mails/delete', { + type: 'POST', + dataType: 'json', + contentType: 'application/json; charset=utf-8', + data: JSON.stringify({idents: mailIdents}) + }).done(this.triggerDeleted(dataToDelete)) + .fail(this.errorMessage(i18n.t('failed-delete-bulk'))); + }; + + this.recoverManyMails = function (ev, data) { + var dataToRecover = data; + var mailIdents = _.map(data.mails, function (mail) { + return mail.ident; + }); + + monitoredAjax(this, '/mails/recover', { + type: 'POST', + dataType: 'json', + contentType: 'application/json; charset=utf-8', + data: JSON.stringify({idents: mailIdents}) + }).done(this.triggerRecovered(dataToRecover)) + .fail(this.errorMessage(i18n.t('Could not move emails to inbox'))); + }; + + function compileQuery(data) { + var query = 'tag:"' + that.attr.currentTag + '"'; + + if (data.tag === 'all') { + query = 'in:all'; + } + return query; + } + + this.fetchByTag = function (ev, data) { + this.attr.currentTag = data.tag; + this.attr.lastQuery = compileQuery(data); + this.updateCurrentPageNumber(1); + + this.refreshMails(); + }; + + this.newSearch = function (ev, data) { + this.attr.lastQuery = data.query; + this.attr.currentTag = 'all'; + this.refreshMails(); + }; + + this.mailFromJSON = function (mail) { + return Mail.create(mail); + }; + + this.parseMails = function (data) { + data.mails = _.map(data.mails, this.mailFromJSON, this); + + return data; + }; + + function escaped(s) { + return encodeURIComponent(s); + } + + this.excludeTrashedEmailsForDraftsAndSent = function (query) { + if (query === 'tag:"drafts"' || query === 'tag:"sent"') { + return query + ' -in:"trash"'; + } + return query; + }; + + this.refreshMails = function () { + var url = this.attr.mailsResource + '?q=' + escaped(this.attr.lastQuery) + '&p=' + this.attr.currentPage + '&w=' + this.attr.pageSize; + + this.attr.lastQuery = this.excludeTrashedEmailsForDraftsAndSent(this.attr.lastQuery); + + monitoredAjax(this, url, { dataType: 'json' }) + .done(function (data) { + this.attr.numPages = Math.ceil(data.stats.total / this.attr.pageSize); + this.trigger(document, events.mails.available, _.merge({tag: this.attr.currentTag, forSearch: this.attr.lastQuery }, this.parseMails(data))); + }.bind(this)) + .fail(function () { + this.trigger(document, events.ui.userAlerts.displayMessage, { message: i18n.t('failed-fetch-messages'), class: 'error' }); + }.bind(this)); + }; + + function createSingleMailUrl(mailsResource, ident) { + return mailsResource + '/' + ident; + } + + this.fetchSingle = function (event, data) { + var fetchUrl = createSingleMailUrl(this.attr.singleMailResource, data.mail); + + monitoredAjax(this, fetchUrl, { dataType: 'json' }) + .done(function (mail) { + if (_.isNull(mail)) { + this.trigger(data.caller, events.mail.notFound); + return; + } + + this.trigger(data.caller, events.mail.here, { mail: this.mailFromJSON(mail) }); + }.bind(this)); + }; + + this.previousPage = function () { + if (this.attr.currentPage > 1) { + this.updateCurrentPageNumber(this.attr.currentPage - 1); + this.refreshMails(); + } + }; + + this.nextPage = function () { + if (this.attr.currentPage < (this.attr.numPages)) { + this.updateCurrentPageNumber(this.attr.currentPage + 1); + this.refreshMails(); + } + }; + + this.updateCurrentPageNumber = function (newCurrentPage) { + this.attr.currentPage = newCurrentPage; + this.trigger(document, events.ui.page.changed, { + currentPage: this.attr.currentPage, + numPages: this.attr.numPages + }); + }; + + this.wantDraftReplyForMail = function (ev, data) { + if (!features.isEnabled('draftReply')) { + this.trigger(document, events.mail.draftReply.notFound); + return; + } + + monitoredAjax(this, '/draft_reply_for/' + data.ident, { dataType: 'json' }) + .done(function (mail) { + if (_.isNull(mail)) { + this.trigger(document, events.mail.draftReply.notFound); + return; + } + this.trigger(document, events.mail.draftReply.here, { mail: this.mailFromJSON(mail) }); + }.bind(this)); + }; + + this.after('initialize', function () { + that = this; + + if (features.isEnabled('tags')) { + this.on(events.mail.tags.update, this.updateTags); + } + + this.on(document, events.mail.draftReply.want, this.wantDraftReplyForMail); + this.on(document, events.mail.want, this.fetchSingle); + this.on(document, events.mail.read, this.readMail); + this.on(document, events.mail.unread, this.unreadMail); + this.on(document, events.mail.delete, this.deleteMail); + this.on(document, events.mail.deleteMany, this.deleteManyMails); + this.on(document, events.mail.recoverMany, this.recoverManyMails); + this.on(document, events.mail.archiveMany, this.archiveManyMails); + this.on(document, events.search.perform, this.newSearch); + this.on(document, events.ui.tag.selected, this.fetchByTag); + this.on(document, events.ui.tag.select, this.fetchByTag); + this.on(document, events.ui.mails.refresh, this.refreshMails); + this.on(document, events.ui.page.previous, this.previousPage); + this.on(document, events.ui.page.next, this.nextPage); + + this.fetchByTag(null, {tag: urlParams.getTag()}); + }); + } + } +); 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..64a10c1c --- /dev/null +++ b/web-ui/app/js/services/model/mail.js @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2014 ThoughtWorks, Inc. + * + * Pixelated is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Pixelated is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Pixelated. If not, see . + */ +define(['helpers/contenttype'], function (contentType) { + 'use strict'; + function isSentMail() { + return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'SENT'; + } + + function isDraftMail() { + return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'DRAFTS'; + } + + function isInTrash() { + return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'TRASH'; + } + + function setDraftReplyFor(ident) { + this.draft_reply_for = ident; + } + + function replyToAddress() { + return { + to: [this.replying.single], + cc: [] + }; + } + + function replyToAllAddress() { + return { + to: this.replying.all['to-field'], + cc: this.replying.all['cc-field'] + }; + } + + 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 _.map(headerLine.split(':'), function(elem){return elem.trim();}); + }); + + 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 { + create: function (mail) { + if (!mail) { return; } + + mail.isSentMail = isSentMail; + mail.isDraftMail = isDraftMail; + mail.isInTrash = isInTrash; + mail.setDraftReplyFor = setDraftReplyFor; + mail.replyToAddress = replyToAddress; + mail.replyToAllAddress = replyToAllAddress; + mail.getMailMediaType = getMailMediaType; + mail.isMailMultipartAlternative = isMailMultipartAlternative; + mail.getMailMultiParts = getMailMultiParts; + mail.availableBodyPartsContentType = availableBodyPartsContentType; + mail.getMailPartByContentType = getMailPartByContentType; + return mail; + } + }; +}); diff --git a/web-ui/app/js/services/recover_service.js b/web-ui/app/js/services/recover_service.js new file mode 100644 index 00000000..d7d9cdc9 --- /dev/null +++ b/web-ui/app/js/services/recover_service.js @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2014 ThoughtWorks, Inc. + * + * Pixelated is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Pixelated is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Pixelated. If not, see . + */ + +define(['flight/lib/component', 'page/events', 'views/i18n'], function (defineComponent, events, i18n) { + 'use strict'; + + return defineComponent(function() { + + this.recoverManyEmails = function (event, data) { + var emails = _.values(data.checkedMails); + + this.trigger(document, events.mail.recoverMany, { + mails: emails, + successMessage: i18n.t('Your messages were moved to inbox!') + }); + + }; + + this.after('initialize', function () { + this.on(document, events.ui.mail.recoverMany, this.recoverManyEmails); + }); + + }); +}); -- cgit v1.2.3