3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Protocol;
24 use Friendica\Core\Protocol;
25 use Friendica\Model\APContact;
26 use Friendica\Model\User;
27 use Friendica\Util\HTTPSignature;
28 use Friendica\Util\JsonLD;
31 * ActivityPub Protocol class
33 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
34 * https://www.w3.org/TR/activitypub/
35 * https://www.w3.org/TR/activitystreams-core/
36 * https://www.w3.org/TR/activitystreams-vocabulary/
38 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
39 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
41 * Digest: https://tools.ietf.org/html/rfc5843
42 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
44 * Mastodon implementation of supported activities:
45 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
48 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
51 * - Polling the outboxes for missing content?
53 * Missing parts from DFRN:
60 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
61 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
62 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
63 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
64 'diaspora' => 'https://diasporafoundation.org/ns/',
65 'litepub' => 'http://litepub.social/ns#',
66 'toot' => 'http://joinmastodon.org/ns#',
67 'schema' => 'http://schema.org#',
68 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
69 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
70 'directMessage' => 'litepub:directMessage',
71 'discoverable' => 'toot:discoverable',
72 'PropertyValue' => 'schema:PropertyValue',
73 'value' => 'schema:value',
75 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application', 'Tombstone'];
77 * Checks if the web request is done for the AP protocol
79 * @return bool is it AP?
81 public static function isRequest()
83 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
84 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') ||
85 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
89 * Fetches ActivityPub content from the given url
91 * @param string $url content url
92 * @param integer $uid User ID for the signature
94 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
96 public static function fetchContent(string $url, int $uid = 0)
98 return HTTPSignature::fetch($url, $uid);
101 private static function getAccountType($apcontact)
105 switch($apcontact['type']) {
107 $accounttype = User::ACCOUNT_TYPE_PERSON;
110 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
113 $accounttype = User::ACCOUNT_TYPE_NEWS;
116 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
119 $accounttype = User::ACCOUNT_TYPE_RELAY;
122 $accounttype = User::ACCOUNT_TYPE_DELETED;
130 * Fetches a profile from the given url into an array that is compatible to Probe::uri
132 * @param string $url profile url
133 * @param boolean $update Update the profile
135 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
136 * @throws \ImagickException
138 public static function probeProfile($url, $update = true)
140 $apcontact = APContact::getByURL($url, $update);
141 if (empty($apcontact)) {
145 $profile = ['network' => Protocol::ACTIVITYPUB];
146 $profile['nick'] = $apcontact['nick'];
147 $profile['name'] = $apcontact['name'];
148 $profile['guid'] = $apcontact['uuid'];
149 $profile['url'] = $apcontact['url'];
150 $profile['addr'] = $apcontact['addr'];
151 $profile['alias'] = $apcontact['alias'];
152 $profile['following'] = $apcontact['following'];
153 $profile['followers'] = $apcontact['followers'];
154 $profile['inbox'] = $apcontact['inbox'];
155 $profile['outbox'] = $apcontact['outbox'];
156 $profile['sharedinbox'] = $apcontact['sharedinbox'];
157 $profile['photo'] = $apcontact['photo'];
158 $profile['header'] = $apcontact['header'];
159 $profile['account-type'] = self::getAccountType($apcontact);
160 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
161 // $profile['keywords']
162 // $profile['location']
163 $profile['about'] = $apcontact['about'];
164 $profile['xmpp'] = $apcontact['xmpp'];
165 $profile['matrix'] = $apcontact['matrix'];
166 $profile['batch'] = $apcontact['sharedinbox'];
167 $profile['notify'] = $apcontact['inbox'];
168 $profile['poll'] = $apcontact['outbox'];
169 $profile['pubkey'] = $apcontact['pubkey'];
170 $profile['subscribe'] = $apcontact['subscribe'];
171 $profile['manually-approve'] = $apcontact['manually-approve'];
172 $profile['baseurl'] = $apcontact['baseurl'];
173 $profile['gsid'] = $apcontact['gsid'];
175 if (!is_null($apcontact['discoverable'])) {
176 $profile['hide'] = !$apcontact['discoverable'];
179 // Remove all "null" fields
180 foreach ($profile as $field => $content) {
181 if (is_null($content)) {
182 unset($profile[$field]);
190 * Fetches activities from the outbox of a given profile and processes it
193 * @param integer $uid User ID
194 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
196 public static function fetchOutbox($url, $uid)
198 $data = self::fetchContent($url, $uid);
203 if (!empty($data['orderedItems'])) {
204 $items = $data['orderedItems'];
205 } elseif (!empty($data['first']['orderedItems'])) {
206 $items = $data['first']['orderedItems'];
207 } elseif (!empty($data['first'])) {
208 self::fetchOutbox($data['first'], $uid);
214 foreach ($items as $activity) {
215 $ldactivity = JsonLD::compact($activity);
216 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
221 * Fetch items from AP endpoints
223 * @param string $url Address of the endpoint
224 * @param integer $uid Optional user id
225 * @return array Endpoint items
227 public static function fetchItems(string $url, int $uid = 0)
229 $data = self::fetchContent($url, $uid);
234 if (!empty($data['orderedItems'])) {
235 $items = $data['orderedItems'];
236 } elseif (!empty($data['first']['orderedItems'])) {
237 $items = $data['first']['orderedItems'];
238 } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) {
239 return self::fetchItems($data['first'], $uid);
244 if (!empty($data['next']) && is_string($data['next'])) {
245 $items = array_merge($items, self::fetchItems($data['next'], $uid));
252 * Checks if the given contact url does support ActivityPub
254 * @param string $url profile url
255 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
257 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
258 * @throws \ImagickException
260 public static function isSupportedByContactUrl($url, $update = null)
262 return !empty(APContact::getByURL($url, $update));