1
1
Fork 0

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

View file

@ -1009,14 +1009,15 @@ class BBCode
/** /**
* @param string $text A BBCode string * @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: * @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 * - comment : Text before the opening share tag
* - shared : Text inside the share tags * - shared : Text inside the share tags
* - author : (Optional) Display name of the shared author * - author : (Optional) Display name of the shared author
* - profile: (Optional) Profile page URL of the shared author * - profile : (Optional) Profile page URL of the shared author
* - avatar : (Optional) Profile picture URL of the shared author * - avatar : (Optional) Profile picture URL of the shared author
* - link : (Optional) Canonical URL of the shared post * - 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) * - posted : (Optional) Date the shared post was initially posted ("Y-m-d H:i:s" in GMT)
* - guid : (Optional) Shared post GUID if any * - message_id: (Optional) Shared post URI if any
* - guid : (Optional) Shared post GUID if any
*/ */
public static function fetchShareAttributes(string $text): array public static function fetchShareAttributes(string $text): array
{ {

View file

@ -3671,7 +3671,15 @@ class Item
return $item['body']; 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()]); 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) { if (!$id) {
return $item['body']; return $item['body'];