3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
7 use Friendica\Util\Network;
8 use Friendica\Core\Protocol;
9 use Friendica\Model\APContact;
12 * @brief ActivityPub Protocol class
13 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
14 * https://www.w3.org/TR/activitypub/
15 * https://www.w3.org/TR/activitystreams-core/
16 * https://www.w3.org/TR/activitystreams-vocabulary/
18 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
19 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
21 * Digest: https://tools.ietf.org/html/rfc5843
22 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
24 * Mastodon implementation of supported activities:
25 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
28 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
31 * - Polling the outboxes for missing content?
35 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
36 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
37 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
38 'diaspora' => 'https://diasporafoundation.org/ns/',
39 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
40 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
41 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
43 * Checks if the web request is done for the AP protocol
47 public static function isRequest()
49 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
50 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
54 * Fetches ActivityPub content from the given url
56 * @param string $url content url
59 public static function fetchContent($url)
61 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
62 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
66 return json_decode($curlResult->getBody(), true);
70 * Fetches a profile from the given url into an array that is compatible to Probe::uri
72 * @param string $url profile url
75 public static function probeProfile($url)
77 $apcontact = APContact::getByURL($url, true);
78 if (empty($apcontact)) {
82 $profile = ['network' => Protocol::ACTIVITYPUB];
83 $profile['nick'] = $apcontact['nick'];
84 $profile['name'] = $apcontact['name'];
85 $profile['guid'] = $apcontact['uuid'];
86 $profile['url'] = $apcontact['url'];
87 $profile['addr'] = $apcontact['addr'];
88 $profile['alias'] = $apcontact['alias'];
89 $profile['photo'] = $apcontact['photo'];
90 // $profile['community']
91 // $profile['keywords']
92 // $profile['location']
93 $profile['about'] = $apcontact['about'];
94 $profile['batch'] = $apcontact['sharedinbox'];
95 $profile['notify'] = $apcontact['inbox'];
96 $profile['poll'] = $apcontact['outbox'];
97 $profile['pubkey'] = $apcontact['pubkey'];
98 $profile['baseurl'] = $apcontact['baseurl'];
100 // Remove all "null" fields
101 foreach ($profile as $field => $content) {
102 if (is_null($content)) {
103 unset($profile[$field]);
111 * Fetches activities from the outbox of a given profile and processes it
114 * @param integer $uid User ID
116 public static function fetchOutbox($url, $uid)
118 $data = self::fetchContent($url);
123 if (!empty($data['orderedItems'])) {
124 $items = $data['orderedItems'];
125 } elseif (!empty($data['first']['orderedItems'])) {
126 $items = $data['first']['orderedItems'];
127 } elseif (!empty($data['first'])) {
128 self::fetchOutbox($data['first'], $uid);
134 foreach ($items as $activity) {
135 $ldactivity = JsonLD::compact($activity);
136 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);