]> git.mxchange.org Git - friendica.git/blobdiff - include/conversation.php
Changed the option to enable the smart threading with the option to disable this
[friendica.git] / include / conversation.php
index 84e47d34e3020bee2d1d78c6aaa3f47918b6a296..44da847a9c24eec4b4de51529e6f72639c94c733 100644 (file)
@@ -402,7 +402,7 @@ function visible_activity($item) {
        /** @var Activity $activity */
        $activity = BaseObject::getClass(Activity::class);
 
-       if ($activity->isHidden($item['verb'])) {
+       if (empty($item['verb']) || $activity->isHidden($item['verb'])) {
                return false;
        }
 
@@ -801,10 +801,12 @@ function conversation(App $a, array $items, Pager $pager, $mode, $update, $previ
 /**
  * Fetch all comments from a query. Additionally set the newest resharer as thread owner.
  *
- * @param $thread_items Database statement with thread posts
+ * @param array   $thread_items Database statement with thread posts
+ * @param boolean $pinned       Is the item pinned?
+ *
  * @return array items with parents and comments
  */
-function conversation_fetch_comments($thread_items) {
+function conversation_fetch_comments($thread_items, $pinned) {
        $comments = [];
        $parentlines = [];
        $lineno = 0;
@@ -822,6 +824,10 @@ function conversation_fetch_comments($thread_items) {
                        $parentlines[] = $lineno;
                }
 
+               if ($row['gravity'] == GRAVITY_PARENT) {
+                       $row['pinned'] = $pinned;
+               }
+
                $comments[] = $row;
                $lineno++;
        }
@@ -872,7 +878,7 @@ function conversation_add_children(array $parents, $block_authors, $order, $uid)
 
                $thread_items = Item::selectForUser(local_user(), array_merge(Item::DISPLAY_FIELDLIST, ['contact-uid', 'gravity']), $condition, $params);
 
-               $comments = conversation_fetch_comments($thread_items);
+               $comments = conversation_fetch_comments($thread_items, $parent['pinned'] ?? false);
 
                if (count($comments) != 0) {
                        $items = array_merge($items, $comments);
@@ -1028,7 +1034,7 @@ function builtin_activity_puller($item, &$conv_responses) {
                /** @var Activity $activity */
                $activity = BaseObject::getClass(Activity::class);
 
-               if ($activity->match($item['verb'], $verb) && ($item['id'] != $item['parent'])) {
+               if (!empty($item['verb']) && $activity->match($item['verb'], $verb) && ($item['id'] != $item['parent'])) {
                        $author = ['uid' => 0, 'id' => $item['author-id'],
                                'network' => $item['author-network'], 'url' => $item['author-link']];
                        $url = Contact::magicLinkByContact($author);
@@ -1451,7 +1457,9 @@ function conv_sort(array $item_list, $order)
                }
        }
 
-       if (stristr($order, 'received')) {
+       if (stristr($order, 'pinned_received')) {
+               usort($parents, 'sort_thr_pinned_received');
+       } elseif (stristr($order, 'received')) {
                usort($parents, 'sort_thr_received');
        } elseif (stristr($order, 'commented')) {
                usort($parents, 'sort_thr_commented');
@@ -1471,7 +1479,7 @@ function conv_sort(array $item_list, $order)
                $parents[$i]['children'] = sort_item_children($parents[$i]['children']);
        }
 
-       if (PConfig::get(local_user(), 'system', 'smart_threading', 0)) {
+       if (!PConfig::get(local_user(), 'system', 'no_smart_threading', 0)) {
                foreach ($parents as $i => $parent) {
                        $parents[$i] = smart_flatten_conversation($parent);
                }
@@ -1488,6 +1496,24 @@ function conv_sort(array $item_list, $order)
        return $parents;
 }
 
+/**
+ * @brief usort() callback to sort item arrays by pinned and the received key
+ *
+ * @param array $a
+ * @param array $b
+ * @return int
+ */
+function sort_thr_pinned_received(array $a, array $b)
+{
+       if ($b['pinned'] && !$a['pinned']) {
+               return 1;
+       } elseif (!$b['pinned'] && $a['pinned']) {
+               return -1;
+       }
+
+       return strcmp($b['received'], $a['received']);
+}
+
 /**
  * @brief usort() callback to sort item arrays by the received key
  *