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 * ActivityPub Protocol class
17 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
18 * https://www.w3.org/TR/activitypub/
19 * https://www.w3.org/TR/activitystreams-core/
20 * https://www.w3.org/TR/activitystreams-vocabulary/
22 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
23 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
25 * Digest: https://tools.ietf.org/html/rfc5843
26 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
28 * Mastodon implementation of supported activities:
29 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
32 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
35 * - Polling the outboxes for missing content?
37 * Missing parts from DFRN:
44 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
45 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
46 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
47 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
48 'diaspora' => 'https://diasporafoundation.org/ns/',
49 'litepub' => 'http://litepub.social/ns#',
50 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
51 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
52 'directMessage' => 'litepub:directMessage']];
53 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
55 * Checks if the web request is done for the AP protocol
57 * @return bool is it AP?
59 public static function isRequest()
61 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
62 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
66 * Fetches ActivityPub content from the given url
68 * @param string $url content url
69 * @param integer $uid User ID for the signature
71 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
73 public static function fetchContent($url, $uid = 0)
76 return HTTPSignature::fetch($url, $uid);
79 $curlResult = Network::curl($url, false, ['accept_content' => 'application/activity+json, application/ld+json']);
80 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
84 $content = json_decode($curlResult->getBody(), true);
86 if (empty($content) || !is_array($content)) {
93 private static function getAccountType($apcontact)
97 switch($apcontact['type']) {
99 $accounttype = User::ACCOUNT_TYPE_PERSON;
102 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
105 $accounttype = User::ACCOUNT_TYPE_NEWS;
108 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
111 $accounttype = User::ACCOUNT_TYPE_RELAY;
119 * Fetches a profile from the given url into an array that is compatible to Probe::uri
121 * @param string $url profile url
122 * @param boolean $update Update the profile
124 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
125 * @throws \ImagickException
127 public static function probeProfile($url, $update = true)
129 $apcontact = APContact::getByURL($url, $update);
130 if (empty($apcontact)) {
134 $profile = ['network' => Protocol::ACTIVITYPUB];
135 $profile['nick'] = $apcontact['nick'];
136 $profile['name'] = $apcontact['name'];
137 $profile['guid'] = $apcontact['uuid'];
138 $profile['url'] = $apcontact['url'];
139 $profile['addr'] = $apcontact['addr'];
140 $profile['alias'] = $apcontact['alias'];
141 $profile['following'] = $apcontact['following'];
142 $profile['followers'] = $apcontact['followers'];
143 $profile['inbox'] = $apcontact['inbox'];
144 $profile['outbox'] = $apcontact['outbox'];
145 $profile['sharedinbox'] = $apcontact['sharedinbox'];
146 $profile['photo'] = $apcontact['photo'];
147 $profile['account-type'] = self::getAccountType($apcontact);
148 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
149 // $profile['keywords']
150 // $profile['location']
151 $profile['about'] = $apcontact['about'];
152 $profile['batch'] = $apcontact['sharedinbox'];
153 $profile['notify'] = $apcontact['inbox'];
154 $profile['poll'] = $apcontact['outbox'];
155 $profile['pubkey'] = $apcontact['pubkey'];
156 $profile['baseurl'] = $apcontact['baseurl'];
158 // Remove all "null" fields
159 foreach ($profile as $field => $content) {
160 if (is_null($content)) {
161 unset($profile[$field]);
169 * Fetches activities from the outbox of a given profile and processes it
172 * @param integer $uid User ID
173 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
175 public static function fetchOutbox($url, $uid)
177 $data = self::fetchContent($url);
182 if (!empty($data['orderedItems'])) {
183 $items = $data['orderedItems'];
184 } elseif (!empty($data['first']['orderedItems'])) {
185 $items = $data['first']['orderedItems'];
186 } elseif (!empty($data['first'])) {
187 self::fetchOutbox($data['first'], $uid);
193 foreach ($items as $activity) {
194 $ldactivity = JsonLD::compact($activity);
195 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
200 * Checks if the given contact url does support ActivityPub
202 * @param string $url profile url
203 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
205 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
206 * @throws \ImagickException
208 public static function isSupportedByContactUrl($url, $update = null)
210 return !empty(APContact::getByURL($url, $update));