3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
7 use Friendica\Util\JsonLD;
8 use Friendica\Util\Network;
9 use Friendica\Core\Protocol;
10 use Friendica\Model\APContact;
11 use Friendica\Util\HTTPSignature;
14 * @brief ActivityPub Protocol class
15 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
16 * https://www.w3.org/TR/activitypub/
17 * https://www.w3.org/TR/activitystreams-core/
18 * https://www.w3.org/TR/activitystreams-vocabulary/
20 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
21 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
23 * Digest: https://tools.ietf.org/html/rfc5843
24 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
26 * Mastodon implementation of supported activities:
27 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
30 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
33 * - Polling the outboxes for missing content?
35 * Missing parts from DFRN:
42 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
43 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
44 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
45 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
46 'diaspora' => 'https://diasporafoundation.org/ns/',
47 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
48 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
49 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
51 * Checks if the web request is done for the AP protocol
53 * @return bool is it AP?
55 public static function isRequest()
57 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
58 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
62 * Fetches ActivityPub content from the given url
64 * @param string $url content url
65 * @param integer $uid User ID for the signature
67 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
69 public static function fetchContent($url, $uid = 0)
72 return HTTPSignature::fetch($url, $uid);
75 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
76 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
80 $content = json_decode($curlResult->getBody(), true);
82 if (empty($content) || !is_array($content)) {
90 * Fetches a profile from the given url into an array that is compatible to Probe::uri
92 * @param string $url profile url
94 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
95 * @throws \ImagickException
97 public static function probeProfile($url)
99 $apcontact = APContact::getByURL($url, true);
100 if (empty($apcontact)) {
104 $profile = ['network' => Protocol::ACTIVITYPUB];
105 $profile['nick'] = $apcontact['nick'];
106 $profile['name'] = $apcontact['name'];
107 $profile['guid'] = $apcontact['uuid'];
108 $profile['url'] = $apcontact['url'];
109 $profile['addr'] = $apcontact['addr'];
110 $profile['alias'] = $apcontact['alias'];
111 $profile['photo'] = $apcontact['photo'];
112 // $profile['community']
113 // $profile['keywords']
114 // $profile['location']
115 $profile['about'] = $apcontact['about'];
116 $profile['batch'] = $apcontact['sharedinbox'];
117 $profile['notify'] = $apcontact['inbox'];
118 $profile['poll'] = $apcontact['outbox'];
119 $profile['pubkey'] = $apcontact['pubkey'];
120 $profile['baseurl'] = $apcontact['baseurl'];
122 // Remove all "null" fields
123 foreach ($profile as $field => $content) {
124 if (is_null($content)) {
125 unset($profile[$field]);
133 * Fetches activities from the outbox of a given profile and processes it
136 * @param integer $uid User ID
137 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
139 public static function fetchOutbox($url, $uid)
141 $data = self::fetchContent($url);
146 if (!empty($data['orderedItems'])) {
147 $items = $data['orderedItems'];
148 } elseif (!empty($data['first']['orderedItems'])) {
149 $items = $data['first']['orderedItems'];
150 } elseif (!empty($data['first'])) {
151 self::fetchOutbox($data['first'], $uid);
157 foreach ($items as $activity) {
158 $ldactivity = JsonLD::compact($activity);
159 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);