Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
UNRELEASED CHANGES:

* Fix Gravatar support
* Remove partial contacts from search results returned by the API
* Fix reset account deleting default account values
* Fix notifications not working with aysnchronous queue
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Commands/ImportVCards.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public function handle(Filesystem $filesystem)
$contactField->save();
}

$contact->updateGravatar();

$contact->logEvent('contact', $contact->id, 'create');

$this->output->progressAdvance();
Expand Down
49 changes: 43 additions & 6 deletions app/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,25 @@ public function getAvatarSource()
return 'internal';
}

/**
* Update the gravatar, using the firt email found.
*/
public function updateGravatar()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could unit test the new methods in Contact.php, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's why I've added "TODO" on the comment of the PR ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some tests

{
// for performance reasons, we check if a gravatar exists for this email
// address. if it does, we store the gravatar url in the database.
// while this is not ideal because the gravatar can change, at least we
// won't make constant call to gravatar to load the avatar on every
// page load.
$response = $this->getGravatar(250);
if ($response != false and is_string($response)) {
$this->gravatar_url = $response;
} else {
$this->gravatar_url = null;
}
$this->save();
}

/**
* Get the gravatar, if it exits.
*
Expand All @@ -889,17 +908,35 @@ public function getAvatarSource()
*/
public function getGravatar($size)
{
if (empty($this->email)) {
$email = $this->getFirstEmail();

if (is_null($email) || empty($email)) {
return false;
}
$gravatar_url = 'https://www.gravatar.com/avatar/'.md5(strtolower(trim($this->email)));
// check if gravatar exists by appending ?d=404, returns 404 response if does not exist
$gravatarHeaders = get_headers($gravatar_url.'?d=404');
if ($gravatarHeaders[0] == 'HTTP/1.1 404 Not Found') {

if (! app('gravatar')->exists($email)) {
return false;
}

return $gravatar_url.'?s='.$size;
return app('gravatar')->get($email, [
'size' => $size,
'secure' => config('app.env') === 'production',
]);
}

public function getFirstEmail()
{
$contact_email = $this->contactFields()
->whereHas('contactFieldType', function ($query) {
$query->where('type', '=', 'email');
})
->first();

if (is_null($contact_email)) {
return;
}

return $contact_email->data;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/Contacts/ContactFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function storeContactField(ContactFieldsRequest $request, Contact $contac
]
);

$contact->updateGravatar();

return $contactField;
}

Expand All @@ -75,11 +77,15 @@ public function editContactField(ContactFieldsRequest $request, Contact $contact
]
);

$contact->updateGravatar();

return $contactField;
}

public function destroyContactField(Contact $contact, ContactField $contactField)
{
$contactField->delete();

$contact->updateGravatar();
}
}
14 changes: 1 addition & 13 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,19 +306,7 @@ public function update(Request $request, Contact $contact)

dispatch(new ResizeAvatars($contact));

// for performance reasons, we check if a gravatar exists for this email
// address. if it does, we store the gravatar url in the database.
// while this is not ideal because the gravatar can change, at least we
// won't make constant call to gravatar to load the avatar on every
// page load.
$response = $contact->getGravatar(250);
if ($response != false and is_string($response)) {
$contact->gravatar_url = $response;
$contact->save();
} else {
$contact->gravatar_url = null;
$contact->save();
}
$contact->updateGravatar();

return redirect('/people/'.$contact->id)
->with('success', trans('people.information_edit_success'));
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ext-intl": "*",
"bacon/bacon-qr-code": "^1.0",
"barryvdh/laravel-debugbar": "^2.2",
"creativeorange/gravatar": "~1.0",
"doctrine/dbal": "^2.5",
"erusev/parsedown": "~1.6",
"fzaninotto/faker": "^1.6",
Expand Down
46 changes: 45 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
Sentry\SentryLaravel\SentryLaravelServiceProvider::class,
Laravel\Passport\PassportServiceProvider::class,
MartinLindhe\VueInternationalizationGenerator\GeneratorProvider::class,
Creativeorange\Gravatar\GravatarServiceProvider::class,
],

/*
Expand Down Expand Up @@ -215,6 +216,7 @@
'Image' => Intervention\Image\Facades\Image::class,
'MoneyHelper' => App\Helpers\MoneyHelper::class,
'Sentry' => Sentry\SentryLaravel\SentryFacade::class,
'Gravatar' => Creativeorange\Gravatar\Facades\Gravatar::class,
],

];
1 change: 1 addition & 0 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
$factory->define(App\ContactField::class, function (Faker\Generator $faker) {
return [
'account_id' => 1,
'contact_id' => 1,
'contact_field_type_id' => 1,
'data' => '[email protected]',
];
Expand Down
91 changes: 91 additions & 0 deletions tests/Unit/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@ public function test_get_avatar_returns_null_if_not_set()
);
}

public function test_set_emailcontact()
{
$account = factory(\App\Account::class)->create();
$contact = factory(\App\Contact::class)->create(['account_id' => $account->id]);
$contactFieldType = factory(\App\ContactFieldType::class)->create(['account_id' => $account->id]);
$contactField = factory(\App\ContactField::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => '[email protected]',
]);

$email = $contact->getFirstEmail();
$this->assertEquals($email, '[email protected]');
}

public function test_get_avatar_returns_gravatar()
{
$contact = new Contact;
Expand All @@ -376,6 +392,81 @@ public function test_get_avatar_returns_gravatar()
);
}

public function test_gravatar_set_noemail()
{
$account = factory(\App\Account::class)->create();
$contact = factory(\App\Contact::class)->create(['account_id' => $account->id]);
$contactFieldType = factory(\App\ContactFieldType::class)->create(['account_id' => $account->id]);
$contactField = factory(\App\ContactField::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
]);

$contact->updateGravatar();

$this->assertNull($contact->getAvatarURL());
}

public function test_gravatar_set_emailnotexists()
{
$account = factory(\App\Account::class)->create();
$contact = factory(\App\Contact::class)->create(['account_id' => $account->id]);
$contactFieldType = factory(\App\ContactFieldType::class)->create(['account_id' => $account->id]);
$contactField = factory(\App\ContactField::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => '[email protected]',
]);

$contact->updateGravatar();

$this->assertNull($contact->getAvatarURL());
}

public function test_gravatar_set_emailreal()
{
$account = factory(\App\Account::class)->create();
$contact = factory(\App\Contact::class)->create(['account_id' => $account->id]);
$contactFieldType = factory(\App\ContactFieldType::class)->create(['account_id' => $account->id]);
$contactField = factory(\App\ContactField::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => '[email protected]',
]);

$contact->updateGravatar();

$url = $contact->getAvatarURL();
$this->assertNotNull($url);
$this->assertContains('s=250&d=mm&r=g', $url);
$this->assertContains('http://www.gravatar.com', $url);
}

public function test_gravatar_set_emailreal_secure()
{
config(['app.env' => 'production']);

$account = factory(\App\Account::class)->create();
$contact = factory(\App\Contact::class)->create(['account_id' => $account->id]);
$contactFieldType = factory(\App\ContactFieldType::class)->create(['account_id' => $account->id]);
$contactField = factory(\App\ContactField::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => '[email protected]',
]);

$contact->updateGravatar();

$url = $contact->getAvatarURL();
$this->assertNotNull($url);
$this->assertContains('s=250&d=mm&r=g', $url);
$this->assertContains('https://secure.gravatar.com', $url);
}

public function test_get_avatar_returns_external_url()
{
$contact = new Contact();
Expand Down