fix(reddit): url encoding (#4010)

This commit is contained in:
Dag 2024-03-12 23:59:10 +01:00 committed by GitHub
parent 5b80af978f
commit 4bad1c140a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 62 additions and 49 deletions

View file

@ -139,36 +139,13 @@ class RedditBridge extends BridgeAbstract
break;
}
if (!($this->getInput('search') === '')) {
$keywords = $this->getInput('search');
$keywords = str_replace([',', ' '], '%20', $keywords);
$keywords = $keywords . '%20';
} else {
$keywords = '';
}
if (!empty($this->getInput('f')) && $this->queriedContext == 'single') {
$flair = $this->getInput('f');
$flair = str_replace(' ', '%20', $flair);
$flair = 'flair%3A%22' . $flair . '%22%20';
} else {
$flair = '';
}
$search = $this->getInput('search');
$flareInput = $this->getInput('f');
foreach ($subreddits as $subreddit) {
$name = trim($subreddit);
$url = self::URI
. '/search.json?q='
. $keywords
. $flair
. ($user ? 'author%3A' : 'subreddit%3A')
. $name
. '&sort='
. $this->getInput('d')
. '&include_over_18=on';
$version = 'v0.0.1';
$useragent = "rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)";
$url = self::createUrl($search, $flareInput, $subreddit, $user, $section, $this->queriedContext);
$json = getContents($url, ['User-Agent: ' . $useragent]);
$parsedJson = Json::decode($json, false);
@ -278,6 +255,32 @@ class RedditBridge extends BridgeAbstract
});
}
public static function createUrl($search, $flareInput, $subreddit, bool $user, $section, $queriedContext): string
{
if ($search === '') {
$keywords = '';
} else {
$keywords = $search;
$keywords = str_replace([',', ' '], ' ', $keywords);
$keywords = $keywords . ' ';
}
if ($flareInput && $queriedContext == 'single') {
$flair = $flareInput;
$flair = str_replace([',', ' '], ' ', $flair);
$flair = 'flair:"' . $flair . '" ';
} else {
$flair = '';
}
$name = trim($subreddit);
$query = [
'q' => $keywords . $flair . ($user ? 'author:' : 'subreddit:') . $name,
'sort' => $section,
'include_over_18' => 'on',
];
return 'https://old.reddit.com/search.json?' . http_build_query($query);
}
public function getIcon()
{
return 'https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png';

View file

@ -157,29 +157,6 @@ class BridgeImplementationTest extends TestCase
}
}
/**
* @dataProvider dataBridgesProvider
*/
public function testVisibleMethods($path)
{
$bridgeAbstractMethods = get_class_methods(BridgeAbstract::class);
sort($bridgeAbstractMethods);
$feedExpanderMethods = get_class_methods(FeedExpander::class);
sort($feedExpanderMethods);
$this->setBridge($path);
$publicMethods = get_class_methods($this->bridge);
sort($publicMethods);
foreach ($publicMethods as $publicMethod) {
if ($this->bridge instanceof FeedExpander) {
$this->assertContains($publicMethod, $feedExpanderMethods);
} else {
$this->assertContains($publicMethod, $bridgeAbstractMethods);
}
}
}
/**
* @dataProvider dataBridgesProvider
*/

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class RedditBridgeTest extends TestCase
{
public function test()
{
$sut = new RedditBridge(new NullCache(), new NullLogger());
// https://old.reddit.com/search.json?q=cats dogs hen subreddit:php&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+subreddit%3Aphp&sort=hot&include_over_18=on';
$actual = RedditBridge::createUrl('cats,dogs hen', '', 'php', false, 'hot', 'single');
$this->assertSame($expected, $actual);
// https://old.reddit.com/search.json?q=author:RavenousRandy&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=author%3ARavenousRandy&sort=hot&include_over_18=on';
$actual = RedditBridge::createUrl('', '', 'RavenousRandy', true, 'hot', 'user');
$this->assertSame($expected, $actual);
// https://old.reddit.com/search.json?q=cats dogs hen flair:"Proxy" subreddit:php&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+flair%3A%22Proxy%22+subreddit%3Aphp&sort=hot&include_over_18=on';
$actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy', 'php', false, 'hot', 'single');
$this->assertSame($expected, $actual);
// https://old.reddit.com/search.json?q=cats dogs hen flair:"Proxy Linux Server" subreddit:php&sort=hot&include_over_18=on
$expected = 'https://old.reddit.com/search.json?q=cats+dogs+hen+flair%3A%22Proxy+Linux+Server%22+subreddit%3Aphp&sort=hot&include_over_18=on';
$actual = RedditBridge::createUrl('cats,dogs hen', 'Proxy,Linux Server', 'php', false, 'hot', 'single');
$this->assertSame($expected, $actual);
}
}