Skip to content

Commit

Permalink
feat: added URLs to be exported in vCards. (#5609)
Browse files Browse the repository at this point in the history
  • Loading branch information
ABOTlegacy authored Oct 19, 2021
1 parent 2b37ad1 commit 38429a2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
45 changes: 25 additions & 20 deletions app/Services/VCard/ExportVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ private function exportContactFields(Contact $contact, VCard $vcard)
$vcard->remove('TEL');
$vcard->remove('EMAIL');
$vcard->remove('socialProfile');
$vcard->remove('URL');

foreach ($contact->contactFields as $contactField) {
$type = $this->getContactFieldLabel($contactField);
Expand All @@ -261,26 +262,30 @@ private function exportContactFields(Contact $contact, VCard $vcard)
$vcard->add('EMAIL', $this->escape($contactField->data), $type);
break;
default:
break;
}
switch ($contactField->contactFieldType->name) {
// See https://tools.ietf.org/id/draft-george-vcarddav-vcard-extension-02.html
case 'Facebook':
$vcard->add('socialProfile', $this->escape('https://www.facebook.com/'.$contactField->data), ['type' => 'facebook']);
break;
case 'Twitter':
$vcard->add('socialProfile', $this->escape('https://twitter.com/'.$contactField->data), ['type' => 'twitter']);
break;
case 'Whatsapp':
$vcard->add('socialProfile', $this->escape('https://wa.me/'.$contactField->data), ['type' => 'whatsapp']);
break;
case 'Telegram':
$vcard->add('socialProfile', $this->escape('http://t.me/'.$contactField->data), ['type' => 'telegram']);
break;
case 'LinkedIn':
$vcard->add('socialProfile', $this->escape('http://www.linkedin.com/in/'.$contactField->data), ['type' => 'linkedin']);
break;
default:
switch ($contactField->contactFieldType->name) {
// See https://tools.ietf.org/id/draft-george-vcarddav-vcard-extension-02.html
case 'Facebook':
$vcard->add('socialProfile', $this->escape('https://www.facebook.com/'.$contactField->data), ['type' => 'facebook']);
break;
case 'Twitter':
$vcard->add('socialProfile', $this->escape('https://twitter.com/'.$contactField->data), ['type' => 'twitter']);
break;
case 'Whatsapp':
$vcard->add('socialProfile', $this->escape('https://wa.me/'.$contactField->data), ['type' => 'whatsapp']);
break;
case 'Telegram':
$vcard->add('socialProfile', $this->escape('http://t.me/'.$contactField->data), ['type' => 'telegram']);
break;
case 'LinkedIn':
$vcard->add('socialProfile', $this->escape('http://www.linkedin.com/in/'.$contactField->data), ['type' => 'linkedin']);
break;
default:
// If field isn't a supported social profile, but still has a protocol, then export it as a url.
if (! empty($contactField->contactFieldType->protocol)) {
$vcard->add('URL', $this->escape($contactField->contactFieldType->protocol.$contactField->data));
}
break;
}
break;
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Services/VCard/ExportVCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,42 @@ public function socialProfileProvider()
];
}

/**
* @test
* @dataProvider contactUrlProvider
*/
public function vcard_add_contact_url($name, $protocol, $data, $result)
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$vCard = new VCard();

$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $account->id,
'name' => $name,
'protocol' => $protocol,
'type' => 'URL',
]);
factory(ContactField::class)->create([
'contact_id' => $contact->id,
'account_id' => $account->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => $data,
]);

$exportVCard = app(ExportVCard::class);
$this->invokePrivateMethod($exportVCard, 'exportContactFields', [$contact, $vCard]);
$this->assertStringContainsString($result, $vCard->serialize());
}

public function contactUrlProvider()
{
return [
['Discord', 'https://www.discord.app/user/', 'test123', 'URL;VALUE=URI:https://www.discord.app/user/test123'],
['Facebook Profile', 'https://www.facebook.com/', 'test123', 'URL;VALUE=URI:https://www.facebook.com/test123'],
];
}

/** @test */
public function vcard_add_addresses_empty()
{
Expand Down

0 comments on commit 38429a2

Please sign in to comment.