| 
									
										
										
										
											2021-12-02 04:25:58 +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 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @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 Component\FreeNetwork\Entity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use App\Core\DB\DB; | 
					
						
							|  |  |  | use App\Core\Entity; | 
					
						
							|  |  |  | use App\Entity\Actor; | 
					
						
							|  |  |  | use Component\FreeNetwork\Util\Discovery; | 
					
						
							|  |  |  | use DateTimeInterface; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Table Definition for free_network_actor_protocol | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class FreeNetworkActorProtocol extends Entity | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // {{{ Autocode
 | 
					
						
							| 
									
										
										
										
											2021-12-26 15:12:06 +00:00
										 |  |  |     // @codeCoverageIgnoreStart
 | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     private int $actor_id; | 
					
						
							|  |  |  |     private ?string $protocol; | 
					
						
							| 
									
										
										
										
											2021-12-26 15:12:06 +00:00
										 |  |  |     private string $addr; | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     private DateTimeInterface $created; | 
					
						
							|  |  |  |     private DateTimeInterface $modified; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setActorId(int $actor_id): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->actor_id = $actor_id; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 15:12:06 +00:00
										 |  |  |     public function getActorId(): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->actor_id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     public function setProtocol(?string $protocol): self | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-26 15:12:06 +00:00
										 |  |  |         $this->protocol = mb_substr($protocol, 0, 32); | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getProtocol(): ?string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->protocol; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setAddr(string $addr): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->addr = $addr; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getAddr(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->addr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setCreated(DateTimeInterface $created): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->created = $created; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 15:12:06 +00:00
										 |  |  |     public function getCreated(): DateTimeInterface | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-26 15:12:06 +00:00
										 |  |  |         return $this->created; | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setModified(DateTimeInterface $modified): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->modified = $modified; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-26 15:12:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function getModified(): DateTimeInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->modified; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     // @codeCoverageIgnoreEnd
 | 
					
						
							|  |  |  |     // }}} Autocode
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function protocolSucceeded(string $protocol, int|Actor $actor_id, string $addr): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         $actor_id            = \is_int($actor_id) ? $actor_id : $actor_id->getId(); | 
					
						
							| 
									
										
										
										
											2021-12-16 11:14:34 +00:00
										 |  |  |         $attributed_protocol = self::getByPK(['actor_id' => $actor_id]); | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         if (\is_null($attributed_protocol)) { | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |             $attributed_protocol = self::create([ | 
					
						
							|  |  |  |                 'actor_id' => $actor_id, | 
					
						
							|  |  |  |                 'protocol' => $protocol, | 
					
						
							|  |  |  |                 'addr'     => Discovery::normalize($addr), | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $attributed_protocol->setProtocol($protocol); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         DB::wrapInTransaction(fn () => DB::persist($attributed_protocol)); | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function canIActor(string $protocol, int|Actor $actor_id): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         $actor_id            = \is_int($actor_id) ? $actor_id : $actor_id->getId(); | 
					
						
							| 
									
										
										
										
											2021-12-16 11:14:34 +00:00
										 |  |  |         $attributed_protocol = self::getByPK(['actor_id' => $actor_id])?->getProtocol(); | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         if (\is_null($attributed_protocol)) { | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |             // If it is not attributed, you can go ahead.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // If it is attributed, you can on the condition that you're assigned to it.
 | 
					
						
							|  |  |  |             return $attributed_protocol === $protocol; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function canIAddr(string $protocol, string $target): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Normalize $addr, i.e. add 'acct:' if missing
 | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         $addr                = Discovery::normalize($target); | 
					
						
							| 
									
										
										
										
											2021-12-16 11:14:34 +00:00
										 |  |  |         $attributed_protocol = self::getByPK(['addr' => $addr])?->getProtocol(); | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         if (\is_null($attributed_protocol)) { | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |             // If it is not attributed, you can go ahead.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // If it is attributed, you can on the condition that you're assigned to it.
 | 
					
						
							|  |  |  |             return $attributed_protocol === $protocol; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function schemaDef(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'name'   => 'free_network_actor_protocol', | 
					
						
							|  |  |  |             'fields' => [ | 
					
						
							|  |  |  |                 'actor_id' => ['type' => 'int', 'not null' => true], | 
					
						
							|  |  |  |                 'protocol' => ['type' => 'varchar',  'length' => 32, 'description' => 'the protocol plugin that should handle federation of this actor'], | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |                 'addr'     => ['type' => 'text',  'not null' => true, 'description' => 'webfinger acct'], | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |                 'created'  => ['type' => 'datetime',  'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], | 
					
						
							|  |  |  |                 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             'primary key'  => ['actor_id'], | 
					
						
							|  |  |  |             'foreign keys' => [ | 
					
						
							|  |  |  |                 'activitypub_actor_actor_id_fkey' => ['actor', ['actor_id' => 'id']], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |