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

[3.x] Allow avatar path customization #890

Merged
merged 2 commits into from
May 11, 2020
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
13 changes: 13 additions & 0 deletions config/telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@
Authorize::class,
],

/*
|--------------------------------------------------------------------------
| Telescope Avatar Service
|--------------------------------------------------------------------------
|
| This option may be used to control how to show an avatar for authorized
| users. 'gravatar' will lookup an avatar by the user's email address.
| 'custom' requires registering a callback via Telescope::avatar().
|
*/

'avatar_driver' => 'gravatar',

/*
|--------------------------------------------------------------------------
| Ignored Paths & Commands
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"jquery": "^3.5",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.13",
"md5": "^2.2.1",
"moment": "^2.10.6",
"moment-timezone": "^0.5.21",
"popper.js": "^1.12",
Expand Down
9 changes: 1 addition & 8 deletions resources/js/components/PreviewScreen.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@
command() {
return _.find(this.batch, {type: 'command'})
},

gravatarUrl() {
if (this.entry.content.user.email) {
const md5 = require('md5')
return 'https://www.gravatar.com/avatar/' + md5(this.entry.content.user.email.toLowerCase()) + '?s=200'
}
}
},


Expand Down Expand Up @@ -242,7 +235,7 @@
<td class="table-fit font-weight-bold align-middle">Name</td>

<td class="align-middle">
<img :src="gravatarUrl" class="mr-2 rounded-circle" height="40" width="40" v-if="gravatarUrl">
<img :src="entry.content.user.avatar" :alt="entry.content.user.name" class="mr-2 rounded-circle" height="40" width="40" v-if="entry.content.user.avatar">
{{entry.content.user.name}}
</td>
</tr>
Expand Down
63 changes: 63 additions & 0 deletions src/Avatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Laravel\Telescope;

use Closure;
use Illuminate\Support\Str;

class Avatar
{
/**
* The callback that should be used to get the Telescope user avatar.
*
* @var \Closure
*/
protected static $callback;

/**
* Get an avatar URL for an entry user.
*
* @param array $user User payload from the EntryResult.
* @return string|null
*/
public static function url(array $user)
{
if (empty($user['email'])) {
return;
}

switch (config('telescope.avatar_driver', 'gravatar')) {
case 'custom':
return static::resolve($user);
case 'gravatar':
$hash = md5(Str::lower($user['email']));

return "https://www.gravatar.com/avatar/{$hash}?s=200";
default:
break;
}
}

/**
* Register the Telescope user avatar callback.
*
* @param \Closure $callback
*/
public static function register(Closure $callback)
{
static::$callback = $callback;
}

/**
* Find the custom avatar for a user.
*
* @param array $user
* @return string|null
*/
protected static function resolve($user)
{
if (static::$callback !== null) {
return call_user_func(static::$callback, $user['id'], $user['email']);
}
}
}
31 changes: 29 additions & 2 deletions src/EntryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class EntryResult implements JsonSerializable
*/
private $tags;

/**
* The generated URL to the entry user's avatar.
*
* @var string
*/
protected $avatar;

/**
* Create a new entry result instance.
*
Expand All @@ -86,14 +93,26 @@ public function __construct($id, $sequence, string $batchId, string $type, ?stri
$this->familyHash = $familyHash;
}

/**
* Set the URL to the entry user's avatar.
*
* @return $this
*/
public function generateAvatar()
{
$this->avatar = Avatar::url($this->content['user'] ?? []);

return $this;
}

/**
* Get the array representation of the entry.
*
* @return array
*/
public function jsonSerialize()
{
return [
return collect([
'id' => $this->id,
'sequence' => $this->sequence,
'batch_id' => $this->batchId,
Expand All @@ -102,6 +121,14 @@ public function jsonSerialize()
'tags' => $this->tags,
'family_hash' => $this->familyHash,
'created_at' => $this->createdAt->toDateTimeString(),
];
])->when($this->avatar, function ($items) {
return $items->mergeRecursive([
'content' => [
'user' => [
'avatar' => $this->avatar,
],
],
]);
})->all();
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function index(Request $request, EntriesRepository $storage)
*/
public function show(EntriesRepository $storage, $id)
{
$entry = $storage->find($id);
$entry = $storage->find($id)->generateAvatar();

return response()->json([
'entry' => $entry,
Expand Down
12 changes: 12 additions & 0 deletions src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,18 @@ public static function night()
return new static;
}

/**
* Register the Telescope user avatar callback.
*
* @param \Closure $callback
*/
public static function avatar(Closure $callback)
{
if (config('telescope.avatar_driver') === 'custom') {
Avatar::register($callback);
}
}

/**
* Get the default JavaScript variables for Telescope.
*
Expand Down
4 changes: 4 additions & 0 deletions stubs/TelescopeServiceProvider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
$entry->isScheduledTask() ||
$entry->hasMonitoredTag();
});

Telescope::avatar(function ($id, $email) {
//
});
}

/**
Expand Down
Loading