diff --git a/.codecov.yml b/.codecov.yml index ec27f44fe..7b6eecb66 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,7 +1,7 @@ codecov: branch: develop ci: - - drone.friendi.ca + - ci.friendi.ca coverage: precision: 2 diff --git a/.editorconfig b/.editorconfig index af67d04a3..610409143 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,9 +6,27 @@ root = true [*] charset = utf-8 end_of_line = lf -trim_trailing_whitespaces = true indent_style = tab +trim_trailing_whitespace = true +insert_final_newline = true quote_type = single +max_line_length = off [*.js] -quote_type = double \ No newline at end of file +quote_type = double +ij_javascript_use_double_quotes = true + +[*.yml] +indent_style = space +indent_size = 2 + +[*.xml] +indent_style = space +indent_size = 2 + +[*.json] +indent_style = space +indent_size = 2 + +[composer.json] +indent_style = tab diff --git a/composer.json b/composer.json index c6746c123..e4e6aa6f0 100644 --- a/composer.json +++ b/composer.json @@ -132,7 +132,13 @@ "test": "phpunit", "lint": "find . -name \\*.php -not -path './vendor/*' -not -path './view/asset/*' -print0 | xargs -0 -n1 php -l", "cs:install": "@composer install --working-dir=bin/dev/php-cs-fixer", - "cs:check": ["@cs:install", "bin/dev/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff"], - "cs:fix": ["@cs:install", "bin/dev/php-cs-fixer/vendor/bin/php-cs-fixer fix"] + "cs:check": [ + "@cs:install", + "bin/dev/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff" + ], + "cs:fix": [ + "@cs:install", + "bin/dev/php-cs-fixer/vendor/bin/php-cs-fixer fix" + ] } } diff --git a/doc/API-Entities.md b/doc/API-Entities.md index 23fc2cec4..213b2494f 100644 --- a/doc/API-Entities.md +++ b/doc/API-Entities.md @@ -908,6 +908,13 @@ Identical to [the Twitter Media Object](https://developer.twitter.com/en/docs/tw Resource ID (32 hex chars) + +media-id +String (Integer) +ID used for attaching images to a Mastodon Post Status + + + created String (Date) @@ -1001,6 +1008,14 @@ Mutually exclusive with data datasize. + +scales +Array of Photo Scales + +List of the various resized versions of the Photo + + + datasize Integer @@ -1040,6 +1055,58 @@ Mutually exclusive with link. +## Photo Scale + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeTypeNullable
idString (Integer)Row ID of this photo scale
scaleIntegerScale number
linkString (URL)URL to this scale's image
heightIntegerImage height in pixels
widthIntegerImage width in pixels
sizeIntegerImage size in bytes
+ + ## Photo List Item @@ -1103,6 +1170,40 @@ Mutually exclusive with link.
+ +## Photo Album + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeTypeDescription
nameStringThe name of the photo album
createdString (Date)The creation date of the album. Format YYYY-MM-DD HH:MM:SS
countIntegerThe number of images in the album
+ ## Private message diff --git a/doc/API-Friendica.md b/doc/API-Friendica.md index 8460fd4ab..a33cd53b8 100644 --- a/doc/API-Friendica.md +++ b/doc/API-Friendica.md @@ -665,8 +665,8 @@ On success: ```json { - "result": "updated", - "message":"album 'abc' with all containing photos has been renamed to 'xyz'." + "result": "updated", + "message":"album 'abc' with all containing photos has been renamed to 'xyz'." } ``` @@ -676,8 +676,92 @@ On error: * 400 BADREQUEST: "no albumname specified", "no new albumname specified", "album not available" * 500 INTERNALSERVERERROR: "unknown error - updating in database failed" +### GET api/friendica/photoalbums + +Get a list of photo albums for the user + +#### Parameters + +None +#### Return values + +On success a list of photo album objects: + +```json +[ + { + "name": "Wall Photos", + "created": "2023-01-22 02:03:19", + "count": 4 + }, + { + "name": "Profile photos", + "created": "2022-11-20 14:40:06", + "count": 1 + } +] +``` + +### GET api/friendica/photoalbum + +Get a list of images in a photo album +#### Parameters + +* `album` (Required): name of the album to be deleted +* `limit` (Optional): Maximum number of items to get, defaults to 50, max 500 +* `offset`(Optional): Offset in results to page through total items, defaults to 0 +* `latest_first` (Optional): Reverse the order so the most recent images are first, defaults to false + +#### Return values + +On success: + +* JSON return with the list of Photo items + +**Example:** +`https:///api/friendica/photoalbum?album=Wall Photos&limit=10&offset=2` + +```json +[ + { + "created": "2023-02-14 14:31:06", + "edited": "2023-02-14 14:31:14", + "title": "", + "desc": "", + "album": "Wall Photos", + "filename": "image.png", + "type": "image/png", + "height": 835, + "width": 693, + "datasize": 119523, + "profile": 0, + "allow_cid": "", + "deny_cid": "", + "allow_gid": "", + "deny_gid": "", + "id": "899184972463eb9b2ae3dc2580502826", + "scale": 0, + "media-id": 52, + "scales": [ + { + "id": 52, + "scale": 0, + "link": "https:///photo/899184972463eb9b2ae3dc2580502826-0.png", + "width": 693, + "height": 835, + "size": 119523 + }, + ... + ], + "thumb": "https:///photo/899184972463eb9b2ae3dc2580502826-2.png" + }, + ... +] +``` + --- + ### GET api/friendica/profile/show Returns the [Profile](help/API-Entities#Profile) data of the authenticated user. @@ -715,6 +799,127 @@ General description of profile data in API returns: --- +### POST api/friendica/statuses/:id/dislike + +Marks the given status as disliked by this user + +#### Path Parameter + +* `id`: the status ID that is being marked + +#### Return values + +A Mastodon [Status Entity](https://docs.joinmastodon.org/entities/Status/) + +#### Example: +`https:///api/friendica/statuses/341/dislike` + +```json +{ + "id": "341", + "created_at": "2023-02-23T01:50:00.000Z", + "in_reply_to_id": null, + "in_reply_to_status": null, + "in_reply_to_account_id": null, + "sensitive": false, + "spoiler_text": "", + "visibility": "public", + "language": "en", + ... + "account": { + "id": "8", + "username": "testuser2", + ... + }, + "media_attachments": [], + "mentions": [], + "tags": [], + "emojis": [], + "card": null, + "poll": null, + "friendica": { + "title": "", + "dislikes_count": 1 + } +} +``` + + +### GET api/friendica/statuses/:id/disliked_by + +Returns the list of accounts that have disliked the status as known by the current server + +#### Path Parameter + +* `id`: the status ID that is being marked + +#### Return values + +A list of [Mastodon Account](https://docs.joinmastodon.org/entities/Account/) objects +in the body and next/previous link headers in the header + +#### Example: +`https:///api/friendica/statuses/341/disliked_by` + +```json +[ + { + "id": "6", + "username": "testuser1", + ... + } +] +``` + + + +### POST api/friendica/statuses/:id/undislike + +Removes the dislike mark (if it exists) on this status for this user + +#### Path Parameter + +* `id`: the status ID that is being marked + +#### Return values + +A Mastodon [Status Entity](https://docs.joinmastodon.org/entities/Status/) + +#### Example: +`https:///api/friendica/statuses/341/dislike` + +```json +{ + "id": "341", + "created_at": "2023-02-23T01:50:00.000Z", + "in_reply_to_id": null, + "in_reply_to_status": null, + "in_reply_to_account_id": null, + "sensitive": false, + "spoiler_text": "", + "visibility": "public", + "language": "en", + ... + "account": { + "id": "8", + "username": "testuser2", + ... + }, + "media_attachments": [], + "mentions": [], + "tags": [], + "emojis": [], + "card": null, + "poll": null, + "friendica": { + "title": "", + "dislikes_count": 0 + } +} +``` + +--- + ## Deprecated endpoints - POST api/statuses/mediap diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index 03d6e3f26..5fcb4782c 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -30,6 +30,100 @@ For supported apps please have a look at the [FAQ](help/FAQ#clients) ## Entities These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/entities/). +With some additional extensions listed below. + +### Instance (Version 2) Entities +Extensions to the [Mastodon Instance::V2 Entities](https://docs.joinmastodon.org/entities/Instance/) +* `friendica`: Friendica specific properties of the V2 Instance including: + * `version`: The Friendica version string + * `codename`: The Friendica version code name + * `db_version`: The database schema version number + +Example: +```json +{ + "domain": "friendicadevtest1.myportal.social", + "title": "Friendica Social Network", + "version": "2.8.0 (compatible; Friendica 2023.03-dev)", + ... + "friendica": { + "version": "2023.03-dev", + "codename": "Giant Rhubarb", + "db_version": 1516 + } +} +``` + +### Notification Entities +Extensions to the [Mastodon Notification Entities](https://docs.joinmastodon.org/entities/Notification/) +* `dismissed`: whether the object has been dismissed or not + +### Status Entities +Extensions to the [Mastodon Status Entities](https://docs.joinmastodon.org/entities/Status/) +* `in_reply_to_status`: A fully populated Mastodon Status entity for the replied to status or null it is a post rather than a response +* `friendica`: Friendica specific properties of a status including: + * `title`: The Friendica title for a post, or empty if the status is a comment + * `delivery_data`: Information about the state of federating a message from the server + * `delivery_queue_count`: Total number of remote servers that the status needs to be federated to. + * `delivery_queue_done`: Total number of remote servers that have successfully been federated to so far. + * `delivery_queue_failed`: Total number of remote servers that have we failed to federate to so far. + * `dislikes_count`: The number of dislikes that a status has accumulated according to the server. + +Example: +```json +{ + "id": "358", + "created_at": "2023-02-23T02:45:46.000Z", + "in_reply_to_id": "356", + "in_reply_to_status": { + "id": "356", + "created_at": "2023-02-23T02:45:35.000Z", + "in_reply_to_id": null, + "in_reply_to_status": null, + "in_reply_to_account_id": null, + ... + "content": "A post from testuser1", + ... + "account": { + "id": "6", + "username": "testuser1", + "acct": "testuser1", + "display_name": "testuser1", + ... + }, + ... + "friendica": { + "title": "", + "dislikes_count": 0 + } + }, + "in_reply_to_account_id": "6", + ... + "replies_count": 0, + "reblogs_count": 0, + "favourites_count": 0, + ... + "content": "A reply from testuser2", + ... + "account": { + "id": "8", + "username": "testuser2", + "acct": "testuser2", + "display_name": "testuser2", + ... + }, + ... + "friendica": { + "title": "", + "delivery_data": { + "delivery_queue_count": 10, + "delivery_queue_done": 3, + "delivery_queue_failed": 0 + }, + "dislikes_count": 0 + } +} +``` ## Implemented endpoints @@ -73,8 +167,8 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - `:id` is a follow request ID, not a regular account id - Returns a [Relationship](https://docs.joinmastodon.org/entities/relationship) object. -- [`GET /api/v1/followed_tags'](https://docs.joinmastodon.org/methods/followed_tags/) -- [`GET /api/v1/instance`](https://docs.joinmastodon.org/methods/instance#fetch-instance) +- [`GET /api/v1/followed_tags`](https://docs.joinmastodon.org/methods/followed_tags/) +- [`GET /api/v1/instance`](https://docs.joinmastodon.org/methods/instance/#v1) - `GET /api/v1/instance/rules` Undocumented, returns Terms of Service - [`GET /api/v1/instance/peers`](https://docs.joinmastodon.org/methods/instance#list-of-connected-domains) - [`GET /api/v1/lists`](https://docs.joinmastodon.org/methods/timelines/lists/) @@ -92,6 +186,10 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`PUT /api/v1/media/:id`](https://docs.joinmastodon.org/methods/statuses/media/) - [`GET /api/v1/mutes`](https://docs.joinmastodon.org/methods/accounts/mutes/) - [`GET /api/v1/notifications`](https://docs.joinmastodon.org/methods/notifications/) + - Additional field `include_all` to return read and unread statuses, defaults to `false` + - Additional field `summary` returns a count of all of the statuses that match the type filter + - Additional field `with_muted` Pleroma extension to return notifications from muted users, defaults to `false` + - Does not support the `type` field, which is the mirror image of the supported `exclude_types` field - [`GET /api/v1/notifications/:id`](https://docs.joinmastodon.org/methods/notifications/) - [`POST /api/v1/notifications/clear`](https://docs.joinmastodon.org/methods/notifications/) - [`POST /api/v1/notifications/:id/dismiss`](https://docs.joinmastodon.org/methods/notifications/) @@ -106,11 +204,22 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`DELETE /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) - [`GET /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) - [`GET /api/v1/search`](https://docs.joinmastodon.org/methods/search/) +- [`PUT /api/v1/statuses`](https://docs.joinmastodon.org/methods/statuses/#edit) + - Does not support `polls` argument as Friendica does not have polls + - Additional fields `friendica` for Friendica specific parameters: + - `title`: Explicitly sets the title for a post status, ignored if used on a comment status. For post statuses the legacy behavior is to use any "spoiler text" as the title if it is provided. If both the title and spoiler text are provided for a post status then they will each be used for their respective roles. If no title is provided then the legacy behavior will persist. If you want to create a post with no title but spoiler text then explicitly set the title but set it to an empty string `""`. - [`POST /api/v1/statuses`](https://docs.joinmastodon.org/methods/statuses/#create) + - Does not support `polls` argument as Friendica does not have polls - Additionally to the static values `public`, `unlisted` and `private`, the `visibility` parameter can contain a numeric value with a group id. + - Additional field `quote_id` for the post that is being quote reshared + - Additional fields `friendica` for Friendica specific parameters: + - `title`: Explicitly sets the title for a post status, ignored if used on a comment status. For post statuses the legacy behavior is to use any "spoiler text" as the title if it is provided. If both the title and spoiler text are provided for a post status then they will each be used for their respective roles. If no title is provided then the legacy behavior will persist. If you want to create a post with no title but spoiler text then explicitly set the title but set it to an empty string `""`. - [`GET /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/#get) - [`DELETE /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/#delete) - [`GET /api/v1/statuses/:id/context`](https://docs.joinmastodon.org/methods/statuses/#context) + - Additional support for paging using `min_id`, `max_id`, `since_id` parameters + - Additional support for previous/next Link Headers to support paging + - Additional flag `show_all` to allow including posts from blocked and ignored/muted users, defaults to `false` - [`GET /api/v1/statuses/:id/reblogged_by`](https://docs.joinmastodon.org/methods/statuses/#reblogged_by) - [`GET /api/v1/statuses/:id/favourited_by`](https://docs.joinmastodon.org/methods/statuses/#favourited_by) - [`POST /api/v1/statuses/:id/favourite`](https://docs.joinmastodon.org/methods/statuses/#favourite) @@ -132,13 +241,24 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`GET /api/v1/tags/:id/unfollow`](https://docs.joinmastodon.org/methods/tags/#unfollow) - [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/home`](https://docs.joinmastodon.org/methods/timelines/) + - Additional field `with_muted` Pleroma extension to return notifications from muted users, defaults to `false` + - Additional field `exclude_replies` to only return post statuses not replies/comments, defaults to `false` - [`GET /api/v1/timelines/list/:id`](https://docs.joinmastodon.org/methods/timelines/) + - Additional field `with_muted` Pleroma extension to return notifications from muted users, defaults to `false` + - Additional field `exclude_replies` to only return post statuses not replies/comments, defaults to `false` - [`GET /api/v1/timelines/public`](https://docs.joinmastodon.org/methods/timelines/) + - Additional field `with_muted` Pleroma extension to return notifications from muted users, defaults to `false` + - Additional field `exclude_replies` to only return post statuses not replies/comments, defaults to `false` - [`GET /api/v1/timelines/tag/:hashtag`](https://docs.joinmastodon.org/methods/timelines/) + - Additional field `with_muted` Pleroma extension to return notifications from muted users, defaults to `false` + - Additional field `exclude_replies` to only return post statuses not replies/comments, defaults to `false` + - Does not support the `any[]`, `all[]`, or `none[]` query parameters - [`GET /api/v1/trends`](https://docs.joinmastodon.org/methods/instance/trends/) - [`GET /api/v1/trends/links`](https://github.com/mastodon/mastodon/pull/16917) - [`GET /api/v1/trends/statuses`](https://docs.joinmastodon.org/methods/trends/#statuses) - [`GET /api/v1/trends/tags`](https://docs.joinmastodon.org/methods/trends/#tags) + - Additional field `friendica_local` to return local trending tags instead of global tags, defaults to `false` +- [`GET /api/v2/instance`](https://docs.joinmastodon.org/methods/instance/#v2) - [`GET /api/v2/search`](https://docs.joinmastodon.org/methods/search/) diff --git a/docblox.dist.xml b/docblox.dist.xml index a2ea8e2cb..d702bc888 100644 --- a/docblox.dist.xml +++ b/docblox.dist.xml @@ -10,4 +10,3 @@ . - diff --git a/mod/item.php b/mod/item.php index 13ffe7568..77cfc4795 100644 --- a/mod/item.php +++ b/mod/item.php @@ -41,10 +41,8 @@ use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\ItemURI; -use Friendica\Model\Photo; use Friendica\Model\Post; use Friendica\Network\HTTPException; -use Friendica\Protocol\Activity; use Friendica\Util\DateTimeFormat; function item_post(App $a) { @@ -108,6 +106,7 @@ function item_edit(int $uid, array $request, bool $preview, string $return_path) $post['edit'] = $post; $post['file'] = Post\Category::getTextByURIId($post['uri-id'], $post['uid']); + Post\Media::deleteByURIId($post['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]); $post = item_process($post, $request, $preview, $return_path); $fields = [ diff --git a/mods/README.md b/mods/README.md index fe7a7fed5..43389eb2c 100644 --- a/mods/README.md +++ b/mods/README.md @@ -4,12 +4,12 @@ Friendica mods files ## `bookmarklet-share2friendica` Browser bookmarklet to share any page with your Friendica account. -Please see `bookmarklet-share2friendica/README.md` for detailed instruction. +Please see `bookmarklet-share2friendica/README.md` for detailed instruction. ## `fpostit` Node-agnostic Friendica bookmarklet by Devlon Duthie. -Unmaintained and unsupported. +Unmaintained and unsupported. ## `home.css` and `home.html` @@ -30,7 +30,11 @@ Please check software documentation to know how modify these examples to make th ## `sample-systemd.timer` and `sample-systemd.service` Sample systemd unit files to start worker.php periodically. - + Please place them in the correct location for your system, typically this is `/etc/systemd/system/friendicaworker.timer` and `/etc/systemd/system/friendicaworker.service`. Please report problems and improvements to `!helpers@forum.friendi.ca` and `@utzer@social.yl.ms` or open an issue in [the Github Friendica page](https://github.com/friendica/friendica/issues). This is for usage of systemd instead of cron to start the worker periodically, the solution is a work-in-progress and can surely be improved. + +## `phpstorm-code-style.xml` + +PHP Storm Code Style settings, used for this codebase diff --git a/mods/phpstorm-code-style.xml b/mods/phpstorm-code-style.xml new file mode 100644 index 000000000..0fb86cfb0 --- /dev/null +++ b/mods/phpstorm-code-style.xml @@ -0,0 +1,34 @@ + + + + + + + + \ No newline at end of file diff --git a/ruleset.xml b/ruleset.xml index a71b7062f..037b0e7b3 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -1,15 +1,15 @@ - Friendica Coding Standards: PSR2 with tabs instead of spaces - - - - - - - - - - - + Friendica Coding Standards: PSR2 with tabs instead of spaces + + + + + + + + + + + diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index e039a5279..299c26399 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -73,7 +73,12 @@ class Avatar return $fields; } - $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + try { + $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + } catch (\Exception $exception) { + Logger::notice('Avatar is invalid', ['avatar' => $avatar, 'exception' => $exception]); + return $fields; + } $img_str = $fetchResult->getBody(); if (empty($img_str)) { diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 6730309e6..949f9d9cf 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -437,7 +437,7 @@ class BBCode * @param boolean $no_link_desc No link description * @return string with replaced body */ - public static function removeAttachment(string $body, bool $no_link_desc = false): string + public static function replaceAttachment(string $body, bool $no_link_desc = false): string { return preg_replace_callback("/\s*\[attachment (.*?)\](.*?)\[\/attachment\]\s*/ism", function ($match) use ($body, $no_link_desc) { @@ -454,6 +454,17 @@ class BBCode }, $body); } + /** + * Remove [attachment] BBCode + * + * @param string $body + * @return string with removed attachment + */ + public static function removeAttachment(string $body): string + { + return trim(preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body)); + } + /** * Converts a BBCode text into plaintext * @@ -469,7 +480,7 @@ class BBCode $text = preg_replace("/\[img.*?\[\/img\]/ism", ' ', $text); // Remove attachment - $text = self::removeAttachment($text); + $text = self::replaceAttachment($text); $naked_text = HTML::toPlaintext(self::convert($text, false, 0, true), 0, !$keep_urls); @@ -1583,9 +1594,9 @@ class BBCode /// @todo Have a closer look at the different html modes // Handle attached links or videos if (in_array($simple_html, [self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) { - $text = self::removeAttachment($text); + $text = self::replaceAttachment($text); } elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) { - $text = self::removeAttachment($text, true); + $text = self::replaceAttachment($text, true); } else { $text = self::convertAttachment($text, $simple_html, $try_oembed, [], $uriid); } diff --git a/src/Core/Storage/Repository/StorageManager.php b/src/Core/Storage/Repository/StorageManager.php index fbe910d01..252454665 100644 --- a/src/Core/Storage/Repository/StorageManager.php +++ b/src/Core/Storage/Repository/StorageManager.php @@ -168,7 +168,7 @@ class StorageManager return $data['storage_config']; } catch (InternalServerErrorException $exception) { - throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception); + throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception->__toString()); } } } @@ -208,7 +208,7 @@ class StorageManager $this->backendInstances[$name] = new Type\SystemResource(); break; case Type\ExternalResource::getName(): - $this->backendInstances[$name] = new Type\ExternalResource(); + $this->backendInstances[$name] = new Type\ExternalResource($this->logger); break; default: $data = [ @@ -223,7 +223,7 @@ class StorageManager $this->backendInstances[$data['name'] ?? $name] = $data['storage']; } catch (InternalServerErrorException $exception) { - throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception); + throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception->__toString()); } break; } diff --git a/src/Core/Storage/Type/ExternalResource.php b/src/Core/Storage/Type/ExternalResource.php index 055db0dea..181312a5b 100644 --- a/src/Core/Storage/Type/ExternalResource.php +++ b/src/Core/Storage/Type/ExternalResource.php @@ -22,12 +22,12 @@ namespace Friendica\Core\Storage\Type; use Exception; -use Friendica\Core\Logger; use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Core\Storage\Capability\ICanReadFromStorage; use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Util\HTTPSignature; +use Psr\Log\LoggerInterface; /** * External resource storage class @@ -39,6 +39,14 @@ class ExternalResource implements ICanReadFromStorage { const NAME = 'ExternalResource'; + /** @var LoggerInterface */ + protected $logger; + + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } + /** * @inheritDoc */ @@ -57,10 +65,11 @@ class ExternalResource implements ICanReadFromStorage try { $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); } catch (Exception $exception) { + $this->logger->notice('URL is invalid', ['url' => $data->url, 'error' => $exception]); throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception); } if (!empty($fetchResult) && $fetchResult->isSuccess()) { - Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => $data->uid, 'url' => $data->url]); + $this->logger->debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => $data->uid, 'url' => $data->url]); return $fetchResult->getBody(); } else { if (empty($fetchResult)) { diff --git a/src/Factory/Api/Mastodon/Notification.php b/src/Factory/Api/Mastodon/Notification.php index 52241e1fc..e6e9f4d09 100644 --- a/src/Factory/Api/Mastodon/Notification.php +++ b/src/Factory/Api/Mastodon/Notification.php @@ -64,7 +64,7 @@ class Notification extends BaseFactory if ($Notification->targetUriId) { try { $status = $this->mstdnStatusFactory->createFromUriId($Notification->targetUriId, $Notification->uid, $display_quotes); - } catch (\Throwable $th) { + } catch (\Exception $exception) { $status = null; } } else { diff --git a/src/Factory/Api/Mastodon/Relationship.php b/src/Factory/Api/Mastodon/Relationship.php index 3c9dca302..f1ca4a1f9 100644 --- a/src/Factory/Api/Mastodon/Relationship.php +++ b/src/Factory/Api/Mastodon/Relationship.php @@ -38,14 +38,14 @@ class Relationship extends BaseFactory public function createFromContactId(int $contactId, int $uid): RelationshipEntity { $cdata = Contact::getPublicAndUserContactID($contactId, $uid); - if (!empty($cdata)) { - $cid = $cdata['user']; - $pcid = $cdata['public']; - } else { - $pcid = $cid = $contactId; - } + $pcid = !empty($cdata['public']) ? $cdata['public'] : $contactId; + $cid = !empty($cdata['user']) ? $cdata['user'] : $contactId; - return new RelationshipEntity($pcid, Contact::getById($cid), - Contact\User::isBlocked($cid, $uid), Contact\User::isIgnored($cid, $uid)); + return new RelationshipEntity( + $pcid, + Contact::getById($cid), + Contact\User::isBlocked($cid, $uid), + Contact\User::isIgnored($cid, $uid) + ); } } diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 788841a9b..19f3866cd 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -33,6 +33,8 @@ use Friendica\Model\Post; use Friendica\Model\Tag as TagModel; use Friendica\Model\Verb; use Friendica\Network\HTTPException; +use Friendica\Object\Api\Mastodon\Status\FriendicaDeliveryData; +use Friendica\Object\Api\Mastodon\Status\FriendicaExtension; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use ImagickException; @@ -97,7 +99,8 @@ class Status extends BaseFactory public function createFromUriId(int $uriId, int $uid = 0, bool $display_quote = false, bool $reblog = true, bool $in_reply_status = true): \Friendica\Object\Api\Mastodon\Status { $fields = ['uri-id', 'uid', 'author-id', 'causer-id', 'author-uri-id', 'author-link', 'causer-uri-id', 'post-reason', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id', - 'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id']; + 'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id', + 'delivery_queue_count', 'delivery_queue_done','delivery_queue_failed']; $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]); if (!$item) { $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]); @@ -266,8 +269,8 @@ class Status extends BaseFactory if ($is_reshare) { try { $reshare = $this->createFromUriId($uriId, $uid, $display_quote, false, false)->toArray(); - } catch (\Throwable $th) { - Logger::info('Reshare not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Reshare not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); $reshare = []; } } else { @@ -277,15 +280,18 @@ class Status extends BaseFactory if ($in_reply_status && ($item['gravity'] == Item::GRAVITY_COMMENT)) { try { $in_reply = $this->createFromUriId($item['thr-parent-id'], $uid, $display_quote, false, false)->toArray(); - } catch (\Throwable $th) { - Logger::info('Reply post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Reply post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); $in_reply = []; } } else { $in_reply = []; } - return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $quote, $poll); + $delivery_data = new FriendicaDeliveryData($item['delivery_queue_count'], $item['delivery_queue_done'], $item['delivery_queue_failed']); + $friendica = new FriendicaExtension($item['title'], $counts->dislikes, $delivery_data); + + return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $friendica, $quote, $poll); } /** @@ -309,8 +315,8 @@ class Status extends BaseFactory if (!empty($quote_id)) { try { $quote = $this->createFromUriId($quote_id, $uid, false, false, false)->toArray(); - } catch (\Throwable $th) { - Logger::info('Quote not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Quote not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); $quote = []; } } else { @@ -349,7 +355,8 @@ class Status extends BaseFactory $attachments = []; $in_reply = []; $reshare = []; + $friendica = new FriendicaExtension('', 0, new FriendicaDeliveryData(0, 0, 0)); - return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare); + return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $friendica); } } diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 215d7e317..20457b46e 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -189,17 +189,22 @@ class APContact if (empty($data)) { $local_owner = []; - $curlResult = HTTPSignature::fetchRaw($url); - $failed = empty($curlResult) || empty($curlResult->getBody()) || - (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410)); + try { + $curlResult = HTTPSignature::fetchRaw($url); + $failed = empty($curlResult) || empty($curlResult->getBody()) || + (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410)); + + if (!$failed) { + $data = json_decode($curlResult->getBody(), true); + $failed = empty($data) || !is_array($data); + } - if (!$failed) { - $data = json_decode($curlResult->getBody(), true); - $failed = empty($data) || !is_array($data); - } - - if (!$failed && ($curlResult->getReturnCode() == 410)) { - $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone']; + if (!$failed && ($curlResult->getReturnCode() == 410)) { + $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone']; + } + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]); + $failed = true; } if ($failed) { diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 571253f42..f35808f05 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2216,16 +2216,21 @@ class Contact if (($contact['avatar'] != $avatar) || empty($contact['blurhash'])) { $update_fields = ['avatar' => $avatar]; if (!Network::isLocalLink($avatar)) { - $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + try { + $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); - $img_str = $fetchResult->getBody(); - if (!empty($img_str)) { - $image = new Image($img_str, Images::getMimeTypeByData($img_str)); - if ($image->isValid()) { - $update_fields['blurhash'] = $image->getBlurHash(); - } else { - return; + $img_str = $fetchResult->getBody(); + if (!empty($img_str)) { + $image = new Image($img_str, Images::getMimeTypeByData($img_str)); + if ($image->isValid()) { + $update_fields['blurhash'] = $image->getBlurHash(); + } else { + return; + } } + } catch (\Exception $exception) { + Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'exception' => $exception]); + return; } } elseif (!empty($contact['blurhash'])) { $update_fields['blurhash'] = null; diff --git a/src/Model/Item.php b/src/Model/Item.php index a62d3c2e6..78275c835 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -210,8 +210,6 @@ class Item } } - Post\Media::insertFromAttachmentData($item['uri-id'], $fields['body']); - $content_fields = ['raw-body' => trim($fields['raw-body'] ?? $fields['body'])]; // Remove all media attachments from the body and store them in the post-media table @@ -220,6 +218,10 @@ class Item $content_fields['raw-body'] = self::setHashtags($content_fields['raw-body']); Post\Media::insertFromRelevantUrl($item['uri-id'], $content_fields['raw-body'], $fields['body'], $item['author-network']); + + Post\Media::insertFromAttachmentData($item['uri-id'], $fields['body']); + $content_fields['raw-body'] = BBCode::removeAttachment($content_fields['raw-body']); + Post\Content::update($item['uri-id'], $content_fields); } @@ -1143,8 +1145,6 @@ class Item $item['body'] = BBCode::removeSharedData($item['body']); } - Post\Media::insertFromAttachmentData($item['uri-id'], $item['body']); - // Remove all media attachments from the body and store them in the post-media table $item['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $item['raw-body']); $item['raw-body'] = self::setHashtags($item['raw-body']); @@ -1152,6 +1152,10 @@ class Item $author = Contact::getById($item['author-id'], ['network']); Post\Media::insertFromRelevantUrl($item['uri-id'], $item['raw-body'], $item['body'], $author['network'] ?? ''); + Post\Media::insertFromAttachmentData($item['uri-id'], $item['body']); + $item['body'] = BBCode::removeAttachment($item['body']); + $item['raw-body'] = BBCode::removeAttachment($item['raw-body']); + // Check for hashtags in the body and repair or add hashtag links $item['body'] = self::setHashtags($item['body']); @@ -3003,8 +3007,9 @@ class Item $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media', 'quote-uri-id', 'post-type']; - $shared_uri_id = 0; - $shared_links = []; + $shared_uri_id = 0; + $shared_links = []; + $quote_shared_links = []; $shared = DI::contentItem()->getSharedPost($item, $fields); if (!empty($shared['post'])) { @@ -3023,7 +3028,14 @@ class Item $shared_links[] = strtolower($media[0]['url']); } - $quote_uri_id = $shared_item['uri-id'] ?? 0; + if (!empty($shared_item['uri-id'])) { + $data = BBCode::getAttachmentData($shared_item['body']); + if (!empty($data['url'])) { + $quote_shared_links[] = $data['url']; + } + + $quote_uri_id = $shared_item['uri-id']; + } } } @@ -3098,7 +3110,7 @@ class Item if (!empty($shared_attachments)) { $s = self::addVisualAttachments($shared_attachments, $shared_item, $s, true); - $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $shared_attachments, $body, $s, true, []); + $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $shared_attachments, $body, $s, true, $quote_shared_links); $s = self::addNonVisualAttachments($shared_attachments, $item, $s, true); $body = BBCode::removeSharedData($body); } @@ -3418,7 +3430,7 @@ class Item DI::profiler()->stopRecording(); if (isset($data['url']) && !in_array(strtolower($data['url']), $ignore_links)) { - if (!empty($data['description']) || !empty($data['image']) || !empty($data['preview'])) { + if (!empty($data['description']) || !empty($data['image']) || !empty($data['preview']) || (!empty($data['title']) && !Strings::compareLink($data['title'], $data['url']))) { $parts = parse_url($data['url']); if (!empty($parts['scheme']) && !empty($parts['host'])) { if (empty($data['provider_name'])) { diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index 343ad815c..dc7797951 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -125,8 +125,13 @@ class Link { $timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]); - if (empty($curlResult) || !$curlResult->isSuccess()) { + try { + $curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]); + if (empty($curlResult) || !$curlResult->isSuccess()) { + return []; + } + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]); return []; } $fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]]; diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index 8216d7383..653bdba98 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -68,18 +68,18 @@ class Media * * @param array $media * @param bool $force - * @return void + * @return bool */ - public static function insert(array $media, bool $force = false) + public static function insert(array $media, bool $force = false): bool { if (empty($media['url']) || empty($media['uri-id']) || !isset($media['type'])) { Logger::warning('Incomplete media data', ['media' => $media]); - return; + return false; } if (DBA::exists('post-media', ['uri-id' => $media['uri-id'], 'preview' => $media['url']])) { Logger::info('Media already exists as preview', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]); - return; + return false; } // "document" has got the lowest priority. So when the same file is both attached as document @@ -87,12 +87,12 @@ class Media $found = DBA::selectFirst('post-media', ['type'], ['uri-id' => $media['uri-id'], 'url' => $media['url']]); if (!$force && !empty($found) && (($found['type'] != self::DOCUMENT) || ($media['type'] == self::DOCUMENT))) { Logger::info('Media already exists', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]); - return; + return false; } if (!ItemURI::exists($media['uri-id'])) { Logger::info('Media referenced URI ID not found', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]); - return; + return false; } $media = self::unsetEmptyFields($media); @@ -114,6 +114,7 @@ class Media } else { Logger::info('Nothing to update', ['media' => $media]); } + return $result; } /** @@ -573,9 +574,14 @@ class Media if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) { foreach ($matches[1] as $url) { Logger::info('Got page url (link without description)', ['uri-id' => $uriid, 'url' => $url]); - self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network); - if ($network == Protocol::DFRN) { + $result = self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network); + if ($result && ($network == Protocol::DFRN)) { self::revertHTMLType($uriid, $url, $fullbody); + Logger::debug('Revert HTML type', ['uri-id' => $uriid, 'url' => $url]); + } elseif ($result) { + Logger::debug('Media had been added', ['uri-id' => $uriid, 'url' => $url]); + } else { + Logger::debug('Media had not been added', ['uri-id' => $uriid, 'url' => $url]); } } } @@ -584,9 +590,14 @@ class Media if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) { foreach ($matches[1] as $url) { Logger::info('Got page url (link with description)', ['uri-id' => $uriid, 'url' => $url]); - self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network); - if ($network == Protocol::DFRN) { + $result = self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network); + if ($result && ($network == Protocol::DFRN)) { self::revertHTMLType($uriid, $url, $fullbody); + Logger::debug('Revert HTML type', ['uri-id' => $uriid, 'url' => $url]); + } elseif ($result) { + Logger::debug('Media has been added', ['uri-id' => $uriid, 'url' => $url]); + } else { + Logger::debug('Media has not been added', ['uri-id' => $uriid, 'url' => $url]); } } } @@ -705,6 +716,25 @@ class Media return DBA::exists('post-media', $condition); } + /** + * Delete media by uri-id and media type + * + * @param int $uri_id URI id + * @param array $types Media types + * @return bool Whether media attachment exists + * @throws \Exception + */ + public static function deleteByURIId(int $uri_id, array $types = []): bool + { + $condition = ['uri-id' => $uri_id]; + + if (!empty($types)) { + $condition = DBA::mergeConditions($condition, ['type' => $types]); + } + + return DBA::delete('post-media', $condition); + } + /** * Split the attachment media in the three segments "visual", "link" and "additional" * @@ -830,7 +860,7 @@ class Media } $original_body = $body; - $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body); + $body = BBCode::removeAttachment($body); foreach (self::getByURIId($uriid, $types) as $media) { if (Item::containsLink($body, $media['preview'] ?? $media['url'], $media['type'])) { diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index 067e33027..463411745 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -120,8 +120,8 @@ class Statuses extends BaseApi self::setBoundaries($item['uri-id']); try { $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Bookmarks.php b/src/Module/Api/Mastodon/Bookmarks.php index 615bfc501..59cf7f54a 100644 --- a/src/Module/Api/Mastodon/Bookmarks.php +++ b/src/Module/Api/Mastodon/Bookmarks.php @@ -77,8 +77,8 @@ class Bookmarks extends BaseApi self::setBoundaries($item['uri-id']); try { $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Favourited.php b/src/Module/Api/Mastodon/Favourited.php index 93c9ef243..7829f37d7 100644 --- a/src/Module/Api/Mastodon/Favourited.php +++ b/src/Module/Api/Mastodon/Favourited.php @@ -79,8 +79,8 @@ class Favourited extends BaseApi self::setBoundaries($item['thr-parent-id']); try { $statuses[] = DI::mstdnStatus()->createFromUriId($item['thr-parent-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $item['thr-parent-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $item['thr-parent-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index 667cd87ea..e19dfb031 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -117,7 +117,7 @@ class Accounts extends BaseApi self::setBoundaries($member['contact-id']); try { $accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid); - } catch (\Throwable $th) { + } catch (\Exception $exception) { } } DBA::close($members); diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index c35e33062..69e262768 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -183,8 +183,8 @@ class Search extends BaseApi self::setBoundaries($item['uri-id']); try { $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Statuses/Context.php b/src/Module/Api/Mastodon/Statuses/Context.php index 76677de4a..f683b0175 100644 --- a/src/Module/Api/Mastodon/Statuses/Context.php +++ b/src/Module/Api/Mastodon/Statuses/Context.php @@ -45,10 +45,11 @@ class Context extends BaseApi } $request = $this->getRequest([ - 'max_id' => 0, // Return results older than this id - 'since_id' => 0, // Return results newer than this id - 'min_id' => 0, // Return results immediately newer than this id - 'limit' => 40, // Maximum number of results to return. Defaults to 40. + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'min_id' => 0, // Return results immediately newer than this id + 'limit' => 40, // Maximum number of results to return. Defaults to 40. + 'show_all' => false, // shows posts for all users including blocked and ignored users ], $request); $id = $this->parameters['id']; @@ -73,6 +74,13 @@ class Context extends BaseApi $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); $params['order'] = ['uri-id']; } + + if (!empty($uid) && !$request['show_all']) { + $condition = DBA::mergeConditions( + $condition, + ["NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid] + ); + } $posts = Post::selectPosts(['uri-id', 'thr-parent-id'], $condition, $params); while ($post = Post::fetch($posts)) { diff --git a/src/Module/Api/Mastodon/Timelines/ListTimeline.php b/src/Module/Api/Mastodon/Timelines/ListTimeline.php index a8de13056..1e9a5810c 100644 --- a/src/Module/Api/Mastodon/Timelines/ListTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/ListTimeline.php @@ -104,8 +104,8 @@ class ListTimeline extends BaseApi self::setBoundaries($item['uri-id']); try { $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 968e34dbb..31750d4a5 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -99,8 +99,8 @@ class PublicTimeline extends BaseApi self::setBoundaries($item['uri-id']); try { $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php index 64cd84366..476c9c2b9 100644 --- a/src/Module/Api/Mastodon/Timelines/Tag.php +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -120,8 +120,8 @@ class Tag extends BaseApi self::setBoundaries($item['uri-id']); try { $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Trends/Statuses.php b/src/Module/Api/Mastodon/Trends/Statuses.php index 884319aa4..336f67155 100644 --- a/src/Module/Api/Mastodon/Trends/Statuses.php +++ b/src/Module/Api/Mastodon/Trends/Statuses.php @@ -57,8 +57,8 @@ class Statuses extends BaseApi while ($status = Post::fetch($statuses)) { try { $trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id'], $uid, $display_quotes); - } catch (\Throwable $th) { - Logger::info('Post not fetchable', ['uri-id' => $status['uri-id'], 'uid' => $uid, 'error' => $th]); + } catch (\Exception $exception) { + Logger::info('Post not fetchable', ['uri-id' => $status['uri-id'], 'uid' => $uid, 'exception' => $exception]); } } DBA::close($statuses); diff --git a/src/Module/Post/Edit.php b/src/Module/Post/Edit.php index 55c86988a..2ca980381 100644 --- a/src/Module/Post/Edit.php +++ b/src/Module/Post/Edit.php @@ -115,6 +115,8 @@ class Edit extends BaseModule $lockstate = 'unlock'; } + $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']); + $jotplugins = ''; Hook::callAll('jot_tool', $jotplugins); diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index ab129a82c..4d1e1c304 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -83,13 +83,18 @@ class Proxy extends BaseModule $request['url'] = str_replace(' ', '+', $request['url']); // Fetch the content with the local user - $fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]); - $img_str = $fetchResult->getBody(); + try { + $fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]); + $img_str = $fetchResult->getBody(); - if (!$fetchResult->isSuccess() || empty($img_str)) { - Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]); + if (!$fetchResult->isSuccess() || empty($img_str)) { + Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]); + self::responseError(); + // stop. + } + } catch (\Exception $exception) { + Logger::notice('Error fetching image', ['image' => $request['url'], 'exception' => $exception]); self::responseError(); - // stop. } Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => DI::userSession()->getLocalUserId(), 'image' => $request['url']]); diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php index 5459694e6..118d491fc 100644 --- a/src/Object/Api/Mastodon/Instance.php +++ b/src/Object/Api/Mastodon/Instance.php @@ -84,14 +84,14 @@ class Instance extends BaseDataTransferObject { $register_policy = intval($config->get('config', 'register_policy')); - $this->uri = $baseUrl; + $this->uri = $baseUrl->getHost(); $this->title = $config->get('config', 'sitename'); $this->short_description = $this->description = $config->get('config', 'info'); $this->email = implode(',', User::getAdminEmailList()); $this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; $this->urls = null; // Not supported $this->stats = new Stats($config, $database); - $this->thumbnail = $baseUrl . 'images/friendica-banner.jpg'; + $this->thumbnail = $baseUrl . '/images/friendica-banner.jpg'; $this->languages = [$config->get('system', 'language')]; $this->max_toot_chars = (int)$config->get('config', 'api_import_size', $config->get('config', 'max_import_size')); $this->registrations = ($register_policy != Register::CLOSED); diff --git a/src/Object/Api/Mastodon/InstanceV2/Contact.php b/src/Object/Api/Mastodon/InstanceV2/Contact.php index f48b027cf..74a6f8e24 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Contact.php +++ b/src/Object/Api/Mastodon/InstanceV2/Contact.php @@ -39,9 +39,9 @@ class Contact extends BaseDataTransferObject /** * @param string $email - * @param Account $account + * @param Account|null $account */ - public function __construct(string $email, Account $account) + public function __construct(string $email, ?Account $account) { $this->email = $email; $this->account = $account; diff --git a/src/Object/Api/Mastodon/InstanceV2/UserStats.php b/src/Object/Api/Mastodon/InstanceV2/UserStats.php index 8f398e0d5..afe6048e5 100644 --- a/src/Object/Api/Mastodon/InstanceV2/UserStats.php +++ b/src/Object/Api/Mastodon/InstanceV2/UserStats.php @@ -31,13 +31,13 @@ use Friendica\BaseDataTransferObject; class UserStats extends BaseDataTransferObject { /** @var int */ - protected $active_monthly = 0; + protected $active_month = 0; /** - * @param $active_monthly + * @param int $active_month */ - public function __construct($active_monthly) + public function __construct(int $active_month) { - $this->active_monthly = $active_monthly; + $this->active_month = $active_month; } } diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index 3fbd009a6..cefae5d21 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -105,7 +105,7 @@ class Status extends BaseDataTransferObject * @param array $item * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $in_reply, array $reblog, array $quote = null, array $poll = null) + public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $in_reply, array $reblog, FriendicaExtension $friendica, array $quote = null, array $poll = null) { $this->id = (string)$item['uri-id']; $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::JSON); @@ -151,7 +151,7 @@ class Status extends BaseDataTransferObject $this->emojis = []; $this->card = $card->toArray() ?: null; $this->poll = $poll; - $this->friendica = new FriendicaExtension($item['title'], $counts->dislikes); + $this->friendica = $friendica; } /** diff --git a/src/Object/Api/Mastodon/Status/FriendicaDeliveryData.php b/src/Object/Api/Mastodon/Status/FriendicaDeliveryData.php new file mode 100644 index 000000000..3da0eae57 --- /dev/null +++ b/src/Object/Api/Mastodon/Status/FriendicaDeliveryData.php @@ -0,0 +1,55 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\Status; + +use Friendica\BaseDataTransferObject; + +/** + * Class FriendicaDeliveryData + * + * Additional fields on Mastodon Statuses for storing Friendica delivery data + * + * @see https://docs.joinmastodon.org/entities/status + */ +class FriendicaDeliveryData extends BaseDataTransferObject +{ + /** @var int|null */ + protected $delivery_queue_count; + + /** @var int|null */ + protected $delivery_queue_done; + + /** @var int|null */ + protected $delivery_queue_failed; + + /** + * Creates a FriendicaDeliveryData object + * + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public function __construct(?int $delivery_queue_count, ?int $delivery_queue_done, ?int $delivery_queue_failed) + { + $this->delivery_queue_count = $delivery_queue_count; + $this->delivery_queue_done = $delivery_queue_done; + $this->delivery_queue_failed = $delivery_queue_failed; + } +} diff --git a/src/Object/Api/Mastodon/Status/FriendicaExtension.php b/src/Object/Api/Mastodon/Status/FriendicaExtension.php index 1db70f731..b2853894a 100644 --- a/src/Object/Api/Mastodon/Status/FriendicaExtension.php +++ b/src/Object/Api/Mastodon/Status/FriendicaExtension.php @@ -35,6 +35,8 @@ class FriendicaExtension extends BaseDataTransferObject /** @var string */ protected $title; + /** @var FriendicaDeliveryData */ + protected $delivery_data; /** @var int */ protected $dislikes_count; @@ -42,11 +44,13 @@ class FriendicaExtension extends BaseDataTransferObject * Creates a status count object * * @param string $title - * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @param int $dislikes_count + * @param FriendicaDeliveryData $delivery_data */ - public function __construct(string $title, int $dislikes_count) + public function __construct(string $title, int $dislikes_count, FriendicaDeliveryData $delivery_data) { $this->title = $title; + $this->delivery_data = $delivery_data; $this->dislikes_count = $dislikes_count; } } diff --git a/src/Object/Image.php b/src/Object/Image.php index d6c897e88..49196a0c4 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -718,7 +718,7 @@ class Image if ($image->isImagick()) { try { $colors = $image->image->getImagePixelColor($x, $y)->getColor(); - } catch (\Throwable $th) { + } catch (\Exception $exception) { return ''; } $row[] = [$colors['r'], $colors['g'], $colors['b']]; diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 11267cfaf..4572bdaa6 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -242,6 +242,7 @@ class Processor $item['changed'] = DateTimeFormat::utcNow(); $item['edited'] = DateTimeFormat::utc($activity['updated']); + Post\Media::deleteByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]); $item = self::processContent($activity, $item); if (empty($item)) { Queue::remove($activity); @@ -570,7 +571,12 @@ class Processor */ public static function isActivityGone(string $url): bool { - $curlResult = HTTPSignature::fetchRaw($url, 0); + try { + $curlResult = HTTPSignature::fetchRaw($url, 0); + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]); + return true; + } if (Network::isUrlBlocked($url)) { return true; diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 02dc7ff54..9123ae533 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1675,7 +1675,7 @@ class Transmitter if ($type == 'Page') { // When we transmit "Page" posts we have to remove the attachment. // The attachment contains the link that we already transmit in the "url" field. - $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body); + $body = BBCode::removeAttachment($body); } $body = BBCode::setMentionsToNicknames($body); @@ -1707,7 +1707,7 @@ class Transmitter $richbody = DI::contentItem()->addSharedPost($item, $richbody); } } - $richbody = BBCode::removeAttachment($richbody); + $richbody = BBCode::replaceAttachment($richbody); $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL); } diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index f082fe32f..d48d02c20 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -422,7 +422,12 @@ class HTTPSignature */ public static function fetch(string $request, int $uid): array { - $curlResult = self::fetchRaw($request, $uid); + try { + $curlResult = self::fetchRaw($request, $uid); + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $request, 'exception' => $exception]); + return []; + } if (empty($curlResult)) { return [];