2018-07-31 10:08:51 +00:00
|
|
|
/**
|
2018-07-10 16:40:59 +00:00
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
*
|
|
|
|
* @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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-07-28 18:13:27 +00:00
|
|
|
|
2018-07-16 14:16:01 +00:00
|
|
|
/* eslint-disable-next-line import/no-webpack-loader-syntax */
|
|
|
|
import vcfFile from '!raw-loader!./FakeName.vcf'
|
2018-07-25 14:30:23 +00:00
|
|
|
import parseVcf from '../services/parseVcf'
|
2018-07-28 18:13:27 +00:00
|
|
|
import Vue from 'vue'
|
2018-07-10 16:40:59 +00:00
|
|
|
|
2018-07-16 14:16:01 +00:00
|
|
|
const state = {
|
|
|
|
addressbooks: []
|
|
|
|
}
|
|
|
|
const mutations = {
|
|
|
|
/**
|
|
|
|
* Store addressbooks into state
|
2018-07-28 18:13:27 +00:00
|
|
|
*
|
2018-07-16 14:16:01 +00:00
|
|
|
* @param {Object} state Default state
|
|
|
|
* @param {Array} addressbooks Addressbooks
|
|
|
|
*/
|
|
|
|
appendAddressbooks(state, addressbooks) {
|
|
|
|
state.addressbooks = addressbooks
|
|
|
|
},
|
2018-07-28 18:13:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a contact list to an addressbook
|
|
|
|
* and remove duplicates
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {Object} data.addressbook the addressbook
|
|
|
|
* @param {Contact[]} data.contacts array of contacts to append
|
|
|
|
*/
|
|
|
|
async appendContactsToAddressbook(state, { addressbook, contacts }) {
|
|
|
|
addressbook = state.addressbooks.find(search => search === addressbook)
|
2018-07-27 13:03:43 +00:00
|
|
|
// convert list into an array and remove duplicate
|
2018-07-28 18:13:27 +00:00
|
|
|
addressbook.contacts = contacts.reduce((list, contact) => {
|
2018-07-27 13:03:43 +00:00
|
|
|
if (list[contact.uid]) {
|
|
|
|
console.debug('Duplicate contact overrided', list[contact.uid], contact)
|
|
|
|
}
|
2018-07-28 18:13:27 +00:00
|
|
|
Vue.set(list, contact.uid, contact)
|
2018-07-27 13:03:43 +00:00
|
|
|
return list
|
|
|
|
}, addressbook.contacts)
|
2018-07-28 18:13:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a contact in a specified addressbook
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Contact} contact the contact to delete
|
|
|
|
*/
|
|
|
|
deleteContactFromAddressbook(state, contact) {
|
|
|
|
let addressbook = state.addressbooks.find(addressbook => addressbook === contact.addressbook)
|
|
|
|
Vue.delete(addressbook, contact.uid)
|
2018-07-31 13:27:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Share addressbook with a user or group
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {Object} data.addressbook the addressbook
|
|
|
|
*/
|
|
|
|
shareAddressbook(state, { addressbook, sharee }) {
|
|
|
|
addressbook = state.addressbooks.find(search => search === addressbook)
|
|
|
|
let newSharee = {}
|
|
|
|
sharee.displayname = sharee
|
|
|
|
sharee.writable = false
|
|
|
|
addressbook.shares.append(newSharee)
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Share addressbook with a user or group
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {Object} data.addressbook the addressbook
|
|
|
|
*/
|
|
|
|
removeSharee(state, { addressbook, sharee }) {
|
|
|
|
addressbook = state.addressbooks.find(search => search === addressbook)
|
|
|
|
addressbook.shares.splice(sharee, 1)
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle sharee's writable permission
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {Object} data.addressbook the addressbook
|
|
|
|
* @param {Object} sharee the sharee
|
|
|
|
*/
|
|
|
|
toggleShareeWritable(state, { addressbook, sharee }) {
|
|
|
|
addressbook = state.addressbooks.find(search => search === addressbook)
|
|
|
|
sharee = addressbook.find(search => search === addressbook)
|
|
|
|
sharee.writable = !sharee.writable
|
2018-07-16 14:16:01 +00:00
|
|
|
}
|
2018-07-31 13:27:56 +00:00
|
|
|
|
2018-07-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
const getters = {
|
|
|
|
getAddressbooks(state) {
|
|
|
|
return state.addressbooks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const actions = {
|
|
|
|
/**
|
|
|
|
* Retrieve and commit addressbooks
|
|
|
|
* @param {Object} context Current context
|
|
|
|
*/
|
|
|
|
getAddressbooks(context) {
|
|
|
|
// Fake data before using real dav requests
|
|
|
|
let addressbooks = [
|
|
|
|
{
|
|
|
|
id: 'ab1',
|
|
|
|
displayName: 'Addressbook 1',
|
|
|
|
enabled: true,
|
2018-07-25 14:30:23 +00:00
|
|
|
owner: 'admin',
|
2018-07-30 15:26:28 +00:00
|
|
|
shares: [
|
2018-07-31 13:27:56 +00:00
|
|
|
{ displayname: 'Bob', writable: true },
|
|
|
|
{ displayname: 'Rita', writable: true },
|
|
|
|
{ displayname: 'Sue', writable: false }
|
2018-07-30 15:26:28 +00:00
|
|
|
],
|
2018-07-27 13:03:43 +00:00
|
|
|
contacts: {}
|
2018-07-16 14:16:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'ab2',
|
|
|
|
displayName: 'Addressbook 2',
|
|
|
|
enabled: false,
|
2018-07-25 14:30:23 +00:00
|
|
|
owner: 'admin',
|
2018-07-31 13:27:56 +00:00
|
|
|
shares: [
|
|
|
|
{ displayname: 'Aimee', writable: true },
|
|
|
|
{ displayname: 'Jaguar', writable: true }
|
|
|
|
],
|
2018-07-27 13:03:43 +00:00
|
|
|
contacts: {}
|
2018-07-16 14:16:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'ab3',
|
|
|
|
displayName: 'Addressbook 3',
|
|
|
|
enabled: true,
|
2018-07-25 14:30:23 +00:00
|
|
|
owner: 'User1',
|
|
|
|
shares: [],
|
2018-07-27 13:03:43 +00:00
|
|
|
contacts: {}
|
2018-07-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
// fake request
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return setTimeout(() => {
|
|
|
|
context.commit('appendAddressbooks', addressbooks)
|
|
|
|
resolve()
|
|
|
|
return addressbooks
|
2018-07-27 13:03:43 +00:00
|
|
|
}, 0)
|
2018-07-16 14:16:01 +00:00
|
|
|
})
|
|
|
|
},
|
2018-07-28 18:13:27 +00:00
|
|
|
async getContactsFromAddressBook(context, addressbook) {
|
|
|
|
let contacts = parseVcf(vcfFile, addressbook)
|
|
|
|
await context.commit('appendContactsToAddressbook', { addressbook, contacts })
|
|
|
|
await context.commit('appendContacts', contacts)
|
|
|
|
await context.commit('sortContacts')
|
2018-07-31 10:08:51 +00:00
|
|
|
await context.commit('appendGroups', contacts)
|
2018-07-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-10 16:40:59 +00:00
|
|
|
|
|
|
|
export default { state, mutations, getters, actions }
|