3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Protocol;
24 use Friendica\Core\Protocol;
25 use Friendica\Model\APContact;
26 use Friendica\Model\User;
27 use Friendica\Util\HTTPSignature;
28 use Friendica\Util\JsonLD;
31 * ActivityPub Protocol class
33 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
34 * https://www.w3.org/TR/activitypub/
35 * https://www.w3.org/TR/activitystreams-core/
36 * https://www.w3.org/TR/activitystreams-vocabulary/
38 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
39 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
41 * Digest: https://tools.ietf.org/html/rfc5843
42 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
44 * Mastodon implementation of supported activities:
45 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
48 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
51 * - Polling the outboxes for missing content?
53 * Missing parts from DFRN:
60 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
61 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
62 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
63 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
64 'diaspora' => 'https://diasporafoundation.org/ns/',
65 'litepub' => 'http://litepub.social/ns#',
66 'toot' => 'http://joinmastodon.org/ns#',
67 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
68 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
69 'directMessage' => 'litepub:directMessage',
70 'discoverable' => 'toot:discoverable']];
71 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application', 'Tombstone'];
73 * Checks if the web request is done for the AP protocol
75 * @return bool is it AP?
77 public static function isRequest()
79 return stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
80 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') ||
81 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
85 * Fetches ActivityPub content from the given url
87 * @param string $url content url
88 * @param integer $uid User ID for the signature
90 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
92 public static function fetchContent(string $url, int $uid = 0)
94 return HTTPSignature::fetch($url, $uid);
97 private static function getAccountType($apcontact)
101 switch($apcontact['type']) {
103 $accounttype = User::ACCOUNT_TYPE_PERSON;
106 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
109 $accounttype = User::ACCOUNT_TYPE_NEWS;
112 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
115 $accounttype = User::ACCOUNT_TYPE_RELAY;
118 $accounttype = User::ACCOUNT_TYPE_DELETED;
126 * Fetches a profile from the given url into an array that is compatible to Probe::uri
128 * @param string $url profile url
129 * @param boolean $update Update the profile
131 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
132 * @throws \ImagickException
134 public static function probeProfile($url, $update = true)
136 $apcontact = APContact::getByURL($url, $update);
137 if (empty($apcontact)) {
141 $profile = ['network' => Protocol::ACTIVITYPUB];
142 $profile['nick'] = $apcontact['nick'];
143 $profile['name'] = $apcontact['name'];
144 $profile['guid'] = $apcontact['uuid'];
145 $profile['url'] = $apcontact['url'];
146 $profile['addr'] = $apcontact['addr'];
147 $profile['alias'] = $apcontact['alias'];
148 $profile['following'] = $apcontact['following'];
149 $profile['followers'] = $apcontact['followers'];
150 $profile['inbox'] = $apcontact['inbox'];
151 $profile['outbox'] = $apcontact['outbox'];
152 $profile['sharedinbox'] = $apcontact['sharedinbox'];
153 $profile['photo'] = $apcontact['photo'];
154 $profile['header'] = $apcontact['header'];
155 $profile['account-type'] = self::getAccountType($apcontact);
156 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
157 // $profile['keywords']
158 // $profile['location']
159 $profile['about'] = $apcontact['about'];
160 $profile['batch'] = $apcontact['sharedinbox'];
161 $profile['notify'] = $apcontact['inbox'];
162 $profile['poll'] = $apcontact['outbox'];
163 $profile['pubkey'] = $apcontact['pubkey'];
164 $profile['subscribe'] = $apcontact['subscribe'];
165 $profile['manually-approve'] = $apcontact['manually-approve'];
166 $profile['baseurl'] = $apcontact['baseurl'];
167 $profile['gsid'] = $apcontact['gsid'];
169 if (!is_null($apcontact['discoverable'])) {
170 $profile['hide'] = !$apcontact['discoverable'];
173 // Remove all "null" fields
174 foreach ($profile as $field => $content) {
175 if (is_null($content)) {
176 unset($profile[$field]);
184 * Fetches activities from the outbox of a given profile and processes it
187 * @param integer $uid User ID
188 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
190 public static function fetchOutbox($url, $uid)
192 $data = self::fetchContent($url, $uid);
197 if (!empty($data['orderedItems'])) {
198 $items = $data['orderedItems'];
199 } elseif (!empty($data['first']['orderedItems'])) {
200 $items = $data['first']['orderedItems'];
201 } elseif (!empty($data['first'])) {
202 self::fetchOutbox($data['first'], $uid);
208 foreach ($items as $activity) {
209 $ldactivity = JsonLD::compact($activity);
210 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
215 * Fetch items from AP endpoints
217 * @param string $url Address of the endpoint
218 * @param integer $uid Optional user id
219 * @return array Endpoint items
221 public static function fetchItems(string $url, int $uid = 0)
223 $data = self::fetchContent($url, $uid);
228 if (!empty($data['orderedItems'])) {
229 $items = $data['orderedItems'];
230 } elseif (!empty($data['first']['orderedItems'])) {
231 $items = $data['first']['orderedItems'];
232 } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) {
233 return self::fetchItems($data['first'], $uid);
238 if (!empty($data['next']) && is_string($data['next'])) {
239 $items = array_merge($items, self::fetchItems($data['next'], $uid));
246 * Checks if the given contact url does support ActivityPub
248 * @param string $url profile url
249 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
251 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
252 * @throws \ImagickException
254 public static function isSupportedByContactUrl($url, $update = null)
256 return !empty(APContact::getByURL($url, $update));