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 'litepub' => 'http://litepub.social/ns#',
48 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
49 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
50 'directMessage' => 'litepub:directMessage']];
51 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
53 * Checks if the web request is done for the AP protocol
55 * @return bool is it AP?
57 public static function isRequest()
59 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
60 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
64 * Fetches ActivityPub content from the given url
66 * @param string $url content url
67 * @param integer $uid User ID for the signature
69 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
71 public static function fetchContent($url, $uid = 0)
74 return HTTPSignature::fetch($url, $uid);
77 $curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
78 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
82 $content = json_decode($curlResult->getBody(), true);
84 if (empty($content) || !is_array($content)) {
92 * Fetches a profile from the given url into an array that is compatible to Probe::uri
94 * @param string $url profile url
95 * @param boolean $update Update the profile
97 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
98 * @throws \ImagickException
100 public static function probeProfile($url, $update = true)
102 $apcontact = APContact::getByURL($url, $update);
103 if (empty($apcontact)) {
107 $profile = ['network' => Protocol::ACTIVITYPUB];
108 $profile['nick'] = $apcontact['nick'];
109 $profile['name'] = $apcontact['name'];
110 $profile['guid'] = $apcontact['uuid'];
111 $profile['url'] = $apcontact['url'];
112 $profile['addr'] = $apcontact['addr'];
113 $profile['alias'] = $apcontact['alias'];
114 $profile['photo'] = $apcontact['photo'];
115 // $profile['community']
116 // $profile['keywords']
117 // $profile['location']
118 $profile['about'] = $apcontact['about'];
119 $profile['batch'] = $apcontact['sharedinbox'];
120 $profile['notify'] = $apcontact['inbox'];
121 $profile['poll'] = $apcontact['outbox'];
122 $profile['pubkey'] = $apcontact['pubkey'];
123 $profile['baseurl'] = $apcontact['baseurl'];
125 // Remove all "null" fields
126 foreach ($profile as $field => $content) {
127 if (is_null($content)) {
128 unset($profile[$field]);
136 * Fetches activities from the outbox of a given profile and processes it
139 * @param integer $uid User ID
140 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
142 public static function fetchOutbox($url, $uid)
144 $data = self::fetchContent($url);
149 if (!empty($data['orderedItems'])) {
150 $items = $data['orderedItems'];
151 } elseif (!empty($data['first']['orderedItems'])) {
152 $items = $data['first']['orderedItems'];
153 } elseif (!empty($data['first'])) {
154 self::fetchOutbox($data['first'], $uid);
160 foreach ($items as $activity) {
161 $ldactivity = JsonLD::compact($activity);
162 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);