]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub.php
We can delete notes / changed credits
[friendica.git] / src / Protocol / ActivityPub.php
index 95542ba966200b9fb14aa261f11013814e0468cc..4d7657865c226e3c19bfd7921cd28504df0a4363 100644 (file)
@@ -34,20 +34,16 @@ use Friendica\Util\LDSignature;
  *
  * Digest: https://tools.ietf.org/html/rfc5843
  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
- * https://github.com/digitalbazaar/php-json-ld
- *
- * Part of the code for HTTP signing is taken from the Osada project.
- * https://framagit.org/macgirvin/osada
  *
  * To-do:
  *
  * Receiver:
- * - Activities: Dislike, Update, Delete
+ * - Activities: Update, Delete (Activities/Notes)
  * - Object Types: Person, Tombstome
  *
  * Transmitter:
- * - Activities: Like, Dislike, Update, Delete, Announce
- * - Object Tyoes: Article, Person, Tombstone
+ * - Activities: Announce
+ * - Object Tyoes: Person
  *
  * General:
  * - Endpoints: Outbox, Follower, Following
@@ -139,7 +135,12 @@ class ActivityPub
                $actor = JsonLD::fetchElement($activity, 'actor', 'id');
                $profile = ActivityPub::fetchprofile($actor);
 
-               $item_profile = ActivityPub::fetchprofile($item['owner-link']);
+               $item_profile = ActivityPub::fetchprofile($item['author-link']);
+               $exclude[] = $item['author-link'];
+
+               if ($item['gravity'] == GRAVITY_PARENT) {
+                       $exclude[] = $item['owner-link'];
+               }
 
                $permissions = [];
 
@@ -155,7 +156,7 @@ class ActivityPub
                                if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
                                        $receiver = $item_profile['followers'];
                                }
-                               if ($receiver != $item['owner-link']) {
+                               if (!in_array($receiver, $exclude)) {
                                        $permissions[$element][] = $receiver;
                                }
                        }
@@ -217,7 +218,7 @@ class ActivityPub
                        }
                }
 
-               $parents = Item::select(['author-link', 'owner-link'], ['parent' => $item['parent']]);
+               $parents = Item::select(['author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
                while ($parent = Item::fetch($parents)) {
                        $profile = self::fetchprofile($parent['author-link']);
                        if (!empty($profile) && empty($contacts[$profile['url']])) {
@@ -225,6 +226,10 @@ class ActivityPub
                                $contacts[$profile['url']] = $profile['url'];
                        }
 
+                       if ($item['gravity'] != GRAVITY_PARENT) {
+                               continue;
+                       }
+
                        $profile = self::fetchprofile($parent['owner-link']);
                        if (!empty($profile) && empty($contacts[$profile['url']])) {
                                $data['cc'][] = $profile['url'];
@@ -250,13 +255,18 @@ class ActivityPub
 
                $inboxes = [];
 
-               $item_profile = ActivityPub::fetchprofile($item['owner-link']);
+               if ($item['gravity'] == GRAVITY_ACTIVITY) {
+                       $item_profile = ActivityPub::fetchprofile($item['author-link']);
+               } else {
+                       $item_profile = ActivityPub::fetchprofile($item['owner-link']);
+               }
 
                $elements = ['to', 'cc', 'bto', 'bcc'];
                foreach ($elements as $element) {
                        if (empty($permissions[$element])) {
                                continue;
                        }
+
                        foreach ($permissions[$element] as $receiver) {
                                if ($receiver == $item_profile['followers']) {
                                        $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
@@ -276,18 +286,35 @@ class ActivityPub
                        }
                }
 
-               if (!empty($item_profile['sharedinbox'])) {
-                       unset($inboxes[$item_profile['sharedinbox']]);
-               }
+               return $inboxes;
+       }
 
-               if (!empty($item_profile['inbox'])) {
-                       unset($inboxes[$item_profile['inbox']]);
+       public static function getTypeOfItem($item)
+       {
+               if ($item['verb'] == ACTIVITY_POST) {
+                       if ($item['created'] == $item['edited']) {
+                               $type = 'Create';
+                       } else {
+                               $type = 'Update';
+                       }
+               } elseif ($item['verb'] == ACTIVITY_LIKE) {
+                       $type = 'Like';
+               } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
+                       $type = 'Dislike';
+               } elseif ($item['verb'] == ACTIVITY_ATTEND) {
+                       $type = 'Accept';
+               } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
+                       $type = 'Reject';
+               } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
+                       $type = 'TentativeAccept';
+               } else {
+                       $type = '';
                }
 
-               return $inboxes;
+               return $type;
        }
 
-       public static function createActivityFromItem($item_id)
+       public static function createActivityFromItem($item_id, $object_mode = false)
        {
                $item = Item::selectFirst([], ['id' => $item_id]);
 
@@ -304,14 +331,26 @@ class ActivityPub
                        }
                }
 
-               $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
-                       ['ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
-                       'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
-                       'conversation' => 'ostatus:conversation',
-                       'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri']]];
+               $type = self::getTypeOfItem($item);
+
+               if (!$object_mode) {
+                       $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
+                               ['ostatus' => 'http://ostatus.org#', 'uuid' => 'http://schema.org/identifier',
+                               'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
+                               'atomUri' => 'ostatus:atomUri', 'conversation' => 'ostatus:conversation',
+                               'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri']]];
 
