Skip to content

Commit

Permalink
Add the ability to set an external avatar (#662)
Browse files Browse the repository at this point in the history
This will allow the new Chrome extension for Monica to fetch
contacts from Facebook and retrieve the avatar.
  • Loading branch information
djaiss authored Nov 28, 2017
1 parent 259be09 commit 84a1461
Show file tree
Hide file tree
Showing 34 changed files with 263 additions and 168 deletions.
29 changes: 28 additions & 1 deletion app/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Contact extends Model
'linkedin_profile_url',
'is_dead',
'deceased_date',
'avatar_external_url',
];

/**
Expand All @@ -94,6 +95,7 @@ class Contact extends Model
protected $casts = [
'is_partial' => 'boolean',
'is_dead' => 'boolean',
'has_avatar' => 'boolean',
];

/**
Expand Down Expand Up @@ -726,8 +728,17 @@ public function getCompletedTasks()
* @param int $size
* @return string
*/
public function getAvatarURL($size)
public function getAvatarURL($size = 100)
{
// it either returns null or the gravatar url if it's defined
if (! $this->has_avatar) {
return $this->gravatar_url;
}

if ($this->avatar_location == 'external') {
return $this->avatar_external_url;
}

$original_avatar_url = Storage::disk($this->avatar_location)->url($this->avatar_file_name);
$avatar_filename = pathinfo($original_avatar_url, PATHINFO_FILENAME);
$avatar_extension = pathinfo($original_avatar_url, PATHINFO_EXTENSION);
Expand All @@ -736,6 +747,22 @@ public function getAvatarURL($size)
return Storage::disk($this->avatar_location)->url($resized_avatar);
}

/**
* Returns the source of the avatar, or null if avatar is undefined.
*/
public function getAvatarSource()
{
if (! $this->has_avatar) {
return;
}

if ($this->avatar_location == 'external') {
return 'external';
}

return 'internal';
}

