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'];
42 const CONTENT_TYPES = ['Note', 'Article', 'Video', 'Image'];
43 const ACTIVITY_TYPES = ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'];
45 * Checks if the web request is done for the AP protocol
49 public static function isRequest()
51 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
52 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
56 * Fetches ActivityPub content from the given url
58 * @param string $url content url
61 public static function fetchContent($url)
63 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
64 if (!$ret['success'] || empty($ret['body'])) {
68 return json_decode($ret['body'], true);
72 * Fetches a profile from the given url into an array that is compatible to Probe::uri
74 * @param string $url profile url
77 public static function probeProfile($url)
79 $apcontact = APContact::getByURL($url, true);
80 if (empty($apcontact)) {
84 $profile = ['network' => Protocol::ACTIVITYPUB];
85 $profile['nick'] = $apcontact['nick'];
86 $profile['name'] = $apcontact['name'];
87 $profile['guid'] = $apcontact['uuid'];
88 $profile['url'] = $apcontact['url'];
89 $profile['addr'] = $apcontact['addr'];
90 $profile['alias'] = $apcontact['alias'];
91 $profile['photo'] = $apcontact['photo'];
92 // $profile['community']
93 // $profile['keywords']
94 // $profile['location']
95 $profile['about'] = $apcontact['about'];
96 $profile['batch'] = $apcontact['sharedinbox'];
97 $profile['notify'] = $apcontact['inbox'];
98 $profile['poll'] = $apcontact['outbox'];
99 $profile['pubkey'] = $apcontact['pubkey'];
100 $profile['baseurl'] = $apcontact['baseurl'];
102 // Remove all "null" fields
103 foreach ($profile as $field => $content) {
104 if (is_null($content)) {
105 unset($profile[$field]);
113 * Fetches activities from the outbox of a given profile and processes it
116 * @param integer $uid User ID
118 public static function fetchOutbox($url, $uid)
120 $data = self::fetchContent($url);
125 if (!empty($data['orderedItems'])) {
126 $items = $data['orderedItems'];
127 } elseif (!empty($data['first']['orderedItems'])) {
128 $items = $data['first']['orderedItems'];
129 } elseif (!empty($data['first'])) {
130 self::fetchOutbox($data['first'], $uid);
136 foreach ($items as $activity) {
137 ActivityPub\Receiver::processActivity($activity, '', $uid, true);