Merge pull request #13956 from annando/issue-13955

Issue 13955: Check for publish date upon receival
This commit is contained in:
Hypolite Petovan 2024-03-03 12:23:13 -05:00 committed by GitHub
commit 424e219c53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 48 additions and 40 deletions

View File

@ -1105,4 +1105,35 @@ class Item
Tag::store($toUriId, $receiver['type'], $receiver['name'], $receiver['url']); Tag::store($toUriId, $receiver['type'], $receiver['name'], $receiver['url']);
} }
} }
/**
* Check if the item is too old
*
* @param string $created
* @param integer $uid
* @return boolean item is too old
*/
public function isTooOld(string $created, int $uid = 0): bool
{
// check for create date and expire time
$expire_interval = DI::config()->get('system', 'dbclean-expire-days', 0);
if ($uid) {
$user = DBA::selectFirst('user', ['expire'], ['uid' => $uid]);
if (DBA::isResult($user) && ($user['expire'] > 0) && (($user['expire'] < $expire_interval) || ($expire_interval == 0))) {
$expire_interval = $user['expire'];
}
}
if (($expire_interval > 0) && !empty($created)) {
$expire_date = time() - ($expire_interval * 86400);
$created_date = strtotime($created);
if ($created_date < $expire_date) {
Logger::notice('Item created before expiration interval.', ['created' => date('c', $created_date), 'expired' => date('c', $expire_date)]);
return true;
}
}
return false;
}
} }

View File

