3 * @copyright Copyright (C) 2010-2022, 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\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Model\APContact;
27 use Friendica\Model\User;
28 use Friendica\Util\HTTPSignature;
29 use Friendica\Util\JsonLD;
32 * ActivityPub Protocol class
34 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
35 * https://www.w3.org/TR/activitypub/
36 * https://www.w3.org/TR/activitystreams-core/
37 * https://www.w3.org/TR/activitystreams-vocabulary/
39 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
40 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
42 * Digest: https://tools.ietf.org/html/rfc5843
43 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
45 * Mastodon implementation of supported activities:
46 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
49 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
52 * - Polling the outboxes for missing content?
54 * Missing parts from DFRN:
61 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
62 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
63 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
64 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
65 'diaspora' => 'https://diasporafoundation.org/ns/',
66 'litepub' => 'http://litepub.social/ns#',
67 'toot' => 'http://joinmastodon.org/ns#',
68 'schema' => 'http://schema.org#',
69 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
70 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
71 'directMessage' => 'litepub:directMessage',
72 'discoverable' => 'toot:discoverable',
73 'PropertyValue' => 'schema:PropertyValue',
74 'value' => 'schema:value',
76 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application', 'Tombstone'];
78 * Checks if the web request is done for the AP protocol
80 * @return bool is it AP?
82 public static function isRequest()
84 $isrequest = stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/activity+json') ||
85 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') ||
86 stristr($_SERVER['HTTP_ACCEPT'] ?? '', 'application/ld+json');
89 Logger::debug('Is AP request', ['accept' => $_SERVER['HTTP_ACCEPT'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
96 * Fetches ActivityPub content from the given url
98 * @param string $url content url
99 * @param integer $uid User ID for the signature
101 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
103 public static function fetchContent(string $url, int $uid = 0)
105 return HTTPSignature::fetch($url, $uid);
108 private static function getAccountType($apcontact)
112 switch($apcontact['type']) {
114 $accounttype = User::ACCOUNT_TYPE_PERSON;
117 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
120 $accounttype = User::ACCOUNT_TYPE_NEWS;
123 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
126 $accounttype = User::ACCOUNT_TYPE_RELAY;
129 $accounttype = User::ACCOUNT_TYPE_DELETED;
137 * Fetches a profile from the given url into an array that is compatible to Probe::uri
139 * @param string $url profile url
140 * @param boolean $update Update the profile
142 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
143 * @throws \ImagickException
145 public static function probeProfile($url, $update = true)
147 $apcontact = APContact::getByURL($url, $update);
148 if (empty($apcontact)) {
152 $profile = ['network' => Protocol::ACTIVITYPUB];
153 $profile['nick'] = $apcontact['nick'];
154 $profile['name'] = $apcontact['name'];
155 $profile['guid'] = $apcontact['uuid'];
156 $profile['url'] = $apcontact['url'];
157 $profile['addr'] = $apcontact['addr'];
158 $profile['alias'] = $apcontact['alias'];
159 $profile['following'] = $apcontact['following'];
160 $profile['followers'] = $apcontact['followers'];
161 $profile['inbox'] = $apcontact['inbox'];
162 $profile['outbox'] = $apcontact['outbox'];
163 $profile['sharedinbox'] = $apcontact['sharedinbox'];
164 $profile['photo'] = $apcontact['photo'];
165 $profile['header'] = $apcontact['header'];
166 $profile['account-type'] = self::getAccountType($apcontact);
167 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
168 // $profile['keywords']
169 // $profile['location']
170 $profile['about'] = $apcontact['about'];
171 $profile['xmpp'] = $apcontact['xmpp'];
172 $profile['matrix'] = $apcontact['matrix'];
173 $profile['batch'] = $apcontact['sharedinbox'];
174 $profile['notify'] = $apcontact['inbox'];
175 $profile['poll'] = $apcontact['outbox'];
176 $profile['pubkey'] = $apcontact['pubkey'];
177 $profile['subscribe'] = $apcontact['subscribe'];
178 $profile['manually-approve'] = $apcontact['manually-approve'];
179 $profile['baseurl'] = $apcontact['baseurl'];
180 $profile['gsid'] = $apcontact['gsid'];
182 if (!is_null($apcontact['discoverable'])) {
183 $profile['hide'] = !$apcontact['discoverable'];
186 // Remove all "null" fields
187 foreach ($profile as $field => $content) {
188 if (is_null($content)) {
189 unset($profile[$field]);
197 * Fetches activities from the outbox of a given profile and processes it
200 * @param integer $uid User ID
201 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
203 public static function fetchOutbox($url, $uid)
205 $data = self::fetchContent($url, $uid);
210 if (!empty($data['orderedItems'])) {
211 $items = $data['orderedItems'];
212 } elseif (!empty($data['first']['orderedItems'])) {
213 $items = $data['first']['orderedItems'];
214 } elseif (!empty($data['first'])) {
215 self::fetchOutbox($data['first'], $uid);
221 foreach ($items as $activity) {
222 $ldactivity = JsonLD::compact($activity);
223 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
228 * Fetch items from AP endpoints
230 * @param string $url Address of the endpoint
231 * @param integer $uid Optional user id
232 * @return array Endpoint items
234 public static function fetchItems(string $url, int $uid = 0)
236 $data = self::fetchContent($url, $uid);
241 if (!empty($data['orderedItems'])) {
242 $items = $data['orderedItems'];
243 } elseif (!empty($data['first']['orderedItems'])) {
244 $items = $data['first']['orderedItems'];
245 } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) {
246 return self::fetchItems($data['first'], $uid);
251 if (!empty($data['next']) && is_string($data['next'])) {
252 $items = array_merge($items, self::fetchItems($data['next'], $uid));
259 * Checks if the given contact url does support ActivityPub
261 * @param string $url profile url
262 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
264 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
265 * @throws \ImagickException
267 public static function isSupportedByContactUrl($url, $update = null)
269 return !empty(APContact::getByURL($url, $update));