]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #8904 from MrPetovan/task/ap-conversion-admin-module
authorMichael Vogel <icarus@dabo.de>
Tue, 21 Jul 2020 19:32:06 +0000 (21:32 +0200)
committerGitHub <noreply@github.com>
Tue, 21 Jul 2020 19:32:06 +0000 (21:32 +0200)
Add new admin debug module for ActivityPub

mod/network.php
src/Model/GServer.php
src/Model/Item.php
src/Model/Tag.php
src/Protocol/ActivityPub/Processor.php
src/Protocol/ActivityPub/Receiver.php

index 3311a796a89b5cd4f3776abfa9478ce9d8542cbb..f847e6757e0da2ab049dc6aaef81e0bd4b790f8d 100644 (file)
@@ -271,7 +271,7 @@ function networkConversation(App $a, $items, Pager $pager, $mode, $update, $orde
        $a->page_contact = $a->contact;
 
        if (!is_array($items)) {
-               Logger::log("Expecting items to be an array. Got " . print_r($items, true));
+               Logger::info('Expecting items to be an array.', ['items' => $items]);
                $items = [];
        }
 
@@ -541,7 +541,6 @@ function networkThreadedView(App $a, $update, $parent)
        }
 
        $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
-       $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
 
        if ($gid) {
                $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
@@ -739,78 +738,6 @@ function networkThreadedView(App $a, $update, $parent)
                );
        }
 
