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\Model\User;
12 use Friendica\Util\HTTPSignature;
15 * @brief ActivityPub Protocol class
16 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
17 * https://www.w3.org/TR/activitypub/
18 * https://www.w3.org/TR/activitystreams-core/
19 * https://www.w3.org/TR/activitystreams-vocabulary/
21 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
22 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
24 * Digest: https://tools.ietf.org/html/rfc5843
25 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
27 * Mastodon implementation of supported activities:
28 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
31 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
34 * - Polling the outboxes for missing content?
36 * Missing parts from DFRN:
43 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
44 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
45 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
46 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
47 'diaspora' => 'https://diasporafoundation.org/ns/',
48 'litepub' => 'http://litepub.social/ns#',
49 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
50 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
51 'directMessage' => 'litepub:directMessage']];
52 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
54 * Checks if the web request is done for the AP protocol
56 * @return bool is it AP?
58 public static function isRequest()
60 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
61 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
65 * Fetches ActivityPub content from the given url
67 * @param string $url content url
68 * @param integer $uid User ID for the signature
70 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
72 public static function fetchContent($url, $uid = 0)
75 return HTTPSignature::fetch($url, $uid);
78 $curlResult = Network::curl($url, false, ['accept_content' => 'application/activity+json, application/ld+json']);
79 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
83 $content = json_decode($curlResult->getBody(), true);
85 if (empty($content) || !is_array($content)) {
92 private static function getAccountType($apcontact)
96 switch($apcontact['type']) {
98 $accounttype = User::ACCOUNT_TYPE_PERSON;
101 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
104 $accounttype = User::ACCOUNT_TYPE_NEWS;
107 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
110 $accounttype = User::ACCOUNT_TYPE_RELAY;
118 * Fetches a profile from the given url into an array that is compatible to Probe::uri
120 * @param string $url profile url
121 * @param boolean $update Update the profile
123 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
124 * @throws \ImagickException
126 public static function probeProfile($url, $update = true)
128 $apcontact = APContact::getByURL($url, $update);
129 if (empty($apcontact)) {
133 $profile = ['network' => Protocol::ACTIVITYPUB];
134 $profile['nick'] = $apcontact['nick'];
135 $profile['name'] = $apcontact['name'];
136 $profile['guid'] = $apcontact['uuid'];
137 $profile['url'] = $apcontact['url'];
138 $profile['addr'] = $apcontact['addr'];
139 $profile['alias'] = $apcontact['alias'];
140 $profile['following'] = $apcontact['following'];
141 $profile['followers'] = $apcontact['followers'];
142 $profile['inbox'] = $apcontact['inbox'];
143 $profile['outbox'] = $apcontact['outbox'];
144 $profile['sharedinbox'] = $apcontact['sharedinbox'];
145 $profile['photo'] = $apcontact['photo'];
146 $profile['account-type'] = self::getAccountType($apcontact);
147 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
148 // $profile['keywords']
149 // $profile['location']
150 $profile['about'] = $apcontact['about'];
151 $profile['batch'] = $apcontact['sharedinbox'];
152 $profile['notify'] = $apcontact['inbox'];
153 $profile['poll'] = $apcontact['outbox'];
154 $profile['pubkey'] = $apcontact['pubkey'];
155 $profile['baseurl'] = $apcontact['baseurl'];
157 // Remove all "null" fields
158 foreach ($profile as $field => $content) {
159 if (is_null($content)) {
160 unset($profile[$field]);
168 * Fetches activities from the outbox of a given profile and processes it
171 * @param integer $uid User ID
172 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
174 public static function fetchOutbox($url, $uid)
176 $data = self::fetchContent($url);
181 if (!empty($data['orderedItems'])) {
182 $items = $data['orderedItems'];
183 } elseif (!empty($data['first']['orderedItems'])) {
184 $items = $data['first']['orderedItems'];
185 } elseif (!empty($data['first'])) {
186 self::fetchOutbox($data['first'], $uid);
192 foreach ($items as $activity) {
193 $ldactivity = JsonLD::compact($activity);
194 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
199 * Checks if the given contact url does support ActivityPub
201 * @param string $url profile url
202 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
204 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
205 * @throws \ImagickException
207 public static function isSupportedByContactUrl($url, $update = null)
209 return !empty(APContact::getByURL($url, $update));