Merge pull request #12070 from annando/share

Some more improvements for posts with shares
This commit is contained in:
Hypolite Petovan 2022-10-27 08:19:23 -04:00 committed by GitHub
commit f418687a71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 208 additions and 153 deletions

View file

@ -2950,20 +2950,18 @@ class Item
$item['mentions'] = $tags['mentions'];
$body = $item['body'] ?? '';
$shared = self::getShareArray($item);
if (!empty($shared['guid'])) {
$shared_item = Post::selectFirst(['uri-id', 'guid', 'plink', 'has-media'], ['guid' => $shared['guid'], 'uid' => [$item['uid'], 0]]);
}
$fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media'];
$shared_uri_id = 0;
$shared_links = [];
if (empty($shared_item['uri-id']) && !empty($item['quote-uri-id'])) {
$shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id']]);
$quote_uri_id = $item['quote-uri-id'] ?? 0;
$shared_links[] = strtolower($item['quote-uri']);
$shared = DI::contentItem()->getSharedPost($item, $fields);
if (!empty($shared['post'])) {
$shared_item = $shared['post'];
$quote_uri_id = $shared['post']['uri-id'];
$shared_links[] = strtolower($shared['post']['uri']);
$item['body'] = BBCode::removeSharedData($item['body']);
} elseif (empty($shared_item['uri-id']) && empty($item['quote-uri-id'])) {
$media = Post\Media::getByURIId($item['uri-id'], [Post\Media::ACTIVITY]);
if (!empty($media)) {
@ -3589,43 +3587,6 @@ class Item
return 0;
}
/**
* Return share data from an item array (if the item is shared item)
* We are providing the complete Item array, because at some time in the future
* we hopefully will define these values not in the body anymore but in some item fields.
* This function is meant to replace all similar functions in the system.
*
* @param array $item
*
* @return array with share information
*/
public static function getShareArray(array $item): array
{
$attributes = BBCode::fetchShareAttributes($item['body'] ?? '');
if (!empty($attributes)) {
return $attributes;
}
if (!empty($item['quote-uri-id'])) {
$shared = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'uri', 'body'], ['uri-id' => $item['quote-uri-id']]);
if (!empty($shared)) {
return [
'author' => $shared['author-name'],
'profile' => $shared['author-link'],
'avatar' => $shared['author-avatar'],
'link' => $shared['plink'],
'posted' => $shared['created'],
'guid' => $shared['guid'],
'message_id' => $shared['uri'],
'comment' => $item['body'],
'shared' => $shared['body'],
];
}
}
return [];
}
/**
* Check a prospective item array against user-level permissions
*
@ -3663,36 +3624,6 @@ class Item
return true;
}
/**
* Improve the data in shared posts
*
* @param array $item
* @param bool $add_media
* @return string body
*/
public static function improveSharedDataInBody(array $item, bool $add_media = false): string
{
$shared = BBCode::fetchShareAttributes($item['body']);
if (empty($shared['guid']) && empty($shared['message_id'])) {
return $item['body'];
}
$link = $shared['link'] ?: $shared['message_id'];
if (empty($shared_content)) {
$shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0, $add_media);
}
if (empty($shared_content)) {
return $item['body'];
}
$item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content, $item['body']);
Logger::debug('New shared data', ['uri-id' => $item['uri-id'], 'link' => $link, 'guid' => $item['guid']]);
return $item['body'];
}
/**
* Fetch the uri-id of a quote
*

View file

@ -404,8 +404,7 @@ class Media
$unshared_body = $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
// Only remove the shared data from "real" reshares
$shared = BBCode::fetchShareAttributes($body);
if (!empty($shared['guid'])) {
if (BBCode::isNativeReshare($body)) {
$unshared_body = BBCode::removeSharedData($body);
}
@ -486,8 +485,7 @@ class Media
public static function insertFromRelevantUrl(int $uriid, string $body)
{
// Only remove the shared data from "real" reshares
$shared = BBCode::fetchShareAttributes($body);
if (!empty($shared['guid'])) {
if (BBCode::isNativeReshare($body)) {
// Don't look at the shared content
$body = BBCode::removeSharedData($body);
}

View file

@ -257,17 +257,16 @@ class Tag
* @param string $hash
* @param string $name
* @param string $url
* @param boolean $probing Whether probing is active
* @return void
*/
public static function storeByHash(int $uriId, string $hash, string $name, string $url = '', bool $probing = true)
public static function storeByHash(int $uriId, string $hash, string $name, string $url = '')
{
$type = self::getTypeForHash($hash);
if ($type == self::UNKNOWN) {
return;
}
self::store($uriId, $type, $name, $url, $probing);
self::store($uriId, $type, $name, $url);
}
/**
@ -297,32 +296,43 @@ class Tag
* @param integer $uriId URI-Id
* @param string $body Body of the post
* @param string $tags Accepted tags
* @param boolean $probing Perform a probing for contacts, adding them if needed
* @return void
*/
public static function storeFromBody(int $uriId, string $body, string $tags = null, bool $probing = true)
public static function storeFromBody(int $uriId, string $body, string $tags = null)
{
Logger::info('Check for tags', ['uri-id' => $uriId, 'hash' => $tags, 'callstack' => System::callstack()]);
$item = ['uri-id' => $uriId, 'body' => $body, 'quote-uri-id' => null];
self::storeFromArray($item, $tags);
}
/**
* Store tags and mentions from the item array
*
* @param array $item Item array
* @param string $tags Accepted tags
* @return void
*/
public static function storeFromArray(array $item, string $tags = null)
{
Logger::info('Check for tags', ['uri-id' => $item['uri-id'], 'hash' => $tags, 'callstack' => System::callstack()]);
if (is_null($tags)) {
$tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
}
// Only remove the shared data from "real" reshares
$shared = BBCode::fetchShareAttributes($body);
if (!empty($shared['guid'])) {
$share_body = $shared['shared'];
$body = BBCode::removeSharedData($body);
$shared = DI::contentItem()->getSharedPost($item, ['uri-id']);
if (!empty($shared)) {
$item['body'] = BBCode::removeSharedData($item['body']);
}
foreach (self::getFromBody($body, $tags) as $tag) {
self::storeByHash($uriId, $tag[1], $tag[3], $tag[2], $probing);
foreach (self::getFromBody($item['body'], $tags) as $tag) {
self::storeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]);
}
// Search for hashtags in the shared body (but only if hashtags are wanted)
if (!empty($share_body) && (strpos($tags, self::TAG_CHARACTER[self::HASHTAG]) !== false)) {
foreach (self::getFromBody($share_body, self::TAG_CHARACTER[self::HASHTAG]) as $tag) {
self::storeByHash($uriId, $tag[1], $tag[3], $tag[2], $probing);
if (!empty($shared) && (strpos($tags, self::TAG_CHARACTER[self::HASHTAG]) !== false)) {
foreach (self::getByURIId($shared['post']['uri-id'], [self::HASHTAG]) as $tag) {
self::store($item['uri-id'], $tag['type'], $tag['name'], $tag['url']);
}
}
}