Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spout: Return DateTimeInterface from getDate #1341

Merged
merged 2 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
* Swedish `sv`
- Wallabag sharer now targets Wallabag 2 by default. This is potentially breaking change but hopefully, no one uses Wallabag 1 any more. ([#1261](https://github.com/fossar/selfoss/pull/1261))
- `defaults.ini` file is no longer used, it is only provided for convenience under a new name `config-example.ini` ([#1261](https://github.com/fossar/selfoss/pull/1261), [#1267](https://github.com/fossar/selfoss/pull/1267))
- `spout::getDate()` method now returns `DateTimeInterface` instead of a string. ([#1341](https://github.com/fossar/selfoss/pull/1341))

### Other changes
- The front-end has been modernized using React framework, this will greatly simplify future development. ([#1216](https://github.com/fossar/selfoss/pull/1216))
Expand Down
8 changes: 2 additions & 6 deletions src/helpers/ContentLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,14 @@ public function fetch($source) {
}

// test date: continue with next if item too old
$itemDate = new \DateTime($item->getDate());
// if date cannot be parsed it will default to epoch. Change to current time.
if ($itemDate->getTimestamp() == 0) {
$itemDate = new \DateTime();
}
$itemDate = $item->getDate();
if ($itemDate < $minDate) {
$this->logger->debug('item "' . $item->getTitle() . '" (' . $item->getDate() . ') older than ' . $this->configuration->itemsLifetime . ' days');
continue;
}

// date in future? Set current date
$now = new \DateTime();
$now = new \DateTimeImmutable();
if ($itemDate > $now) {
$itemDate = $now;
}
Expand Down
10 changes: 7 additions & 3 deletions src/spouts/facebook/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct(WebClient $webClient) {
}

public function load(array $params) {
// https://developers.facebook.com/docs/graph-api/reference/user
$http = $this->webClient->getHttpClient();
$url = new Uri('https://graph.facebook.com/' . urlencode($params['user']));
$url = $url->withQueryValues($url, [
Expand All @@ -75,6 +76,7 @@ public function load(array $params) {
$this->spoutTitle = $data['name'];
$this->pagePicture = $data['picture']['data']['url'];
$this->pageLink = $data['picture']['link'];
// https://developers.facebook.com/docs/graph-api/reference/user/feed/
$this->items = new ArrayIterator($data['feed']['data']);
}

Expand Down Expand Up @@ -150,10 +152,12 @@ public function getLink() {
public function getDate() {
if ($this->valid()) {
$item = $this->items->current();

return $item['created_time'];
// The docs say UNIX timestamp but appears to be ISO 8601.
// https://developers.facebook.com/docs/graph-api/reference/post/
// https://stackoverflow.com/questions/14516792/what-is-the-time-format-used-in-facebook-created-date
return new \DateTimeImmutable($item['created_time']);
}

return false;
return new \DateTimeImmutable();
}
}
9 changes: 4 additions & 5 deletions src/spouts/github/commits.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct(WebClient $webClient) {
public function load(array $params) {
$this->htmlUrl = 'https://github.com/' . urlencode($params['owner']) . '/' . urlencode($params['repo']) . '/' . urlencode($params['branch']);

// https://docs.github.com/en/rest/commits/commits#list-commits
$jsonUrl = 'https://api.github.com/repos/' . urlencode($params['owner']) . '/' . urlencode($params['repo']) . '/commits?sha=' . urlencode($params['branch']);

$http = $this->webClient->getHttpClient();
Expand Down Expand Up @@ -123,13 +124,11 @@ public function getLink() {

public function getDate() {
if ($this->valid()) {
$date = date('Y-m-d H:i:s', strtotime($this->items->current()['commit']['author']['date']));
}
if (strlen($date) === 0) {
$date = date('Y-m-d H:i:s');
// Appears to be ISO 8601.
return new \DateTimeImmutable($this->items->current()['commit']['author']['date']);
}

return $date;
return new \DateTimeImmutable();
}

public function destroy() {
Expand Down
8 changes: 6 additions & 2 deletions src/spouts/reddit/reddit2.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,14 @@ public function getThumbnail() {

public function getdate() {
if ($this->valid()) {
$date = date('Y-m-d H:i:s', $this->items->current()['data']['created_utc']);
// UNIX timestamp
// https://www.reddit.com/r/redditdev/comments/3qsv97/whats_the_time_unit_for_created_utc_and_what_time/
$timestamp = (string) $this->items->current()['data']['created_utc'];

return new \DateTimeImmutable('@' . $timestamp);
}

return $date;
return new \DateTimeImmutable();
}

public function destroy() {
Expand Down
9 changes: 4 additions & 5 deletions src/spouts/rss/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,12 @@ public function getLink() {

public function getDate() {
if ($this->valid()) {
$date = $this->items->current()->get_date('Y-m-d H:i:s');
}
if (strlen($date) === 0) {
$date = date('Y-m-d H:i:s');
$timestamp = (string) $this->items->current()->get_date('U');

return new \DateTimeImmutable('@' . $timestamp);
}

return $date;
return new \DateTimeImmutable();
}

public function getAuthor() {
Expand Down
2 changes: 1 addition & 1 deletion src/spouts/spout.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ abstract public function getLink();
/**
* returns the date of this item
*
* @return string date
* @return \DateTimeInterface date
*/
abstract public function getDate();

Expand Down
9 changes: 4 additions & 5 deletions src/spouts/twitter/usertimeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,12 @@ public function getThumbnail() {

public function getDate() {
if ($this->items !== null) {
$date = date('Y-m-d H:i:s', strtotime($this->items->current()->created_at));
}
if (strlen($date) === 0) {
$date = date('Y-m-d H:i:s');
// Format of `created_at` field not specified, looks US-centric.
// https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet
return new \DateTimeImmutable($this->items->current()->created_at);
}

return $date;
return new \DateTimeImmutable();
}

public function destroy() {
Expand Down