-               $data['id'] = $item['uri'] . '#activity';
-               $data['type'] = 'Create';
+                       if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
+                               $type = 'Undo';
+                       } elseif ($item['deleted']) {
+                               $type = 'Delete';
+                       }
+               } else {
+                       $data = [];
+               }
+
+               $data['id'] = $item['uri'] . '#' . $type;
+               $data['type'] = $type;
                $data['actor'] = $item['author-link'];
 
                $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
@@ -320,16 +359,25 @@ class ActivityPub
                        $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
                }
 
-               $data['context_id'] = $item['parent'];
                $data['context'] = self::createConversationURLFromItem($item);
 
                $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
 
-               $data['object'] = self::createObjectTypeFromItem($item);
+               if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
+                       $data['object'] = self::CreateNote($item);
+               } elseif ($data['type'] == 'Undo') {
+                       $data['object'] = self::createActivityFromItem($item_id, true);
+               } else {
+                       $data['object'] = $item['thr-parent'];
+               }
 
                $owner = User::getOwnerDataById($item['uid']);
 
-               return LDSignature::sign($data, $owner);
+               if (!$object_mode) {
+                       return LDSignature::sign($data, $owner);
+               } else {
+                       return $data;
+               }
        }
 
        public static function createObjectFromItemID($item_id)
@@ -341,12 +389,12 @@ class ActivityPub
                }
 
                $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
-                       ['ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
-                       'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
-                       'conversation' => 'ostatus:conversation',
+                       ['ostatus' => 'http://ostatus.org#', 'uuid' => 'http://schema.org/identifier',
+                       'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
+                       'atomUri' => 'ostatus:atomUri', 'conversation' => 'ostatus:conversation',
                        'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri']]];
 
-               $data = array_merge($data, self::createObjectTypeFromItem($item));
+               $data = array_merge($data, self::CreateNote($item));
 
 
                return $data;
@@ -383,7 +431,7 @@ class ActivityPub
                return $conversation_uri;
        }
 
-       private static function createObjectTypeFromItem($item)
+       private static function CreateNote($item)
        {
                if (!empty($item['title'])) {
                        $type = 'Article';
@@ -391,9 +439,18 @@ class ActivityPub
                        $type = 'Note';
                }
 
+               if ($item['deleted']) {
+                       $type = 'Tombstone';
+               }
+
                $data = [];
                $data['id'] = $item['uri'];
                $data['type'] = $type;
+
+               if ($item['deleted']) {
+                       return $data;
+               }
+
                $data['summary'] = null; // Ignore by now
 
                if ($item['uri'] != $item['thr-parent']) {
@@ -402,6 +459,7 @@ class ActivityPub
                        $data['inReplyTo'] = null;
                }
 
+               $data['uuid'] = $item['guid'];
                $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
 
                if ($item["created"] != $item["edited"]) {
@@ -412,7 +470,6 @@ class ActivityPub
                $data['attributedTo'] = $item['author-link'];
                $data['actor'] = $item['author-link'];
                $data['sensitive'] = false; // - Query NSFW
-               $data['context_id'] = $item['parent'];
                $data['conversation'] = $data['context'] = self::createConversationURLFromItem($item);
 
                if (!empty($item['title'])) {
@@ -875,6 +932,7 @@ class ActivityPub
                                break;
 
                        case 'Dislike':
+                               self::dislikeItem($object_data, $body);
                                break;
 
                        case 'Update':
@@ -1022,7 +1080,7 @@ class ActivityPub
                                return false;
                        }
                        logger('Using already stored item for url ' . $object_url, LOGGER_DEBUG);
-                       $data = self::createObjectTypeFromItem($item);
+                       $data = self::CreateNote($item);
                }
 
                if (empty($data['type'])) {
@@ -1239,6 +1297,17 @@ class ActivityPub
                self::postItem($activity, $item, $body);
        }
 
+       private static function dislikeItem($activity, $body)
+       {
+               $item = [];
+               $item['verb'] = ACTIVITY_DISLIKE;
+               $item['parent-uri'] = $activity['object'];
+               $item['gravity'] = GRAVITY_ACTIVITY;
+               $item['object-type'] = ACTIVITY_OBJ_NOTE;
+
+               self::postItem($activity, $item, $body);
+       }
+
        private static function postItem($activity, $item, $body)
        {
                /// @todo What to do with $activity['context']?