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?
33 * Missing parts from DFRN:
40 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
41 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
42 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
43 'diaspora' => 'https://diasporafoundation.org/ns/',
44 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
45 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
46 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
48 * Checks if the web request is done for the AP protocol
52 public static function isRequest()
54 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
55 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
59 * Fetches ActivityPub content from the given url
61 * @param string $url content url
64 public static function fetchContent($url)
66 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
67 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
71 $content = json_decode($curlResult->getBody(), true);
73 if (empty($content) || !is_array($content)) {
81 * Fetches a profile from the given url into an array that is compatible to Probe::uri
83 * @param string $url profile url
86 public static function probeProfile($url)
88 $apcontact = APContact::getByURL($url, true);
89 if (empty($apcontact)) {
93 $profile = ['network' => Protocol::ACTIVITYPUB];
94 $profile['nick'] = $apcontact['nick'];
95 $profile['name'] = $apcontact['name'];
96 $profile['guid'] = $apcontact['uuid'];
97 $profile['url'] = $apcontact['url'];
98 $profile['addr'] = $apcontact['addr'];
99 $profile['alias'] = $apcontact['alias'];
100 $profile['photo'] = $apcontact['photo'];
101 // $profile['community']
102 // $profile['keywords']
103 // $profile['location']
104 $profile['about'] = $apcontact['about'];
105 $profile['batch'] = $apcontact['sharedinbox'];
106 $profile['notify'] = $apcontact['inbox'];
107 $profile['poll'] = $apcontact['outbox'];
108 $profile['pubkey'] = $apcontact['pubkey'];
109 $profile['baseurl'] = $apcontact['baseurl'];
111 // Remove all "null" fields
112 foreach ($profile as $field => $content) {
113 if (is_null($content)) {
114 unset($profile[$field]);
122 * Fetches activities from the outbox of a given profile and processes it
125 * @param integer $uid User ID
127 public static function fetchOutbox($url, $uid)
129 $data = self::fetchContent($url);
134 if (!empty($data['orderedItems'])) {
135 $items = $data['orderedItems'];
136 } elseif (!empty($data['first']['orderedItems'])) {
137 $items = $data['first']['orderedItems'];
138 } elseif (!empty($data['first'])) {
139 self::fetchOutbox($data['first'], $uid);
145 foreach ($items as $activity) {
146 $ldactivity = JsonLD::compact($activity);
147 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);