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

feat: vcard services for import/export #1996

Merged
merged 45 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
25abfda
import service
asbiin Nov 3, 2018
3804276
Apply fixes from StyleCI
asbiin Nov 3, 2018
6de8b83
wip
asbiin Nov 4, 2018
643f21b
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 4, 2018
6a031f8
Apply fixes from StyleCI
asbiin Nov 4, 2018
c63c9d4
fix psalm
asbiin Nov 4, 2018
0ae4e53
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 4, 2018
e9abca3
Apply fixes from StyleCI
asbiin Nov 4, 2018
99a405e
carddav is a paid feature
asbiin Nov 4, 2018
6f09db4
use middleware
asbiin Nov 4, 2018
57a5ab5
update
asbiin Nov 4, 2018
58958d1
Apply fixes from StyleCI
asbiin Nov 4, 2018
94d65ad
update
asbiin Nov 6, 2018
23d3f24
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 6, 2018
6f1dd9a
Apply fixes from StyleCI
asbiin Nov 6, 2018
ea963fe
fix tests
asbiin Nov 6, 2018
73c7d46
Apply fixes from StyleCI
asbiin Nov 6, 2018
ea58fdc
activate carddav
asbiin Nov 7, 2018
cb51578
revert bad commit
asbiin Nov 7, 2018
a20c412
fix page
asbiin Nov 7, 2018
afce9c6
update
asbiin Nov 7, 2018
c1c4e16
fix
asbiin Nov 7, 2018
939b657
add tests
asbiin Nov 7, 2018
76f7b0b
Apply fixes from StyleCI
asbiin Nov 7, 2018
002261c
Merge remote-tracking branch 'origin/master' into feat/vcard-import-e…
asbiin Nov 7, 2018
733924a
remove jeroendesloovere/vcard
asbiin Nov 7, 2018
7dacde2
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 7, 2018
d7f58b2
add some test
asbiin Nov 7, 2018
fb2fa8f
add tests
asbiin Nov 7, 2018
90529a8
Apply fixes from StyleCI
asbiin Nov 7, 2018
f078de9
update
asbiin Nov 7, 2018
c0212b2
rename addressbook url
asbiin Nov 7, 2018
a8eca02
Apply fixes from StyleCI
asbiin Nov 7, 2018
58ce6f5
updates
asbiin Nov 9, 2018
a1b590d
some improvments
asbiin Nov 10, 2018
89b0dfc
Apply fixes from StyleCI
asbiin Nov 10, 2018
bfcd83c
chore(assets): Update assets
MonicaBot Nov 10, 2018
7d2e7a0
improve
asbiin Nov 10, 2018
8d259d2
Apply fixes from StyleCI
asbiin Nov 10, 2018
d93381a
update functions names
asbiin Nov 10, 2018
f81972f
Apply fixes from StyleCI
asbiin Nov 10, 2018
6e48878
Merge remote-tracking branch 'origin/master' into feat/vcard-import-e…
asbiin Nov 10, 2018
2b47ff1
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 10, 2018
d79a6b9
fix tests
asbiin Nov 10, 2018
31df324
Merge remote-tracking branch 'origin/master' into feat/vcard-import-e…
asbiin Nov 11, 2018
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
Prev Previous commit
Next Next commit
update
  • Loading branch information
asbiin committed Nov 7, 2018
commit afce9c64cc4c9226a68ba5e00fa1c2817f4b5c7c
18 changes: 16 additions & 2 deletions app/Services/VCard/ExportVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function execute(array $data) : VCard
// Basic information
$vcard = new VCard([
'UID' => $contact->hashid(),
'SOURCE' => route('people.show', $contact),
]);

$this->exportNames($contact, $vcard);
Expand Down Expand Up @@ -173,13 +174,26 @@ private function exportContactFields(Contact $contact, VCard $vcard)
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($contactField->data), ['type' => '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;
// ... Twitter, Whatsapp, Telegram, other
default:
break;
}
}

if (! is_null($contact->linkedin_profile_url)) {
$vcard->add('socialProfile', $this->escape('http://www.linkedin.com/in/'.$contactField->data), ['type' => 'linkedin']);
}
}
}
57 changes: 55 additions & 2 deletions app/Services/VCard/ImportVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ private function existingContactWithName(VCard $entry)
* @param null|string $value
* @return null|string
*/
private function formatValue($value): string
private function formatValue($value)
{
return ! empty((string) $value) ? str_replace('\;', ';', trim((string) $value)) : (string) null;
return ! empty($value) ? str_replace('\;', ';', trim((string) $value)) : null;
}

/**
Expand Down Expand Up @@ -352,6 +352,7 @@ private function createContactFromCurrentEntry($contact, VCard $entry): Contact
$this->importAddress($contact, $entry);
$this->importEmail($contact, $entry);
$this->importTel($contact, $entry);
$this->importSocialProfile($contact, $entry);

$contact->save();

Expand Down Expand Up @@ -596,6 +597,58 @@ private function importTel(Contact $contact, VCard $entry): void
}
}

/**
* @param Contact $contact
* @param VCard $entry
* @return void
*/
private function importSocialProfile(Contact $contact, VCard $entry): void
{
if (is_null($entry->socialProfile)) {
return;
}

foreach ($entry->socialProfile as $socialProfile) {
$type = $socialProfile['type'];
$contactFieldTypeId = null;
switch ((string) $type) {
case 'facebook':
$contactFieldTypeId = $this->contactFieldTypeId('Facebook');
$data = str_replace('https://www.facebook.com/', '', $this->formatValue((string) $socialProfile));
break;
case 'twitter':
$contactFieldTypeId = $this->contactFieldTypeId('Twitter');
$data = str_replace('https://twitter.com/', '', $this->formatValue((string) $socialProfile));
break;
case 'whatsapp':
$contactFieldTypeId = $this->contactFieldTypeId('Whatsapp');
$data = str_replace('https://wa.me/', '', $this->formatValue((string) $socialProfile));
break;
case 'telegram':
$contactFieldTypeId = $this->contactFieldTypeId('Telegram');
$data = str_replace('http://t.me/', '', $this->formatValue((string) $socialProfile));
break;
case 'linkedin':
$data = str_replace('http://www.linkedin.com/in/', '', $this->formatValue((string) $socialProfile));
$contact->linkedin_profile_url = $data;
continue;
break;
default:
// Not supported
break;
}

if (! is_null($contactFieldTypeId)) {
ContactField::firstOrCreate([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'data' => $data,
'contact_field_type_id' => $contactFieldTypeId,
]);
}
}
}

/**
* Get the contact field type id for the $type.
*
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Services/VCard/ImportVCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Sabre\VObject\Component\VCard;
use App\Services\VCard\ImportVCard;
use App\Models\Contact\ContactField;
use Sabre\VObject\PHPUnitAssertions;
use App\Models\Contact\ContactFieldType;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ImportVCardTest extends TestCase
{
use DatabaseTransactions;
use DatabaseTransactions,
PHPUnitAssertions;

public function test_it_can_not_import_because_no_firstname_or_nickname_in_vcard()
{
Expand Down Expand Up @@ -198,7 +200,7 @@ public function test_it_formats_value()
$importVCard = new ImportVCard($account->id);

$result = $this->invokePrivateMethod($importVCard, 'formatValue', ['']);
//$this->assertNull($result);
$this->assertNull($result);

$result = $this->invokePrivateMethod($importVCard, 'formatValue', ['This is a value']);
$this->assertEquals(
Expand Down