Extract isDuplicate() into ItemHelper

This commit is contained in:
Art4 2025-01-23 13:17:04 +00:00
parent 7a2e1e0511
commit 118e59ac5c
2 changed files with 49 additions and 45 deletions

View file

@ -580,51 +580,7 @@ class Item
}
}
/**
* Check if the item array is a duplicate
*
* @param array $item Item record
* @return boolean is it a duplicate?
*/
private static function isDuplicate(array $item): bool
{
// Checking if there is already an item with the same guid
$condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
if (Post::exists($condition)) {
DI::logger()->notice('Found already existing item', $condition);
return true;
}
$condition = [
'uri-id' => $item['uri-id'], 'uid' => $item['uid'],
'network' => [$item['network'], Protocol::DFRN]
];
if (Post::exists($condition)) {
DI::logger()->notice('duplicated item with the same uri found.', $condition);
return true;
}
// On Friendica and Diaspora the GUID is unique
if (in_array($item['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
$condition = ['guid' => $item['guid'], 'uid' => $item['uid']];
if (Post::exists($condition)) {
DI::logger()->notice('duplicated item with the same guid found.', $condition);
return true;
}
}
/*
* Check for already added items.
* There is a timing issue here that sometimes creates double postings.
* An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this.
*/
if (($item['uid'] == 0) && Post::exists(['uri-id' => $item['uri-id'], 'uid' => 0])) {
DI::logger()->notice('Global item already stored.', ['uri-id' => $item['uri-id'], 'network' => $item['network']]);
return true;
}
return false;
}
/**
* Check if the item array is valid
@ -809,7 +765,7 @@ class Item
// Additional duplicate checks
/// @todo Check why the first duplication check returns the item number and the second a 0
if (self::isDuplicate($item)) {
if ($itemHelper->isDuplicate($item)) {
return 0;
}

View file

@ -77,6 +77,54 @@ final class ItemHelper
return $item;
}
/**
* Check if the item array is a duplicate
*
* @private
*
* @param array $item Item record
* @return boolean is it a duplicate?
*/
public function isDuplicate(array $item): bool
{
// Checking if there is already an item with the same guid
$condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
if (Post::exists($condition)) {
$this->logger->notice('Found already existing item', $condition);
return true;
}
$condition = [
'uri-id' => $item['uri-id'], 'uid' => $item['uid'],
'network' => [$item['network'], Protocol::DFRN]
];
if (Post::exists($condition)) {
$this->logger->notice('duplicated item with the same uri found.', $condition);
return true;
}
// On Friendica and Diaspora the GUID is unique
if (in_array($item['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
$condition = ['guid' => $item['guid'], 'uid' => $item['uid']];
if (Post::exists($condition)) {
$this->logger->notice('duplicated item with the same guid found.', $condition);
return true;
}
}
/*
* Check for already added items.
* There is a timing issue here that sometimes creates double postings.
* An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this.
*/
if (($item['uid'] == 0) && Post::exists(['uri-id' => $item['uri-id'], 'uid' => 0])) {
$this->logger->notice('Global item already stored.', ['uri-id' => $item['uri-id'], 'network' => $item['network']]);
return true;
}
return false;
}
public function validateItemData(array $item): array
{
$item['wall'] = intval($item['wall'] ?? 0);