3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
29 * @brief ActivityPub Protocol class
30 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
31 * https://www.w3.org/TR/activitypub/
32 * https://www.w3.org/TR/activitystreams-core/
33 * https://www.w3.org/TR/activitystreams-vocabulary/
35 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
36 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
38 * Digest: https://tools.ietf.org/html/rfc5843
39 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
41 * Mastodon implementation of supported activities:
42 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
45 * - Polling the outboxes for missing content?
49 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
50 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
51 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
52 'diaspora' => 'https://diasporafoundation.org/ns/',
53 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
54 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag']];
55 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
56 const CONTENT_TYPES = ['Note', 'Article', 'Video', 'Image'];
57 const ACTIVITY_TYPES = ['Like', 'Dislike', 'Accept', 'Reject', 'TentativeAccept'];
59 * @brief Checks if the web request is done for the AP protocol
63 public static function isRequest()
65 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
66 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
70 * Fetches ActivityPub content from the given url
72 * @param string $url content url
75 public static function fetchContent($url)
77 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
78 if (!$ret['success'] || empty($ret['body'])) {
82 return json_decode($ret['body'], true);
86 * Fetches a profile from the given url into an array that is compatible to Probe::uri
88 * @param string $url profile url
91 public static function probeProfile($url)
93 $apcontact = APContact::getByURL($url, true);
94 if (empty($apcontact)) {
98 $profile = ['network' => Protocol::ACTIVITYPUB];
99 $profile['nick'] = $apcontact['nick'];
100 $profile['name'] = $apcontact['name'];
101 $profile['guid'] = $apcontact['uuid'];
102 $profile['url'] = $apcontact['url'];
103 $profile['addr'] = $apcontact['addr'];
104 $profile['alias'] = $apcontact['alias'];
105 $profile['photo'] = $apcontact['photo'];
106 // $profile['community']
107 // $profile['keywords']
108 // $profile['location']
109 $profile['about'] = $apcontact['about'];
110 $profile['batch'] = $apcontact['sharedinbox'];
111 $profile['notify'] = $apcontact['inbox'];
112 $profile['poll'] = $apcontact['outbox'];
113 $profile['pubkey'] = $apcontact['pubkey'];
114 $profile['baseurl'] = $apcontact['baseurl'];
116 // Remove all "null" fields
117 foreach ($profile as $field => $content) {
118 if (is_null($content)) {
119 unset($profile[$field]);
130 * @param integer $uid User ID
132 public static function fetchOutbox($url, $uid)
134 $data = self::fetchContent($url);
139 if (!empty($data['orderedItems'])) {
140 $items = $data['orderedItems'];
141 } elseif (!empty($data['first']['orderedItems'])) {
142 $items = $data['first']['orderedItems'];
143 } elseif (!empty($data['first'])) {
144 self::fetchOutbox($data['first'], $uid);
150 foreach ($items as $activity) {
151 ActivityPub\Receiver::processActivity($activity, '', $uid, true);