Post view works now, "file" handling improved

This commit is contained in:
Michael 2021-01-14 14:45:40 +00:00
parent ca16a4892b
commit 8e12dd1658
3 changed files with 244 additions and 40 deletions

View File

@ -262,7 +262,7 @@ class Item
// We can always comment on posts from these networks // We can always comment on posts from these networks
if (array_key_exists('writable', $row) && if (array_key_exists('writable', $row) &&
in_array($row['internal-network'], Protocol::FEDERATED)) { in_array($row['internal-network'], Protocol::FEDERATED)) {
$row['writable'] = true; $row['writable'] = 1;
} }
// ---------------------- Transform item content data ---------------------- // ---------------------- Transform item content data ----------------------
@ -663,7 +663,7 @@ class Item
'resource-id', 'event-id', 'post-type', 'file', 'resource-id', 'event-id', 'post-type', 'file',
'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', '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', 'uri-id' => 'internal-uri-id', 'uid' => 'internal-uid',
'network' => 'internal-network', 'psid' => 'internal-psid']; 'network' => 'internal-network', 'psid' => 'internal-psid'];
@ -703,7 +703,7 @@ class Item
'summary' => 'event-summary','desc' => 'event-desc', 'summary' => 'event-summary','desc' => 'event-desc',
'location' => 'event-location', 'type' => 'event-type', 'location' => 'event-location', 'type' => 'event-type',
'nofinish' => 'event-nofinish','adjust' => 'event-adjust', 'nofinish' => 'event-nofinish','adjust' => 'event-adjust',
'ignore' => 'event-ignore', 'id' => 'event-id']; 'ignore' => 'event-ignore'];
$fields['diaspora-interaction'] = ['interaction', 'interaction' => 'signed_text']; $fields['diaspora-interaction'] = ['interaction', 'interaction' => 'signed_text'];

204
src/Model/Post.php Normal file
View File

@ -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);
}
}

View File

