mirror of
https://github.com/friendica/friendica
synced 2025-02-22 13:58:14 +00:00
Extract getDuplicateID() into ItemHelper
This commit is contained in:
parent
118e59ac5c
commit
db949270f5
2 changed files with 40 additions and 38 deletions
|
@ -650,42 +650,6 @@ class Item
|
||||||
return true;
|
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
|
* 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
|
* We have to check several networks since Friendica posts could be repeated
|
||||||
* via Diaspora.
|
* via Diaspora.
|
||||||
*/
|
*/
|
||||||
$duplicate = self::getDuplicateID($item);
|
$duplicate = $itemHelper->getDuplicateID($item);
|
||||||
if ($duplicate) {
|
if ($duplicate) {
|
||||||
return $duplicate;
|
return $duplicate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ use Psr\Log\LoggerInterface;
|
||||||
/**
|
/**
|
||||||
* A helper class for handling an Item Model
|
* 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()
|
* @see Item::insert()
|
||||||
*/
|
*/
|
||||||
|
@ -77,6 +77,44 @@ final class ItemHelper
|
||||||
return $item;
|
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
|
* Check if the item array is a duplicate
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue