| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // {{{ 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 | 
					
						
							|  |  |  |  * @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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use ActivityPhp\Type\AbstractObject; | 
					
						
							| 
									
										
										
										
											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-04 04:07:08 +00:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							|  |  |  | use App\Entity\Language; | 
					
						
							|  |  |  | use App\Entity\Note as GSNote; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  | use App\Util\TemporaryFile; | 
					
						
							|  |  |  | use Component\Attachment\Entity\ActorToAttachment; | 
					
						
							|  |  |  | use Component\Attachment\Entity\AttachmentToNote; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											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-05 21:04:20 +00:00
										 |  |  | use function App\Core\I18n\_m; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  | use function is_null; | 
					
						
							|  |  |  | use function is_string; | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string|AbstractObject $json | 
					
						
							|  |  |  |      * @param array $options | 
					
						
							|  |  |  |      * @return GSNote | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |      * @throws ClientException | 
					
						
							|  |  |  |      * @throws DuplicateFoundException | 
					
						
							|  |  |  |      * @throws NoSuchActorException | 
					
						
							|  |  |  |      * @throws ServerException | 
					
						
							|  |  |  |      * @throws ClientExceptionInterface | 
					
						
							|  |  |  |      * @throws RedirectionExceptionInterface | 
					
						
							|  |  |  |      * @throws ServerExceptionInterface | 
					
						
							|  |  |  |      * @throws TransportExceptionInterface | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function fromJson(string|AbstractObject $json, array $options = []): GSNote | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         $source = $options['source'] ?? 'ActivityPub'; | 
					
						
							|  |  |  |         $type_note = is_string($json) ? self::jsonToType($json) : $json; | 
					
						
							|  |  |  |         $actor = null; | 
					
						
							|  |  |  |         $actor_id = null; | 
					
						
							|  |  |  |         if ($json instanceof AbstractObject | 
					
						
							|  |  |  |             && array_key_exists('test_authority', $options) | 
					
						
							|  |  |  |             && $options['test_authority'] | 
					
						
							|  |  |  |             && array_key_exists('actor_uri', $options) | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             $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 { | 
					
						
							|  |  |  |                 $actor = $options['actor'] ?? null; | 
					
						
							|  |  |  |                 $actor_id = $options['actor_id'] ?? $actor?->getId(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         if (is_null($actor_id)) { | 
					
						
							|  |  |  |             $actor = ActivityPub::getActorByUri($type_note->get('attributedTo')); | 
					
						
							|  |  |  |             $actor_id = $actor->getId(); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         $map = [ | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             'is_local' => false, | 
					
						
							|  |  |  |             'created' => new DateTime($type_note->get('published') ?? 'now'), | 
					
						
							|  |  |  |             'content' => $type_note->get('content') ?? null, | 
					
						
							|  |  |  |             'rendered' => null, | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |             'content_type' => 'text/html', | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             'language_id' => $type_note->get('contentLang') ?? null, | 
					
						
							|  |  |  |             'url' => $type_note->get('url') ?? $type_note->get('id'), | 
					
						
							|  |  |  |             'actor_id' => $actor_id, | 
					
						
							|  |  |  |             'modified' => new DateTime(), | 
					
						
							|  |  |  |             'source' => $source, | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         if ($map['content'] !== null) { | 
					
						
							|  |  |  |             $mentions = []; | 
					
						
							|  |  |  |             Event::handle('RenderNoteContent', [ | 
					
						
							|  |  |  |                 $map['content'], | 
					
						
							|  |  |  |                 $map['content_type'], | 
					
						
							|  |  |  |                 &$map['rendered'], | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |                 $actor, | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |                 $map['language_id'], | 
					
						
							|  |  |  |                 &$mentions, | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $obj = new GSNote(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($map as $prop => $val) { | 
					
						
							|  |  |  |             $set = Formatting::snakeCaseToCamelCase("set_{$prop}"); | 
					
						
							|  |  |  |             $obj->{$set}($val); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |         // Attachments
 | 
					
						
							|  |  |  |         $processed_attachments = []; | 
					
						
							|  |  |  |         foreach ($type_note->get('attachment') as $attachment) { | 
					
						
							|  |  |  |             if ($attachment->get('type') === 'Document') { | 
					
						
							|  |  |  |                 // Retrieve media
 | 
					
						
							|  |  |  |                 $get_response = HTTPClient::get($attachment->get('url')); | 
					
						
							|  |  |  |                 $media = $get_response->getContent(); | 
					
						
							|  |  |  |                 unset($get_response); | 
					
						
							|  |  |  |                 // Ignore empty files
 | 
					
						
							|  |  |  |                 if (!empty($media)) { | 
					
						
							|  |  |  |                     // Create an attachment for this
 | 
					
						
							|  |  |  |                     $temp_file = new TemporaryFile(); | 
					
						
							|  |  |  |                     $temp_file->write($media); | 
					
						
							|  |  |  |                     $filesize = $temp_file->getSize(); | 
					
						
							|  |  |  |                     $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. ' | 
					
						
							|  |  |  |                             . 'Try to upload a smaller version.', ['quota' => $max_file_size, 'size' => $filesize],)); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     Event::handle('EnforceUserFileQuota', [$filesize, $actor_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     $processed_attachments[] = [GSFile::storeFileAsAttachment($temp_file), $attachment->get('name')]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         DB::persist($obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Need file and note ids for the next step
 | 
					
						
							|  |  |  |         Event::handle('ProcessNoteContent', [$obj, $obj->getContent(), $obj->getContentType(), $process_note_content_extra_args = []]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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 = [ | 
					
						
							|  |  |  |             'object_uri' => $type_note->get('id'), | 
					
						
							|  |  |  |             'object_type' => 'note', | 
					
						
							|  |  |  |             'object_id' => $obj->getId(), | 
					
						
							|  |  |  |             'created' => new DateTime($type_note->get('published') ?? 'now'), | 
					
						
							|  |  |  |             'modified' => new DateTime(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $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 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $object | 
					
						
							|  |  |  |      * @param int|null $options | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function toJson(mixed $object, ?int $options = null): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($object::class !== 'App\Entity\Note') { | 
					
						
							|  |  |  |             throw new InvalidArgumentException('First argument type is Note'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $attr = [ | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             '@context' => 'https://www.w3.org/ns/activitystreams', | 
					
						
							|  |  |  |             'type' => 'Note', | 
					
						
							| 
									
										
										
										
											2021-12-12 06:43:43 +00:00
										 |  |  |             'id' => $object->getUrl(), | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |             'attributedTo' => $object->getActor()->getUri(Router::ABSOLUTE_URL), | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address
 | 
					
						
							|  |  |  |             'cc' => ['https://www.w3.org/ns/activitystreams#Public'], | 
					
						
							|  |  |  |             'content' => $object->getRendered(), | 
					
						
							|  |  |  |             'attachment' => [], | 
					
						
							| 
									
										
										
										
											2021-12-12 06:43:43 +00:00
										 |  |  |             'tag' => [], | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 06:43:43 +00:00
										 |  |  |         // Mentions
 | 
					
						
							|  |  |  |         foreach ($object->getNotificationTargets() as $mention) { | 
					
						
							|  |  |  |             $attr['tag'][] = [ | 
					
						
							|  |  |  |                 'type' => 'Mention', | 
					
						
							|  |  |  |                 'href' => ($href = $mention->getUri()), | 
					
						
							|  |  |  |                 'name' => '@'.$mention->getNickname().'@'.parse_url($href, PHP_URL_HOST) | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |         // Attachments
 | 
					
						
							|  |  |  |         foreach ($object->getAttachments() as $attachment) { | 
					
						
							|  |  |  |             $attr['attachment'][] = [ | 
					
						
							|  |  |  |                 'type' => 'Document', | 
					
						
							|  |  |  |                 'mediaType' => $attachment->getMimetype(), | 
					
						
							|  |  |  |                 'url' => $attachment->getUrl(Router::ABSOLUTE_URL), | 
					
						
							| 
									
										
										
										
											2021-12-16 11:14:34 +00:00
										 |  |  |                 'name' => AttachmentToNote::getByPK(['attachment_id' => $attachment->getId(), 'note_id' => $object->getId()])->getTitle(), | 
					
						
							| 
									
										
										
										
											2021-12-05 21:04:20 +00:00
										 |  |  |                 'width' => $attachment->getWidth(), | 
					
						
							|  |  |  |                 'height' => $attachment->getHeight(), | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |