Merge pull request #3912 from MrPetovan/task/smart_threading

Introducing smart threading
This commit is contained in:
Michael Vogel 2017-11-19 14:48:58 +01:00 committed by GitHub
commit 97f063e4be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 217 additions and 85 deletions

View File

@ -1361,13 +1361,23 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false) {
return $o;
}
function get_item_children($arr, $parent) {
$children = array();
$a = get_app();
foreach ($arr as $item) {
/**
* Returns all the children in the given item list of the given parent, recusrsively
* or not.
*
* @brief Returns all the children in the given item list of the given parent
*
* @param array $item_list
* @param array $parent
* @param bool $recursive
* @return type
*/
function get_item_children(array $item_list, array $parent, $recursive = true)
{
$children = [];
foreach ($item_list as $item) {
if ($item['id'] != $item['parent']) {
if (Config::get('system', 'thread_allow') && $a->theme_thread_allow) {
if ($recursive) {
// Fallback to parent-uri if thr-parent is not set
$thr_parent = $item['thr-parent'];
if ($thr_parent == '') {
@ -1375,7 +1385,7 @@ function get_item_children($arr, $parent) {
}
if ($thr_parent == $parent['uri']) {
$item['children'] = get_item_children($arr, $item);
$item['children'] = get_item_children($item_list, $item);
$children[] = $item;
}
} elseif ($item['parent'] == $parent['id']) {
@ -1386,54 +1396,128 @@ function get_item_children($arr, $parent) {
return $children;
}
/// @TODO Add type-hint
function sort_item_children($items) {
/**
* @brief Recursively sorts a tree-like item array
*
* @param array $items
* @return array
*/
function sort_item_children(array $items)
{
$result = $items;
usort($result, 'sort_thr_created_rev');
foreach ($result as $k => $i) {
if (count($result[$k]['children'])) {
if (isset($result[$k]['children'])) {
$result[$k]['children'] = sort_item_children($result[$k]['children']);
}
}
return $result;
}
/// @TODO Add type-hint
function add_children_to_list($children, &$arr) {
foreach ($children as $y) {
$arr[] = $y;
if (count($y['children'])) {
add_children_to_list($y['children'], $arr);
/**
* @brief Recursively add all children items at the top level of a list
*
* @param array $children List of items to append
* @param array $item_list
*/
function add_children_to_list(array $children, array &$item_list)
{
foreach ($children as $child) {
$item_list[] = $child;
if (isset($child['children'])) {
add_children_to_list($child['children'], $item_list);
}
}
}
/// @TODO Add type-hint
function conv_sort($arr, $order) {
if ((!(is_array($arr) && count($arr)))) {
return array();
/**
* This recursive function takes the item tree structure created by conv_sort() and
* flatten the extraneous depth levels when people reply sequentially, removing the
* stairs effect in threaded conversations limiting the available content width.
*
* The basic principle is the following: if a post item has only one reply and is
* the last reply of its parent, then the reply is moved to the parent.
*
* This process is rendered somewhat more complicated because items can be either
* replies or likes, and these don't factor at all in the reply count/last reply.
*
* @brief Selectively flattens a tree-like item structure to prevent threading stairs
*
* @param array $parent A tree-like array of items
* @return array
*/
function smart_flatten_conversation(array $parent)
{
if (! isset($parent['children']) || count($parent['children']) == 0) {
return $parent;
}
$parents = array();
$children = array();
$newarr = array();
// We use a for loop to ensure we process the newly-moved items
for ($i = 0; $i < count($parent['children']); $i++) {
$child = $parent['children'][$i];
/*
* This is a preparation for having two different items with the same uri in one thread
* This will otherwise lead to an endless loop.
*/
foreach ($arr as $x) {
if (!isset($newarr[$x['uri']])) {
$newarr[$x['uri']] = $x;
if (isset($child['children']) && count($child['children'])) {
// This helps counting only the regular posts
$count_post_closure = function($var) {
return $var['verb'] === ACTIVITY_POST;
};
$child_post_count = count(array_filter($child['children'], $count_post_closure));
$remaining_post_count = count(array_filter(array_slice($parent['children'], $i), $count_post_closure));
// If there's only one child's children post and this is the last child post
if ($child_post_count == 1 && $remaining_post_count == 1) {
// Searches the post item in the children
$j = 0;
while($child['children'][$j]['verb'] !== ACTIVITY_POST && $j < count($child['children'])) {
$j ++;
}
$moved_item = $child['children'][$j];
unset($parent['children'][$i]['children'][$j]);
$parent['children'][] = $moved_item;
} else {
$parent['children'][$i] = smart_flatten_conversation($child);
}
}
}
$arr = $newarr;
return $parent;
}
foreach ($arr as $x) {
if ($x['id'] == $x['parent']) {
$parents[] = $x;
/**
* Expands a flat list of items into corresponding tree-like conversation structures,
* sort the top-level posts either on "created" 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"
* @return array
*/
function conv_sort(array $item_list, $order)
{
$parents = [];
if (!(is_array($item_list) && count($item_list))) {
return $parents;
}
$item_array = [];
// Dedupes the item list on the uri to prevent infinite loops
foreach ($item_list as $item) {
$item_array[$item['uri']] = $item;
}
// Extract the top level items
foreach ($item_array as $item) {
if ($item['id'] == $item['parent']) {
$parents[] = $item;
}
}
@ -1443,73 +1527,69 @@ function conv_sort($arr, $order) {
usort($parents, 'sort_thr_commented');
}
if (count($parents)) {
foreach ($parents as $i => $_x) {
$parents[$i]['children'] = get_item_children($arr, $_x);
$thread_allowed = Config::get('system', 'thread_allow') && get_app()->theme_thread_allow;
foreach ($parents as $i => $parent) {
$parents[$i]['children'] = get_item_children($item_array, $parent, $thread_allowed);
}
foreach ($parents as $i => $parent) {
$parents[$i]['children'] = sort_item_children($parents[$i]['children']);
}
if ($thread_allowed && PConfig::get(local_user(), 'system', 'smart_threading', 0)) {
foreach ($parents as $i => $parent) {
$parents[$i] = smart_flatten_conversation($parent);
}
}
/// @TODO Old-lost code?
/*foreach ($arr as $x) {
if ($x['id'] != $x['parent']) {
$p = find_thread_parent_index($parents,$x);
if ($p !== false)
$parents[$p]['children'][] = $x;
}
}*/
if (count($parents)) {
foreach ($parents as $k => $v) {
if (count($parents[$k]['children'])) {
$parents[$k]['children'] = sort_item_children($parents[$k]['children']);
/// @TODO Old-lost code?
/*$y = $parents[$k]['children'];
usort($y,'sort_thr_created_rev');
$parents[$k]['children'] = $y;*/
}
/// @TODO: Stop recusrsively adding all children back to the top level (!!!)
/// However, this apparently ensures responses (likes, attendance) display (?!)
foreach ($parents as $parent) {
if (count($parent['children'])) {
add_children_to_list($parent['children'], $parents);
}
}
$ret = array();
if (count($parents)) {
foreach ($parents as $x) {
$ret[] = $x;
if (count($x['children'])) {
add_children_to_list($x['children'], $ret);
/// @TODO Old-lost code?
/*foreach ($x['children'] as $y)
$ret[] = $y;*/
}
}
}
return $ret;
return $parents;
}
/// @TODO Add type-hint
function sort_thr_created($a, $b) {
/**
* @brief usort() callback to sort item arrays by the created key
*
* @param array $a
* @param array $b
* @return int
*/
function sort_thr_created(array $a, array $b)
{
return strcmp($b['created'], $a['created']);
}
/// @TODO Add type-hint
function sort_thr_created_rev($a, $b) {
/**
* @brief usort() callback to reverse sort item arrays by the created key
*
* @param array $a
* @param array $b
* @return int
*/
function sort_thr_created_rev(array $a, array $b)
{
return strcmp($a['created'], $b['created']);
}
/// @TODO Add type-hint
function sort_thr_commented($a, $b) {
/**
* @brief usort() callback to sort item arrays by the commented key
*
* @param array $a
* @param array $b
* @return type
*/
function sort_thr_commented(array $a, array $b)
{
return strcmp($b['commented'], $a['commented']);
}
/// @TODO Add type-hint
function find_thread_parent_index($arr, $x) {
foreach ($arr as $k => $v) {
if ($v['id'] == $x['parent']) {
return $k;
}
}
return false;
}
/// @TODO Add type-hint
function render_location_dummy($item) {
if ($item['location'] != "") {

View File

@ -303,6 +303,7 @@ function settings_post(App $a) {
$infinite_scroll = x($_POST, 'infinite_scroll') ? intval($_POST['infinite_scroll']) : 0;
$no_auto_update = x($_POST, 'no_auto_update') ? intval($_POST['no_auto_update']) : 0;
$bandwidth_saver = x($_POST, 'bandwidth_saver') ? intval($_POST['bandwidth_saver']) : 0;
$smart_threading = x($_POST, 'smart_threading') ? intval($_POST['smart_threading']) : 0;
$nowarn_insecure = x($_POST, 'nowarn_insecure') ? intval($_POST['nowarn_insecure']) : 0;
$browser_update = x($_POST, 'browser_update') ? intval($_POST['browser_update']) : 0;
if ($browser_update != -1) {
@ -335,6 +336,7 @@ function settings_post(App $a) {
PConfig::set(local_user(), 'system', 'infinite_scroll' , $infinite_scroll);
PConfig::set(local_user(), 'system', 'no_auto_update' , $no_auto_update);
PConfig::set(local_user(), 'system', 'bandwidth_saver' , $bandwidth_saver);
PConfig::set(local_user(), 'system', 'smart_threading' , $smart_threading);
if ($theme == $a->user['theme']) {
// call theme_post only if theme has not been changed
@ -989,6 +991,7 @@ function settings_content(App $a) {
$infinite_scroll = PConfig::get(local_user(), 'system', 'infinite_scroll', 0);
$no_auto_update = PConfig::get(local_user(), 'system', 'no_auto_update', 0);
$bandwidth_saver = PConfig::get(local_user(), 'system', 'bandwidth_saver', 0);
$smart_threading = PConfig::get(local_user(), 'system', 'smart_threading', 0);
$theme_config = "";
if (($themeconfigfile = get_theme_config_file($theme_selected)) != null) {
@ -1017,6 +1020,7 @@ function settings_content(App $a) {
'$infinite_scroll' => array('infinite_scroll', t("Infinite scroll"), $infinite_scroll, ''),
'$no_auto_update' => array('no_auto_update', t("Automatic updates only at the top of the network page"), $no_auto_update, t('When disabled, the network page is updated all the time, which could be confusing while reading.')),
'$bandwidth_saver' => array('bandwidth_saver', t('Bandwith Saver Mode'), $bandwidth_saver, t('When enabled, embedded content is not displayed on automatic updates, they only show on page reload.')),
'$smart_threading' => array('smart_threading', t('Smart Threading'), $smart_threading, t('When enabled, suppress extraneous thread indentation while keeping it where it matters. Only works if threading is available and enabled.')),
'$d_tset' => t('General Theme Settings'),
'$d_ctset' => t('Custom Theme Settings'),

View File

@ -10,17 +10,40 @@ return array(
'Console_Getopt' => $vendorDir . '/pear-pear.php.net/Console_Getopt/Console/Getopt.php',
'Detection\\MobileDetect' => $vendorDir . '/mobiledetect/mobiledetectlib/namespaced/Detection/MobileDetect.php',
'Friendica\\App' => $baseDir . '/src/App.php',
'Friendica\\Content\\Smilies' => $baseDir . '/src/Content/Smilies.php',
'Friendica\\Core\\BaseObject' => $baseDir . '/src/Core/BaseObject.php',
'Friendica\\Core\\Cache' => $baseDir . '/src/Core/Cache.php',
'Friendica\\Core\\Config' => $baseDir . '/src/Core/Config.php',
'Friendica\\Core\\Conversation' => $baseDir . '/src/Core/Conversation.php',
'Friendica\\Core\\Item' => $baseDir . '/src/Core/Item.php',
'Friendica\\Core\\NotificationsManager' => $baseDir . '/src/Core/NotificationsManager.php',
'Friendica\\Core\\PConfig' => $baseDir . '/src/Core/PConfig.php',
'Friendica\\Core\\System' => $baseDir . '/src/Core/System.php',
'Friendica\\Core\\Worker' => $baseDir . '/src/Core/Worker.php',
'Friendica\\Database\\DBM' => $baseDir . '/src/Database/DBM.php',
'Friendica\\Model\\GlobalContact' => $baseDir . '/src/Model/GlobalContact.php',
'Friendica\\Network\\Probe' => $baseDir . '/src/Network/Probe.php',
'Friendica\\ParseUrl' => $baseDir . '/src/ParseUrl.php',
'Friendica\\Protocol\\DFRN' => $baseDir . '/src/Protocol/DFRN.php',
'Friendica\\Protocol\\Diaspora' => $baseDir . '/src/Protocol/Diaspora.php',
'Friendica\\Protocol\\OStatus' => $baseDir . '/src/Protocol/OStatus.php',
'Friendica\\Protocol\\PortableContact' => $baseDir . '/src/Protocol/PortableContact.php',
'Friendica\\Util\\Lock' => $baseDir . '/src/Util/Lock.php',
'Friendica\\Util\\XML' => $baseDir . '/src/Util/XML.php',
'Friendica\\Worker\\CheckVersion' => $baseDir . '/src/Worker/CheckVersion.php',
'Friendica\\Worker\\Cron' => $baseDir . '/src/Worker/Cron.php',
'Friendica\\Worker\\CronHooks' => $baseDir . '/src/Worker/CronHooks.php',
'Friendica\\Worker\\CronJobs' => $baseDir . '/src/Worker/CronJobs.php',
'Friendica\\Worker\\DBClean' => $baseDir . '/src/Worker/DBClean.php',
'Friendica\\Worker\\DBUpdate' => $baseDir . '/src/Worker/DBUpdate.php',
'Friendica\\Worker\\Directory' => $baseDir . '/src/Worker/Directory.php',
'Friendica\\Worker\\DiscoverPoCo' => $baseDir . '/src/Worker/DiscoverPoCo.php',
'Friendica\\Worker\\OnePoll' => $baseDir . '/src/Worker/OnePoll.php',
'Friendica\\Worker\\RemoveContact' => $baseDir . '/src/Worker/RemoveContact.php',
'Friendica\\Worker\\SpoolPost' => $baseDir . '/src/Worker/SpoolPost.php',
'Friendica\\Worker\\TagUpdate' => $baseDir . '/src/Worker/TagUpdate.php',
'Friendica\\Worker\\ThreadUpdate' => $baseDir . '/src/Worker/ThreadUpdate.php',
'Friendica\\Worker\\UpdateGContact' => $baseDir . '/src/Worker/UpdateGContact.php',
'HTMLPurifier' => $vendorDir . '/ezyang/htmlpurifier/library/HTMLPurifier.php',
'HTMLPurifier_Arborize' => $vendorDir . '/ezyang/htmlpurifier/library/HTMLPurifier/Arborize.php',
'HTMLPurifier_AttrCollections' => $vendorDir . '/ezyang/htmlpurifier/library/HTMLPurifier/AttrCollections.php',

View File

@ -62,17 +62,40 @@ class ComposerStaticInitFriendica
'Console_Getopt' => __DIR__ . '/..' . '/pear-pear.php.net/Console_Getopt/Console/Getopt.php',
'Detection\\MobileDetect' => __DIR__ . '/..' . '/mobiledetect/mobiledetectlib/namespaced/Detection/MobileDetect.php',
'Friendica\\App' => __DIR__ . '/../..' . '/src/App.php',
'Friendica\\Content\\Smilies' => __DIR__ . '/../..' . '/src/Content/Smilies.php',
'Friendica\\Core\\BaseObject' => __DIR__ . '/../..' . '/src/Core/BaseObject.php',
'Friendica\\Core\\Cache' => __DIR__ . '/../..' . '/src/Core/Cache.php',
'Friendica\\Core\\Config' => __DIR__ . '/../..' . '/src/Core/Config.php',
'Friendica\\Core\\Conversation' => __DIR__ . '/../..' . '/src/Core/Conversation.php',
'Friendica\\Core\\Item' => __DIR__ . '/../..' . '/src/Core/Item.php',
'Friendica\\Core\\NotificationsManager' => __DIR__ . '/../..' . '/src/Core/NotificationsManager.php',
'Friendica\\Core\\PConfig' => __DIR__ . '/../..' . '/src/Core/PConfig.php',
'Friendica\\Core\\System' => __DIR__ . '/../..' . '/src/Core/System.php',
'Friendica\\Core\\Worker' => __DIR__ . '/../..' . '/src/Core/Worker.php',
'Friendica\\Database\\DBM' => __DIR__ . '/../..' . '/src/Database/DBM.php',
'Friendica\\Model\\GlobalContact' => __DIR__ . '/../..' . '/src/Model/GlobalContact.php',
'Friendica\\Network\\Probe' => __DIR__ . '/../..' . '/src/Network/Probe.php',
'Friendica\\ParseUrl' => __DIR__ . '/../..' . '/src/ParseUrl.php',
'Friendica\\Protocol\\DFRN' => __DIR__ . '/../..' . '/src/Protocol/DFRN.php',
'Friendica\\Protocol\\Diaspora' => __DIR__ . '/../..' . '/src/Protocol/Diaspora.php',
'Friendica\\Protocol\\OStatus' => __DIR__ . '/../..' . '/src/Protocol/OStatus.php',
'Friendica\\Protocol\\PortableContact' => __DIR__ . '/../..' . '/src/Protocol/PortableContact.php',
'Friendica\\Util\\Lock' => __DIR__ . '/../..' . '/src/Util/Lock.php',
'Friendica\\Util\\XML' => __DIR__ . '/../..' . '/src/Util/XML.php',
'Friendica\\Worker\\CheckVersion' => __DIR__ . '/../..' . '/src/Worker/CheckVersion.php',
'Friendica\\Worker\\Cron' => __DIR__ . '/../..' . '/src/Worker/Cron.php',
'Friendica\\Worker\\CronHooks' => __DIR__ . '/../..' . '/src/Worker/CronHooks.php',
'Friendica\\Worker\\CronJobs' => __DIR__ . '/../..' . '/src/Worker/CronJobs.php',
'Friendica\\Worker\\DBClean' => __DIR__ . '/../..' . '/src/Worker/DBClean.php',
'Friendica\\Worker\\DBUpdate' => __DIR__ . '/../..' . '/src/Worker/DBUpdate.php',
'Friendica\\Worker\\Directory' => __DIR__ . '/../..' . '/src/Worker/Directory.php',
'Friendica\\Worker\\DiscoverPoCo' => __DIR__ . '/../..' . '/src/Worker/DiscoverPoCo.php',
'Friendica\\Worker\\OnePoll' => __DIR__ . '/../..' . '/src/Worker/OnePoll.php',
'Friendica\\Worker\\RemoveContact' => __DIR__ . '/../..' . '/src/Worker/RemoveContact.php',
'Friendica\\Worker\\SpoolPost' => __DIR__ . '/../..' . '/src/Worker/SpoolPost.php',
'Friendica\\Worker\\TagUpdate' => __DIR__ . '/../..' . '/src/Worker/TagUpdate.php',
'Friendica\\Worker\\ThreadUpdate' => __DIR__ . '/../..' . '/src/Worker/ThreadUpdate.php',
'Friendica\\Worker\\UpdateGContact' => __DIR__ . '/../..' . '/src/Worker/UpdateGContact.php',
'HTMLPurifier' => __DIR__ . '/..' . '/ezyang/htmlpurifier/library/HTMLPurifier.php',
'HTMLPurifier_Arborize' => __DIR__ . '/..' . '/ezyang/htmlpurifier/library/HTMLPurifier/Arborize.php',
'HTMLPurifier_AttrCollections' => __DIR__ . '/..' . '/ezyang/htmlpurifier/library/HTMLPurifier/AttrCollections.php',

View File

@ -20,6 +20,7 @@
{{include file="field_checkbox.tpl" field=$noinfo}}
{{include file="field_checkbox.tpl" field=$infinite_scroll}}
{{include file="field_checkbox.tpl" field=$bandwidth_saver}}
{{include file="field_checkbox.tpl" field=$smart_threading}}
<h2>{{$calendar_title}}</h2>
{{include file="field_select.tpl" field=$first_day_of_week}}

View File

@ -75,6 +75,7 @@
{{include file="field_checkbox.tpl" field=$noinfo}}
{{include file="field_checkbox.tpl" field=$infinite_scroll}}
{{include file="field_checkbox.tpl" field=$bandwidth_saver}}
{{include file="field_checkbox.tpl" field=$smart_threading}}
<div class="form-group pull-right settings-submit-wrapper" >
<button type="submit" name="submit" class="btn btn-primary" value="{{$submit|escape:'html'}}">{{$submit}}</button>