Issue 11969: Simplified share

This commit is contained in:
Michael 2022-10-08 09:36:35 +00:00
parent b92e23273d
commit 4a37c978c2
3 changed files with 27 additions and 29 deletions

View File

@ -21,13 +21,11 @@
use Friendica\App;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Util\Network;
function share_init(App $a) {
$post_id = ((DI::args()->getArgc() > 1) ? intval(DI::args()->getArgv()[1]) : 0);
@ -36,30 +34,21 @@ function share_init(App $a) {
System::exit();
}
$fields = ['private', 'body', 'author-name', 'author-link', 'author-avatar',
'guid', 'created', 'plink', 'uri', 'title', 'network'];
$fields = ['private', 'body', 'uri'];
$item = Post::selectFirst($fields, ['id' => $post_id]);
if (!DBA::isResult($item) || $item['private'] == Item::PRIVATE) {
System::exit();
}
if (strpos($item['body'], "[/share]") !== false) {
$pos = strpos($item['body'], "[share");
$o = substr($item['body'], $pos);
} elseif (Network::isValidHttpUrl($item['uri']) && in_array($item['network'], Protocol::FEDERATED)) {
$o = "[share]" . $item['uri'] . "[/share]";
$shared = BBCode::fetchShareAttributes($item['body']);
if (!empty($shared['message_id']) || !empty($shared['link'])) {
$content = '[share]' . ($shared['message_id'] ?: $shared['link']) . '[/share]';
} elseif (strpos($item['body'], '[/share]') !== false) {
$pos = strpos($item['body'], '[share');
$content = substr($item['body'], $pos);
} else {
$o = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid']);
if ($item['title']) {
$o .= '[h3]'.$item['title'].'[/h3]'."\n";
}
$o .= $item['body'];
$o .= "[/share]";
$content = '[share]' . $item['uri'] . '[/share]';
}
echo $o;
System::exit();
System::httpExit($content);
}

View File

@ -1009,14 +1009,15 @@ class BBCode
/**
* @param string $text A BBCode string
* @return array Empty array if no share tag is present or the following array, missing attributes end up empty strings:
* - comment: Text before the opening share tag
* - shared : Text inside the share tags
* - author : (Optional) Display name of the shared author
* - profile: (Optional) Profile page URL of the shared author
* - avatar : (Optional) Profile picture URL of the shared author
* - link : (Optional) Canonical URL of the shared post
* - posted : (Optional) Date the shared post was initially posted ("Y-m-d H:i:s" in GMT)
* - guid : (Optional) Shared post GUID if any
* - comment : Text before the opening share tag
* - shared : Text inside the share tags
* - author : (Optional) Display name of the shared author
* - profile : (Optional) Profile page URL of the shared author
* - avatar : (Optional) Profile picture URL of the shared author
* - link : (Optional) Canonical URL of the shared post
* - posted : (Optional) Date the shared post was initially posted ("Y-m-d H:i:s" in GMT)
* - message_id: (Optional) Shared post URI if any
* - guid : (Optional) Shared post GUID if any
*/
public static function fetchShareAttributes(string $text): array
{

View File

@ -3671,7 +3671,15 @@ class Item
return $item['body'];
}
$id = self::fetchByLink($shared['link'] ?: $shared['message_id']);
$link = $shared['link'] ?: $shared['message_id'];
if (!empty($item['uid'])) {
$id = self::searchByLink($link, $item['uid']);
}
if (empty($id)) {
$id = self::fetchByLink($link);
}
Logger::debug('Fetched shared post', ['uri-id' => $item['uri-id'], 'id' => $id, 'author' => $shared['profile'], 'url' => $shared['link'], 'guid' => $shared['guid'], 'uri' => $shared['message_id'], 'callstack' => System::callstack()]);
if (!$id) {
return $item['body'];