/**
* Get the gravatar, if it exits.
*
Expand Down
61 changes: 27 additions & 34 deletions app/Http/Controllers/Api/ApiContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,24 @@ public function store(Request $request)
Rule::in(['exact', 'approximate', 'unknown']),
],
'age' => 'nullable|integer',
'email' => 'nullable|email|max:255',
'phone_number' => 'nullable|max:255',
'job' => 'nullable|max:255',
'company' => 'nullable|max:255',
'food_preferencies' => 'nullable|max:100000',
'facebook_profile_url' => 'nullable|max:255',
'twitter_profile_url' => 'nullable|max:255',
'linkedin_profile_url' => 'nullable|max:255',
'first_met_information' => 'nullable|max:1000000',
'first_met_date' => 'nullable|date',
'first_met_through_contact_id' => 'nullable|integer',
'is_partial' => 'required|integer',
'is_dead' => 'required|integer',
'deceased_date' => 'nullable|date',
'avatar_url' => 'nullable|max:400',
]);

if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}

// Make sure the email is unique
if ($request->input('email') != '') {
$otherContact = Contact::where('email', $request->input('email'))
->count();

if ($otherContact > 0) {
return $this->setErrorCode(35)
->setHTTPStatusCode(500)
->respondWithError(trans('people.people_edit_email_error'));
}
}

// Make sure the `first_met_through_contact_id` is a contact id that the
// user is authorized to access
if ($request->get('first_met_through_contact_id')) {
Expand All @@ -113,11 +98,36 @@ public function store(Request $request)

// Create the contact
try {
$contact = Contact::create($request->all());
$contact = Contact::create(
$request->only([
'first_name',
'last_name',
'gender',
'birthdate',
'is_birthdate_approximate',
'age',
'job',
'company',
'food_preferencies',
'linkedin_profile_url',
'first_met_information',
'first_met_date',
'first_met_through_contact_id',
'is_partial',
'is_dead',
'deceased_date',
]) + [
'avatar_external_url' => $request->get('avatar_url'),
]);
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
}

if ($request->get('avatar_url')) {
$contact->has_avatar = true;
$contact->avatar_location = 'external';
}

// Saving first met information (these are not the same field names than
// the ones provided in the JSON)
if ($request->get('first_met_date')) {
Expand Down Expand Up @@ -170,13 +180,9 @@ public function update(Request $request, $contactId)
Rule::in(['exact', 'approximate', 'unknown']),
],
'age' => 'nullable|integer',
'email' => 'nullable|email|max:255',
'phone_number' => 'nullable|max:255',
'job' => 'nullable|max:255',
'company' => 'nullable|max:255',
'food_preferencies' => 'nullable|max:100000',
'facebook_profile_url' => 'nullable|max:255',
'twitter_profile_url' => 'nullable|max:255',
'linkedin_profile_url' => 'nullable|max:255',
'first_met_information' => 'nullable|max:1000000',
'first_met_date' => 'nullable|date',
Expand All @@ -191,19 +197,6 @@ public function update(Request $request, $contactId)
->respondWithError($validator->errors()->all());
}

// Make sure the email is unique
if ($request->input('email') != '') {
$otherContact = Contact::where('email', $request->input('email'))
->where('id', '!=', $contactId)
->count();

if ($otherContact > 0) {
return $this->setErrorCode(35)
->setHTTPStatusCode(500)
->respondWithError(trans('people.people_edit_email_error'));
}
}

// Make sure the `first_met_through_contact_id` is a contact id that the
// user is authorized to access
if ($request->get('first_met_through_contact_id')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use Auth;
use App\Address;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Call;
use App\Contact;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use Auth;
use App\Contact;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Debt;
use App\Contact;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Gift;
use App\Contact;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Contact;
use App\Http\Controllers\Controller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Contact;
use App\Http\Controllers\Controller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Note;
use App\Contact;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Contact;
use App\Relationship;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Contact;
use App\Reminder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Contact;
use App\Http\Controllers\Controller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\People;
namespace App\Http\Controllers\Contacts;

use App\Task;
use App\Contact;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PeopleController extends Controller
class ContactsController extends Controller
{
/**
* Display a listing of the resource.
Expand Down Expand Up @@ -165,23 +165,12 @@ public function update(Request $request, Contact $contact)
->withErrors($validator);
}

// Make sure the email address is unique in this account
if ($request->input('email') != '') {
$otherContact = Contact::where('email', $request->input('email'))
->where('id', '!=', $contact->id)
->count();

if ($otherContact > 0) {
return redirect()->back()->withErrors(trans('people.people_edit_email_error'))->withInput();
}
}

$contact->gender = $request->input('gender');
$contact->first_name = $request->input('firstname');
$contact->last_name = $request->input('lastname');

if ($request->file('avatar') != '') {
$contact->has_avatar = 'true';
$contact->has_avatar = true;
$contact->avatar_location = config('filesystems.default');
$contact->avatar_file_name = $request->avatar->store('avatars', config('filesystems.default'));
}
Expand Down
30 changes: 6 additions & 24 deletions app/Http/Resources/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,17 @@ public function toArray($request)
'job' => $this->job,
'company' => $this->company,
]),
'avatar' => [
'gravatar_url' => $this->getGravatar(110),
],
'avatar' => $this->when(! $this->is_partial, [
'url' => $this->getAvatarUrl(),
'source' => $this->getAvatarSource(),
]),
'food_preferencies' => $this->when(! $this->is_partial, $this->food_preferencies),
'how_you_met' => [
'how_you_met' => $this->when(! $this->is_partial, [
'general_information' => $this->first_met_additional_info,
'first_met_date' => (is_null($this->first_met) ? null : $this->first_met->format(config('api.timestamp_format'))),
'first_met_through_contact' => new ContactShortResource($this->getIntroducer()),
],
]),
],
'contact' => $this->when(! $this->is_partial, [
'emails' => [
[
'name' => 'personal',
'email' => $this->email,
],
],
'phone_numbers' => [
[
'name' => 'home',
'phone_number' => $this->phone_number,
],
],
'social_network' => [
'facebook_profile_url' => $this->facebook_profile_url,
'twitter_profile_url' => $this->twitter_profile_url,
'linkedin_profile_url' => $this->linkedin_profile_url,
],
]),
'addresses' => $this->when(! $this->is_partial, $this->getAddressesForAPI()),
'tags' => $this->when(! $this->is_partial, $this->getTagsForAPI()),
'statistics' => $this->when(! $this->is_partial, [
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ResizeAvatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(Contact $contact)
*/
public function handle()
{
if ($this->contact->has_avatar == 'true') {
if ($this->contact->has_avatar == true) {
try {
$avatar_file = Storage::disk($this->contact->avatar_location)->get($this->contact->avatar_file_name);
$avatar_path = Storage::disk($this->contact->avatar_location)->url($this->contact->avatar_file_name);
Expand Down
1 change: 1 addition & 0 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'first_name' => 'John',
'last_name' => 'Doe',
'birthdate' => \Carbon\Carbon::createFromTimeStamp($faker->dateTimeThisCentury()->getTimeStamp()),
'has_avatar' => false,
];
});

Expand Down
Loading

0 comments on commit 84a1461

Please sign in to comment.