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;
10 use Friendica\Util\HTTPSignature;
13 * @brief ActivityPub Protocol class
14 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
15 * https://www.w3.org/TR/activitypub/
16 * https://www.w3.org/TR/activitystreams-core/
17 * https://www.w3.org/TR/activitystreams-vocabulary/
19 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
20 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
22 * Digest: https://tools.ietf.org/html/rfc5843
23 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
25 * Mastodon implementation of supported activities:
26 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
29 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
32 * - Polling the outboxes for missing content?
34 * Missing parts from DFRN:
41 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
42 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
43 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
44 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
45 'diaspora' => 'https://diasporafoundation.org/ns/',
46 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
47 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
48 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
50 * Checks if the web request is done for the AP protocol
54 public static function isRequest()
56 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
57 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
61 * Fetches ActivityPub content from the given url
63 * @param string $url content url
64 * @param integer $uid User ID for the signature
67 public static function fetchContent($url, $uid = 0)
70 return HTTPSignature::fetch($url, 1);
73 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
74 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
78 $content = json_decode($curlResult->getBody(), true);
80 if (empty($content) || !is_array($content)) {
88 * Fetches a profile from the given url into an array that is compatible to Probe::uri
90 * @param string $url profile url
93 public static function probeProfile($url)
95 $apcontact = APContact::getByURL($url, true);
96 if (empty($apcontact)) {
100 $profile = ['network' => Protocol::ACTIVITYPUB];
101 $profile['nick'] = $apcontact['nick'];
102 $profile['name'] = $apcontact['name'];
103 $profile['guid'] = $apcontact['uuid'];
104 $profile['url'] = $apcontact['url'];
105 $profile['addr'] = $apcontact['addr'];
106 $profile['alias'] = $apcontact['alias'];
107 $profile['photo'] = $apcontact['photo'];
108 // $profile['community']
109 // $profile['keywords']
110 // $profile['location']
111 $profile['about'] = $apcontact['about'];
112 $profile['batch'] = $apcontact['sharedinbox'];
113 $profile['notify'] = $apcontact['inbox'];
114 $profile['poll'] = $apcontact['outbox'];
115 $profile['pubkey'] = $apcontact['pubkey'];
116 $profile['baseurl'] = $apcontact['baseurl'];
118 // Remove all "null" fields
119 foreach ($profile as $field => $content) {
120 if (is_null($content)) {
121 unset($profile[$field]);
129 * Fetches activities from the outbox of a given profile and processes it
132 * @param integer $uid User ID
134 public static function fetchOutbox($url, $uid)
136 $data = self::fetchContent($url);
141 if (!empty($data['orderedItems'])) {
142 $items = $data['orderedItems'];
143 } elseif (!empty($data['first']['orderedItems'])) {
144 $items = $data['first']['orderedItems'];
145 } elseif (!empty($data['first'])) {
146 self::fetchOutbox($data['first'], $uid);
152 foreach ($items as $activity) {
153 $ldactivity = JsonLD::compact($activity);
154 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);