diff --git a/sources/AppBundle/Event/Model/Speaker.php b/sources/AppBundle/Event/Model/Speaker.php index 4fa8376ff..3b7e928b2 100644 --- a/sources/AppBundle/Event/Model/Speaker.php +++ b/sources/AppBundle/Event/Model/Speaker.php @@ -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) { @@ -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 diff --git a/sources/AppBundle/VideoNotifier/TweetGenerator.php b/sources/AppBundle/VideoNotifier/TweetGenerator.php index c4de65f52..9ac96b03a 100644 --- a/sources/AppBundle/VideoNotifier/TweetGenerator.php +++ b/sources/AppBundle/VideoNotifier/TweetGenerator.php @@ -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; diff --git a/tests/units/AppBundle/Event/Model/Speaker.php b/tests/units/AppBundle/Event/Model/Speaker.php new file mode 100644 index 000000000..c0eedae11 --- /dev/null +++ b/tests/units/AppBundle/Event/Model/Speaker.php @@ -0,0 +1,52 @@ +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]); + } + } +}