Skip to content

Commit

Permalink
fix(reddit): url encoding (RSS-Bridge#4010)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan authored Mar 12, 2024
1 parent 5b80af9 commit 4bad1c1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 49 deletions.
55 changes: 29 additions & 26 deletions bridges/RedditBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,36 +139,13 @@ private function collectDataInternal(): void
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);

Expand Down Expand Up @@ -278,6 +255,32 @@ private function collectDataInternal(): void
});
}

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';
Expand Down
23 changes: 0 additions & 23 deletions tests/BridgeImplementationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,29 +157,6 @@ public function testParameters($path)
}
}

/**
* @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
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/RedditBridgeTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4bad1c1

Please sign in to comment.