]> git.mxchange.org Git - friendica.git/blobdiff - include/conversation.php
enable codecov
[friendica.git] / include / conversation.php
index f7e08571c09ab6f23ed4b847b43980841e572b7e..66b6d2a11cc48cb6de5862c534f34406b706c8ec 100644 (file)
@@ -15,6 +15,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
+use Friendica\Core\Session;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
@@ -528,7 +529,7 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ
 
                if (!$update) {
                        $live_update_div = '<div id="live-display"></div>' . "\r\n"
-                               . "<script> var profile_uid = " . defaults($_SESSION, 'uid', 0) . ";"
+                               . "<script> var profile_uid = " . Session::get('uid', 0) . ";"
                                . " var profile_page = 1; </script>";
                }
        } elseif ($mode === 'community') {
@@ -565,8 +566,12 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ
        $items = $cb['items'];
 
        $conv_responses = [
-               'like' => ['title' => L10n::t('Likes','title')], 'dislike' => ['title' => L10n::t('Dislikes','title')],
-               'attendyes' => ['title' => L10n::t('Attending','title')], 'attendno' => ['title' => L10n::t('Not attending','title')], 'attendmaybe' => ['title' => L10n::t('Might attend','title')]
+               'like' => ['title' => L10n::t('Likes','title')],
+               'dislike' => ['title' => L10n::t('Dislikes','title')],
+               'attendyes' => ['title' => L10n::t('Attending','title')],
+               'attendno' => ['title' => L10n::t('Not attending','title')],
+               'attendmaybe' => ['title' => L10n::t('Might attend','title')],
+               'announce' => ['title' => L10n::t('Reshares','title')]
        ];
 
        // array with html for each thread (parent+comments)
@@ -579,7 +584,7 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ
                if (in_array($mode, ['community', 'contacts'])) {
                        $writable = true;
                } else {
-                       $writable = ($items[0]['uid'] == 0) && in_array($items[0]['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN]);
+                       $writable = ($items[0]['uid'] == 0) && in_array($items[0]['network'], Protocol::FEDERATED);
                }
 
                if (!local_user()) {
@@ -672,6 +677,7 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ
                                        'guid' => ($preview ? 'Q0' : $item['guid']),
                                        'network' => $item['network'],
                                        'network_name' => ContactSelector::networkToName($item['network'], $item['author-link']),
+                                       'network_icon' => ContactSelector::networkToIcon($item['network'], $item['author-link']),
                                        'linktitle' => L10n::t('View %s\'s profile @ %s', $profile_name, $item['author-link']),
                                        'profile_url' => $profile_link,
                                        'item_photo_menu' => item_photo_menu($item),
@@ -784,6 +790,46 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ
        return $o;
 }
 
+/**
+ * Fetch all comments from a query. Additionally set the newest resharer as thread owner.
+ *
+ * @param $thread_items Database statement with thread posts
+ * @return array items with parents and comments
+ */
+function conversation_fetch_comments($thread_items) {
+       $comments = [];
+       $parentlines = [];
+       $lineno = 0;
+       $actor = [];
+       $received = '';
+
+       while ($row = Item::fetch($thread_items)) {
+               if (($row['verb'] == ACTIVITY2_ANNOUNCE) && !empty($row['contact-uid']) && ($row['received'] > $received) && ($row['thr-parent'] == $row['parent-uri'])) {
+                       $actor = ['link' => $row['author-link'], 'avatar' => $row['author-avatar'], 'name' => $row['author-name']];
+                       $received = $row['received'];
+               }
+
+               if ((($row['gravity'] == GRAVITY_PARENT) && !$row['origin'] && !in_array($row['network'], [Protocol::DIASPORA])) &&
+                       (empty($row['contact-uid']) || !in_array($row['network'], Protocol::NATIVE_SUPPORT))) {
+                       $parentlines[] = $lineno;
+               }
+
+               $comments[] = $row;
+               $lineno++;
+       }
+
+       DBA::close($thread_items);
+
+       if (!empty($actor)) {
+               foreach ($parentlines as $line) {
+                       $comments[$line]['owner-link'] = $actor['link'];
+                       $comments[$line]['owner-avatar'] = $actor['avatar'];
+                       $comments[$line]['owner-name'] = $actor['name'];
+               }
+       }
+       return $comments;
+}
+
 /**
  * @brief Add comments to top level entries that had been fetched before
  *
@@ -815,9 +861,10 @@ function conversation_add_children(array $parents, $block_authors, $order, $uid)
                if ($block_authors) {
                        $condition[0] .= "AND NOT `author`.`hidden`";
                }
-               $thread_items = Item::selectForUser(local_user(), [], $condition, $params);
 
-               $comments = Item::inArray($thread_items);
+               $thread_items = Item::selectForUser(local_user(), array_merge(Item::DISPLAY_FIELDLIST, ['contact-uid', 'gravity']), $condition, $params);
+
+               $comments = conversation_fetch_comments($thread_items);
 
                if (count($comments) != 0) {
                        $items = array_merge($items, $comments);
@@ -826,7 +873,7 @@ function conversation_add_children(array $parents, $block_authors, $order, $uid)
 
        foreach ($items as $index => $item) {
                if ($item['uid'] == 0) {
-                       $items[$index]['writable'] = in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::DFRN]);
+                       $items[$index]['writable'] = in_array($item['network'], Protocol::FEDERATED);
                }
        }
 
@@ -885,7 +932,7 @@ function item_photo_menu($item) {
                $contact_url = 'contact/' . $cid;
                $posts_link = 'contact/' . $cid . '/posts';
 
-               if (in_array($network, [Protocol::DFRN, Protocol::DIASPORA])) {
+               if (in_array($network, [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA])) {
                        $pm_url = 'message/new/' . $cid;
                }
        }
@@ -908,7 +955,7 @@ function item_photo_menu($item) {
                }
 
                if ((($cid == 0) || ($rel == Contact::FOLLOWER)) &&
-                       in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA])) {
+                       in_array($item['network'], Protocol::FEDERATED)) {
                        $menu[L10n::t('Connect/Follow')] = 'follow?url=' . urlencode($item['author-link']);
                }
        } else {
@@ -963,6 +1010,9 @@ function builtin_activity_puller($item, &$conv_responses) {
                        case 'attendmaybe':
                                $verb = ACTIVITY_ATTENDMAYBE;
                                break;
+                       case 'announce':
+                               $verb = ACTIVITY2_ANNOUNCE;
+                               break;
                        default:
                                return;
                }
@@ -1045,6 +1095,9 @@ function format_like($cnt, array $arr, $type, $id) {
                        case 'attendmaybe' :
                                $phrase = L10n::t('%s attends maybe.', $likers);
                                break;
+                       case 'announce' :
+                               $phrase = L10n::t('%s reshared this.', $likers);
+                               break;
                }
        }
 
@@ -1084,6 +1137,10 @@ function format_like($cnt, array $arr, $type, $id) {
                                $phrase = L10n::t('<span  %1$s>%2$d people</span> attend maybe', $spanatts, $cnt);
                                $explikers = L10n::t('%s attend maybe.', $likers);
                                break;
+                       case 'announce':
+                               $phrase = L10n::t('<span  %1$s>%2$d people</span> reshared this', $spanatts, $cnt);
+                               $explikers = L10n::t('%s reshared this.', $likers);
+                               break;
                }
 
                $expanded .= "\t" . '<p class="wall-item-' . $type . '-expanded" id="' . $type . 'list-' . $id . '" style="display: none;" >' . $explikers . EOL . '</p>';
@@ -1260,7 +1317,7 @@ function get_item_children(array &$item_list, array $parent, $recursive = true)
 function sort_item_children(array $items)
 {
        $result = $items;
-       usort($result, 'sort_thr_created_rev');
+       usort($result, 'sort_thr_received_rev');
        foreach ($result as $k => $i) {
                if (isset($result[$k]['children'])) {
                        $result[$k]['children'] = sort_item_children($result[$k]['children']);
@@ -1345,13 +1402,13 @@ function smart_flatten_conversation(array $parent)
 
 /**
  * Expands a flat list of items into corresponding tree-like conversation structures,
- * sort the top-level posts either on "created" or "commented", and finally
+ * sort the top-level posts either on "received" or "commented", and finally
  * append all the items at the top level (???)
  *
  * @brief Expands a flat item list into a conversation array for display
  *
  * @param array  $item_list A list of items belonging to one or more conversations
- * @param string $order     Either on "created" or "commented"
+ * @param string $order     Either on "received" or "commented"
  * @return array
  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  */
@@ -1383,8 +1440,8 @@ function conv_sort(array $item_list, $order)
                }
        }
 
-       if (stristr($order, 'created')) {
-               usort($parents, 'sort_thr_created');
+       if (stristr($order, 'received')) {
+               usort($parents, 'sort_thr_received');
        } elseif (stristr($order, 'commented')) {
                usort($parents, 'sort_thr_commented');
        }
@@ -1421,27 +1478,27 @@ function conv_sort(array $item_list, $order)
 }
 
 /**
- * @brief usort() callback to sort item arrays by the created key
+ * @brief usort() callback to sort item arrays by the received key
  *
  * @param array $a
  * @param array $b
  * @return int
  */
-function sort_thr_created(array $a, array $b)
+function sort_thr_received(array $a, array $b)
 {
-       return strcmp($b['created'], $a['created']);
+       return strcmp($b['received'], $a['received']);
 }
 
 /**
- * @brief usort() callback to reverse sort item arrays by the created key
+ * @brief usort() callback to reverse sort item arrays by the received key
  *
  * @param array $a
  * @param array $b
  * @return int
  */
-function sort_thr_created_rev(array $a, array $b)
+function sort_thr_received_rev(array $a, array $b)
 {
-       return strcmp($a['created'], $b['created']);
+       return strcmp($a['received'], $b['received']);
 }
 
 /**