3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol\ActivityPub;
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\APContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Model\Term;
19 use Friendica\Model\User;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Crypto;
22 use Friendica\Content\Text\BBCode;
23 use Friendica\Content\Text\HTML;
24 use Friendica\Util\JsonLD;
25 use Friendica\Util\LDSignature;
26 use Friendica\Core\Config;
27 use Friendica\Protocol\ActivityPub;
30 * @brief ActivityPub Protocol class
35 * @brief Converts mentions from Pleroma into the Friendica format
39 * @return converted body
41 private static function convertMentions($body)
43 $URLSearchString = "^\[\]";
44 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
50 * @brief Constructs a string with tags for a given tag array
53 * @param boolean $sensitive
55 * @return string with tags
57 private static function constructTagList($tags, $sensitive)
64 foreach ($tags as $tag) {
65 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
66 if (!empty($tag_text)) {
70 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
74 /// @todo add nsfw for $sensitive
87 private static function constructAttachList($attachments, $item)
89 if (empty($attachments)) {
93 foreach ($attachments as $attach) {
94 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
95 if ($filetype == 'image') {
96 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
98 if (!empty($item["attach"])) {
99 $item["attach"] .= ',';
101 $item["attach"] = '';
103 if (!isset($attach['length'])) {
104 $attach['length'] = "0";
106 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
116 * @param array $activity
119 public static function createItem($activity, $body)
122 $item['verb'] = ACTIVITY_POST;
123 $item['parent-uri'] = $activity['reply-to-id'];
125 if ($activity['reply-to-id'] == $activity['id']) {
126 $item['gravity'] = GRAVITY_PARENT;
127 $item['object-type'] = ACTIVITY_OBJ_NOTE;
129 $item['gravity'] = GRAVITY_COMMENT;
130 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
133 if (($activity['id'] != $activity['reply-to-id']) && !Item::exists(['uri' => $activity['reply-to-id']])) {
134 logger('Parent ' . $activity['reply-to-id'] . ' not found. Try to refetch it.');
135 self::fetchMissingActivity($activity['reply-to-id'], $activity);
138 self::postItem($activity, $item, $body);
144 * @param array $activity
147 public static function likeItem($activity, $body)
150 $item['verb'] = ACTIVITY_LIKE;
151 $item['parent-uri'] = $activity['object'];
152 $item['gravity'] = GRAVITY_ACTIVITY;
153 $item['object-type'] = ACTIVITY_OBJ_NOTE;
155 self::postItem($activity, $item, $body);
159 * @brief Delete items
161 * @param array $activity
164 public static function deleteItem($activity)
166 $owner = Contact::getIdForURL($activity['owner']);
167 $object = JsonLD::fetchElement($activity, 'object', 'id');
168 logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG);
169 Item::delete(['uri' => $object, 'owner-id' => $owner]);
175 * @param array $activity
178 public static function dislikeItem($activity, $body)
181 $item['verb'] = ACTIVITY_DISLIKE;
182 $item['parent-uri'] = $activity['object'];
183 $item['gravity'] = GRAVITY_ACTIVITY;
184 $item['object-type'] = ACTIVITY_OBJ_NOTE;
186 self::postItem($activity, $item, $body);
192 * @param array $activity
196 private static function postItem($activity, $item, $body)
198 /// @todo What to do with $activity['context']?
200 if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
201 logger('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', LOGGER_DEBUG);
205 $item['network'] = Protocol::ACTIVITYPUB;
206 $item['private'] = !in_array(0, $activity['receiver']);
207 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
208 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
209 $item['uri'] = $activity['id'];
210 $item['created'] = $activity['published'];
211 $item['edited'] = $activity['updated'];
212 $item['guid'] = $activity['diaspora:guid'];
213 $item['title'] = HTML::toBBCode($activity['name']);
214 $item['content-warning'] = HTML::toBBCode($activity['summary']);
215 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
216 $item['location'] = $activity['location'];
217 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
218 $item['app'] = $activity['service'];
219 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
221 $item = self::constructAttachList($activity['attachments'], $item);
223 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
224 if (!empty($source)) {
225 $item['body'] = $source;
228 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
229 $item['source'] = $body;
230 $item['conversation-href'] = $activity['context'];
231 $item['conversation-uri'] = $activity['conversation'];
233 foreach ($activity['receiver'] as $receiver) {
234 $item['uid'] = $receiver;
235 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
237 if (($receiver != 0) && empty($item['contact-id'])) {
238 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
241 $item_id = Item::insert($item);
242 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
252 private static function fetchMissingActivity($url, $child)
254 if (Config::get('system', 'ostatus_full_threads')) {
258 $object = ActivityPub::fetchContent($url);
259 if (empty($object)) {
260 logger('Activity ' . $url . ' was not fetchable, aborting.');
265 $activity['@context'] = $object['@context'];
266 unset($object['@context']);
267 $activity['id'] = $object['id'];
268 $activity['to'] = defaults($object, 'to', []);
269 $activity['cc'] = defaults($object, 'cc', []);
270 $activity['actor'] = $child['author'];
271 $activity['object'] = $object;
272 $activity['published'] = $object['published'];
273 $activity['type'] = 'Create';
275 ActivityPub\Receiver::processActivity($activity);
276 logger('Activity ' . $url . ' had been fetched and processed.');
280 * @brief perform a "follow" request
282 * @param array $activity
284 public static function followUser($activity)
286 $actor = JsonLD::fetchElement($activity, 'object', 'id');
287 $uid = User::getIdForURL($actor);
292 $owner = User::getOwnerDataById($uid);
294 $cid = Contact::getIdForURL($activity['owner'], $uid);
296 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
301 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
302 'author-link' => $activity['owner']];
304 Contact::addRelationship($owner, $contact, $item);
305 $cid = Contact::getIdForURL($activity['owner'], $uid);
310 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
311 if ($contact['network'] != Protocol::ACTIVITYPUB) {
312 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
315 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
316 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
320 * @brief Update the given profile
322 * @param array $activity
324 public static function updatePerson($activity)
326 if (empty($activity['object']['id'])) {
330 logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG);
331 APContact::getByURL($activity['object']['id'], true);
335 * @brief Delete the given profile
337 * @param array $activity
339 public static function deletePerson($activity)
341 if (empty($activity['object']['id']) || empty($activity['object']['actor'])) {
342 logger('Empty object id or actor.', LOGGER_DEBUG);
346 if ($activity['object']['id'] != $activity['object']['actor']) {
347 logger('Object id does not match actor.', LOGGER_DEBUG);
351 $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($activity['object']['id'])]);
352 while ($contact = DBA::fetch($contacts)) {
353 Contact::remove($contact["id"]);
355 DBA::close($contacts);
357 logger('Deleted contact ' . $activity['object']['id'], LOGGER_DEBUG);
361 * @brief Accept a follow request
363 * @param array $activity
365 public static function acceptFollowUser($activity)
367 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
368 $uid = User::getIdForURL($actor);
373 $owner = User::getOwnerDataById($uid);
375 $cid = Contact::getIdForURL($activity['owner'], $uid);
377 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
381 $fields = ['pending' => false];
383 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
384 if ($contact['rel'] == Contact::FOLLOWER) {
385 $fields['rel'] = Contact::FRIEND;
388 $condition = ['id' => $cid];
389 DBA::update('contact', $fields, $condition);
390 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
394 * @brief Reject a follow request
396 * @param array $activity
398 public static function rejectFollowUser($activity)
400 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
401 $uid = User::getIdForURL($actor);
406 $owner = User::getOwnerDataById($uid);
408 $cid = Contact::getIdForURL($activity['owner'], $uid);
410 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
414 if (DBA::exists('contact', ['id' => $cid, 'rel' => Contact::SHARING, 'pending' => true])) {
415 Contact::remove($cid);
416 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . ' - contact had been removed.', LOGGER_DEBUG);
418 logger('Rejected contact request from contact ' . $cid . ' for user ' . $uid . '.', LOGGER_DEBUG);
423 * @brief Undo activity like "like" or "dislike"
425 * @param array $activity
427 public static function undoActivity($activity)
429 $activity_url = JsonLD::fetchElement($activity, 'object', 'id');
430 if (empty($activity_url)) {
434 $actor = JsonLD::fetchElement($activity, 'object', 'actor');
439 $author_id = Contact::getIdForURL($actor);
440 if (empty($author_id)) {
444 Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
448 * @brief Activity to remove a follower
450 * @param array $activity
452 public static function undoFollowUser($activity)
454 $object = JsonLD::fetchElement($activity, 'object', 'object');
455 $uid = User::getIdForURL($object);
460 $owner = User::getOwnerDataById($uid);
462 $cid = Contact::getIdForURL($activity['owner'], $uid);
464 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
468 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
469 if (!DBA::isResult($contact)) {
473 Contact::removeFollower($owner, $contact);
474 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);