Skip to content

Commit

Permalink
feat: generate unique avatar on contact creation (monicahq/chandler#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored May 2, 2022
1 parent a5dbf2d commit c003ab0
Show file tree
Hide file tree
Showing 15 changed files with 980 additions and 4 deletions.
56 changes: 56 additions & 0 deletions app/Helpers/AvatarHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Helpers;

use App\Models\Avatar;
use App\Models\Contact;
use App\Models\MultiAvatar;
use Faker\Factory as Faker;

class AvatarHelper
{
/**
* Generate a new random avatar.
*
* The Multiavatar library takes a name to generate a unique avatar.
* However, contacts can be created in Monica without a name. When this case
* happens, we'll generate a fake name for the contact, and generate an avatar
* based on that name.
*
* @param Contact $contact
* @return Avatar
*/
public static function generateRandomAvatar(Contact $contact): Avatar
{
$multiavatar = new MultiAvatar();

if (is_null($contact->first_name)) {
$name = Faker::create()->name();
} else {
$name = $contact->first_name.' '.$contact->last_name;
}

$svgCode = $multiavatar($name, null, null);

$avatar = Avatar::create([
'contact_id' => $contact->id,
'type' => Avatar::TYPE_GENERATED,
'svg' => $svgCode,
]);

return $avatar;
}

/**
* Get the avatar of a contact.
*
* @param Contact $contact
* @return string
*/
public static function getSVG(Contact $contact): string
{
$avatar = $contact->avatar;

return $avatar->svg;
}
}
5 changes: 5 additions & 0 deletions app/Helpers/NameHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class NameHelper
/**
* Format the name of the contact according to the user preferences.
*
* Users can format the name however they want, using variables like
* %first_name%, %last_name%, %middle_name%, %nickname%, %maiden_name%, and
* so on). We need to parse this string and replace the variables with the
* appropriate values.
*
* @param User $user
* @param Contact $contact
* @return string
Expand Down
40 changes: 40 additions & 0 deletions app/Models/Avatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Avatar extends Model
{
use HasFactory;

protected $table = 'avatars';

/**
* Possible type.
*/
const TYPE_GENERATED = 'generated';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'contact_id',
'type',
'svg',
];

/**
* Get the contact associated with the contact log.
*
* @return BelongsTo
*/
public function contact()
{
return $this->belongsTo(Contact::class);
}
}
21 changes: 21 additions & 0 deletions app/Models/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Contact extends Model
'last_updated_at',
'company_id',
'job_position',
'avatar_id',
];

/**
Expand Down Expand Up @@ -235,6 +236,26 @@ public function company(): BelongsTo
return $this->belongsTo(Company::class);
}

/**
* Get the current avatar associated with the contact.
*
* @return BelongsTo
*/
public function avatar(): BelongsTo
{
return $this->belongsTo(Avatar::class);
}

/**
* Get the avatars associated with the contact.
*
* @return HasMany
*/
public function avatars()
{
return $this->hasMany(Avatar::class);
}

/**
* Get the name of the contact, according to the user preference.
*
Expand Down
Loading

0 comments on commit c003ab0

Please sign in to comment.