From 8e12dd1658ff33acc2783c85bd05ac5044384332 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Thu, 14 Jan 2021 14:45:40 +0000
Subject: [PATCH] Post view works now, "file" handling improved

---
 src/Model/Item.php       |   6 +-
 src/Model/Post.php       | 204 +++++++++++++++++++++++++++++++++++++++
 static/dbview.config.php |  74 +++++++-------
 3 files changed, 244 insertions(+), 40 deletions(-)
 create mode 100644 src/Model/Post.php

diff --git a/src/Model/Item.php b/src/Model/Item.php
index ee30f41ca0..60976363a6 100644
--- a/src/Model/Item.php
+++ b/src/Model/Item.php
@@ -262,7 +262,7 @@ class Item
 		// We can always comment on posts from these networks
 		if (array_key_exists('writable', $row) &&
 			in_array($row['internal-network'], Protocol::FEDERATED)) {
-			$row['writable'] = true;
+			$row['writable'] = 1;
 		}
 
 		// ---------------------- Transform item content data ----------------------
@@ -663,7 +663,7 @@ class Item
 			'resource-id', 'event-id', 'post-type', 'file',
 			'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
 			'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
-			'id' => 'item_id', 'network', 'icid',
+			'id' => 'item_id', 'network', 'icid', 'event-id',
 			'uri-id' => 'internal-uri-id', 'uid' => 'internal-uid',
 			'network' => 'internal-network', 'psid' => 'internal-psid'];
 
@@ -703,7 +703,7 @@ class Item
 			'summary' => 'event-summary','desc' => 'event-desc',
 			'location' => 'event-location', 'type' => 'event-type',
 			'nofinish' => 'event-nofinish','adjust' => 'event-adjust',
-			'ignore' => 'event-ignore', 'id' => 'event-id'];
+			'ignore' => 'event-ignore'];
 
 		$fields['diaspora-interaction'] = ['interaction', 'interaction' => 'signed_text'];
 
