Extract getDuplicateID() into ItemHelper

This commit is contained in:
Art4 2025-01-23 13:26:58 +00:00
parent 118e59ac5c
commit db949270f5
2 changed files with 40 additions and 38 deletions

View file

@ -650,42 +650,6 @@ class Item
return true;
}
/**
* Return the id of the given item array if it has been stored before
*
* @param array $item Item record
* @return integer Item id or zero on error
*/
private static function getDuplicateID(array $item): int
{
if (empty($item['network']) || in_array($item['network'], Protocol::FEDERATED)) {
$condition = [
'`uri-id` = ? AND `uid` = ? AND `network` IN (?, ?, ?)',
$item['uri-id'],
$item['uid'],
Protocol::ACTIVITYPUB,
Protocol::DIASPORA,
Protocol::DFRN
];
$existing = Post::selectFirst(['id', 'network'], $condition);
if (DBA::isResult($existing)) {
// We only log the entries with a different user id than 0. Otherwise we would have too many false positives
if ($item['uid'] != 0) {
DI::logger()->notice('Item already existed for user', [
'uri-id' => $item['uri-id'],
'uid' => $item['uid'],
'network' => $item['network'],
'existing_id' => $existing['id'],
'existing_network' => $existing['network']
]);
}
return $existing['id'];
}
}
return 0;
}
/**
* Fetch the uri-id of the parent for the given uri-id
*
@ -758,7 +722,7 @@ class Item
* We have to check several networks since Friendica posts could be repeated
* via Diaspora.
*/
$duplicate = self::getDuplicateID($item);
$duplicate = $itemHelper->getDuplicateID($item);
if ($duplicate) {
return $duplicate;
}

View file

@ -18,7 +18,7 @@ use Psr\Log\LoggerInterface;
/**
* A helper class for handling an Item Model
*
* @internal only for use in Friendica\Content\Item class
* @internal ONLY for use in Friendica\Content\Item class
*
* @see Item::insert()
*/
@ -77,6 +77,44 @@ final class ItemHelper
return $item;
}
/**
* Return the id of the given item array if it has been stored before
*
* @param array $item Item record
* @return integer Item id or zero on error
*/
public function getDuplicateID(array $item): int
{
if (empty($item['network']) || in_array($item['network'], Protocol::FEDERATED)) {
$condition = [
'`uri-id` = ? AND `uid` = ? AND `network` IN (?, ?, ?)',
$item['uri-id'],
$item['uid'],
Protocol::ACTIVITYPUB,
Protocol::DIASPORA,
Protocol::DFRN
];
$existing = Post::selectFirst(['id', 'network'], $condition);
if ($this->database->isResult($existing)) {
// We only log the entries with a different user id than 0. Otherwise we would have too many false positives
if ($item['uid'] != 0) {
$this->logger->notice('Item already existed for user', [
'uri-id' => $item['uri-id'],
'uid' => $item['uid'],
'network' => $item['network'],
'existing_id' => $existing['id'],
'existing_network' => $existing['network']
]);
}
return $existing['id'];
}
}
return 0;
}
/**
* Check if the item array is a duplicate
*