3 * @copyright Copyright (C) 2020, Friendica
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\Database\DBA;
27 use Friendica\Model\APContact;
28 use Friendica\Model\User;
29 use Friendica\Util\HTTPSignature;
30 use Friendica\Util\JsonLD;
33 * ActivityPub Protocol class
35 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
36 * https://www.w3.org/TR/activitypub/
37 * https://www.w3.org/TR/activitystreams-core/
38 * https://www.w3.org/TR/activitystreams-vocabulary/
40 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
41 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
43 * Digest: https://tools.ietf.org/html/rfc5843
44 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
46 * Mastodon implementation of supported activities:
47 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
50 * http://docs-funkwhale-funkwhale-549-music-federation-documentation.preview.funkwhale.audio/federation/index.html
53 * - Polling the outboxes for missing content?
55 * Missing parts from DFRN:
62 const PUBLIC_COLLECTION = 'https://www.w3.org/ns/activitystreams#Public';
63 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
64 ['vcard' => 'http://www.w3.org/2006/vcard/ns#',
65 'dfrn' => 'http://purl.org/macgirvin/dfrn/1.0/',
66 'diaspora' => 'https://diasporafoundation.org/ns/',
67 'litepub' => 'http://litepub.social/ns#',
68 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
69 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
70 'directMessage' => 'litepub:directMessage']];
71 const ACCOUNT_TYPES = ['Person', 'Organization', 'Service', 'Group', 'Application'];
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/ld+json');
84 * Fetches ActivityPub content from the given url
86 * @param string $url content url
87 * @param integer $uid User ID for the signature
89 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
91 public static function fetchContent(string $url, int $uid = 0)
93 return HTTPSignature::fetch($url, $uid);
96 private static function getAccountType($apcontact)
100 switch($apcontact['type']) {
102 $accounttype = User::ACCOUNT_TYPE_PERSON;
105 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
108 $accounttype = User::ACCOUNT_TYPE_NEWS;
111 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
114 $accounttype = User::ACCOUNT_TYPE_RELAY;
122 * Fetches a profile from the given url into an array that is compatible to Probe::uri
124 * @param string $url profile url
125 * @param boolean $update Update the profile
127 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
128 * @throws \ImagickException
130 public static function probeProfile($url, $update = true)
132 $apcontact = APContact::getByURL($url, $update);
133 if (empty($apcontact)) {
137 $profile = ['network' => Protocol::ACTIVITYPUB];
138 $profile['nick'] = $apcontact['nick'];
139 $profile['name'] = $apcontact['name'];
140 $profile['guid'] = $apcontact['uuid'];
141 $profile['url'] = $apcontact['url'];
142 $profile['addr'] = $apcontact['addr'];
143 $profile['alias'] = $apcontact['alias'];
144 $profile['following'] = $apcontact['following'];
145 $profile['followers'] = $apcontact['followers'];
146 $profile['inbox'] = $apcontact['inbox'];
147 $profile['outbox'] = $apcontact['outbox'];
148 $profile['sharedinbox'] = $apcontact['sharedinbox'];
149 $profile['photo'] = $apcontact['photo'];
150 $profile['account-type'] = self::getAccountType($apcontact);
151 $profile['community'] = ($profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
152 // $profile['keywords']
153 // $profile['location']
154 $profile['about'] = $apcontact['about'];
155 $profile['batch'] = $apcontact['sharedinbox'];
156 $profile['notify'] = $apcontact['inbox'];
157 $profile['poll'] = $apcontact['outbox'];
158 $profile['pubkey'] = $apcontact['pubkey'];
159 $profile['subscribe'] = $apcontact['subscribe'];
160 $profile['manually-approve'] = $apcontact['manually-approve'];
161 $profile['baseurl'] = $apcontact['baseurl'];
162 $profile['gsid'] = $apcontact['gsid'];
164 // Remove all "null" fields
165 foreach ($profile as $field => $content) {
166 if (is_null($content)) {
167 unset($profile[$field]);
175 * Fetches activities from the outbox of a given profile and processes it
178 * @param integer $uid User ID
179 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
181 public static function fetchOutbox($url, $uid)
183 $data = self::fetchContent($url, $uid);
188 if (!empty($data['orderedItems'])) {
189 $items = $data['orderedItems'];
190 } elseif (!empty($data['first']['orderedItems'])) {
191 $items = $data['first']['orderedItems'];
192 } elseif (!empty($data['first'])) {
193 self::fetchOutbox($data['first'], $uid);
199 foreach ($items as $activity) {
200 $ldactivity = JsonLD::compact($activity);
201 ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
206 * Fetch items from AP endpoints
208 * @param string $url Address of the endpoint
209 * @param integer $uid Optional user id
210 * @return array Endpoint items
212 public static function fetchItems(string $url, int $uid = 0)
214 $data = self::fetchContent($url, $uid);
219 if (!empty($data['orderedItems'])) {
220 $items = $data['orderedItems'];
221 } elseif (!empty($data['first']['orderedItems'])) {
222 $items = $data['first']['orderedItems'];
223 } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) {
224 return self::fetchItems($data['first'], $uid);
229 if (!empty($data['next']) && is_string($data['next'])) {
230 $items = array_merge($items, self::fetchItems($data['next'], $uid));
237 * Checks if the given contact url does support ActivityPub
239 * @param string $url profile url
240 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
242 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
243 * @throws \ImagickException
245 public static function isSupportedByContactUrl($url, $update = null)
247 return !empty(APContact::getByURL($url, $update));