diff --git a/src/Model/Post.php b/src/Model/Post.php
new file mode 100644
index 0000000000..d29728e490
--- /dev/null
+++ b/src/Model/Post.php
@@ -0,0 +1,204 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Model;
+
+use Friendica\Core\Protocol;
+use Friendica\Database\DBA;
+use Friendica\Protocol\Activity;
+use Friendica\Repository\PermissionSet;
+
+class Post
+{
+	/**
+	 * Fetch a single post row
+	 *
+	 * @param mixed $stmt statement object
+	 * @return array|false current row or false
+	 * @throws \Exception
+	 */
+	public static function fetch($stmt)
+	{
+		$row = DBA::fetch($stmt);
+
+		if (!is_array($row)) {
+			return $row;
+		}
+
+		if (array_key_exists('verb', $row)) {
+			if (in_array($row['verb'], Item::ACTIVITIES)) {
+				if (array_key_exists('title', $row)) {
+					$row['title'] = '';
+				}
+				if (array_key_exists('body', $row)) {
+					$row['body'] = $row['verb'];
+				}
+				if (array_key_exists('object', $row)) {
+					$row['object'] = '';
+				}
+				if (array_key_exists('object-type', $row)) {
+					$row['object-type'] = Activity\ObjectType::NOTE;
+				}
+			} elseif (in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
+				// Posts don't have a target - but having tags or files.
+				if (array_key_exists('target', $row)) {
+					$row['target'] = '';
+				}
+			}
+		}
+
+		if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
+			// Build the file string out of the term entries
+			if (array_key_exists('file', $row)) {
+				if ($row['internal-file-count'] > 0) {
+					$row['file'] = Post\Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
+				} else {
+					$row['file'] = '';
+				}
+			}
+		}
+
+		// Remove internal fields
+		unset($row['internal-file-count']);
+		unset($row['internal-uri-id']);
+		unset($row['internal-uid']);
+
+		return $row;
+	}
+
+	/**
+	 * Fills an array with data from an post query
+	 *
+	 * @param object $stmt statement object
+	 * @param bool   $do_close
+	 * @return array Data array
+	 */
+	public static function inArray($stmt, $do_close = true) {
+		if (is_bool($stmt)) {
+			return $stmt;
+		}
+
+		$data = [];
+		while ($row = self::fetch($stmt)) {
+			$data[] = $row;
+		}
+		if ($do_close) {
+			DBA::close($stmt);
+		}
+		return $data;
+	}
+
+	/**
+	 * Check if post data exists
+	 *
+	 * @param array $condition array of fields for condition
+	 *
+	 * @return boolean Are there rows for that condition?
+	 * @throws \Exception
+	 */
+	public static function exists($condition) {
+		$stmt = self::select(['id'], $condition, ['limit' => 1]);
+
+		if (is_bool($stmt)) {
+			$retval = $stmt;
+		} else {
+			$retval = (DBA::numRows($stmt) > 0);
+		}
+
+		DBA::close($stmt);
+
+		return $retval;
+	}
+
+	/**
+	 * Retrieve a single record from the post table and returns it in an associative array
+	 *
+	 * @param array $fields
+	 * @param array $condition
+	 * @param array $params
+	 * @return bool|array
+	 * @throws \Exception
+	 * @see   DBA::select
+	 */
+	public static function selectFirst(array $fields = [], array $condition = [], $params = [])
+	{
+		$params['limit'] = 1;
+
+		$result = self::select($fields, $condition, $params);
+
+		if (is_bool($result)) {
+			return $result;
+		} else {
+			$row = self::fetch($result);
+			DBA::close($result);
+			return $row;
+		}
+	}
+
+	/**
+	 * Select rows from the post table and returns them as an array
+	 *
+	 * @param array $selected  Array of selected fields, empty for all
+	 * @param array $condition Array of fields for condition
+	 * @param array $params    Array of several parameters
+	 *
+	 * @return array
+	 * @throws \Exception
+	 */
+	public static function selectToArray(array $fields = [], array $condition = [], $params = [])
+	{
+		$result = self::select($fields, $condition, $params);
+
+		if (is_bool($result)) {
+			return [];
+		}
+
+		$data = [];
+		while ($row = self::fetch($result)) {
+			$data[] = $row;
+		}
+		DBA::close($result);
+
+		return $data;
+	}
+
+	/**
+	 * Select rows from the post table
+	 *
+	 * @param array $selected  Array of selected fields, empty for all
+	 * @param array $condition Array of fields for condition
+	 * @param array $params    Array of several parameters
+	 *
+	 * @return boolean|object
+	 * @throws \Exception
+	 */
+	public static function select(array $selected = [], array $condition = [], $params = [])
+	{
+		if (empty($selected)) {
+			$selected = Item::DISPLAY_FIELDLIST;
+		}
+
+		$selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']);
+		$selected = array_unique($selected);
+
+		return DBA::select('post-view', $selected, $condition, $params);
+	}
+}
diff --git a/static/dbview.config.php b/static/dbview.config.php
index ff8769a196..111aa67a9d 100644
--- a/static/dbview.config.php
+++ b/static/dbview.config.php
@@ -42,12 +42,14 @@ return [
 			"id" => ["item", "id"],
 			"item_id" => ["item", "id"],
 			"uid" => ["item", "uid"],
+			"internal-uid" => ["item", "uid"],
 			"parent" => ["item", "parent"],
 			"uri" => ["item", "uri"],
 			"parent-uri" => ["item", "parent-uri"],
 			"thr-parent" => ["item", "thr-parent"],
 			"guid" => ["item", "guid"],
 			"uri-id" => ["item", "uri-id"],
+			"internal-uri-id" => ["item", "uri-id"],
 			"parent-uri-id" => ["item", "parent-uri-id"],
 			"thr-parent-id" => ["item", "thr-parent-id"],
 			"contact-id" => ["item", "contact-id"],
@@ -55,7 +57,7 @@ return [
 			"contact-link" => ["contact", "url"],
 			"contact-name" => ["contact", "name"],
 			"contact-avatar" => ["contact", "thumb"],
-			"writable" => ["contact", "writable"],
+			"writable" => "IF (`item`.`network` IN ('apub', 'dfrn', 'dspr', 'stat'), true, `contact`.`writable`)",
 			"self" => ["contact", "self"],
 			"cid" => ["contact", "id"],
 			"alias" => ["contact", "alias"],
@@ -82,15 +84,15 @@ return [
 			"language" => ["item-content", "language"],
 			"raw-body" => ["item-content", "raw-body"],
 			"resource-id" => ["item", "resource-id"],
-			"event-id" => ["item", "event-id"],
 			"attach" => ["item", "attach"],
 			"postopts" => ["post-delivery-data", "postopts"],
 			"inform" => ["post-delivery-data", "inform"],
+			"internal-file-count" => "(SELECT COUNT(*) FROM `post-category` WHERE `post-category`.`uri-id` = `item`.`uri-id`)",
 			"file" => "NULL",
-			"allow_cid" => ["permissionset", "allow_cid"],
-			"allow_gid" => ["permissionset", "allow_gid"],
-			"deny_cid" => ["permissionset", "deny_cid"],
-			"deny_gid" => ["permissionset", "deny_gid"],
+			"allow_cid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_cid`)",
+			"allow_gid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_gid`)",
+			"deny_cid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_cid`)",
+			"deny_gid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_gid`)",
 			"post-type" => ["item", "post-type"],
 			"private" => ["item", "private"],
 			"pubmail" => ["item", "pubmail"],
@@ -120,16 +122,16 @@ return [
 			"author-id" => ["item", "author-id"],
 			"author-link" => ["author", "url"],
 			"author-addr" => ["author", "addr"],
-			"author-name" => ["author", "name"],
+			"author-name" => "IF (`contact`.`url` = `author`.`url`, `contact`.`name`, `author`.`name`)",
 			"author-nick" => ["author", "nick"],
-			"author-avatar" => ["author", "thumb"],
+			"author-avatar" => "IF (`contact`.`url` = `author`.`url`, `contact`.`thumb`, `author`.`thumb`)",
 			"author-network" => ["author", "network"],
 			"owner-id" => ["item", "owner-id"],
 			"owner-link" => ["owner", "url"],
 			"owner-addr" => ["owner", "addr"],
-			"owner-avatar" => ["owner", "thumb"],
+			"owner-name" => "IF (`contact`.`url` = `owner`.`url`, `contact`.`name`, `owner`.`name`)",
 			"owner-nick" => ["owner", "nick"],
-			"owner-name" => ["owner", "name"],
+			"owner-avatar" => "IF (`contact`.`url` = `owner`.`url`, `contact`.`thumb`, `owner`.`thumb`)",
 			"owner-network" => ["owner", "network"],
 			"causer-id" => ["item", "causer-id"],
 			"causer-link" => ["causer", "url"],
@@ -139,44 +141,42 @@ return [
 			"causer-avatar" => ["causer", "thumb"],
 			"causer-network" => ["causer", "network"],
 			"causer-contact-type" => ["causer", "contact-type"],
-			"event-id" => ["event"."id"],
-			"event-created" => ["event"."created"],
-			"event-edited" => ["event"."edited"],
-			"event-start" => ["event"."start"],
-			"event-finish" => ["event"."finish"],
-			"event-summary" => ["event"."summary"],
-			"event-desc" => ["event"."id"],
-			"event-location" => ["event"."location"],
-			"event-type" => ["event"."type"],
-			"event-nofinish" => ["event"."nofinish"],
-			"event-adjust" => ["event"."adjust"],
-			"event-ignore" => ["event"."ignore"],
-			"delivery_queue_count" => ["post-delivery-data"."queue_count"],
-			"delivery_queue_done" => ["post-delivery-data"."queue_done"],
-			"delivery_queue_failed" => ["post-delivery-data"."queue_failed"],
+			"event-id" => ["item", "event-id"],
+			"event-created" => ["event", "created"],
+			"event-edited" => ["event", "edited"],
+			"event-start" => ["event", "start"],
+			"event-finish" => ["event", "finish"],
+			"event-summary" => ["event", "summary"],
+			"event-desc" => ["event", "desc"],
+			"event-location" => ["event", "location"],
+			"event-type" => ["event", "type"],
+			"event-nofinish" => ["event", "nofinish"],
+			"event-adjust" => ["event", "adjust"],
+			"event-ignore" => ["event", "ignore"],
+			"delivery_queue_count" => ["post-delivery-data", "queue_count"],
+			"delivery_queue_done" => ["post-delivery-data", "queue_done"],
+			"delivery_queue_failed" => ["post-delivery-data", "queue_failed"],
+			"signed_text" => ["diaspora-interaction", "interaction"],
 			"parent-guid" => ["parent-item", "guid"],
 			"parent-network" => ["parent-item", "network"],
 			"parent-author-id" => ["parent-item", "author-id"],
 			"parent-author-link" => ["parent-item-author", "url"],  
 			"parent-author-name" => ["parent-item-author", "name"],
 			"parent-author-network" => ["parent-item-author", "network"], 
-			"signed_text" => ["diaspora-interaction", "interaction"],
 		],
 		"query" => "FROM `item`
-			LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-			LEFT JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
-			LEFT JOIN `contact` AS `owner` ON `owner`.`id` = `item`.`owner-id`
-			LEFT JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id`
-			STRAIGHT_JOIN `group_member` ON `group_member`.`contact-id` = `item`.`contact-id`
-			STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
-			LEFT JOIN `event` ON `event-id` = `event`.`id`
+			STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+			STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
+			STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = `item`.`owner-id`
+			STRAIGHT_JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id`
+			LEFT JOIN `verb` ON `verb`.`id` = `item`.`vid`
+			STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent`
+			STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`
+			LEFT JOIN `event` ON `event`.`id` = `item`.`event-id`
 			LEFT JOIN `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `item`.`uri-id`
 			LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
 			LEFT JOIN `post-delivery-data` ON `post-delivery-data`.`uri-id` = `item`.`uri-id` AND `item`.`origin`
-			LEFT JOIN `verb` ON `verb`.`id` = `item`.`vid`
-			LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`
-			STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent`
-			STRAIGHT_JOIN `contact` AS `parent-item-author` ON `parent-item-author`.`id` = `parent-item`.`author-id`"
+			LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid`"
 	],
 	"category-view" => [
 		"fields" => [
-- 
2.39.5