Merge pull request #12061 from annando/share-rework

The "share" handling is improved
This commit is contained in:
Hypolite Petovan 2022-10-25 09:04:16 -04:00 committed by GitHub
commit a7e9457ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 75 additions and 112 deletions

View File

@ -122,11 +122,9 @@ function display_init(App $a)
function display_fetchauthor($item)
{
if (Diaspora::isReshare($item['body'], true)) {
$shared = Item::getShareArray($item);
if (!empty($shared['profile'])) {
$contact = Contact::getByURLForUser($shared['profile'], DI::userSession()->getLocalUserId());
}
$shared = Item::getShareArray($item);
if (empty($shared['comment']) && !empty($shared['guid']) && !empty($shared['profile'])) {
$contact = Contact::getByURLForUser($shared['profile'], DI::userSession()->getLocalUserId());
}
if (empty($contact)) {

View File

@ -41,7 +41,7 @@ function share_init(App $a) {
System::exit();
}
$shared = BBCode::fetchShareAttributes($item['body']);
$shared = Item::getShareArray($item);
if (empty($shared['comment']) && (!empty($shared['message_id']) || !empty($shared['link']))) {
$content = '[share]' . ($shared['message_id'] ?: $shared['link']) . '[/share]';
} else {

View File

@ -680,10 +680,10 @@ class Item
$shared_content .= '[h3]' . $item['title'] . "[/h3]\n";
}
$shared = BBCode::fetchShareAttributes($item['body']);
$shared = ItemModel::getShareArray($item);
// If it is a reshared post then reformat it to avoid display problems with two share elements
if (Diaspora::isReshare($item['body'], false)) {
if (!empty($shared)) {
if (!empty($shared['guid']) && ($encaspulated_share = self::createSharedPostByGuid($shared['guid'], 0, '', $add_media))) {
$item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
}

View File

@ -1023,6 +1023,7 @@ class BBCode
{
DI::profiler()->startRecording('rendering');
if (preg_match('~(.*?)\[share](.*)\[/share]~ism', $text, $matches)) {
DI::profiler()->stopRecording();
return [
'author' => '',
'profile' => '',
@ -1093,12 +1094,7 @@ class BBCode
*/
public static function removeSharedData(string $body): string
{
return BBCode::convertShare(
$body,
function (array $attributes) {
return '';
}
);
return preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
}
/**

View File

@ -155,7 +155,7 @@ class Status extends BaseFactory
$poll = null;
}
$shared = BBCode::fetchShareAttributes($item['body']);
$shared = Item::getShareArray($item);
if (!empty($shared['guid'])) {
$shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);

View File

@ -178,7 +178,7 @@ class Status extends BaseFactory
$friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid);
$shared = BBCode::fetchShareAttributes($item['body']);
$shared = Item::getShareArray($item);
if (!empty($shared['guid'])) {
$shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);

View File

@ -96,11 +96,11 @@ class Contact
* Relationship types
* @{
*/
const NOTHING = 0;
const FOLLOWER = 1;
const SHARING = 2;
const FRIEND = 3;
const SELF = 4;
const NOTHING = 0; // There is no relationship between the contact and the user
const FOLLOWER = 1; // The contact is following this user (the contact is the subscriber)
const SHARING = 2; // The contact shares their content with this user (the user is the subscriber)
const FRIEND = 3; // There is a mutual relationship between the contact and the user
const SELF = 4; // This is the user theirself
/**
* @}
*/

View File

@ -112,7 +112,7 @@ class Item
'private', 'title', 'body', 'raw-body', 'location', 'coord', 'app',
'inform', 'deleted', 'extid', 'post-type', 'post-reason', 'gravity',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
'author-id', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
'author-id', 'author-addr', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
'signed_text', 'network', 'wall', 'contact-id', 'plink', 'origin',
'thr-parent-id', 'parent-uri-id', 'quote-uri', 'quote-uri-id', 'postopts', 'pubmail',
'event-created', 'event-edited', 'event-start', 'event-finish',
@ -2949,7 +2949,7 @@ class Item
$item['mentions'] = $tags['mentions'];
$body = $item['body'] ?? '';
$shared = BBCode::fetchShareAttributes($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]]);
}
@ -3042,7 +3042,7 @@ class Item
$s = self::addVisualAttachments($shared_attachments, $item, $s, true);
$s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $shared_attachments, $body, $s, true, []);
$s = self::addNonVisualAttachments($shared_attachments, $item, $s, true);
$body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
$body = BBCode::removeSharedData($body);
}
$s = self::addVisualAttachments($attachments, $item, $s, false);
@ -3600,7 +3600,29 @@ class Item
*/
public static function getShareArray(array $item): array
{
return BBCode::fetchShareAttributes($item['body']);
$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 [];
}
/**

View File

@ -406,7 +406,7 @@ class Media
// Only remove the shared data from "real" reshares
$shared = BBCode::fetchShareAttributes($body);
if (!empty($shared['guid'])) {
$unshared_body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
$unshared_body = BBCode::removeSharedData($body);
}
$attachments = [];
@ -489,7 +489,7 @@ class Media
$shared = BBCode::fetchShareAttributes($body);
if (!empty($shared['guid'])) {
// Don't look at the shared content
$body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
$body = BBCode::removeSharedData($body);
}
// Remove all hashtags and mentions
@ -522,7 +522,7 @@ class Media
public static function insertFromAttachmentData(int $uriid, string $body)
{
// Don't look at the shared content
$body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
$body = BBCode::removeSharedData($body);
$data = BBCode::getAttachmentData($body);
if (empty($data)) {

View File

@ -311,10 +311,8 @@ class Tag
// Only remove the shared data from "real" reshares
$shared = BBCode::fetchShareAttributes($body);
if (!empty($shared['guid'])) {
if (preg_match("/\s*\[share .*?\](.*?)\[\/share\]\s*/ism", $body, $matches)) {
$share_body = $matches[1];
}
$body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
$share_body = $shared['shared'];
$body = BBCode::removeSharedData($body);
}
foreach (self::getFromBody($body, $tags) as $tag) {

View File

@ -1664,17 +1664,18 @@ class Transmitter
$body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
}
$body = BBCode::setMentionsToNicknames($body);
$body = BBCode::setMentionsToNicknames($body);
$shared = BBCode::fetchShareAttributes($body);
if (!empty($item['quote-uri']) && Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
$real_quote = true;
if (Diaspora::isReshare($body, false)) {
if (!empty($shared['link'])) {
$body = BBCode::replaceSharedData($body);
} elseif (strpos($body, $item['quote-uri']) === false) {
$body .= "\n" . $item['quote-uri'];
}
$data['quoteUrl'] = $item['quote-uri'];
} elseif (!empty($item['quote-uri']) && !Diaspora::isReshare($body, false)) {
} elseif (!empty($item['quote-uri']) && empty($shared)) {
$body .= "\n" . DI::contentItem()->createSharedPostByUriId($item['quote-uri-id'], $item['uid'], true);
$item['body'] = Item::improveSharedDataInBody($item, true);
}
@ -1691,8 +1692,10 @@ class Transmitter
if ($real_quote) {
$shared = BBCode::fetchShareAttributes($richbody);
if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) {
if (!empty($shared['link'])) {
$richbody = BBCode::replaceSharedData($richbody);
} elseif (strpos($richbody, $item['quote-uri']) === false) {
$richbody .= "\n" . $item['quote-uri'];
}
}

View File

@ -776,36 +776,6 @@ class Diaspora
return '';
}
/**
* get a handle (user@domain.tld) from a given contact id
*
* @param int $contact_id The id in the contact table
* @param int $pcontact_id The id in the contact table (Used for the public contact)
*
* @return string the handle
* @throws \Exception
*/
private static function handleFromContact(int $contact_id, int $pcontact_id = 0): string
{
$handle = '';
if ($pcontact_id != 0) {
$contact = Contact::getById($pcontact_id, ['addr']);
if (DBA::isResult($contact)) {
$handle = $contact['addr'];
}
}
if (empty($handle)) {
$contact = Contact::getById($contact_id, ['addr']);
if (DBA::isResult($contact)) {
$handle = $contact['addr'];
}
}
return strtolower($handle);
}
/**
* Get a contact id for a given handle
*
@ -3201,61 +3171,37 @@ class Diaspora
}
/**
* Checks a message body if it is a reshare
* Fetch reshare details
*
* @param string $body The message body that is to be check
* @param bool $complete Should it be a complete check or a simple check?
* @param array $item The message body that is to be check
*
* @return array|bool Reshare details or "false" if no reshare
* @return array Reshare details (empty if the item is no reshare)
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function isReshare(string $body, bool $complete = true)
public static function getReshareDetails(array $item): array
{
$body = trim($body);
$reshared = Item::getShareArray(['body' => $body]);
$reshared = Item::getShareArray($item);
if (empty($reshared)) {
return false;
return [];
}
// Skip if it isn't a pure repeated messages
// Does it start with a share?
if (!empty($reshared['comment']) && $complete) {
return false;
// Skip if it isn't a pure repeated messages or not a real reshare
if (!empty($reshared['comment']) || empty($reshared['guid'])) {
return [];
}
if (!empty($reshared['guid']) && $complete) {
$condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
$item = Post::selectFirst(['contact-id'], $condition);
if (DBA::isResult($item)) {
$ret = [];
$ret['root_handle'] = self::handleFromContact($item['contact-id']);
$ret['root_guid'] = $reshared['guid'];
return $ret;
} elseif ($complete) {
// We are resharing something that isn't a DFRN or Diaspora post.
// So we have to return "false" on "$complete" to not trigger a reshare.
return false;
}
} elseif (empty($reshared['guid']) && $complete) {
return false;
$condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
$item = Post::selectFirst(['author-addr'], $condition);
if (DBA::isResult($item)) {
return [
'root_handle' => strtolower($item['author-addr']),
'root_guid' => $reshared['guid']
];
}
$ret = [];
if (!empty($reshared['profile']) && ($cid = Contact::getIdForURL($reshared['profile']))) {
$contact = DBA::selectFirst('contact', ['addr'], ['id' => $cid]);
if (!empty($contact['addr'])) {
$ret['root_handle'] = $contact['addr'];
}
}
if (empty($ret) && !$complete) {
return true;
}
return $ret;
// We are resharing something that isn't a DFRN or Diaspora post.
return [];
}
/**
@ -3352,7 +3298,7 @@ class Diaspora
$edited = DateTimeFormat::utc($item['edited'] ?? $item['created'], DateTimeFormat::ATOM);
// Detect a share element and do a reshare
if (($item['private'] != Item::PRIVATE) && ($ret = self::isReshare($item['body']))) {
if (($item['private'] != Item::PRIVATE) && ($ret = self::getReshareDetails($item))) {
$message = [
'author' => $myaddr,
'guid' => $item['guid'],
@ -3725,7 +3671,7 @@ class Diaspora
*/
public static function sendRetraction(array $item, array $owner, array $contact, bool $public_batch = false, bool $relay = false): int
{
$itemaddr = self::handleFromContact($item['contact-id'], $item['author-id']);
$itemaddr = strtolower($item['author-addr']);
$msg_type = 'retraction';

View File

@ -438,7 +438,7 @@ class Feed
}
if (!empty($href)) {
$attachment = ['type' => Post\Media::UNKNOWN, 'url' => $href, 'mimetype' => $type, 'size' => $length];
$attachment = ['uri-id' => -1, 'type' => Post\Media::UNKNOWN, 'url' => $href, 'mimetype' => $type, 'size' => $length];
$attachment = Post\Media::fetchAdditionalData($attachment);

View File

@ -274,7 +274,7 @@ class Delivery
private static function deliverDFRN(string $cmd, array $contact, array $owner, array $items, array $target_item, bool $public_message, bool $top_level, bool $followup, int $server_protocol = null)
{
// Transmit Diaspora reshares via Diaspora if the Friendica contact support Diaspora
if (Diaspora::isReshare($target_item['body'] ?? '') && !empty(FContact::getByURL($contact['addr'], false))) {
if (Diaspora::getReshareDetails($target_item ?? []) && !empty(FContact::getByURL($contact['addr'], false))) {
Logger::info('Reshare will be transmitted via Diaspora', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
return;