| @@ -1,27 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Application model file. | |
| 4 | - * | |
| 5 | - * @deprecated Use {@see \Activitypub\Application} for key management | |
| 6 | - * and {@see \Activitypub\Rest\Application_Controller} for the REST endpoint. | |
| 7 | - * | |
| 8 | - * @package Activitypub | |
| 9 | - */ | |
| 10 | - | |
| 11 | 2 | namespace Activitypub\Model; |
| 12 | 3 | |
| 4 | +use WP_Query; | |
| 5 | +use Activitypub\Signature; | |
| 13 | 6 | use Activitypub\Activity\Actor; |
| 14 | -use Activitypub\Application as Application_Utility; | |
| 7 | +use Activitypub\Collection\Users; | |
| 15 | 8 | |
| 16 | 9 | use function Activitypub\get_rest_url_by_path; |
| 17 | -use function Activitypub\home_host; | |
| 18 | 10 | |
| 19 | -/** | |
| 20 | - * Application class. | |
| 21 | - * | |
| 22 | - * @deprecated Use {@see \Activitypub\Application} instead. | |
| 23 | - */ | |
| 24 | 11 | class Application extends Actor { |
| 25 | 12 | /** |
| 26 | 13 | * The User-ID |
| 27 | 14 | * |
| @@ -26,99 +13,47 @@ | ||
| 26 | 13 | * The User-ID |
| 27 | 14 | * |
| 28 | 15 | * @var int |
| 29 | 16 | */ |
| 30 | - protected $_id = -1; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore | |
| 17 | + protected $_id = Users::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore | |
| 31 | 18 | |
| 32 | 19 | /** |
| 33 | - * Whether the Application is discoverable. | |
| 20 | + * If the User is discoverable. | |
| 34 | 21 | * |
| 35 | 22 | * @see https://docs.joinmastodon.org/spec/activitypub/#discoverable |
| 36 | 23 | * |
| 37 | 24 | * @context http://joinmastodon.org/ns#discoverable |
| 38 | 25 | * |
| 39 | - * @var bool | |
| 26 | + * @var boolean | |
| 40 | 27 | */ |
| 41 | 28 | protected $discoverable = false; |
| 42 | 29 | |
| 43 | 30 | /** |
| 44 | - * Whether the Application is indexable. | |
| 31 | + * If the User is indexable. | |
| 45 | 32 | * |
| 46 | 33 | * @context http://joinmastodon.org/ns#indexable |
| 47 | 34 | * |
| 48 | - * @var bool | |
| 35 | + * @var boolean | |
| 49 | 36 | */ |
| 50 | 37 | protected $indexable = false; |
| 51 | 38 | |
| 52 | 39 | /** |
| 53 | - * Whether the Application manually approves followers. | |
| 40 | + * The WebFinger Resource. | |
| 54 | 41 | * |
| 55 | - * @see https://docs.joinmastodon.org/spec/activitypub/#as | |
| 56 | - * | |
| 57 | - * @context as:manuallyApprovesFollowers | |
| 58 | - * | |
| 59 | - * @var bool | |
| 42 | + * @var string<url> | |
| 60 | 43 | */ |
| 61 | - protected $manually_approves_followers = true; | |
| 44 | + protected $webfinger; | |
| 62 | 45 | |
| 63 | - /** | |
| 64 | - * List of software capabilities implemented by the Application. | |
| 65 | - * | |
| 66 | - * @see https://codeberg.org/silverpill/feps/src/branch/main/844e/fep-844e.md | |
| 67 | - * | |
| 68 | - * @var array | |
| 69 | - */ | |
| 70 | - protected $implements = array( | |
| 71 | - array( | |
| 72 | - 'href' => 'https://datatracker.ietf.org/doc/html/rfc9421', | |
| 73 | - 'name' => 'RFC-9421: HTTP Message Signatures', | |
| 74 | - ), | |
| 75 | - ); | |
| 46 | + public function get_type() { | |
| 47 | + return 'Application'; | |
| 48 | + } | |
| 76 | 49 | |
| 77 | - /** | |
| 78 | - * Set Application as invisible. | |
| 79 | - * | |
| 80 | - * @see https://litepub.social/ | |
| 81 | - * | |
| 82 | - * @var bool | |
| 83 | - */ | |
| 84 | - protected $invisible = true; | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * The type of the Actor. | |
| 88 | - * | |
| 89 | - * @var string | |
| 90 | - */ | |
| 91 | - protected $type = 'Application'; | |
| 92 | - | |
| 93 | - /** | |
| 94 | - * The Username. | |
| 95 | - * | |
| 96 | - * @var string | |
| 97 | - */ | |
| 98 | - protected $name = 'application'; | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * The preferred username. | |
| 102 | - * | |
| 103 | - * @var string | |
| 104 | - */ | |
| 105 | - protected $preferred_username = 'application'; | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * Constructor. | |
| 109 | - */ | |
| 110 | - public function __construct() { | |
| 111 | - \_deprecated_class( __CLASS__, '9.1.0', 'Activitypub\Application' ); | |
| 50 | + public function get_manually_approves_followers() { | |
| 51 | + return true; | |
| 112 | 52 | } |
| 113 | 53 | |
| 114 | - /** | |
| 115 | - * Returns the ID of the Application. | |
| 116 | - * | |
| 117 | - * @return string The ID of the Application. | |
| 118 | - */ | |
| 119 | 54 | public function get_id() { |
| 120 | - return Application_Utility::get_id(); | |
| 55 | + return get_rest_url_by_path( 'application' ); | |
| 121 | 56 | } |
| 122 | 57 | |
| 123 | 58 | /** |
| 124 | 59 | * Get the User-Url. |
| @@ -134,30 +69,63 @@ | ||
| 134 | 69 | * |
| 135 | 70 | * @return string The User-URL with @-Prefix for the username. |
| 136 | 71 | */ |
| 137 | 72 | public function get_alternate_url() { |
| 138 | - return $this->get_id(); | |
| 73 | + return $this->get_url(); | |
| 139 | 74 | } |
| 140 | 75 | |
| 141 | - /** | |
| 76 | + public function get_name() { | |
| 77 | + return 'application'; | |
| 78 | + } | |
| 79 | + | |
| 80 | + public function get_preferred_username() { | |
| 81 | + return $this->get_name(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 142 | 85 | * Get the User-Icon. |
| 143 | 86 | * |
| 144 | - * @return string[] The User-Icon. | |
| 87 | + * @return array The User-Icon. | |
| 145 | 88 | */ |
| 146 | 89 | public function get_icon() { |
| 147 | - return Application_Utility::get_icon(); | |
| 90 | + // try site icon first | |
| 91 | + $icon_id = get_option( 'site_icon' ); | |
| 92 | + | |
| 93 | + // try custom logo second | |
| 94 | + if ( ! $icon_id ) { | |
| 95 | + $icon_id = get_theme_mod( 'custom_logo' ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + $icon_url = false; | |
| 99 | + | |
| 100 | + if ( $icon_id ) { | |
| 101 | + $icon = wp_get_attachment_image_src( $icon_id, 'full' ); | |
| 102 | + if ( $icon ) { | |
| 103 | + $icon_url = $icon[0]; | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + if ( ! $icon_url ) { | |
| 108 | + // fallback to default icon | |
| 109 | + $icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE ); | |
| 110 | + } | |
| 111 | + | |
| 112 | + return array( | |
| 113 | + 'type' => 'Image', | |
| 114 | + 'url' => esc_url( $icon_url ), | |
| 115 | + ); | |
| 148 | 116 | } |
| 149 | 117 | |
| 150 | 118 | /** |
| 151 | 119 | * Get the User-Header-Image. |
| 152 | 120 | * |
| 153 | - * @return string[]|null The User-Header-Image. | |
| 121 | + * @return array|null The User-Header-Image. | |
| 154 | 122 | */ |
| 155 | 123 | public function get_header_image() { |
| 156 | 124 | if ( \has_header_image() ) { |
| 157 | 125 | return array( |
| 158 | 126 | 'type' => 'Image', |
| 159 | - 'url' => \esc_url_raw( \get_header_image() ), | |
| 127 | + 'url' => esc_url( \get_header_image() ), | |
| 160 | 128 | ); |
| 161 | 129 | } |
| 162 | 130 | |
| 163 | 131 | return null; |
| @@ -162,15 +130,24 @@ | ||
| 162 | 130 | |
| 163 | 131 | return null; |
| 164 | 132 | } |
| 165 | 133 | |
| 166 | - /** | |
| 167 | - * Get the first published date. | |
| 168 | - * | |
| 169 | - * @return string The published date. | |
| 170 | - */ | |
| 171 | 134 | public function get_published() { |
| 172 | - return Application_Utility::get_published(); | |
| 135 | + $first_post = new WP_Query( | |
| 136 | + array( | |
| 137 | + 'orderby' => 'date', | |
| 138 | + 'order' => 'ASC', | |
| 139 | + 'number' => 1, | |
| 140 | + ) | |
| 141 | + ); | |
| 142 | + | |
| 143 | + if ( ! empty( $first_post->posts[0] ) ) { | |
| 144 | + $time = \strtotime( $first_post->posts[0]->post_date_gmt ); | |
| 145 | + } else { | |
| 146 | + $time = \time(); | |
| 147 | + } | |
| 148 | + | |
| 149 | + return \gmdate( 'Y-m-d\TH:i:s\Z', $time ); | |
| 173 | 150 | } |
| 174 | 151 | |
| 175 | 152 | /** |
| 176 | 153 | * Returns the Inbox-API-Endpoint. |
| @@ -177,9 +154,9 @@ | ||
| 177 | 154 | * |
| 178 | 155 | * @return string The Inbox-Endpoint. |
| 179 | 156 | */ |
| 180 | 157 | public function get_inbox() { |
| 181 | - return get_rest_url_by_path( 'inbox' ); | |
| 158 | + return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) ); | |
| 182 | 159 | } |
| 183 | 160 | |
| 184 | 161 | /** |
| 185 | 162 | * Returns the Outbox-API-Endpoint. |
| @@ -186,9 +163,9 @@ | ||
| 186 | 163 | * |
| 187 | 164 | * @return string The Outbox-Endpoint. |
| 188 | 165 | */ |
| 189 | 166 | public function get_outbox() { |
| 190 | - return get_rest_url_by_path( 'application/outbox' ); | |
| 167 | + return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) ); | |
| 191 | 168 | } |
| 192 | 169 | |
| 193 | 170 | /** |
| 194 | 171 | * Returns a user@domain type of identifier for the user. |
| @@ -198,39 +175,30 @@ | ||
| 198 | 175 | public function get_webfinger() { |
| 199 | 176 | return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); |
| 200 | 177 | } |
| 201 | 178 | |
| 202 | - /** | |
| 203 | - * Returns the public key. | |
| 204 | - * | |
| 205 | - * @return string[] The public key. | |
| 206 | - */ | |
| 207 | 179 | public function get_public_key() { |
| 208 | 180 | return array( |
| 209 | - 'id' => Application_Utility::get_key_id(), | |
| 210 | - 'owner' => $this->get_id(), | |
| 211 | - 'publicKeyPem' => Application_Utility::get_public_key(), | |
| 181 | + 'id' => $this->get_id() . '#main-key', | |
| 182 | + 'owner' => $this->get_id(), | |
| 183 | + 'publicKeyPem' => Signature::get_public_key_for( Users::APPLICATION_USER_ID ), | |
| 212 | 184 | ); |
| 213 | 185 | } |
| 214 | 186 | |
| 215 | 187 | /** |
| 216 | - * Get the User description. | |
| 188 | + * Get the User-Description. | |
| 217 | 189 | * |
| 218 | - * @return string The User description. | |
| 190 | + * @return string The User-Description. | |
| 219 | 191 | */ |
| 220 | 192 | public function get_summary() { |
| 221 | - return \sprintf( | |
| 222 | - /* translators: %s: Domain of the site */ | |
| 223 | - \__( 'This is the Application Actor for %s.', 'activitypub' ), | |
| 224 | - home_host() | |
| 193 | + return \wpautop( | |
| 194 | + \wp_kses( | |
| 195 | + \get_bloginfo( 'description' ), | |
| 196 | + 'default' | |
| 197 | + ) | |
| 225 | 198 | ); |
| 226 | 199 | } |
| 227 | 200 | |
| 228 | - /** | |
| 229 | - * Returns the canonical URL of the object. | |
| 230 | - * | |
| 231 | - * @return string|null The canonical URL of the object. | |
| 232 | - */ | |
| 233 | 201 | public function get_canonical_url() { |
| 234 | 202 | return \home_url(); |
| 235 | 203 | } |
| 236 | 204 | } |