@ -689,38 +689,6 @@ class Item
return true; return true;
} }
/**
* Check if the item array is too old
*
* @param array $item Item record
* @return boolean item is too old
*/
public static function isTooOld(array $item): bool
{
// check for create date and expire time
$expire_interval = DI::config()->get('system', 'dbclean-expire-days', 0);
$user = DBA::selectFirst('user', ['expire'], ['uid' => $item['uid']]);
if (DBA::isResult($user) && ($user['expire'] > 0) && (($user['expire'] < $expire_interval) || ($expire_interval == 0))) {
$expire_interval = $user['expire'];
}
if (($expire_interval > 0) && !empty($item['created'])) {
$expire_date = time() - ($expire_interval * 86400);
$created_date = strtotime($item['created']);
if ($created_date < $expire_date) {
Logger::notice('Item created before expiration interval.', [
'created' => date('c', $created_date),
'expired' => date('c', $expire_date),
'$item' => $item
]);
return true;
}
}
return false;
}
/** /**
* Return the id of the given item array if it has been stored before * Return the id of the given item array if it has been stored before
* *
@ -1032,7 +1000,7 @@ class Item
if ( if (
!empty($item['direction']) && in_array($item['direction'], [Conversation::PUSH, Conversation::RELAY]) && !empty($item['direction']) && in_array($item['direction'], [Conversation::PUSH, Conversation::RELAY]) &&
empty($item['origin']) && self::isTooOld($item) empty($item['origin']) && DI::contentItem()->isTooOld($item['created'], $item['uid'])
) { ) {
Logger::info('Item is too old', ['item' => $item]); Logger::info('Item is too old', ['item' => $item]);
return 0; return 0;

View File

@ -634,6 +634,16 @@ class Receiver
if (is_array($activity['as:object'])) { if (is_array($activity['as:object'])) {
$attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id'); $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
$published = JsonLD::fetchElement($activity['as:object'], 'as:published', '@value');
$object_type = JsonLD::fetchElement($activity['as:object'], '@type');
$id = JsonLD::fetchElement($activity, '@id');
$object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
if (!empty($published) && !empty($object_id) && in_array($type, ['as:Create', 'as:Update']) && in_array($object_type, self::CONTENT_TYPES)
&& ($push || ($completion != self::COMPLETION_MANUAL)) && DI::contentItem()->isTooOld($published) && !Post::exists(['uri' => $object_id])) {
Logger::debug('Activity is too old. It will not be processed', ['push' => $push, 'completion' => $completion, 'type' => $type, 'object-type' => $object_type, 'published' => $published, 'id' => $id, 'object-id' => $object_id]);
return true;
}
} else { } else {
$attributed_to = ''; $attributed_to = '';
} }
@ -652,7 +662,6 @@ class Receiver
// For announced "create" activities we remove the middle layer. // For announced "create" activities we remove the middle layer.
// For the rest (like, dislike, update, ...) we just process the activity directly. // For the rest (like, dislike, update, ...) we just process the activity directly.
$original_actor = ''; $original_actor = '';
$object_type = JsonLD::fetchElement($activity['as:object'] ?? [], '@type');
if (($type == 'as:Announce') && !empty($object_type) && !in_array($object_type, self::CONTENT_TYPES) && self::isGroup($actor)) { if (($type == 'as:Announce') && !empty($object_type) && !in_array($object_type, self::CONTENT_TYPES) && self::isGroup($actor)) {
$object_object_type = JsonLD::fetchElement($activity['as:object']['as:object'] ?? [], '@type'); $object_object_type = JsonLD::fetchElement($activity['as:object']['as:object'] ?? [], '@type');
if (in_array($object_type, ['as:Create']) && in_array($object_object_type, self::CONTENT_TYPES)) { if (in_array($object_type, ['as:Create']) && in_array($object_object_type, self::CONTENT_TYPES)) {

View File

@ -1602,7 +1602,7 @@ class Diaspora
$datarray['diaspora_signed_text'] = json_encode($data); $datarray['diaspora_signed_text'] = json_encode($data);
} }
if (Item::isTooOld($datarray)) { if (DI::contentItem()->isTooOld($datarray['created'], $datarray['uid'])) {
Logger::info('Comment is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]); Logger::info('Comment is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
return false; return false;
} }
@ -1860,7 +1860,7 @@ class Diaspora
$datarray['diaspora_signed_text'] = json_encode($data); $datarray['diaspora_signed_text'] = json_encode($data);
} }
if (Item::isTooOld($datarray)) { if (DI::contentItem()->isTooOld($datarray['created'], $datarray['uid'])) {
Logger::info('Like is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]); Logger::info('Like is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
return false; return false;
} }
@ -2023,7 +2023,7 @@ class Diaspora
// Diaspora doesn't provide a date for a participation // Diaspora doesn't provide a date for a participation
$datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow(); $datarray['changed'] = $datarray['created'] = $datarray['edited'] = DateTimeFormat::utcNow();
if (Item::isTooOld($datarray)) { if (DI::contentItem()->isTooOld($datarray['created'], $datarray['uid'])) {
Logger::info('Participation is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]); Logger::info('Participation is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
return false; return false;
} }
@ -2393,7 +2393,7 @@ class Diaspora
self::fetchGuid($datarray); self::fetchGuid($datarray);
if (Item::isTooOld($datarray)) { if (DI::contentItem()->isTooOld($datarray['created'], $datarray['uid'])) {
Logger::info('Reshare is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]); Logger::info('Reshare is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
return false; return false;
} }
@ -2738,7 +2738,7 @@ class Diaspora
self::fetchGuid($datarray); self::fetchGuid($datarray);
if (Item::isTooOld($datarray)) { if (DI::contentItem()->isTooOld($datarray['created'], $datarray['uid'])) {
Logger::info('Status is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]); Logger::info('Status is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
return false; return false;
} }

View File

@ -563,7 +563,7 @@ class Feed
} elseif (!Item::isValid($item)) { } elseif (!Item::isValid($item)) {
Logger::info('Feed item is invalid', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]); Logger::info('Feed item is invalid', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
continue; continue;
} elseif (Item::isTooOld($item)) { } elseif (DI::contentItem()->isTooOld($item['created'], $item['uid'])) {
Logger::info('Feed is too old', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]); Logger::info('Feed is too old', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
continue; continue;
} }