From c18cbe95a63338fd2fdb117901b365af95ab0401 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 22 May 2021 18:20:11 +0000 Subject: [PATCH 01/12] Fix inserting mails --- src/Model/Mail.php | 57 +++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/Model/Mail.php b/src/Model/Mail.php index aa90586601..9c3bd58bb6 100644 --- a/src/Model/Mail.php +++ b/src/Model/Mail.php @@ -36,15 +36,14 @@ use Friendica\Worker\Delivery; class Mail { /** - * Insert received private message + * Insert private message * * @param array $msg + * @param bool $notifiction * @return int|boolean Message ID or false on error */ - public static function insert($msg) + public static function insert($msg, $notifiction = true) { - $user = User::getById($msg['uid']); - if (!isset($msg['reply'])) { $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]); } @@ -63,6 +62,10 @@ class Mail $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow()); + $msg['author-id'] = Contact::getIdForURL($msg['from-url'], 0, false); + $msg['uri-id'] = ItemURI::insert(['uri' => $msg['uri'], 'guid' => $msg['guid']]); + $msg['parent-uri-id'] = ItemURI::getIdByURI($msg['parent-uri']); + DBA::lock('mail'); if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) { @@ -71,12 +74,8 @@ class Mail return false; } - $msg['author-id'] = Contact::getIdForURL($msg['from-url'], 0, false); - $msg['uri-id'] = ItemURI::insert(['uri' => $msg['uri'], 'guid' => $msg['guid']]); - $msg['parent-uri-id'] = ItemURI::getIdByURI($msg['parent-uri']); - if ($msg['reply']) { - $reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]); + $reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $msg['parent-uri'], 'reply' => false]); $msg['thr-parent'] = $reply['uri']; $msg['thr-parent-id'] = $reply['uri-id']; @@ -95,19 +94,22 @@ class Mail DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]); } - // send notifications. - $notif_params = [ - 'type' => Notification\Type::MAIL, - 'otype' => Notification\ObjectType::MAIL, - 'verb' => Activity::POST, - 'uid' => $user['uid'], - 'cid' => $msg['contact-id'], - 'link' => DI::baseUrl() . '/message/' . $msg['id'], - ]; + if ($notifiction) { + $user = User::getById($msg['uid']); + // send notifications. + $notif_params = [ + 'type' => Notification\Type::MAIL, + 'otype' => Notification\ObjectType::MAIL, + 'verb' => Activity::POST, + 'uid' => $user['uid'], + 'cid' => $msg['contact-id'], + 'link' => DI::baseUrl() . '/message/' . $msg['id'], + ]; - notification($notif_params); + notification($notif_params); - Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]); + Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]); + } return $msg['id']; } @@ -195,9 +197,7 @@ class Mail $replyto = $convuri; } - $post_id = null; - $success = DBA::insert( - 'mail', + $post_id =self::insert( [ 'uid' => local_user(), 'guid' => $guid, @@ -214,13 +214,9 @@ class Mail 'uri' => $uri, 'parent-uri' => $replyto, 'created' => DateTimeFormat::utcNow() - ] + ], false ); - if ($success) { - $post_id = DBA::lastInsertId(); - } - /** * * When a photo was uploaded into the message using the (profile wall) ajax @@ -301,8 +297,7 @@ class Mail return -4; } - DBA::insert( - 'mail', + self::insert( [ 'uid' => $recipient['uid'], 'guid' => $guid, @@ -320,7 +315,7 @@ class Mail 'parent-uri' => $me['url'], 'created' => DateTimeFormat::utcNow(), 'unknown' => 1 - ] + ], false ); return 0; From 965cd8b096f181c8e66cbd2ab52376be698bbee2 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 22 May 2021 23:45:15 +0200 Subject: [PATCH 02/12] Fix PHP 7.0 Coding Standards --- include/api.php | 6 +++--- mod/settings.php | 2 +- mod/unfollow.php | 2 +- mod/update_notes.php | 2 +- src/Content/Widget/TagCloud.php | 6 +++--- src/Module/Attach.php | 2 +- src/Network/HTTPRequest.php | 8 ++++---- src/Object/Api/Mastodon/Status.php | 3 ++- 8 files changed, 16 insertions(+), 15 deletions(-) diff --git a/include/api.php b/include/api.php index 0b925aee0f..8e89895ded 100644 --- a/include/api.php +++ b/include/api.php @@ -1186,12 +1186,12 @@ function api_statuses_update($type) INNER JOIN `user` ON `user`.`uid` = `photo`.`uid` WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ? ORDER BY `photo`.`width` DESC LIMIT 2", $id, api_user())); - + if (!empty($media)) { $ressources[] = $media[0]['resource-id']; $phototypes = Images::supportedTypes(); $ext = $phototypes[$media[0]['type']]; - + $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'], 'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext, 'size' => $media[0]['datasize'], @@ -1199,7 +1199,7 @@ function api_statuses_update($type) 'description' => $media[0]['desc'] ?? '', 'width' => $media[0]['width'], 'height' => $media[0]['height']]; - + if (count($media) > 1) { $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext; $attachment['preview-width'] = $media[1]['width']; diff --git a/mod/settings.php b/mod/settings.php index 3a3f0b65dc..ec81339848 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -326,7 +326,7 @@ function settings_post(App $a) $detailed_notif = (($_POST['detailed_notif'] == 1) ? 1 : 0); $notify_ignored = (($_POST['notify_ignored'] == 1) ? 1 : 0); - + $notify = 0; if (!empty($_POST['notify1'])) { diff --git a/mod/unfollow.php b/mod/unfollow.php index 1c6022a4cb..aca17727b6 100644 --- a/mod/unfollow.php +++ b/mod/unfollow.php @@ -158,4 +158,4 @@ function unfollow_process(string $url) } DI::baseUrl()->redirect($return_path); -} \ No newline at end of file +} diff --git a/mod/update_notes.php b/mod/update_notes.php index 3b85d34b9a..641f7db8be 100644 --- a/mod/update_notes.php +++ b/mod/update_notes.php @@ -43,4 +43,4 @@ function update_notes_content(App $a) { $text = notes_content($a, $profile_uid); System::htmlUpdateExit($text); -} \ No newline at end of file +} diff --git a/src/Content/Widget/TagCloud.php b/src/Content/Widget/TagCloud.php index f315b3b537..4263d8cf4a 100644 --- a/src/Content/Widget/TagCloud.php +++ b/src/Content/Widget/TagCloud.php @@ -127,8 +127,8 @@ class TagCloud private static function tagCalc(array $arr) { $tags = []; - $min = 1e9; - $max = -1e9; + $min = 1000000000.0; + $max = -1000000000.0; $x = 0; if (!$arr) { @@ -145,7 +145,7 @@ class TagCloud } usort($tags, 'self::tagsSort'); - $range = max(.01, $max - $min) * 1.0001; + $range = max(0.01, $max - $min) * 1.0001; for ($x = 0; $x < count($tags); $x ++) { $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range); diff --git a/src/Module/Attach.php b/src/Module/Attach.php index 7a288fb3b9..2acbf31b42 100644 --- a/src/Module/Attach.php +++ b/src/Module/Attach.php @@ -43,7 +43,7 @@ class Attach extends BaseModule // @TODO: Replace with parameter from router $item_id = intval($a->argv[1]); - + // Check for existence $item = MAttach::exists(['id' => $item_id]); if ($item === false) { diff --git a/src/Network/HTTPRequest.php b/src/Network/HTTPRequest.php index 74cf9cd055..bd31ac1e14 100644 --- a/src/Network/HTTPRequest.php +++ b/src/Network/HTTPRequest.php @@ -70,7 +70,7 @@ class HTTPRequest implements IHTTPRequest * * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function get(string $url, array $opts = [], int &$redirects = 0) + public function get(string $url, array $opts = [], &$redirects = 0) { $stamp1 = microtime(true); @@ -222,7 +222,7 @@ class HTTPRequest implements IHTTPRequest * * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function post(string $url, $params, array $headers = [], int $timeout = 0, int &$redirects = 0) + public function post(string $url, $params, array $headers = [], int $timeout = 0, &$redirects = 0) { $stamp1 = microtime(true); @@ -447,7 +447,7 @@ class HTTPRequest implements IHTTPRequest * * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) + public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', &$redirects = 0) { $ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar, $redirects); @@ -461,7 +461,7 @@ class HTTPRequest implements IHTTPRequest * * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) + public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', &$redirects = 0) { return $this->get( $url, diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index 227395d171..eb88ce5f1a 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -114,7 +114,8 @@ class Status extends BaseDataTransferObject $this->visibility = $visibility[$item['private']]; $languages = json_decode($item['language'], true); - $this->language = is_array($languages) ? array_key_first($languages) : null; + reset($languages); + $this->language = is_array($languages) ? key($languages) : null; $this->uri = $item['uri']; $this->url = $item['plink'] ?? null; From d00a4eacdc162d8a8dc60e69b632fde1b6de5aca Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 23 May 2021 07:37:17 +0200 Subject: [PATCH 03/12] Update src/Model/Mail.php Co-authored-by: Hypolite Petovan --- src/Model/Mail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Mail.php b/src/Model/Mail.php index 9c3bd58bb6..307724a262 100644 --- a/src/Model/Mail.php +++ b/src/Model/Mail.php @@ -197,7 +197,7 @@ class Mail $replyto = $convuri; } - $post_id =self::insert( + $post_id = self::insert( [ 'uid' => local_user(), 'guid' => $guid, From 55dda47f39f77537f0527ffd486003d15e77a850 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 23 May 2021 07:50:32 +0200 Subject: [PATCH 04/12] Issue 10293: mention the required function in the error message --- src/Core/Installer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Installer.php b/src/Core/Installer.php index fe6d13b1db..b2b84c6182 100644 --- a/src/Core/Installer.php +++ b/src/Core/Installer.php @@ -465,7 +465,7 @@ class Installer $status = $this->checkFunction('proc_open', DI::l10n()->t('Program execution functions'), - DI::l10n()->t('Error: Program execution functions required but not enabled.'), + DI::l10n()->t('Error: Program execution functions (proc_open) required but not enabled.'), true ); $returnVal = $returnVal ? $status : false; From 918f869aef9ee9b70e442df87e6004f3fbe3fdda Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 23 May 2021 07:50:37 +0200 Subject: [PATCH 05/12] regen messages.po --- view/lang/C/messages.po | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/view/lang/C/messages.po b/view/lang/C/messages.po index ac5bf2bf9a..eeb96ef227 100644 --- a/view/lang/C/messages.po +++ b/view/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 2021.06-rc\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-05-21 18:18+0000\n" +"POT-Creation-Date: 2021-05-23 07:49+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1117,11 +1117,11 @@ msgstr "" msgid "Invalid profile URL." msgstr "" -#: mod/dfrn_request.php:355 src/Model/Contact.php:2159 +#: mod/dfrn_request.php:355 src/Model/Contact.php:2164 msgid "Disallowed profile URL." msgstr "" -#: mod/dfrn_request.php:361 src/Model/Contact.php:2164 +#: mod/dfrn_request.php:361 src/Model/Contact.php:2169 #: src/Module/Friendica.php:80 msgid "Blocked domain" msgstr "" @@ -4072,7 +4072,8 @@ msgid "Program execution functions" msgstr "" #: src/Core/Installer.php:468 -msgid "Error: Program execution functions required but not enabled." +msgid "" +"Error: Program execution functions (proc_open) required but not enabled." msgstr "" #: src/Core/Installer.php:474 @@ -4645,60 +4646,60 @@ msgstr "" msgid "Forum" msgstr "" -#: src/Model/Contact.php:2169 +#: src/Model/Contact.php:2174 msgid "Connect URL missing." msgstr "" -#: src/Model/Contact.php:2178 +#: src/Model/Contact.php:2183 msgid "" "The contact could not be added. Please check the relevant network " "credentials in your Settings -> Social Networks page." msgstr "" -#: src/Model/Contact.php:2219 +#: src/Model/Contact.php:2224 msgid "" "This site is not configured to allow communications with other networks." msgstr "" -#: src/Model/Contact.php:2220 src/Model/Contact.php:2233 +#: src/Model/Contact.php:2225 src/Model/Contact.php:2238 msgid "No compatible communication protocols or feeds were discovered." msgstr "" -#: src/Model/Contact.php:2231 +#: src/Model/Contact.php:2236 msgid "The profile address specified does not provide adequate information." msgstr "" -#: src/Model/Contact.php:2236 +#: src/Model/Contact.php:2241 msgid "An author or name was not found." msgstr "" -#: src/Model/Contact.php:2239 +#: src/Model/Contact.php:2244 msgid "No browser URL could be matched to this address." msgstr "" -#: src/Model/Contact.php:2242 +#: src/Model/Contact.php:2247 msgid "" "Unable to match @-style Identity Address with a known protocol or email " "contact." msgstr "" -#: src/Model/Contact.php:2243 +#: src/Model/Contact.php:2248 msgid "Use mailto: in front of address to force email check." msgstr "" -#: src/Model/Contact.php:2249 +#: src/Model/Contact.php:2254 msgid "" "The profile address specified belongs to a network which has been disabled " "on this site." msgstr "" -#: src/Model/Contact.php:2254 +#: src/Model/Contact.php:2259 msgid "" "Limited profile. This person will be unable to receive direct/personal " "notifications from you." msgstr "" -#: src/Model/Contact.php:2313 +#: src/Model/Contact.php:2318 msgid "Unable to retrieve contact information." msgstr "" @@ -4845,7 +4846,7 @@ msgstr "" msgid "View on separate page" msgstr "" -#: src/Model/Mail.php:120 src/Model/Mail.php:258 +#: src/Model/Mail.php:134 src/Model/Mail.php:272 msgid "[no subject]" msgstr "" From 390fa23bce0ee186c3723d22876e5cacce0a07a1 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 23 May 2021 09:43:02 +0200 Subject: [PATCH 06/12] fix failing test for the installer wizard --- tests/src/Core/InstallerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Core/InstallerTest.php b/tests/src/Core/InstallerTest.php index 7404e216e2..f94300fc1c 100644 --- a/tests/src/Core/InstallerTest.php +++ b/tests/src/Core/InstallerTest.php @@ -103,7 +103,7 @@ class InstallerTest extends MockedTest $this->mockL10nT('File Information PHP module', 1); $this->mockL10nT('Error: File Information PHP module required but not installed.', 1); $this->mockL10nT('Program execution functions', 1); - $this->mockL10nT('Error: Program execution functions required but not enabled.', 1); + $this->mockL10nT('Error: Program execution functions (proc_open) required but not enabled.', 1); } private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray) From 6ea52cfbf8ff0f2ea931592329e831efce17ddf5 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 23 May 2021 09:47:25 +0200 Subject: [PATCH 07/12] now... --- tests/src/Core/InstallerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Core/InstallerTest.php b/tests/src/Core/InstallerTest.php index f94300fc1c..8cd1a3fad9 100644 --- a/tests/src/Core/InstallerTest.php +++ b/tests/src/Core/InstallerTest.php @@ -248,7 +248,7 @@ class InstallerTest extends MockedTest self::assertFalse($install->checkFunctions()); self::assertCheckExist(9, 'Program execution functions', - 'Error: Program execution functions required but not enabled.', + 'Error: Program execution functions (proc_open) required but not enabled.', false, true, $install->getChecks()); From 8f9033633cfe36fa760c7147c8dca9b7cd287868 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 23 May 2021 08:56:24 +0000 Subject: [PATCH 08/12] Issue 10287: Make block and ignore work for the API as well --- src/Module/Api/Mastodon/Timelines/PublicTimeline.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 87d0dc1d96..113a5cac2e 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -41,6 +41,8 @@ class PublicTimeline extends BaseApi */ public static function rawContent(array $parameters = []) { + $uid = self::getCurrentUserID(); + $request = self::getRequest([ 'local' => false, // Show only local statuses? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false. @@ -88,7 +90,12 @@ class PublicTimeline extends BaseApi $condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]); } - $items = Post::selectForUser(0, ['uri-id', 'uid'], $condition, $params); + if (!empty($uid)) { + $condition = DBA::mergeConditions($condition, + ["NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `parent-author-id` AND (`blocked` OR `ignored`))", $uid]); + } + + $items = Post::selectForUser($uid, ['uri-id', 'uid'], $condition, $params); $statuses = []; while ($item = Post::fetch($items)) { From 050bc673aacd7d1ab4e78335cfb4d8341f0daea0 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 23 May 2021 10:39:11 +0000 Subject: [PATCH 09/12] Issue 10264: Remote comments for visitors --- mod/dfrn_poll.php | 3 +++ src/App.php | 6 ++++++ src/Object/Post.php | 6 +++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mod/dfrn_poll.php b/mod/dfrn_poll.php index 54a996f651..f0a8e855e9 100644 --- a/mod/dfrn_poll.php +++ b/mod/dfrn_poll.php @@ -128,6 +128,7 @@ function dfrn_poll_init(App $a) $_SESSION['visitor_handle'] = $r[0]['addr']; $_SESSION['visitor_visiting'] = $r[0]['uid']; $_SESSION['my_url'] = $r[0]['url']; + $_SESSION['remote_comment'] = $r[0]['subscribe']; Session::setVisitorsContacts(); @@ -497,8 +498,10 @@ function dfrn_poll_content(App $a) $_SESSION['authenticated'] = 1; $_SESSION['visitor_id'] = $r[0]['id']; $_SESSION['visitor_home'] = $r[0]['url']; + $_SESSION['visitor_handle'] = $r[0]['addr']; $_SESSION['visitor_visiting'] = $r[0]['uid']; $_SESSION['my_url'] = $r[0]['url']; + $_SESSION['remote_comment'] = $r[0]['subscribe']; Session::setVisitorsContacts(); diff --git a/src/App.php b/src/App.php index 772d273c52..5c74b1b539 100644 --- a/src/App.php +++ b/src/App.php @@ -34,6 +34,7 @@ use Friendica\Core\L10n; use Friendica\Core\System; use Friendica\Core\Theme; use Friendica\Database\Database; +use Friendica\Model\Contact; use Friendica\Model\Profile; use Friendica\Module\Special\HTTPException as ModuleHTTPException; use Friendica\Network\HTTPException; @@ -464,6 +465,11 @@ class App if (Core\Session::get('visitor_home') != $_GET["zrl"]) { Core\Session::set('my_url', $_GET['zrl']); Core\Session::set('authenticated', 0); + + $remote_contact = Contact::getByURL($_GET['zrl'], false, ['subscribe']); + if (!empty($remote_contact['subscribe'])) { + $_SESSION['remote_comment'] = $remote_contact['subscribe']; + } } Model\Profile::zrlInit($this); diff --git a/src/Object/Post.php b/src/Object/Post.php index 1f38fa556d..93abf351f9 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -401,9 +401,13 @@ class Post } // Fetching of Diaspora posts doesn't always work. There are issues with reshares and possibly comments - if (($item['network'] != Protocol::DIASPORA) && empty($comment) && !empty(Session::get('remote_comment'))) { + if (!local_user() && ($item['network'] != Protocol::DIASPORA) && !empty(Session::get('remote_comment'))) { $remote_comment = [DI::l10n()->t('Comment this item on your system'), DI::l10n()->t('Remote comment'), str_replace('{uri}', urlencode($item['uri']), Session::get('remote_comment'))]; + + // Ensure to either display the remote comment or the local activities + $buttons = []; + $comment_html = ''; } else { $remote_comment = ''; } From b7a7ae58e9cb157e22495d300b5f62cce48d7e8a Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 23 May 2021 12:35:05 +0000 Subject: [PATCH 10/12] Issue 10232: Improved search for embedded media links --- src/Model/Item.php | 13 ++++++++++--- src/Model/Post/Media.php | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index e8044ecd7a..4dbea3eb8b 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2739,9 +2739,10 @@ class Item * * @param string $body * @param string $url + * @param int $type * @return bool */ - public static function containsLink(string $body, string $url) + public static function containsLink(string $body, string $url, int $type = 0) { // Make sure that for example site parameters aren't used when testing if the link is contained in the body $urlparts = parse_url($url); @@ -2749,6 +2750,12 @@ class Item unset($urlparts['fragment']); $url = Network::unparseURL($urlparts); + // Remove media links to only search in embedded content + // @todo Check images for image link, audio for audio links, ... + if (in_array($type, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE])) { + $body = preg_replace("/\[url=[^\[\]]*\](.*)\[\/url\]/Usi", ' $1 ', $body); + } + if (strpos($body, $url)) { return true; } @@ -2777,7 +2784,7 @@ class Item // @todo In the future we should make a single for the template engine with all media in it. This allows more flexibilty. foreach ($attachments['visual'] as $attachment) { - if (self::containsLink($item['body'], $attachment['url'])) { + if (self::containsLink($item['body'], $attachment['url'], $attachment['type'])) { continue; } @@ -2955,7 +2962,7 @@ class Item // @todo Use a template $rendered = BBCode::convertAttachment('', BBCode::INTERNAL, false, $data); - } elseif (!self::containsLink($content, $data['url'])) { + } elseif (!self::containsLink($content, $data['url'], Post\Media::HTML)) { $rendered = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/link.tpl'), [ '$url' => $data['url'], '$title' => $data['title'], diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index c3f745fee6..48bf435fab 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -600,7 +600,7 @@ class Media $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body); foreach (self::getByURIId($uriid, [self::IMAGE, self::AUDIO, self::VIDEO]) as $media) { - if (Item::containsLink($body, $media['url'])) { + if (Item::containsLink($body, $media['url'], $media['type'])) { continue; } From 36c5e360f6d0a86c533395c8d68bde0be28eefb2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 23 May 2021 13:29:25 +0000 Subject: [PATCH 11/12] Fix missing media on posts to Mastodon --- src/Model/Post/Media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index c3f745fee6..7834fc4713 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -351,7 +351,7 @@ class Media foreach ($attachments as $attachment) { // Only store attachments that are part of the unshared body - if (strpos($unshared_body, $attachment['url']) !== false) { + if (Item::containsLink($unshared_body, $attachment['url'], $attachment['type'])) { self::insert($attachment); } } From 4ac68394c358f4a33fc42a5fa345a98ca69e55d5 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 23 May 2021 16:37:34 +0000 Subject: [PATCH 12/12] Fix warning "reset() expects parameter 1 to be array, null given" --- src/Object/Api/Mastodon/Status.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index eb88ce5f1a..974ac0dc74 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -114,8 +114,12 @@ class Status extends BaseDataTransferObject $this->visibility = $visibility[$item['private']]; $languages = json_decode($item['language'], true); - reset($languages); - $this->language = is_array($languages) ? key($languages) : null; + if (is_array($languages)) { + reset($languages); + $this->language = key($languages); + } else { + $this->language = null; + } $this->uri = $item['uri']; $this->url = $item['plink'] ?? null;