aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Zotlabs/Module/Channel.php13
-rw-r--r--Zotlabs/Module/Display.php15
-rw-r--r--Zotlabs/Module/Hq.php2
-rw-r--r--Zotlabs/Module/Network.php8
-rw-r--r--Zotlabs/Module/Pubstream.php9
-rw-r--r--include/items.php75
6 files changed, 101 insertions, 21 deletions
diff --git a/Zotlabs/Module/Channel.php b/Zotlabs/Module/Channel.php
index 9671cedf3..93803da18 100644
--- a/Zotlabs/Module/Channel.php
+++ b/Zotlabs/Module/Channel.php
@@ -417,12 +417,17 @@ class Channel extends Controller {
}
}
if ($r) {
- $parents_str = ids_to_querystr($r, 'item_id');
+ //$parents_str = ids_to_querystr($r, 'item_id');
- $r = items_by_parent_ids($parents_str, permission_sql: $permission_sql, blog_mode: $blog_mode);
+ //$r = items_by_parent_ids($parents_str, permission_sql: $permission_sql, blog_mode: $blog_mode);
+ $items = [];
+ foreach($r as $parent) {
+ $nitems = items_by_parent_id($parent['item_id'], permission_sql: $permission_sql, blog_mode: $blog_mode);
+ $items = array_merge($items, $nitems);
+ }
- xchan_query($r);
- $items = fetch_post_tags($r, true);
+ xchan_query($items);
+ $items = fetch_post_tags($items, true);
$items = conv_sort($items, $ordering);
if ($load && $mid && (!count($items))) {
diff --git a/Zotlabs/Module/Display.php b/Zotlabs/Module/Display.php
index 07973431c..d3657149f 100644
--- a/Zotlabs/Module/Display.php
+++ b/Zotlabs/Module/Display.php
@@ -288,15 +288,12 @@ class Display extends Controller {
}
if($r) {
- $parents_str = ids_to_querystr($r,'item_id');
- if($parents_str) {
- $thr_parents = get_recursive_thr_parents($target_item);
- $items = items_by_parent_ids($parents_str, $thr_parents, $permission_sql);
-
- xchan_query($items);
- $items = fetch_post_tags($items,true);
- $items = conv_sort($items,'created');
- }
+ $thr_parents = get_recursive_thr_parents($target_item);
+ $items = items_by_parent_id($r[0]['item_id'], $thr_parents, $permission_sql);
+
+ xchan_query($items);
+ $items = fetch_post_tags($items,true);
+ $items = conv_sort($items,'created');
}
else {
$items = array();
diff --git a/Zotlabs/Module/Hq.php b/Zotlabs/Module/Hq.php
index 562278973..c4f812854 100644
--- a/Zotlabs/Module/Hq.php
+++ b/Zotlabs/Module/Hq.php
@@ -201,7 +201,7 @@ class Hq extends \Zotlabs\Web\Controller {
if($r) {
$thr_parents = get_recursive_thr_parents($target_item);
- $items = items_by_parent_ids($r[0]['item_id'], $thr_parents);
+ $items = items_by_parent_id($r[0]['item_id'], $thr_parents);
xchan_query($items,true,(($sys_item) ? local_channel() : 0));
$items = fetch_post_tags($items,true);
diff --git a/Zotlabs/Module/Network.php b/Zotlabs/Module/Network.php
index fd30adccc..6dd00f13c 100644
--- a/Zotlabs/Module/Network.php
+++ b/Zotlabs/Module/Network.php
@@ -507,9 +507,11 @@ class Network extends \Zotlabs\Web\Controller {
// Then fetch all the children of the parents that are on this page
if($r) {
- $parents_str = ids_to_querystr($r, 'item_id');
-
- $items = items_by_parent_ids($parents_str, blog_mode: $blog_mode);
+ $items = [];
+ foreach($r as $parent) {
+ $nitems = items_by_parent_id($parent['item_id'], blog_mode: $blog_mode);
+ $items = array_merge($items, $nitems);
+ }
xchan_query($items, true);
$items = fetch_post_tags($items, true);
diff --git a/Zotlabs/Module/Pubstream.php b/Zotlabs/Module/Pubstream.php
index 79245f9c2..2944e496a 100644
--- a/Zotlabs/Module/Pubstream.php
+++ b/Zotlabs/Module/Pubstream.php
@@ -251,10 +251,11 @@ class Pubstream extends \Zotlabs\Web\Controller {
$parents_str = '';
if($r) {
-
- $parents_str = ids_to_querystr($r,'item_id');
-
- $items = items_by_parent_ids($parents_str);
+ $items = [];
+ foreach($r as $parent) {
+ $nitems = items_by_parent_id($parent['item_id']);
+ $items = array_merge($items, $nitems);
+ }
// use effective_uid param of xchan_query to help sort out comment permission
// for sys_channel owned items.
diff --git a/include/items.php b/include/items.php
index 157c2dbe0..94e909293 100644
--- a/include/items.php
+++ b/include/items.php
@@ -5450,6 +5450,7 @@ function item_by_item_id(int $id): array
* @param bool $blog_mode (optional) - if set to yes only the parent items will be returned
*/
+// TODO: improve SQL performance
function items_by_parent_ids(string $ids, array $thr_parents = [], string $permission_sql = '', bool $blog_mode = false): array
{
if (!$ids) {
@@ -5520,6 +5521,80 @@ function items_by_parent_ids(string $ids, array $thr_parents = [], string $permi
}
/**
+ * @brief returns an array of items by ids
+ * ATTENTION: no permissions for the pa are checked here!!!
+ * Permissions MUST be checked by the function which returns the ids.
+ * @param int $id - a parent item id
+ * @param array $thr_parents (optional) - a string with thr_parent mids separated by comma
+ * which will be included
+ * @param string $permission_sql (optional) - SQL provided by item_permission_sql() from the calling module
+ * @param bool $blog_mode (optional) - if set to yes only the parent items will be returned
+ */
+
+function items_by_parent_id(int $id, array $thr_parents = [], string $permission_sql = '', bool $blog_mode = false): array
+{
+ if (!$id) {
+ return [];
+ }
+
+ $item_normal = item_normal();
+ $item_normal_c = item_normal(prefix: 'c');
+ $activity_sql = item_activity_sql('c');
+ $thread_allow = ((local_channel()) ? PConfig::Get(local_channel(), 'system', 'thread_allow', true) : Config::Get('system', 'thread_allow', true));
+
+ $blog_mode_sql = (($blog_mode) ? 'item.id' : 'item.parent');
+
+ $thr_parent_sql = (($thread_allow) ? " AND item.thr_parent = item.parent_mid " : '');
+ if ($thr_parents && $thread_allow) {
+ $thr_parent_str = stringify_array($thr_parents, true);
+ $thr_parent_sql = " AND item.thr_parent IN (" . protect_sprintf($thr_parent_str) . ") ";
+ }
+
+ $permission_sql_c = '';
+ if ($permission_sql) {
+ $permission_sql_c = str_replace('item.', 'c.', $permission_sql);
+ }
+
+ $thread_limit_sql = '';
+ if (!$blog_mode && $thread_allow) {
+ // Get the last x replies but make sure the toplevel is included anyway
+ $thread_limit_sql = <<<SQL
+ ORDER BY
+ CASE WHEN item.id = item.parent THEN 0 ELSE 1 END,
+ item.created DESC
+ LIMIT 101
+ SQL;
+ }
+
+ $ret = q(
+ "SELECT item.*,
+ $activity_sql
+ FROM item
+ LEFT JOIN item c
+ ON c.parent = item.parent
+ AND c.item_thread_top = 0
+ AND c.thr_parent = item.mid
+ $item_normal_c
+ $permission_sql_c
+ WHERE $blog_mode_sql = %d
+ AND (
+ item.verb NOT IN ('Like', 'Dislike', 'Announce')
+ OR (item.verb = 'Announce' AND item.item_thread_top = 1)
+ )
+ $thr_parent_sql
+ $item_normal
+ $permission_sql
+ GROUP BY item.id
+ $thread_limit_sql",
+ intval($id)
+ );
+
+ return $ret;
+}
+
+
+
+/**
* @brief returns SQL which counts activities for an item and
* if there is an observer also count activities authored by observer.
* @param string $prefix (optional)