refactor: FeedItem::setTimestamp() (#3711)

This commit is contained in:
Dag 2023-09-26 00:27:45 +02:00 committed by GitHub
parent f421c45b21
commit ae53adefad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 15 deletions

View file

@ -54,7 +54,8 @@ class CNETFranceBridge extends FeedExpander
}
foreach ($this->bannedURL as $term) {
if (preg_match('/' . $term . '/mi', $item['uri']) === 1) {
$preg_match = preg_match('#' . $term . '#mi', $item['uri']);
if ($preg_match === 1) {
return null;
}
}

View file

@ -161,8 +161,8 @@ class MastodonBridge extends BridgeAbstract
$object = $this->fetchAP($object);
}
$item['content'] = $object['content'];
$strippedContent = strip_tags(str_replace('<br>', ' ', $object['content']));
$item['content'] = $object['content'] ?? '';
$strippedContent = strip_tags(str_replace('<br>', ' ', $item['content']));
if (isset($object['name'])) {
$item['title'] = $object['name'];
@ -186,9 +186,10 @@ class MastodonBridge extends BridgeAbstract
foreach ($object['attachment'] as $attachment) {
// Only process REMOTE pictures (prevent xss)
$mediaType = $attachment['mediaType'] ?? null;
if (
$attachment['mediaType']
&& preg_match('/^image\//', $attachment['mediaType'], $match)
$mediaType
&& preg_match('/^image\//', $mediaType, $match)
&& preg_match('/^http(s|):\/\//', $attachment['url'], $match)
) {
$item['content'] = $item['content'] . '<br /><img ';

View file

@ -1,10 +1,12 @@
This area is intended for developers who decide to contribute to **RSS-Bridge** which is primarily written in [`PHP`](http://www.php.net/) with some aspects of [`HTML`](https://en.wikipedia.org/wiki/HTML) and [`CSS`](https://en.wikipedia.org/wiki/Cascading_Style_Sheets).
This area is intended for developers who decide to contribute to **RSS-Bridge**.
It is written in PHP.
If you are new to **RSS-Bridge** you should make yourself familiar with some general aspects:
- [Coding style policy](./01_Coding_style_policy.md)
- [Folder structure](./03_Folder_structure.md)
- [Debug mode](./05_Debug_mode.md)
- [Bridge API](../05_Bridge_API/index.md)
- [Cache API](../07_Cache_API/index.md)
- [Format API](../08_Format_API/index.md)
- [Technical recommendations](../09_Technical_recommendations/index.md)
- [Technical recommendations](../09_Technical_recommendations/index.md)

View file

@ -147,14 +147,16 @@ class FeedItem
return $this->timestamp;
}
public function setTimestamp($timestamp)
public function setTimestamp($datetime)
{
$this->timestamp = null;
if (
!is_numeric($timestamp)
&& !$timestamp = strtotime($timestamp)
) {
Debug::log('Unable to parse timestamp!');
if (is_numeric($datetime)) {
$timestamp = $datetime;
} else {
$timestamp = strtotime($datetime);
if ($timestamp === false) {
Debug::log('Unable to parse timestamp!');
}
}
if ($timestamp <= 0) {
Debug::log('Timestamp must be greater than zero!');

View file

@ -24,7 +24,24 @@ class FeedItemTest extends TestCase
$item = new FeedItem();
$item->title = 'aa';
$this->assertSame('aa', $item->getTitle());
$this->assertSame('aa', $item->title);
$this->assertSame('aa', $item->getTitle());
}
public function testTimestamp()
{
$item = new FeedItem();
$item->setTimestamp(5);
$this->assertSame(5, $item->getTimestamp());
$item->setTimestamp('5');
$this->assertSame(5, $item->getTimestamp());
$item->setTimestamp('1970-01-01 18:00:00');
$this->assertSame(64800, $item->getTimestamp());
$item->setTimestamp('1st jan last year');
// This will fail at 2024-01-01 hehe
$this->assertSame(1640995200, $item->getTimestamp());
}
}