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 $content = json_decode($curlResult->getBody(), true);
68 if (empty($content) || !is_array($content)) {
76 * Fetches a profile from the given url into an array that is compatible to Probe::uri
78 * @param string $url profile url
81 public static function probeProfile($url)
83 $apcontact = APContact::getByURL($url, true);
84 if (empty($apcontact)) {
88 $profile = ['network' => Protocol::ACTIVITYPUB];
89 $profile['nick'] = $apcontact['nick'];
90 $profile['name'] = $apcontact['name'];
91 $profile['guid'] = $apcontact['uuid'];
92 $profile['url'] = $apcontact['url'];
93 $profile['addr'] = $apcontact['addr'];
94 $profile['alias'] = $apcontact['alias'];
95 $profile['photo'] = $apcontact['photo'];
96 // $profile['community']
97 // $profile['keywords']
98 // $profile['location']
99 $profile['about'] = $apcontact['about'];
100 $profile['batch'] = $apcontact['sharedinbox'];
101 $profile['notify'] = $apcontact['inbox'];
102 $profile['poll'] = $apcontact['outbox'];
103 $profile['pubkey'] = $apcontact['pubkey'];
104 $profile['baseurl'] = $apcontact['baseurl'];
106 // Remove all "null" fields
107 foreach ($profile as $field => $content) {
108 if (is_null($content)) {
109 unset($profile[$field]);
117 * Fetches activities from the outbox of a given profile and processes it
120 * @param integer $uid User ID
122 public static function fetchOutbox($url, $uid)
124 $data = self::fetchContent($url);
129 if (!empty($data['orderedItems'])) {
130 $items = $data['orderedItems'];
131 } elseif (!empty($data['first']['orderedItems'])) {
132 $items = $data['first']['orderedItems'];
133 } elseif (!empty($data['first'])) {
134 self::fetchOutbox($data['first'], $uid);
140 foreach ($items as $activity) {
141 $ldactivity = JsonLD::compact($activity);
142 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);