[Gab] feat: add new bridge GabBridge (#2920)

This commit is contained in:
Dag 2022-07-31 03:52:27 +02:00 committed by GitHub
parent 5b5f3b4254
commit 0a060b2ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 1 deletions

57
bridges/GabBridge.php Normal file
View File

@ -0,0 +1,57 @@
<?php
final class GabBridge extends BridgeAbstract
{
const NAME = 'Gab';
const URI = 'https://gab.com/';
const DESCRIPTION = 'Gab is an American alt-tech microblogging and social networking service';
const MAINTAINER = 'dvikan';
const PARAMETERS = [
[
'username' => [
'name' => 'username',
'type' => 'text',
'defaultValue' => 'realdonaldtrump',
],
]
];
public function collectData()
{
$username = $this->getUsername();
$response = json_decode(getContents(sprintf('https://gab.com/api/v1/account_by_username/%s', $username)));
$id = $response->id;
$gabs = json_decode(getContents(sprintf('https://gab.com/api/v1/accounts/%s/statuses?exclude_replies=true', $id)));
foreach ($gabs as $gab) {
if ($gab->reblog) {
continue;
}
$this->items[] = [
'title' => $gab->content ?: 'Untitled',
'author' => $username,
'uri' => $gab->url ?? sprintf('https://gab.com/%s', $username),
'content' => $gab->content,
'timestamp' => (new \DateTime($gab->created_at))->getTimestamp(),
];
}
}
public function getName()
{
return 'Gab - ' . $this->getUsername();
}
public function getURI()
{
return 'https://gab.com/' . $this->getUsername();
}
private function getUsername(): string
{
$username = ltrim($this->getInput('username') ?? '', '@');
if (preg_match('#https?://gab\.com/(\w+)#', $username, $m)) {
return $m[1];
}
return $username;
}
}

View File

@ -207,7 +207,7 @@ class FeedItem
if (!is_string($title)) {
Debug::log('Title must be a string!');
} else {
$this->title = trim($title);
$this->title = truncate(trim($title));
}
return $this;

View File

@ -50,6 +50,15 @@ function raw(string $s): string
return $s;
}
function truncate(string $s, int $length = 150, $marker = '...'): string
{
$s = trim($s);
if (mb_strlen($s) <= $length) {
return $s;
}
return mb_substr($s, 0, $length) . $marker;
}
/**
* Removes unwanted tags from a given HTML text.
*

19
tests/UtilsTest.php Normal file
View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace RssBridge\Tests;
use PHPUnit\Framework\TestCase;
final class UtilsTest extends TestCase
{
public function testTruncate()
{
$this->assertSame('f...', truncate('foo', 1));
$this->assertSame('fo...', truncate('foo', 2));
$this->assertSame('foo', truncate('foo', 3));
$this->assertSame('foo', truncate('foo', 4));
$this->assertSame('fo[...]', truncate('foo', 2, '[...]'));
}
}