| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // {{{ License
 | 
					
						
							|  |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ActivityPub implementation for GNU social | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package   GNUsocial | 
					
						
							|  |  |  |  * @category  ActivityPub | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |  * @author    Diogo Peralta Cordeiro <@diogo.site> | 
					
						
							|  |  |  |  * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Plugin\ActivityPub\Util\Model; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  | use ActivityPhp\Type; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use ActivityPhp\Type\AbstractObject; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  | use App\Core\Cache; | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Core\Event; | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  | use App\Core\GSFile; | 
					
						
							|  |  |  | use App\Core\HTTPClient; | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  | use function App\Core\I18n\_m; | 
					
						
							|  |  |  | use App\Core\Log; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  | use App\Core\VisibilityScope; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Entity\Note as GSNote; | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  | use App\Entity\NoteType; | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  | use App\Util\Common; | 
					
						
							|  |  |  | use App\Util\Exception\ClientException; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  | use App\Util\Exception\DuplicateFoundException; | 
					
						
							|  |  |  | use App\Util\Exception\NoSuchActorException; | 
					
						
							|  |  |  | use App\Util\Exception\ServerException; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Util\Formatting; | 
					
						
							| 
									
										
										
										
											2022-02-16 03:01:25 +00:00
										 |  |  | use App\Util\HTML; | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  | use App\Util\TemporaryFile; | 
					
						
							|  |  |  | use Component\Attachment\Entity\ActorToAttachment; | 
					
						
							|  |  |  | use Component\Attachment\Entity\AttachmentToNote; | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  | use Component\Conversation\Conversation; | 
					
						
							| 
									
										
										
										
											2021-12-27 04:13:09 +00:00
										 |  |  | use Component\FreeNetwork\FreeNetwork; | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  | use Component\Language\Entity\Language; | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  | use Component\Notification\Entity\Attention; | 
					
						
							| 
									
										
										
										
											2022-01-04 22:20:12 +00:00
										 |  |  | use Component\Tag\Entity\NoteTag; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  | use Component\Tag\Tag; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use DateTime; | 
					
						
							|  |  |  | use DateTimeInterface; | 
					
						
							|  |  |  | use Exception; | 
					
						
							|  |  |  | use InvalidArgumentException; | 
					
						
							|  |  |  | use Plugin\ActivityPub\ActivityPub; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  | use Plugin\ActivityPub\Entity\ActivitypubObject; | 
					
						
							| 
									
										
										
										
											2022-02-16 03:01:25 +00:00
										 |  |  | use Plugin\ActivityPub\Util\Explorer; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use Plugin\ActivityPub\Util\Model; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  | use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | 
					
						
							|  |  |  | use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | 
					
						
							|  |  |  | use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | 
					
						
							|  |  |  | use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This class handles translation between JSON and GSNotes | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Note extends Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create an Entity from an ActivityStreams 2.0 JSON string | 
					
						
							|  |  |  |      * This will persist a new GSNote | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |      * @throws ClientException | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |      * @throws ClientExceptionInterface | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |      * @throws DuplicateFoundException | 
					
						
							|  |  |  |      * @throws NoSuchActorException | 
					
						
							|  |  |  |      * @throws RedirectionExceptionInterface | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |      * @throws ServerException | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |      * @throws ServerExceptionInterface | 
					
						
							|  |  |  |      * @throws TransportExceptionInterface | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function fromJson(string|AbstractObject $json, array $options = []): GSNote | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |         $handleInReplyTo = function (AbstractObject|string $type_note): ?int { | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |                 $parent_note = \is_null($type_note->get('inReplyTo')) ? null : ActivityPub::getObjectByUri($type_note->get('inReplyTo'), try_online: true); | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |                 if ($parent_note instanceof GSNote) { | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |                     return $parent_note->getId(); | 
					
						
							|  |  |  |                 } elseif ($parent_note instanceof Type\AbstractObject && $parent_note->get('type') === 'Note') { | 
					
						
							|  |  |  |                     return self::fromJson($parent_note)->getId(); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     return null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 Log::debug('ActivityStreams:Model:Note-> An error occurred retrieving parent note.', [$e]); | 
					
						
							|  |  |  |                 // Sadly we won't be able to have this note inside the correct conversation for now.
 | 
					
						
							|  |  |  |                 // TODO: Create an entity that registers notes falsely without parent so, when the parent is retrieved,
 | 
					
						
							|  |  |  |                 // we can update the child with the correct parent.
 | 
					
						
							|  |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $source    = $options['source'] ?? 'ActivityPub'; | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         $type_note = \is_string($json) ? self::jsonToType($json) : $json; | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |         $actor_id  = null; | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |         $actor     = null; | 
					
						
							| 
									
										
										
										
											2022-02-16 03:01:25 +00:00
										 |  |  |         $to        = $type_note->has('to') ? (\is_string($type_note->get('to')) ? [$type_note->get('to')] : $type_note->get('to')) : []; | 
					
						
							|  |  |  |         $cc        = $type_note->has('cc') ? (\is_string($type_note->get('cc')) ? [$type_note->get('cc')] : $type_note->get('cc')) : []; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         if ($json instanceof AbstractObject | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |             && \array_key_exists('test_authority', $options) | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             && $options['test_authority'] | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |             && \array_key_exists('actor_uri', $options) | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         ) { | 
					
						
							|  |  |  |             $actor_uri = $options['actor_uri']; | 
					
						
							|  |  |  |             if ($actor_uri !== $type_note->get('attributedTo')) { | 
					
						
							|  |  |  |                 if (parse_url($actor_uri)['host'] !== parse_url($type_note->get('attributedTo'))['host']) { | 
					
						
							|  |  |  |                     throw new Exception('You don\'t seem to have enough authority to create this note.'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |                 $actor    = $options['actor']    ?? null; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |                 $actor_id = $options['actor_id'] ?? $actor?->getId(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         if (\is_null($actor_id)) { | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |             $actor    = ActivityPub::getActorByUri($type_note->get('attributedTo')); | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             $actor_id = $actor->getId(); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         $map = [ | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |             'is_local'     => false, | 
					
						
							|  |  |  |             'created'      => new DateTime($type_note->get('published') ?? 'now'), | 
					
						
							|  |  |  |             'content'      => $type_note->get('content') ?? null, | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |             'rendered'     => \is_null($type_note->get('content')) ? null : HTML::sanitize($type_note->get('content')), | 
					
						
							| 
									
										
										
										
											2022-02-17 18:45:30 +00:00
										 |  |  |             'title'        => $type_note->get('name') ?? null, | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |             'content_type' => 'text/html', | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |             'language_id'  => $type_note->get('contentLang') ?? null, | 
					
						
							|  |  |  |             'url'          => $type_note->get('url') ?? $type_note->get('id'), | 
					
						
							|  |  |  |             'actor_id'     => $actor_id, | 
					
						
							|  |  |  |             'reply_to'     => $reply_to = $handleInReplyTo($type_note), | 
					
						
							|  |  |  |             'modified'     => new DateTime(), | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |             'type'         => match ($type_note->get('type')) { | 
					
						
							|  |  |  |                 'Page'     => NoteType::PAGE, default => NoteType::NOTE | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             'source' => $source, | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         if (!\is_null($map['language_id'])) { | 
					
						
							| 
									
										
										
										
											2021-12-04 19:58:00 +00:00
										 |  |  |             $map['language_id'] = Language::getByLocale($map['language_id'])->getId(); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $map['language_id'] = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |         // Scope
 | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |         if (\in_array('https://www.w3.org/ns/activitystreams#Public', $to)) { | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |             // Public: Visible for all, shown in public feeds
 | 
					
						
							| 
									
										
										
										
											2021-12-26 17:31:53 +00:00
										 |  |  |             $map['scope'] = VisibilityScope::EVERYWHERE; | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |         } elseif (\in_array('https://www.w3.org/ns/activitystreams#Public', $cc)) { | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |             // Unlisted: Visible for all but not shown in public feeds
 | 
					
						
							|  |  |  |             // It isn't the note that dictates what feed is shown in but the feed, it only dictates who can access it.
 | 
					
						
							| 
									
										
										
										
											2022-02-17 20:06:18 +00:00
										 |  |  |             $map['scope'] = 'unlisted'; | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             // Either Followers-only or Direct
 | 
					
						
							| 
									
										
										
										
											2022-02-23 22:25:11 +00:00
										 |  |  |             if ($type_note->get('type') === 'ChatMessage' // Is DM explicitly?
 | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |             || (empty($type_note->get('cc')))) { // Only has TO targets
 | 
					
						
							|  |  |  |                 $map['scope'] = VisibilityScope::MESSAGE; | 
					
						
							|  |  |  |             } else { // Then is collection
 | 
					
						
							|  |  |  |                 $map['scope'] = VisibilityScope::COLLECTION; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $object_mentions_ids = []; | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |         foreach ($to as $target) { | 
					
						
							|  |  |  |             if ($target === 'https://www.w3.org/ns/activitystreams#Public') { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |                 $actor                                = ActivityPub::getActorByUri($target); | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |                 $object_mentions_ids[$actor->getId()] = $target; | 
					
						
							| 
									
										
										
										
											2022-02-17 20:06:18 +00:00
										 |  |  |                 // If $to is a group and note is unlisted, set note's scope as Group
 | 
					
						
							|  |  |  |                 if ($actor->isGroup() && $map['scope'] === 'unlisted') { | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |                     $map['scope'] = VisibilityScope::GROUP; | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-17 20:06:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // We can drop this insight already
 | 
					
						
							|  |  |  |         if ($map['scope'] === 'unlisted') { | 
					
						
							|  |  |  |             $map['scope'] = VisibilityScope::EVERYWHERE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |         foreach ($cc as $target) { | 
					
						
							|  |  |  |             if ($target === 'https://www.w3.org/ns/activitystreams#Public') { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |                 $actor                                = ActivityPub::getActorByUri($target); | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |                 $object_mentions_ids[$actor->getId()] = $target; | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 Log::debug('ActivityPub->Model->Note->fromJson->getActorByUri', [$e]); | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 17:48:06 +00:00
										 |  |  |         $obj = GSNote::create($map); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |         // Attachments
 | 
					
						
							|  |  |  |         $processed_attachments = []; | 
					
						
							| 
									
										
										
										
											2022-02-18 15:28:48 +00:00
										 |  |  |         foreach ($type_note->get('attachment') ?? [] as $attachment) { | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |             if ($attachment->get('type') === 'Document') { | 
					
						
							|  |  |  |                 // Retrieve media
 | 
					
						
							|  |  |  |                 $get_response = HTTPClient::get($attachment->get('url')); | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |                 $media        = $get_response->getContent(); | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |                 unset($get_response); | 
					
						
							|  |  |  |                 // Ignore empty files
 | 
					
						
							|  |  |  |                 if (!empty($media)) { | 
					
						
							|  |  |  |                     // Create an attachment for this
 | 
					
						
							|  |  |  |                     $temp_file = new TemporaryFile(); | 
					
						
							|  |  |  |                     $temp_file->write($media); | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |                     $filesize      = $temp_file->getSize(); | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |                     $max_file_size = Common::getUploadLimit(); | 
					
						
							|  |  |  |                     if ($max_file_size < $filesize) { | 
					
						
							|  |  |  |                         throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |                             . 'Try to upload a smaller version.', ['quota' => $max_file_size, 'size' => $filesize], )); | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |                     } | 
					
						
							|  |  |  |                     Event::handle('EnforceUserFileQuota', [$filesize, $actor_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     $processed_attachments[] = [GSFile::storeFileAsAttachment($temp_file), $attachment->get('name')]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         DB::persist($obj); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |         // Assign conversation to this note
 | 
					
						
							|  |  |  |         Conversation::assignLocalConversation($obj, $reply_to); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 15:28:48 +00:00
										 |  |  |         foreach ($type_note->get('tag') ?? [] as $ap_tag) { | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |             switch ($ap_tag->get('type')) { | 
					
						
							|  |  |  |                 case 'Mention': | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |                     $explorer = new Explorer(); | 
					
						
							|  |  |  |                     try { | 
					
						
							|  |  |  |                         $actors = $explorer->lookup($ap_tag->get('href')); | 
					
						
							| 
									
										
										
										
											2022-02-16 03:01:25 +00:00
										 |  |  |                         foreach ($actors as $actor) { | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |                             $object_mentions_ids[$actor->getId()] = $ap_tag->get('href'); | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |                         } | 
					
						
							|  |  |  |                     } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2022-02-23 22:17:52 +00:00
										 |  |  |                         Log::debug('ActivityPub->Model->Note->fromJson->Mention->Explorer', [$e]); | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |                     } | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 case 'Hashtag': | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |                     $match         = ltrim($ap_tag->get('name'), '#'); | 
					
						
							|  |  |  |                     $tag           = Tag::extract($match); | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |                     $canonical_tag = $ap_tag->get('canonical') ?? Tag::canonicalTag($tag, \is_null($lang_id = $obj->getLanguageId()) ? null : Language::getById($lang_id)->getLocale()); | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |                     DB::persist(NoteTag::create([ | 
					
						
							|  |  |  |                         'tag'           => $tag, | 
					
						
							|  |  |  |                         'canonical'     => $canonical_tag, | 
					
						
							|  |  |  |                         'note_id'       => $obj->getId(), | 
					
						
							|  |  |  |                         'use_canonical' => $ap_tag->get('canonical') ?? false, | 
					
						
							| 
									
										
										
										
											2022-02-12 04:34:10 +00:00
										 |  |  |                         'language_id'   => $lang_id ?? null, | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |                     ])); | 
					
						
							|  |  |  |                     Cache::pushList("tag-{$canonical_tag}", $obj); | 
					
						
							|  |  |  |                     foreach (Tag::cacheKeys($canonical_tag) as $key) { | 
					
						
							|  |  |  |                         Cache::delete($key); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |         // The content would be non-sanitized text/html
 | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |         Event::handle('ProcessNoteContent', [$obj, $obj->getRendered(), $obj->getContentType(), $process_note_content_extra_args = ['TagProcessed' => true, 'ignoreLinks' => $object_mentions_ids]]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $object_mentions_ids = array_keys($object_mentions_ids); | 
					
						
							|  |  |  |         $obj->setObjectMentionsIds($object_mentions_ids); | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($processed_attachments !== []) { | 
					
						
							|  |  |  |             foreach ($processed_attachments as [$a, $fname]) { | 
					
						
							|  |  |  |                 if (DB::count('actor_to_attachment', $args = ['attachment_id' => $a->getId(), 'actor_id' => $actor_id]) === 0) { | 
					
						
							|  |  |  |                     DB::persist(ActorToAttachment::create($args)); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $obj->getId(), 'title' => $fname])); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         $map = [ | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |             'object_uri'  => $type_note->get('id'), | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             'object_type' => 'note', | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |             'object_id'   => $obj->getId(), | 
					
						
							|  |  |  |             'created'     => new DateTime($type_note->get('published') ?? 'now'), | 
					
						
							|  |  |  |             'modified'    => new DateTime(), | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         $ap_obj = new ActivitypubObject(); | 
					
						
							|  |  |  |         foreach ($map as $prop => $val) { | 
					
						
							|  |  |  |             $set = Formatting::snakeCaseToCamelCase("set_{$prop}"); | 
					
						
							|  |  |  |             $ap_obj->{$set}($val); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         DB::persist($ap_obj); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         return $obj; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a JSON | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function toJson(mixed $object, ?int $options = null): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-28 16:07:35 +00:00
										 |  |  |         if ($object::class !== GSNote::class) { | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |             throw new InvalidArgumentException('First argument type must be a Note.'); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $attr = [ | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |             '@context'         => ActivityPub::$activity_streams_two_context, | 
					
						
							| 
									
										
										
										
											2022-02-23 22:25:11 +00:00
										 |  |  |             'type'             => $object->getScope() === VisibilityScope::MESSAGE ? 'ChatMessage' : (match ($object->getType()) { | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |                 NoteType::NOTE => 'Note', NoteType::PAGE => 'Page' | 
					
						
							| 
									
										
										
										
											2022-02-23 22:25:11 +00:00
										 |  |  |             }), | 
					
						
							| 
									
										
										
										
											2021-12-24 21:02:02 +00:00
										 |  |  |             'id'             => $object->getUrl(), | 
					
						
							|  |  |  |             'published'      => $object->getCreated()->format(DateTimeInterface::RFC3339), | 
					
						
							|  |  |  |             'attributedTo'   => $object->getActor()->getUri(Router::ABSOLUTE_URL), | 
					
						
							| 
									
										
										
										
											2022-02-17 18:45:30 +00:00
										 |  |  |             'name'           => $object->getTitle(), | 
					
						
							| 
									
										
										
										
											2021-12-24 21:02:02 +00:00
										 |  |  |             'content'        => $object->getRendered(), | 
					
						
							| 
									
										
										
										
											2022-02-16 23:57:24 +00:00
										 |  |  |             'mediaType'      => 'text/html', | 
					
						
							|  |  |  |             'source'         => ['content' => $object->getContent(), 'mediaType' => $object->getContentType()], | 
					
						
							| 
									
										
										
										
											2021-12-24 21:02:02 +00:00
										 |  |  |             'attachment'     => [], | 
					
						
							|  |  |  |             'tag'            => [], | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |             'inReplyTo'      => \is_null($object->getReplyTo()) ? null : ActivityPub::getUriByObject(GSNote::getById($object->getReplyTo())), | 
					
						
							| 
									
										
										
										
											2021-12-24 21:02:02 +00:00
										 |  |  |             'inConversation' => $object->getConversationUri(), | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |         // Target scope
 | 
					
						
							|  |  |  |         switch ($object->getScope()) { | 
					
						
							| 
									
										
										
										
											2021-12-26 17:31:53 +00:00
										 |  |  |             case VisibilityScope::EVERYWHERE: | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |                 $attr['to'] = ['https://www.w3.org/ns/activitystreams#Public']; | 
					
						
							|  |  |  |                 $attr['cc'] = [Router::url('actor_subscribers_id', ['id' => $object->getActor()->getId()], Router::ABSOLUTE_URL)]; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |             case VisibilityScope::LOCAL: | 
					
						
							|  |  |  |                 throw new ClientException('This note was not federated.', 403); | 
					
						
							|  |  |  |             case VisibilityScope::ADDRESSEE: | 
					
						
							|  |  |  |             case VisibilityScope::MESSAGE: | 
					
						
							|  |  |  |                 $attr['to'] = []; // Will be filled later
 | 
					
						
							|  |  |  |                 $attr['cc'] = []; | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |             case VisibilityScope::GROUP: | 
					
						
							| 
									
										
										
										
											2022-02-17 20:06:18 +00:00
										 |  |  |                 // Will have the group in the To coming from attentions
 | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |             case VisibilityScope::COLLECTION: | 
					
						
							|  |  |  |                 // Since we don't support sending unlisted/followers-only
 | 
					
						
							|  |  |  |                 // notices, arriving here means we're instead answering to that type
 | 
					
						
							|  |  |  |                 // of posts. In this situation, it's safer to always send answers of type unlisted.
 | 
					
						
							|  |  |  |                 $attr['to'] = []; | 
					
						
							|  |  |  |                 $attr['cc'] = ['https://www.w3.org/ns/activitystreams#Public']; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 Log::error('ActivityPub->Note->toJson: Found an unknown visibility scope.'); | 
					
						
							|  |  |  |                 throw new ServerException('Found an unknown visibility scope which cannot federate.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-19 04:45:33 +00:00
										 |  |  |         $attention_cc = DB::findBy(Attention::class, ['note_id' => $object->getId()]); | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |         foreach ($attention_cc as $cc_id) { | 
					
						
							| 
									
										
										
										
											2022-02-17 20:06:18 +00:00
										 |  |  |             $target = \App\Entity\Actor::getById($cc_id->getTargetId()); | 
					
						
							|  |  |  |             if ($object->getScope() === VisibilityScope::GROUP && $target->isGroup()) { | 
					
						
							|  |  |  |                 $attr['to'][] = $target->getUri(Router::ABSOLUTE_URL); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $attr['cc'][] = $target->getUri(Router::ABSOLUTE_URL); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-02-19 04:45:33 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 06:43:43 +00:00
										 |  |  |         // Mentions
 | 
					
						
							|  |  |  |         foreach ($object->getNotificationTargets() as $mention) { | 
					
						
							| 
									
										
										
										
											2022-02-23 22:17:52 +00:00
										 |  |  |             $attr['tag'][] = [ | 
					
						
							|  |  |  |                 'type' => 'Mention', | 
					
						
							|  |  |  |                 'href' => ($href = $mention->getUri()), | 
					
						
							|  |  |  |                 'name' => $mention->isGroup() ? FreeNetwork::groupTagToName($mention->getNickname(), $href) : FreeNetwork::mentionTagToName($mention->getNickname(), $href), | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |             $attr['to'][] = $href; | 
					
						
							| 
									
										
										
										
											2021-12-12 06:43:43 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-24 21:02:02 +00:00
										 |  |  |         // Hashtags
 | 
					
						
							|  |  |  |         foreach ($object->getTags() as $hashtag) { | 
					
						
							|  |  |  |             $attr['tag'][] = [ | 
					
						
							| 
									
										
										
										
											2021-12-25 17:46:45 +00:00
										 |  |  |                 'type'      => 'Hashtag', | 
					
						
							|  |  |  |                 'href'      => $hashtag->getUrl(type: Router::ABSOLUTE_URL), | 
					
						
							|  |  |  |                 'name'      => "#{$hashtag->getTag()}", | 
					
						
							|  |  |  |                 'canonical' => $hashtag->getCanonical(), | 
					
						
							| 
									
										
										
										
											2021-12-24 21:02:02 +00:00
										 |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |         // Attachments
 | 
					
						
							|  |  |  |         foreach ($object->getAttachments() as $attachment) { | 
					
						
							|  |  |  |             $attr['attachment'][] = [ | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |                 'type'      => 'Document', | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |                 'mediaType' => $attachment->getMimetype(), | 
					
						
							| 
									
										
										
										
											2021-12-27 02:47:04 +00:00
										 |  |  |                 'url'       => $attachment->getUrl(note: $object, type: Router::ABSOLUTE_URL), | 
					
						
							| 
									
										
										
										
											2022-02-18 17:48:06 +00:00
										 |  |  |                 'name'      => DB::findOneBy(AttachmentToNote::class, ['attachment_id' => $attachment->getId(), 'note_id' => $object->getId()], return_null: true)?->getTitle(), | 
					
						
							| 
									
										
										
										
											2021-12-23 16:21:19 +00:00
										 |  |  |                 'width'     => $attachment->getWidth(), | 
					
						
							|  |  |  |                 'height'    => $attachment->getHeight(), | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         $type = self::jsonToType($attr); | 
					
						
							|  |  |  |         Event::handle('ActivityPubAddActivityStreamsTwoData', [$type->get('type'), &$type]); | 
					
						
							|  |  |  |         return $type->toJson($options); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-04 19:58:00 +00:00
										 |  |  | } |