Skip to content

Commit

Permalink
Merge pull request afup#1557 from stakovicz/issue-1285-mastodon-link
Browse files Browse the repository at this point in the history
afup#1285 speaker display mastodon link better username match
  • Loading branch information
stakovicz authored Nov 27, 2024
2 parents 4685a92 + 0e8b1a5 commit ead87f3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
12 changes: 6 additions & 6 deletions app/Resources/views/blog/speakers.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
{{ row.speaker.label }}
{% if row.speaker.company %}<span class="societe">{{ row.speaker.company }}</span>{% endif %}
</h3>
{% if row.speaker.twitter %}
{% if row.speaker.getUsernameTwitter %}
<a class="follow-button"
href="https://twitter.com/{{ row.speaker.getCleanedTwitter }}"
href="{{ row.speaker.getUrlTwitter }}"
target="_blank">
<svg height="12" viewBox="0 0 271 300" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="m236 0h46l-101 115 118 156h-92.6l-72.5-94.8-83 94.8h-46l107-123-113-148h94.9l65.5 86.6zm-16.1 244h25.5l-165-218h-27.4z"/>
</svg>
<span>Suivre @{{ row.speaker.getCleanedTwitter }}</span>
<span>Suivre @{{ row.speaker.getUsernameTwitter }}</span>
</a>
{% endif %}
{% if row.speaker.getCleanedMastodon %}
{% if row.speaker.getUsernameMastodon %}
<a class="follow-button"
href="{{ row.speaker.mastodon }}"
href="{{ row.speaker.getUrlMastodon }}"
target="_blank">
<svg width="12" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="m21.327 8.566c0-4.339-2.843-5.61-2.843-5.61-1.433-.658-3.894-.935-6.451-.956h-.063c-2.557.021-5.016.298-6.45.956 0 0-2.843 1.272-2.843 5.61 0 .993-.019 2.181.012 3.441.103 4.243.778 8.425 4.701 9.463 1.809.479 3.362.579 4.612.51 2.268-.126 3.541-.809 3.541-.809l-.075-1.646s-1.621.511-3.441.449c-1.804-.062-3.707-.194-3.999-2.409a4.523 4.523 0 0 1 -.04-.621s1.77.433 4.014.536c1.372.063 2.658-.08 3.965-.236 2.506-.299 4.688-1.843 4.962-3.254.434-2.223.398-5.424.398-5.424zm-3.353 5.59h-2.081v-5.099c0-1.075-.452-1.62-1.357-1.62-1 0-1.501.647-1.501 1.927v2.791h-2.069v-2.791c0-1.28-.501-1.927-1.502-1.927-.905 0-1.357.546-1.357 1.62v5.099h-2.081v-5.253c0-1.074.273-1.927.823-2.558.566-.631 1.307-.955 2.228-.955 1.065 0 1.872.409 2.405 1.228l.518.869.519-.869c.533-.819 1.34-1.228 2.405-1.228.92 0 1.662.324 2.228.955.549.631.822 1.484.822 2.558z"/>
</svg>
<span>Suivre @{{ row.speaker.getCleanedMastodon }}</span>
<span>Suivre @{{ row.speaker.getUsernameMastodon }}</span>
</a>
{% endif %}
<div class="article-body conferencier">
Expand Down
42 changes: 25 additions & 17 deletions sources/AppBundle/Event/Model/Speaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,36 +392,31 @@ public function setBiography($biography)
/**
* @return string
*/
public function getTwitter()
public function getTwitter(): string
{
return $this->twitter;
return (string) $this->twitter;
}

/**
* @return string
*/
public function getMastodon()
public function getMastodon(): string
{
return $this->mastodon;
return (string) $this->mastodon;
}

/**
* @return bool|string
*/
public function getCleanedTwitter()
public function getUsernameTwitter(): string
{
$twitter = $this->getTwitter();
$twitter = trim($twitter, '@');
$twitter = preg_replace('!^https?://twitter.com/!', '', $twitter);
$twitter = preg_replace('!^(https?://(twitter|x).com/)!', '', $twitter);

if (0 === strlen(trim($twitter))) {
return null;
}
return trim($twitter);
}

return $twitter;
public function getUrlTwitter(): string
{
return $this->getUsernameTwitter() ? sprintf('https://x.com/%s', $this->getUsernameTwitter()) : '';
}

public function getCleanedMastodon(): string
public function getUsernameMastodon(): string
{
$mastodon = $this->getMastodon();
if (strpos($mastodon, '@') === false) {
Expand All @@ -432,6 +427,19 @@ public function getCleanedMastodon(): string
return trim($username);
}

public function getUrlMastodon(): string
{
if (!$this->getUsernameMastodon()) {
return '';
}
$mastodon = $this->getMastodon();
if (preg_match('#https?://@(.+)@(.+)#', $mastodon, $matches)) {
return sprintf('https://%s/@%s', $matches[2], $matches[1]);
}

return trim($mastodon);
}

/**
* @param string $twitter
* @return Speaker
Expand Down
2 changes: 1 addition & 1 deletion sources/AppBundle/VideoNotifier/TweetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function generate(Talk $talk, array $speakers)
{
$twitters = [];
foreach ($speakers as $speaker) {
if (!$twitter = $speaker->getCleanedTwitter()) {
if (!$twitter = $speaker->getUsernameTwitter()) {
$twitters[] = $speaker->getFirstname() . " " . $speaker->getLastname();
} else {
$twitters[] = "@" . $twitter;
Expand Down
52 changes: 52 additions & 0 deletions tests/units/AppBundle/Event/Model/Speaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace AppBundle\Event\Model\tests\units;

use AppBundle\Event\Model\Speaker as TestedClass;

class Speaker extends \atoum
{
public function testMastodon()
{
$data = [
['', '', ''],
['https://phpc.social/@username', 'username', 'https://phpc.social/@username'],
['https://mastodon.social/@username', 'username', 'https://mastodon.social/@username'],
['https://@[email protected]', 'username', 'https://mastodon.social/@username'],
['http://@[email protected]', 'username', 'https://mastodon.social/@username'],
];

foreach($data as $expected) {
$this
->given($speaker = new TestedClass())
->when($speaker->setMastodon($expected[0]))
->then
->string($speaker->getUsernameMastodon())
->isEqualTo($expected[1])
->string($speaker->getUrlMastodon())
->isEqualTo($expected[2]);
}
}


public function testTwitter()
{
$data = [
['', '', ''],
['https://twitter.com/username', 'username', 'https://x.com/username'],
['https://x.com/username', 'username', 'https://x.com/username'],
['http://twitter.com/username', 'username', 'https://x.com/username'],
];

foreach($data as $expected) {
$this
->given($speaker = new TestedClass())
->when($speaker->setTwitter($expected[0]))
->then
->string($speaker->getUsernameTwitter())
->isEqualTo($expected[1])
->string($speaker->getUrlTwitter())
->isEqualTo($expected[2]);
}
}
}

0 comments on commit ead87f3

Please sign in to comment.