From 0896e9f9cf19f1e33608dfce7137ae1c80f59c03 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Thu, 15 Sep 2016 09:57:19 +0300 Subject: [PATCH] v1.19.0 --- CHANGELOG.txt | 4 ++ LICENSE | 28 +++++----- README.md | 3 +- lib/models/subscriptions.js | 100 ++++++++++++++++++++++++++++++++++++ package.json | 5 +- views/index.hbs | 2 +- 6 files changed, 124 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 75065173..0949bb67 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,5 +1,9 @@ # Changelog +## 1.19.0 2016-09-15 + + * Changed license from GPL-V3 to MIT + ## 1.18.0 2016-09-08 * Updated installation script to bundle ZoneMTA as the default sending engine diff --git a/LICENSE b/LICENSE index 9e344928..a22c7ce1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,16 +1,16 @@ -Mailtrain, self hosted email newsletter app -Copyright (C) 2016 Kreata OÜ +Copyright (c) 2016 Andris Reinman -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program 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 General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 7483a0a2..1b38eb02 100644 --- a/README.md +++ b/README.md @@ -183,4 +183,5 @@ This command generates a CSV file with 100 000 subscriber accounts ## License -**GPL-V3.0** + * Versions 1.19.0 and up: **MIT** + * Up to versions 1.18.0 **GPL-V3.0** diff --git a/lib/models/subscriptions.js b/lib/models/subscriptions.js index 17f20d76..328d6f5c 100644 --- a/lib/models/subscriptions.js +++ b/lib/models/subscriptions.js @@ -10,6 +10,7 @@ let settings = require('./settings'); let mailer = require('../mailer'); let urllib = require('url'); let log = require('npmlog'); +let csvGenerate = require('csv-generate'); module.exports.list = (listId, start, limit, callback) => { listId = Number(listId) || 0; @@ -1115,3 +1116,102 @@ module.exports.listImports = (listId, callback) => { }); }); }; + +module.exports.exportList = (listId, request, columns, segmentId, callback) => { + listId = Number(listId) || 0; + segmentId = Number(segmentId) || 0; + + if (!listId) { + return callback(new Error('Missing List ID')); + } + + let processQuery = queryData => { + + db.getConnection((err, connection) => { + if (err) { + return callback(err); + } + + let query = 'SELECT COUNT(id) AS total FROM `subscription__' + listId + '`'; + let values = []; + + if (queryData.where) { + query += ' WHERE ' + queryData.where; + values = values.concat(queryData.values || []); + } + + connection.query(query, values, (err, total) => { + if (err) { + connection.release(); + return callback(err); + } + total = total && total[0] && total[0].total || 0; + + let ordering = []; + + if (request.order && request.order.length) { + + request.order.forEach(order => { + let orderField = columns[Number(order.column)]; + let orderDirection = (order.dir || '').toString().toLowerCase() === 'desc' ? 'DESC' : 'ASC'; + if (orderField) { + ordering.push('`' + orderField + '` ' + orderDirection); + } + }); + } + + if (!ordering.length) { + ordering.push('`email` ASC'); + } + + let args = [Number(request.length) || 50, Number(request.start) || 0]; + let query; + + + let generator = csvGenerate({ + columns: , + length: 2 + }); + + if (request.search && request.search.value) { + query = 'SELECT SQL_CALC_FOUND_ROWS * FROM `subscription__' + listId + '` WHERE email LIKE ? OR first_name LIKE ? OR last_name LIKE ? ' + (queryData.where ? ' AND (' + queryData.where + ')' : '') + ' ORDER BY ' + ordering.join(', ') + ' LIMIT ? OFFSET ?'; + + let searchVal = '%' + request.search.value.replace(/\\/g, '\\\\').replace(/([%_])/g, '\\$1') + '%'; + args = [searchVal, searchVal, searchVal].concat(queryData.values || []).concat(args); + } else { + query = 'SELECT SQL_CALC_FOUND_ROWS * FROM `subscription__' + listId + '` WHERE 1 ' + (queryData.where ? ' AND (' + queryData.where + ')' : '') + ' ORDER BY ' + ordering.join(', ') + ' LIMIT ? OFFSET ?'; + args = [].concat(queryData.values || []).concat(args); + } + + connection.query(query, args, (err, rows) => { + if (err) { + connection.release(); + return callback(err); + } + connection.query('SELECT FOUND_ROWS() AS total', (err, filteredTotal) => { + connection.release(); + if (err) { + return callback(err); + } + + let subscriptions = rows.map(row => tools.convertKeys(row)); + + filteredTotal = filteredTotal && filteredTotal[0] && filteredTotal[0].total || 0; + return callback(null, subscriptions, total, filteredTotal); + }); + }); + }); + }); + }; + + if (segmentId) { + segments.getQuery(segmentId, false, (err, queryData) => { + if (err) { + return callback(err); + } + processQuery(queryData); + }); + } else { + processQuery(false); + } +}; diff --git a/package.json b/package.json index 4862b97c..27c31ea5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mailtrain", "private": true, - "version": "1.18.0", + "version": "1.19.0", "description": "Self hosted email newsletter app", "main": "index.js", "scripts": { @@ -17,7 +17,7 @@ "url": "git://github.com/andris9/mailtrain.git" }, "author": "Andris Reinman", - "license": "GPL-3.0", + "license": "MIT", "homepage": "http://mailtrain.org", "engines": { "node": ">=5.0.0" @@ -38,6 +38,7 @@ "connect-redis": "^3.1.0", "cookie-parser": "^1.4.3", "csurf": "^1.9.0", + "csv-generate": "^1.0.0", "csv-parse": "^1.1.7", "escape-html": "^1.0.3", "express": "^4.14.0", diff --git a/views/index.hbs b/views/index.hbs index e50c9cd3..630651cd 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -58,7 +58,7 @@

Open source

-

Mailtrain is available under GPLv3 license and completely open source.

+

Mailtrain is available under MIT license and completely open source.

Send via any provider