Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-#39] It is now possible to download group of contacts. #667

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/views/Contacts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- @copyright Copyright (c) 2018 John Molakvoæ <[email protected]>
-
- @author John Molakvoæ <[email protected]>
- @author Charismatic Claire <[email protected]>
-
- @license GNU AGPL version 3 or any later version
-
Expand Down Expand Up @@ -48,6 +49,8 @@
</template>

<script>
import moment from 'moment'

import SettingsSection from 'Components/SettingsSection'
import ContactsList from 'Components/ContactsList'
import ContactDetails from 'Components/ContactDetails'
Expand Down Expand Up @@ -145,7 +148,14 @@ export default {
},
text: group.name,
utils: {
counter: group.contacts.length
counter: group.contacts.length,
actions: [
{
icon: 'icon-download',
text: 'Download',
action: () => this.downloadGroup(group)
}
]
}
}
}).sort(function(a, b) {
Expand Down Expand Up @@ -315,6 +325,53 @@ export default {
}
},

/**
* Download vcard promise as vcard file
*
* @param {Object} vcardPromise object to be downloaded
*/
downloadVcardPromise(vcardPromise) {
vcardPromise.then(response => {
const blob = new Blob([response.data], { type: 'text/vcard' })
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
const filename = moment().format('YYYY-MM-DD_HH-mm') + '_' + response.groupName + '.vcf'
link.href = url
link.download = filename
link.click()
})
},

/**
* Download group of contacts
*
* @param {Object} group of contacts to be downloaded
*/
downloadGroup(group) {
// get grouped contacts
let groupedContacts = {}
group.contacts.map((key) => {
const id = this.contacts[key].addressbook.id
groupedContacts = Object.assign({
[id]: {
addressbook: this.contacts[key].addressbook,
contacts: []
}
}, groupedContacts)
groupedContacts[id].contacts.push(this.contacts[key].url)
})
// create vcard promise with the requested contacts
const vcardPromise = Promise.all(
Object.keys(groupedContacts).map(key =>
groupedContacts[key].addressbook.dav.addressbookMultigetExport(groupedContacts[key].contacts)))
.then(response => ({
groupName: group.name,
data: response.map(data => data.body).join('')
}))
// download vcard
this.downloadVcardPromise(vcardPromise)
},

/* SEARCH */
search(query) {
this.searchQuery = query
Expand Down