@ -42,12 +42,14 @@ return [
"id" => ["item", "id"], "id" => ["item", "id"],
"item_id" => ["item", "id"], "item_id" => ["item", "id"],
"uid" => ["item", "uid"], "uid" => ["item", "uid"],
"internal-uid" => ["item", "uid"],
"parent" => ["item", "parent"], "parent" => ["item", "parent"],
"uri" => ["item", "uri"], "uri" => ["item", "uri"],
"parent-uri" => ["item", "parent-uri"], "parent-uri" => ["item", "parent-uri"],
"thr-parent" => ["item", "thr-parent"], "thr-parent" => ["item", "thr-parent"],
"guid" => ["item", "guid"], "guid" => ["item", "guid"],
"uri-id" => ["item", "uri-id"], "uri-id" => ["item", "uri-id"],
"internal-uri-id" => ["item", "uri-id"],
"parent-uri-id" => ["item", "parent-uri-id"], "parent-uri-id" => ["item", "parent-uri-id"],
"thr-parent-id" => ["item", "thr-parent-id"], "thr-parent-id" => ["item", "thr-parent-id"],
"contact-id" => ["item", "contact-id"], "contact-id" => ["item", "contact-id"],
@ -55,7 +57,7 @@ return [
"contact-link" => ["contact", "url"], "contact-link" => ["contact", "url"],
"contact-name" => ["contact", "name"], "contact-name" => ["contact", "name"],
"contact-avatar" => ["contact", "thumb"], "contact-avatar" => ["contact", "thumb"],
"writable" => ["contact", "writable"], "writable" => "IF (`item`.`network` IN ('apub', 'dfrn', 'dspr', 'stat'), true, `contact`.`writable`)",
"self" => ["contact", "self"], "self" => ["contact", "self"],
"cid" => ["contact", "id"], "cid" => ["contact", "id"],
"alias" => ["contact", "alias"], "alias" => ["contact", "alias"],
@ -82,15 +84,15 @@ return [
"language" => ["item-content", "language"], "language" => ["item-content", "language"],
"raw-body" => ["item-content", "raw-body"], "raw-body" => ["item-content", "raw-body"],
"resource-id" => ["item", "resource-id"], "resource-id" => ["item", "resource-id"],
"event-id" => ["item", "event-id"],
"attach" => ["item", "attach"], "attach" => ["item", "attach"],
"postopts" => ["post-delivery-data", "postopts"], "postopts" => ["post-delivery-data", "postopts"],
"inform" => ["post-delivery-data", "inform"], "inform" => ["post-delivery-data", "inform"],
"internal-file-count" => "(SELECT COUNT(*) FROM `post-category` WHERE `post-category`.`uri-id` = `item`.`uri-id`)",
"file" => "NULL", "file" => "NULL",
"allow_cid" => ["permissionset", "allow_cid"], "allow_cid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_cid`)",
"allow_gid" => ["permissionset", "allow_gid"], "allow_gid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`allow_gid`)",
"deny_cid" => ["permissionset", "deny_cid"], "deny_cid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_cid`)",
"deny_gid" => ["permissionset", "deny_gid"], "deny_gid" => "IF (`item`.`psid` IS NULL, '', `permissionset`.`deny_gid`)",
"post-type" => ["item", "post-type"], "post-type" => ["item", "post-type"],
"private" => ["item", "private"], "private" => ["item", "private"],
"pubmail" => ["item", "pubmail"], "pubmail" => ["item", "pubmail"],
@ -120,16 +122,16 @@ return [
"author-id" => ["item", "author-id"], "author-id" => ["item", "author-id"],
"author-link" => ["author", "url"], "author-link" => ["author", "url"],
"author-addr" => ["author", "addr"], "author-addr" => ["author", "addr"],
"author-name" => ["author", "name"], "author-name" => "IF (`contact`.`url` = `author`.`url`, `contact`.`name`, `author`.`name`)",
"author-nick" => ["author", "nick"], "author-nick" => ["author", "nick"],
"author-avatar" => ["author", "thumb"], "author-avatar" => "IF (`contact`.`url` = `author`.`url`, `contact`.`thumb`, `author`.`thumb`)",
"author-network" => ["author", "network"], "author-network" => ["author", "network"],
"owner-id" => ["item", "owner-id"], "owner-id" => ["item", "owner-id"],
"owner-link" => ["owner", "url"], "owner-link" => ["owner", "url"],
"owner-addr" => ["owner", "addr"], "owner-addr" => ["owner", "addr"],
"owner-avatar" => ["owner", "thumb"], "owner-name" => "IF (`contact`.`url` = `owner`.`url`, `contact`.`name`, `owner`.`name`)",
"owner-nick" => ["owner", "nick"], "owner-nick" => ["owner", "nick"],
"owner-name" => ["owner", "name"], "owner-avatar" => "IF (`contact`.`url` = `owner`.`url`, `contact`.`thumb`, `owner`.`thumb`)",
"owner-network" => ["owner", "network"], "owner-network" => ["owner", "network"],
"causer-id" => ["item", "causer-id"], "causer-id" => ["item", "causer-id"],
"causer-link" => ["causer", "url"], "causer-link" => ["causer", "url"],
@ -139,44 +141,42 @@ return [
"causer-avatar" => ["causer", "thumb"], "causer-avatar" => ["causer", "thumb"],
"causer-network" => ["causer", "network"], "causer-network" => ["causer", "network"],
"causer-contact-type" => ["causer", "contact-type"], "causer-contact-type" => ["causer", "contact-type"],
"event-id" => ["event"."id"], "event-id" => ["item", "event-id"],
"event-created" => ["event"."created"], "event-created" => ["event", "created"],
"event-edited" => ["event"."edited"], "event-edited" => ["event", "edited"],
"event-start" => ["event"."start"], "event-start" => ["event", "start"],
"event-finish" => ["event"."finish"], "event-finish" => ["event", "finish"],
"event-summary" => ["event"."summary"], "event-summary" => ["event", "summary"],
"event-desc" => ["event"."id"], "event-desc" => ["event", "desc"],
"event-location" => ["event"."location"], "event-location" => ["event", "location"],
"event-type" => ["event"."type"], "event-type" => ["event", "type"],
"event-nofinish" => ["event"."nofinish"], "event-nofinish" => ["event", "nofinish"],
"event-adjust" => ["event"."adjust"], "event-adjust" => ["event", "adjust"],
"event-ignore" => ["event"."ignore"], "event-ignore" => ["event", "ignore"],
"delivery_queue_count" => ["post-delivery-data"."queue_count"], "delivery_queue_count" => ["post-delivery-data", "queue_count"],
"delivery_queue_done" => ["post-delivery-data"."queue_done"], "delivery_queue_done" => ["post-delivery-data", "queue_done"],
"delivery_queue_failed" => ["post-delivery-data"."queue_failed"], "delivery_queue_failed" => ["post-delivery-data", "queue_failed"],
"signed_text" => ["diaspora-interaction", "interaction"],
"parent-guid" => ["parent-item", "guid"], "parent-guid" => ["parent-item", "guid"],
"parent-network" => ["parent-item", "network"], "parent-network" => ["parent-item", "network"],
"parent-author-id" => ["parent-item", "author-id"], "parent-author-id" => ["parent-item", "author-id"],
"parent-author-link" => ["parent-item-author", "url"], "parent-author-link" => ["parent-item-author", "url"],
"parent-author-name" => ["parent-item-author", "name"], "parent-author-name" => ["parent-item-author", "name"],
"parent-author-network" => ["parent-item-author", "network"], "parent-author-network" => ["parent-item-author", "network"],
"signed_text" => ["diaspora-interaction", "interaction"],
], ],
"query" => "FROM `item` "query" => "FROM `item`
LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
LEFT JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id` STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
LEFT JOIN `contact` AS `owner` ON `owner`.`id` = `item`.`owner-id` STRAIGHT_JOIN `contact` AS `owner` ON `owner`.`id` = `item`.`owner-id`
LEFT JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id` STRAIGHT_JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id`
STRAIGHT_JOIN `group_member` ON `group_member`.`contact-id` = `item`.`contact-id` LEFT JOIN `verb` ON `verb`.`id` = `item`.`vid`
STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid` STRAIGHT_JOIN `item` AS `parent-item` ON `parent-item`.`id` = `item`.`parent`
LEFT JOIN `event` ON `event-id` = `event`.`id` 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 `diaspora-interaction` ON `diaspora-interaction`.`uri-id` = `item`.`uri-id`
LEFT JOIN `item-content` ON `item-content`.`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 `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`"
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`"
], ],
"category-view" => [ "category-view" => [
"fields" => [ "fields" => [