From d982fcf753353236cbaa65a8715cbb4097d2e49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Fri, 25 Jan 2019 09:48:19 +0100 Subject: [PATCH 1/2] Properly format displayName and validate fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- .eslintrc.js | 2 +- src/models/contact.js | 50 ++++++++++++++++++++++++-------- src/services/checks/index.js | 27 +++++++++++++++++ src/services/checks/missingFN.js | 48 ++++++++++++++++++++++++++++++ src/services/parseVcf.js | 2 +- src/services/validate.js | 47 ++++++++++++++++++++++++++++++ src/store/addressbooks.js | 2 +- src/store/contacts.js | 4 +++ src/views/Contacts.vue | 1 + 9 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 src/services/checks/index.js create mode 100644 src/services/checks/missingFN.js create mode 100644 src/services/validate.js diff --git a/.eslintrc.js b/.eslintrc.js index 738e0a83e..6e1d2a98f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -53,7 +53,7 @@ module.exports = { 'no-tabs': 0, 'vue/html-indent': ['error', 'tab'], // only debug console - 'no-console': ['error', { allow: ['error', 'warn', 'debug'] }], + 'no-console': ['error', { allow: ['error', 'warn', 'info', 'debug'] }], // classes blocks 'padded-blocks': ['error', { classes: 'always' }], // always have the operator in front diff --git a/src/models/contact.js b/src/models/contact.js index 2f877d2e2..1240d3d29 100644 --- a/src/models/contact.js +++ b/src/models/contact.js @@ -23,6 +23,8 @@ import uuid from 'uuid' import ICAL from 'ical.js' +import store from '../store' + export default class Contact { /** @@ -52,7 +54,7 @@ export default class Contact { // if no uid set, create one if (!this.vCard.hasProperty('uid')) { - console.debug('This contact did not have a proper uid. Setting a new one for ', this) + console.info('This contact did not have a proper uid. Setting a new one for ', this) this.vCard.addPropertyWithValue('uid', uuid()) } } @@ -271,25 +273,49 @@ export default class Contact { } /** - * Return the display name + * Formatted display name based on the order key * * @readonly * @memberof Contact - * @returns {string} the displayName */ get displayName() { + const orderKey = store.getters.getOrderKey + const n = this.vCard.getFirstPropertyValue('n') + const fn = this.vCard.getFirstPropertyValue('fn') + const org = this.vCard.getFirstPropertyValue('org') + + // if ordered by last or first name we need the N property + if (orderKey && n) { + switch (orderKey) { + case 'firstName': + // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. + // -> John Stevenson + return n.slice(0, 2).reverse().join(' ') + + case 'lastName': + // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. + // -> Stevenson, John + return n.slice(0, 2).join(', ') + } + } + // otherwise the FN is enough if (this.vCard.hasProperty('fn')) { - return this.vCard.getFirstPropertyValue('fn') + return fn } - if (this.vCard.hasProperty('n')) { - // reverse and join - return this.vCard.getFirstPropertyValue('n') - .filter(function(part) { - return part - }) - .join(' ') + // BUT if no FN property use the N anyway + if (n) { + // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. + // -> John Stevenson + return n.slice(0, 2).reverse().join(' ') + } + // LAST chance, use the org ir that's the only thing we have + if (org) { + // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. + // -> John Stevenson + return org } - return null + return '' + } /** diff --git a/src/services/checks/index.js b/src/services/checks/index.js new file mode 100644 index 000000000..4a72ce096 --- /dev/null +++ b/src/services/checks/index.js @@ -0,0 +1,27 @@ +/** + * @copyright Copyright (c) 2019 John Molakvoæ + * + * @author John Molakvoæ + * + * @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 + * 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 this program. If not, see . + * + */ + +import missingFN from './missingFN' + +export default [ + missingFN +] diff --git a/src/services/checks/missingFN.js b/src/services/checks/missingFN.js new file mode 100644 index 000000000..3c7ea9e64 --- /dev/null +++ b/src/services/checks/missingFN.js @@ -0,0 +1,48 @@ +/** + * @copyright Copyright (c) 2019 John Molakvoæ + * + * @author John Molakvoæ + * + * @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 + * 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 this program. If not, see . + * + */ + +/** + * the FN field is mandatory. If there is none we need to + * create it based on the available data + */ +export default { + name: 'missing FN', + run: contact => { + return !contact.vCard.hasProperty('fn') + }, + fix: contact => { + if (contact.vCard.hasProperty('n')) { + // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. + // -> John Stevenson + const n = contact.vCard.getFirstPropertyValue('n') + contact.fullName = n.slice(0, 2).reverse().join(' ') + return true + } else if (contact.vCard.hasProperty('org')) { + const org = contact.vCard.getFirstPropertyValue('org') + // ABC, Inc.;North American Division;Marketing + // -> ABC, Inc. + contact.fullName = org[0] + return true + } + return false + } +} diff --git a/src/services/parseVcf.js b/src/services/parseVcf.js index ea733eaae..184c8e764 100644 --- a/src/services/parseVcf.js +++ b/src/services/parseVcf.js @@ -28,7 +28,7 @@ export default function parseVcf(data = '', addressbook) { let vCards = data.match(regexp) if (!vCards) { - console.debug('Error during the parsing of the following vcf file: ', data) + console.error('Error during the parsing of the following vcf file: ', data) return [] } diff --git a/src/services/validate.js b/src/services/validate.js new file mode 100644 index 000000000..986dd0a2f --- /dev/null +++ b/src/services/validate.js @@ -0,0 +1,47 @@ +/** + * @copyright Copyright (c) 2019 John Molakvoæ + * + * @author John Molakvoæ + * + * @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 + * 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 this program. If not, see . + * + */ + +import Contact from '../models/contact' +import checks from './checks/' + +export default function(contact) { + if (contact instanceof Contact) { + + // Going through every checks + checks.forEach(check => { + if (check.run(contact)) { + + // A fix is needed, running ⏳ + if (!check.fix(contact)) { + // FAILURE 🙅 + console.warn('The following contact needed a correction that failed', check.name, contact) + } else { + // SUCCESS 💪 + console.info('The following contact has been repaired', check.name, contact) + } + } + }) + + } else { + throw new Error('Invalid contact provided') + } +} diff --git a/src/store/addressbooks.js b/src/store/addressbooks.js index 3a35b0d1c..768b6a0fc 100644 --- a/src/store/addressbooks.js +++ b/src/store/addressbooks.js @@ -150,7 +150,7 @@ const mutations = { // convert list into an array and remove duplicate addressbook.contacts = contacts.reduce((list, contact) => { if (list[contact.uid]) { - console.debug('Duplicate contact overrided', list[contact.uid], contact) + console.info('Duplicate contact overrided', list[contact.uid], contact) } Vue.set(list, contact.uid, contact) return list diff --git a/src/store/contacts.js b/src/store/contacts.js index da47e0676..5258d8612 100644 --- a/src/store/contacts.js +++ b/src/store/contacts.js @@ -23,6 +23,7 @@ import Vue from 'vue' import ICAL from 'ical.js' import Contact from '../models/contact' +import validate from '../services/validate' const state = { // Using objects for performance @@ -78,6 +79,9 @@ const mutations = { addContact(state, contact) { if (contact instanceof Contact) { + // Checking contact validity 🙈 + validate(contact) + let sortedContact = { key: contact.key, value: contact[state.orderKey] diff --git a/src/views/Contacts.vue b/src/views/Contacts.vue index c7c80a429..894389fd2 100644 --- a/src/views/Contacts.vue +++ b/src/views/Contacts.vue @@ -291,6 +291,7 @@ export default { }) } catch (error) { OC.Notification.showTemporary(t('contacts', 'Unable to create the contact.')) + console.error(error) } }, From 1f864843455282d4e4a02a46ad1560ed7abe27cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Fri, 25 Jan 2019 11:33:44 +0100 Subject: [PATCH 2/2] Fix displayName checks and replace FN if empty on validate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- src/models/contact.js | 16 +++++++++------- src/services/checks/missingFN.js | 3 ++- src/services/validate.js | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/models/contact.js b/src/models/contact.js index 1240d3d29..f12cef50e 100644 --- a/src/models/contact.js +++ b/src/models/contact.js @@ -285,7 +285,10 @@ export default class Contact { const org = this.vCard.getFirstPropertyValue('org') // if ordered by last or first name we need the N property - if (orderKey && n) { + // ! by checking the property we check for null AND empty string + // ! that means we can then check for empty array and be safe not to have + // ! 'xxxx'.join('') !== '' + if (orderKey && n && n.join('') !== '') { switch (orderKey) { case 'firstName': // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. @@ -299,20 +302,19 @@ export default class Contact { } } // otherwise the FN is enough - if (this.vCard.hasProperty('fn')) { + if (fn) { return fn } // BUT if no FN property use the N anyway - if (n) { + if (n && n.join('') !== '') { // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. // -> John Stevenson return n.slice(0, 2).reverse().join(' ') } // LAST chance, use the org ir that's the only thing we have - if (org) { - // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. - // -> John Stevenson - return org + if (org && org.join('') !== '') { + // org is supposed to be an array but is also used as plain string + return Array.isArray(org) ? org[0] : org } return '' diff --git a/src/services/checks/missingFN.js b/src/services/checks/missingFN.js index 3c7ea9e64..1f17afc85 100644 --- a/src/services/checks/missingFN.js +++ b/src/services/checks/missingFN.js @@ -27,7 +27,8 @@ export default { name: 'missing FN', run: contact => { - return !contact.vCard.hasProperty('fn') + return !contact.vCard.hasProperty('fn') // No FN + || contact.vCard.getFirstPropertyValue('fn') === '' // Empty FN }, fix: contact => { if (contact.vCard.hasProperty('n')) { diff --git a/src/services/validate.js b/src/services/validate.js index 986dd0a2f..be080988d 100644 --- a/src/services/validate.js +++ b/src/services/validate.js @@ -33,10 +33,10 @@ export default function(contact) { // A fix is needed, running ⏳ if (!check.fix(contact)) { // FAILURE 🙅 - console.warn('The following contact needed a correction that failed', check.name, contact) + console.warn('The following contact needed a correction that failed:', check.name, contact) } else { // SUCCESS 💪 - console.info('The following contact has been repaired', check.name, contact) + console.info('The following contact has been repaired:', check.name, contact) } } })