Add avatar url customization

This commit is contained in:
Michael Crawford
2015-12-22 17:20:32 -05:00
parent 3dd25f3b87
commit 73c362fa52
7 changed files with 25 additions and 8 deletions

View File

@@ -32,3 +32,8 @@ title = ""
[date] [date]
; timezone = UTC ; timezone = UTC
; format = 'd/m/Y H:i:s' ; format = 'd/m/Y H:i:s'
; custom avatar service
[avatar]
; url = '//gravatar.com/avatar/'
; query[] = 'd=identicon'

View File

@@ -37,6 +37,8 @@ class Application extends SilexApplication
$this['filetypes'] = $config->getSection('filetypes'); $this['filetypes'] = $config->getSection('filetypes');
$this['binary_filetypes'] = $config->getSection('binary_filetypes'); $this['binary_filetypes'] = $config->getSection('binary_filetypes');
$this['cache.archives'] = $this->getCachePath() . 'archives'; $this['cache.archives'] = $this->getCachePath() . 'archives';
$this['avatar.url'] = $config->get('avatar', 'url');
$this['avatar.query'] = $config->get('avatar', 'query');
// Register services // Register services
$this->register(new TwigServiceProvider(), array( $this->register(new TwigServiceProvider(), array(
@@ -67,6 +69,7 @@ class Application extends SilexApplication
$twig->addFilter(new \Twig_SimpleFilter('md5', 'md5')); $twig->addFilter(new \Twig_SimpleFilter('md5', 'md5'));
$twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate'))); $twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate')));
$twig->addFilter(new \Twig_SimpleFilter('format_size', array($app, 'formatSize'))); $twig->addFilter(new \Twig_SimpleFilter('format_size', array($app, 'formatSize')));
$twig->addFunction(new \Twig_SimpleFunction('avatar', array($app, 'getAvatar')));
return $twig; return $twig;
})); }));
@@ -107,6 +110,18 @@ class Application extends SilexApplication
return round($size, 2) . $units[$i]; return round($size, 2) . $units[$i];
} }
public function getAvatar($email, $size)
{
$url = $this['avatar.url'] ? $this['avatar.url'] : "//gravatar.com/avatar/";
$query = array("s=$size");
if (is_string($this['avatar.query']))
$query[] = $this['avatar.query'];
else if (is_array($this['avatar.query']))
$query = array_merge($query, $this['avatar.query']);
$id = md5(strtolower($email));
return $url . $id . "?" . implode('&', $query);
}
public function getPath() public function getPath()
{ {
return $this->path . DIRECTORY_SEPARATOR; return $this->path . DIRECTORY_SEPARATOR;

View File

@@ -46,10 +46,7 @@ class NetworkController implements ControllerProviderInterface
'author' => array( 'author' => array(
'name' => $commit->getAuthor()->getName(), 'name' => $commit->getAuthor()->getName(),
'email' => $commit->getAuthor()->getEmail(), 'email' => $commit->getAuthor()->getEmail(),
// due to the lack of a inbuilt javascript md5 mechanism, build the full avatar url on the php side 'image' => $app->getAvatar($commit->getAuthor()->getEmail(), 40)
'image' => '//gravatar.com/avatar/' . md5(
strtolower($commit->getAuthor()->getEmail())
) . '?s=40'
) )
); );
} }

View File

@@ -17,7 +17,7 @@
{% if commit.body is not empty %} {% if commit.body is not empty %}
<p>{{ commit.body | nl2br }}</p> <p>{{ commit.body | nl2br }}</p>
{% endif %} {% endif %}
<img src="https://gravatar.com/avatar/{{ commit.author.email | lower | md5 }}?s=32" class="pull-left space-right" /> <img src="{{ avatar(commit.author.email, 32) }}" class="pull-left space-right" />
<span> <span>
<a href="mailto:{{ commit.author.email }}">{{ commit.author.name }}</a> authored on {{ commit.date | format_date }} <a href="mailto:{{ commit.author.email }}">{{ commit.author.name }}</a> authored on {{ commit.date | format_date }}
{% if commit.author.email != commit.commiter.email %} {% if commit.author.email != commit.commiter.email %}

View File

@@ -9,7 +9,7 @@
<tbody> <tbody>
{% for item in commit %} {% for item in commit %}
<tr> <tr>
<td width="5%"><img src="https://gravatar.com/avatar/{{ item.author.email | lower | md5 }}?s=40" /></td> <td width="5%"><img src="{{ avatar(item.author.email, 40) }}" /></td>
<td width="95%"> <td width="95%">
<span class="pull-right"><a class="btn btn-default btn-sm" href="{{ path('commit', {repo: repo, commit: item.hash}) }}"><span class="fa fa-list-alt"></span> View {{ item.shortHash }}</a></span> <span class="pull-right"><a class="btn btn-default btn-sm" href="{{ path('commit', {repo: repo, commit: item.hash}) }}"><span class="fa fa-list-alt"></span> View {{ item.shortHash }}</a></span>
<h4>{{ item.message }}</h4> <h4>{{ item.message }}</h4>

View File

@@ -16,7 +16,7 @@
{% if commit.body is not empty %} {% if commit.body is not empty %}
<p>{{ commit.body | nl2br }}</p> <p>{{ commit.body | nl2br }}</p>
{% endif %} {% endif %}
<img src="https://gravatar.com/avatar/{{ commit.author.email | lower | md5 }}?s=32" class="pull-left space-right" /> <img src="{{ avatar(commit.author.email, 32) }}" class="pull-left space-right" />
<span> <span>
<a href="mailto:{{ commit.author.email }}">{{ commit.author.name }}</a> authored on {{ commit.date | format_date }} <a href="mailto:{{ commit.author.email }}">{{ commit.author.name }}</a> authored on {{ commit.date | format_date }}
{% if commit.author.email != commit.commiter.email %} {% if commit.author.email != commit.commiter.email %}

View File

@@ -9,7 +9,7 @@
<tbody> <tbody>
{% for item in commit %} {% for item in commit %}
<tr> <tr>
<td width="5%"><img src="https://gravatar.com/avatar/{{ item.author.email | lower | md5 }}?s=40" /></td> <td width="5%"><img src="{{ avatar( item.author.email, 40 ) }}" /></td>
<td width="95%"> <td width="95%">
<span class="pull-right"><a class="btn btn-small" href="{{ path('commit', {repo: repo, commit: item.hash}) }}"><i class="icon-list-alt"></i> View {{ item.shortHash }}</a></span> <span class="pull-right"><a class="btn btn-small" href="{{ path('commit', {repo: repo, commit: item.hash}) }}"><i class="icon-list-alt"></i> View {{ item.shortHash }}</a></span>
<h4>{{ item.message }}</h4> <h4>{{ item.message }}</h4>