2018-07-31 10:08:51 +00:00
|
|
|
/**
|
2018-08-28 16:27:58 +00:00
|
|
|
* @copyright Copyright (c) 2018 Team Popcorn <teampopcornberlin@gmail.com>
|
2018-07-25 14:30:23 +00:00
|
|
|
*
|
2018-08-28 16:27:58 +00:00
|
|
|
* @author Team Popcorn <teampopcornberlin@gmail.com>
|
2018-07-25 14:30:23 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2018-10-25 15:30:29 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-07-25 14:30:23 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Contact from '../models/contact'
|
2018-08-29 12:53:00 +00:00
|
|
|
import Store from '../store/index'
|
2018-07-25 14:30:23 +00:00
|
|
|
|
2018-08-29 13:55:33 +00:00
|
|
|
export default function parseVcf(data = '', addressbook) {
|
2018-07-25 14:30:23 +00:00
|
|
|
let regexp = /BEGIN:VCARD[\s\S]*?END:VCARD/mgi
|
|
|
|
let vCards = data.match(regexp)
|
2018-08-28 16:27:58 +00:00
|
|
|
|
2018-07-25 14:30:23 +00:00
|
|
|
if (!vCards) {
|
|
|
|
console.debug('Error during the parsing of the following vcf file: ', data)
|
|
|
|
return []
|
|
|
|
}
|
2018-08-29 12:53:00 +00:00
|
|
|
|
2018-09-27 13:39:12 +00:00
|
|
|
Store.dispatch('setTotal', vCards.length)
|
2018-08-29 12:53:00 +00:00
|
|
|
|
2018-09-03 11:29:06 +00:00
|
|
|
// Not using map because we want to only push valid contacts
|
|
|
|
// map force to return at least undefined
|
|
|
|
return vCards.reduce((contacts, vCard) => {
|
2018-08-10 08:47:14 +00:00
|
|
|
try {
|
2018-08-28 16:27:58 +00:00
|
|
|
// console.log(vCards.indexOf(vCard))
|
|
|
|
let contact = new Contact(vCard, addressbook)
|
2018-09-03 11:29:06 +00:00
|
|
|
contacts.push(contact)
|
2018-08-10 08:47:14 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Parse error! Do not stop here...
|
2018-09-27 13:39:12 +00:00
|
|
|
Store.dispatch('incrementDenied')
|
2018-08-10 08:47:14 +00:00
|
|
|
console.error(e)
|
|
|
|
}
|
2018-09-03 11:29:06 +00:00
|
|
|
return contacts
|
|
|
|
}, [])
|
2018-07-25 14:30:23 +00:00
|
|
|
}
|