-       // Only show it when unfiltered (no groups, no networks, ...)
-       if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
-               if (DBA::isResult($r)) {
-                       $top_limit = current($r)['order_date'];
-                       $bottom_limit = end($r)['order_date'];
-                       if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
-                               $_SESSION['network_last_top_limit'] = $top_limit;
-                       }
-               } else {
-                       $top_limit = $bottom_limit = DateTimeFormat::utcNow();
-               }
-
-               // When checking for updates we need to fetch from the newest date to the newest date before
-               // Only do this, when the last stored date isn't too long ago (10 times the update interval)
-               $browser_update = DI::pConfig()->get(local_user(), 'system', 'update_interval', 40000) / 1000;
-
-               if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
-                       (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
-                       ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
-                       $bottom_limit = $_SESSION['network_last_date'];
-               }
-               $_SESSION['network_last_date'] = Session::get('network_last_top_limit', $top_limit);
-               $_SESSION['network_last_date_timestamp'] = time();
-
-               if ($last_date > $top_limit) {
-                       $top_limit = $last_date;
-               } elseif ($pager->getPage() == 1) {
-                       // Highest possible top limit when we are on the first page
-                       $top_limit = DateTimeFormat::utcNow();
-               }
-
-               // Handle bad performance situations when the distance between top and bottom is too high
-               // See issue https://github.com/friendica/friendica/issues/8619
-               if (strtotime($top_limit) - strtotime($bottom_limit) > 86400) {
-                       // Set the bottom limit to one day in the past at maximum
-                       $bottom_limit = DateTimeFormat::utc(date('c', strtotime($top_limit) - 86400));
-               }
-
-               $items = DBA::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date`, `author`.`url` AS `author-link` FROM `item`
-                       STRAIGHT_JOIN (SELECT `uri-id` FROM `tag-search-view` WHERE `name` IN
-                               (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `uid` = 0) AS `tag-search`
-                       ON `item`.`uri-id` = `tag-search`.`uri-id`
-                       STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
-                       WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ? AND `item`.`gravity` = ?
-                               AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
-                       local_user(), $top_limit, $bottom_limit, GRAVITY_PARENT);
-
-               $data = DBA::toArray($items);
-
-               if (count($data) > 0) {
-                       $tag_top_limit = current($data)['order_date'];
-                       if ($_SESSION['network_last_date'] < $tag_top_limit) {
-                               $_SESSION['network_last_date'] = $tag_top_limit;
-                       }
-
-                       Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
-                       $s = [];
-                       foreach ($r as $item) {
-                               $s[$item['uri']] = $item;
-                       }
-                       foreach ($data as $item) {
-                               // Don't show hash tag posts from blocked or ignored contacts
-                               $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
-                                       Strings::normaliseLink($item['author-link']), local_user()];
-                               if (!DBA::exists('contact', $condition)) {
-                                       $s[$item['uri']] = $item;
-                               }
-                       }
-                       $r = $s;
-               }
-       }
-
        $parents_str = '';
        $date_offset = '';
 
index 3d268c37b91f699bb0c4950f2c64636b9d828901..8cad1aad08c5e21a7e55b13c28d11a333649e445 100644 (file)
@@ -1637,7 +1637,6 @@ class GServer
                                if (!empty($data['data']['nodes'])) {
                                        foreach ($data['data']['nodes'] as $server) {
                                                // Using "only_nodeinfo" since servers that are listed on that page should always have it.
-                                               echo $server['host']."\n";
                                                Worker::add(PRIORITY_LOW, 'UpdateGServer', 'https://' . $server['host'], true);
                                        }
                                }
index e31097f53cc48f2d75b02a80e0cedc8f2a60f0fd..f7e7ae8737d0d7647f373a5ffd23deee9e381a5d 100644 (file)
@@ -1964,6 +1964,9 @@ class Item
 
                check_user_notification($current_post);
 
+               // Distribute items to users who subscribed to their tags
+               self::distributeByTags($item, $orig_item);
+
                $transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin']));
 
                if ($transmit) {
@@ -1983,6 +1986,26 @@ class Item
                return $current_post;
        }
 
+       /**
+        * Distribute the given item to users who subscribed to their tags
+        *
+        * @param array $item     Processed item
+        * @param array $original Original item
+        */
+       private static function distributeByTags(array $item, array $original)
+       {
+               if (($item['uid'] != 0) || ($item['gravity'] != GRAVITY_PARENT) || !in_array($item['network'], Protocol::FEDERATED)) {
+                       return;
+               }
+
+               $uids = Tag::getUIDListByURIId($item['uri-id']);
+               foreach ($uids as $uid) {
+                       $original['uri-id'] = $item['uri-id'];
+                       $stored = self::storeForUser($original, $uid);
+                       Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
+               }
+       }
+
        /**
         * Insert a new item content entry
         *
@@ -2079,13 +2102,6 @@ class Item
 
                $origin = $item['origin'];
 
-               unset($item['id']);
-               unset($item['parent']);
-               unset($item['mention']);
-               unset($item['wall']);
-               unset($item['origin']);
-               unset($item['starred']);
-
                $users = [];
 
                /// @todo add a field "pcid" in the contact table that referrs to the public contact id.
@@ -2145,33 +2161,50 @@ class Item
                        if ($origin_uid == $uid) {
                                $item['diaspora_signed_text'] = $signed_text;
                        }
-                       self::storeForUser($itemid, $item, $uid);
+                       self::storeForUser($item, $uid);
                }
        }
 
        /**
         * Store public items for the receivers
         *
-        * @param integer $itemid Item ID that should be added
         * @param array   $item   The item entry that will be stored
         * @param integer $uid    The user that will receive the item entry
+        * @return integer stored item id
         * @throws \Exception
         */
-       private static function storeForUser($itemid, $item, $uid)
+       private static function storeForUser(array $item, int $uid)
        {
+               if (self::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) {
+                       Logger::info('Item already exists', ['uri-id' => $item['uri-id'], 'uid' => $uid]);
+                       return 0;
+               }
+
+               unset($item['id']);
+               unset($item['parent']);
+               unset($item['mention']);
+               unset($item['starred']);
+
                $item['uid'] = $uid;
                $item['origin'] = 0;
                $item['wall'] = 0;
+
                if ($item['uri'] == $item['parent-uri']) {
-                       $item['contact-id'] = Contact::getIdForURL($item['owner-link'], $uid);
+                       $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']);
                } else {
-                       $item['contact-id'] = Contact::getIdForURL($item['author-link'], $uid);
+                       $contact = Contact::getByURLForUser($item['author-link'], $uid, false, ['id']);
                }
 
-               if (empty($item['contact-id'])) {
+               if (!empty($contact['id'])) {
+                       $item['contact-id'] = $contact['id'];
+               } else {
+                       // Shouldn't happen at all
+                       Logger::warning('contact-id could not be fetched', ['uid' => $uid, 'item' => $item]);
                        $self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]);
                        if (!DBA::isResult($self)) {
-                               return;
+                               // Shouldn't happen even less
+                               Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]);
+                               return 0;
                        }
                        $item['contact-id'] = $self['id'];
                }
@@ -2189,10 +2222,11 @@ class Item
                $distributed = self::insert($item, $notify, true);
 
                if (!$distributed) {
-                       Logger::info("Distributed public item wasn't stored", ['id' => $itemid, 'user' => $uid]);
+                       Logger::info("Distributed public item wasn't stored", ['uri-id' => $item['uri-id'], 'user' => $uid]);
                } else {
-                       Logger::info('Distributed public item was stored', ['id' => $itemid, 'user' => $uid, 'stored' => $distributed]);
+                       Logger::info('Distributed public item was stored', ['uri-id' => $item['uri-id'], 'user' => $uid, 'stored' => $distributed]);
                }
+               return $distributed;
        }
 
        /**
index 3424a2377110d7e32aca51b9b864d2220038412d..a48f2cb92b6dc71308576e274d78d1fb45667dc1 100644 (file)
@@ -536,5 +536,41 @@ class Tag
                }
 
                return Strings::startsWithChars($tag, $tag_chars);
-       }       
+       }
+
+       /**
+        * Fetch user who subscribed to the given tag
+        *
+        * @param string $tag
+        * @return array User list
+        */
+       private static function getUIDListByTag(string $tag)
+       {
+               $uids = [];
+               $searches = DBA::select('search', ['uid'], ['term' => $tag]);
+               while ($search = DBA::fetch($searches)) {
+                       $uids[] = $search['uid'];
+               }
+               DBA::close($searches);
+
+               return $uids;
+       }
+
+       /**
+        * Fetch user who subscribed to the tags of the given item
+        *
+        * @param integer $uri_id
+        * @return array User list
+        */
+       public static function getUIDListByURIId(int $uri_id)
+       {
+               $uids = [];
+               $tags = self::getByURIId($uri_id, [self::HASHTAG]);
+
+               foreach ($tags as $tag) {
+                       $uids = array_merge($uids, self::getUIDListByTag(self::TAG_CHARACTER[self::HASHTAG] . $tag['name']));
+               }
+
+               return array_unique($uids);
+       }
 }
index 0ff2a588ea8a84a089ddaee89d6934eaac8e5ef3..241907f62359885289ce88ef4c8238f906c6d22a 100644 (file)
@@ -115,10 +115,22 @@ class Processor
                                                        continue 2;
                                                }
 
+                                               $item['body'] .= "\n";
+
+                                               // image is the preview/thumbnail URL
+                                               if (!empty($attach['image'])) {
+                                                       $item['body'] .= '[url=' . $attach['url'] . ']';
+                                                       $attach['url'] = $attach['image'];
+                                               }
+
                                                if (empty($attach['name'])) {
-                                                       $item['body'] .= "\n[img]" . $attach['url'] . '[/img]';
+                                                       $item['body'] .= '[img]' . $attach['url'] . '[/img]';
                                                } else {
-                                                       $item['body'] .= "\n[img=" . $attach['url'] . ']' . $attach['name'] . '[/img]';
+                                                       $item['body'] .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
+                                               }
+
+                                               if (!empty($attach['image'])) {
+                                                       $item['body'] .= '[/url]';
                                                }
                                        } elseif ($filetype == 'audio') {
                                                if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
index 51157c259a0a41c59ea3ef2ee2c91330b43a44e7..226ad601044c68f112850985e8013a0a724886ed 100644 (file)
@@ -879,7 +879,7 @@ class Receiver
         *
         * @param array $attachments Attachments in JSON-LD format
         *
-        * @return array with attachmants in a simplified format
+        * @return array Attachments in a simplified format
         */
        private static function processAttachments(array $attachments)
        {
@@ -927,6 +927,62 @@ class Receiver
                                                'url' => JsonLD::fetchElement($attachment, 'as:href', '@id')
                                        ];
                                        break;
+                               case 'as:Image':
+                                       $mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value');
+                                       $imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id');
+                                       $imagePreviewUrl = null;
+                                       // Multiple URLs?
+                                       if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) {
+                                               $imageVariants = [];
+                                               $previewVariants = [];
+                                               foreach ($urls as $url) {
+                                                       // Scalar URL, no discrimination possible
+                                                       if (is_string($url)) {
+                                                               $imageFullUrl = $url;
+                                                               continue;
+                                                       }
+
+                                                       // Not sure what to do with a different Link media type than the base Image, we skip
+                                                       if ($mediaType != JsonLD::fetchElement($url, 'as:mediaType', '@value')) {
+                                                               continue;
+                                                       }
+
+                                                       $href = JsonLD::fetchElement($url, 'as:href', '@id');
+
+                                                       // Default URL choice if no discriminating width is provided
+                                                       $imageFullUrl = $href ?? $imageFullUrl;
+
+                                                       $width = intval(JsonLD::fetchElement($url, 'as:width', '@value') ?? 1);
+
+                                                       if ($href && $width) {
+                                                               $imageVariants[$width] = $href;
+                                                               // 632 is the ideal width for full screen frio posts, we compute the absolute distance to it
+                                                               $previewVariants[abs(632 - $width)] = $href;
+                                                       }
+                                               }
+
+                                               if ($imageVariants) {
+                                                       // Taking the maximum size image
+                                                       ksort($imageVariants);
+                                                       $imageFullUrl = array_pop($imageVariants);
+
+                                                       // Taking the minimum number distance to the target distance
+                                                       ksort($previewVariants);
+                                                       $imagePreviewUrl = array_shift($previewVariants);
+                                               }
+
+                                               unset($imageVariants);
+                                               unset($previewVariants);
+                                       }
+
+                                       $attachlist[] = [
+                                               'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
+                                               'mediaType' => $mediaType,
+                                               'name'  => JsonLD::fetchElement($attachment, 'as:name', '@value'),
+                                               'url'   => $imageFullUrl,
+                                               'image' => $imagePreviewUrl !== $imageFullUrl ? $imagePreviewUrl : null,
+                                       ];
+                                       break;
                                default:
                                        $attachlist[] = [
                                                'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),