Merge pull request #4193 from annando/item-unification

We now are using a single function to store items
This commit is contained in:
Hypolite Petovan 2018-01-10 09:11:36 -05:00 committed by GitHub
commit 3f3e1c78dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 129 additions and 203 deletions

View file

@ -13,9 +13,6 @@
* *
* All of these become an "item" which is our basic unit of * All of these become an "item" which is our basic unit of
* information. * information.
*
* Posts that originate externally or do not fall into the above
* posting categories go through item_store() instead of this function.
*/ */
use Friendica\App; use Friendica\App;
use Friendica\Core\Config; use Friendica\Core\Config;
@ -24,6 +21,7 @@ use Friendica\Core\Worker;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\GContact; use Friendica\Model\GContact;
use Friendica\Model\Item;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Protocol\Diaspora; use Friendica\Protocol\Diaspora;
use Friendica\Protocol\Email; use Friendica\Protocol\Email;
@ -37,7 +35,6 @@ require_once 'include/text.php';
require_once 'include/items.php'; require_once 'include/items.php';
function item_post(App $a) { function item_post(App $a) {
if (!local_user() && !remote_user() && !x($_REQUEST, 'commenter')) { if (!local_user() && !remote_user() && !x($_REQUEST, 'commenter')) {
return; return;
} }
@ -58,12 +55,12 @@ function item_post(App $a) {
// logger('postinput ' . file_get_contents('php://input')); // logger('postinput ' . file_get_contents('php://input'));
logger('postvars ' . print_r($_REQUEST,true), LOGGER_DATA); logger('postvars ' . print_r($_REQUEST,true), LOGGER_DATA);
$api_source = ((x($_REQUEST, 'api_source') && $_REQUEST['api_source']) ? true : false); $api_source = x($_REQUEST, 'api_source') && $_REQUEST['api_source'];
$message_id = ((x($_REQUEST, 'message_id') && $api_source) ? strip_tags($_REQUEST['message_id']) : ''); $message_id = ((x($_REQUEST, 'message_id') && $api_source) ? strip_tags($_REQUEST['message_id']) : '');
$return_path = ((x($_REQUEST, 'return')) ? $_REQUEST['return'] : ''); $return_path = (x($_REQUEST, 'return') ? $_REQUEST['return'] : '');
$preview = ((x($_REQUEST, 'preview')) ? intval($_REQUEST['preview']) : 0); $preview = (x($_REQUEST, 'preview') ? intval($_REQUEST['preview']) : 0);
/* /*
* Check for doubly-submitted posts, and reject duplicates * Check for doubly-submitted posts, and reject duplicates
@ -132,7 +129,6 @@ function item_post(App $a) {
$parent = $r[0]['id']; $parent = $r[0]['id'];
// multi-level threading - preserve the info but re-parent to our single level threading // multi-level threading - preserve the info but re-parent to our single level threading
//if(($parid) && ($parid != $parent))
$thr_parent = $parent_uri; $thr_parent = $parent_uri;
if ($parent_item['contact-id'] && $uid) { if ($parent_item['contact-id'] && $uid) {
@ -175,11 +171,11 @@ function item_post(App $a) {
logger('mod_item: item_post parent=' . $parent); logger('mod_item: item_post parent=' . $parent);
} }
$profile_uid = ((x($_REQUEST, 'profile_uid')) ? intval($_REQUEST['profile_uid']) : 0); $profile_uid = (x($_REQUEST, 'profile_uid') ? intval($_REQUEST['profile_uid']) : 0);
$post_id = ((x($_REQUEST, 'post_id')) ? intval($_REQUEST['post_id']) : 0); $post_id = (x($_REQUEST, 'post_id') ? intval($_REQUEST['post_id']) : 0);
$app = ((x($_REQUEST, 'source')) ? strip_tags($_REQUEST['source']) : ''); $app = (x($_REQUEST, 'source') ? strip_tags($_REQUEST['source']) : '');
$extid = ((x($_REQUEST, 'extid')) ? strip_tags($_REQUEST['extid']) : ''); $extid = (x($_REQUEST, 'extid') ? strip_tags($_REQUEST['extid']) : '');
$object = ((x($_REQUEST, 'object')) ? $_REQUEST['object'] : ''); $object = (x($_REQUEST, 'object') ? $_REQUEST['object'] : '');
// Check for multiple posts with the same message id (when the post was created via API) // Check for multiple posts with the same message id (when the post was created via API)
if (($message_id != '') && ($profile_uid != 0)) { if (($message_id != '') && ($profile_uid != 0)) {
@ -276,11 +272,11 @@ function item_post(App $a) {
* been supplied via a form. * been supplied via a form.
*/ */
/// @TODO use x($_REQUEST, 'foo') here /// @TODO use x($_REQUEST, 'foo') here
if (($api_source) if ($api_source
&& (! array_key_exists('contact_allow', $_REQUEST)) && !array_key_exists('contact_allow', $_REQUEST)
&& (! array_key_exists('group_allow', $_REQUEST)) && !array_key_exists('group_allow', $_REQUEST)
&& (! array_key_exists('contact_deny', $_REQUEST)) && !array_key_exists('contact_deny', $_REQUEST)
&& (! array_key_exists('group_deny', $_REQUEST))) { && !array_key_exists('group_deny', $_REQUEST)) {
$str_group_allow = $user['allow_gid']; $str_group_allow = $user['allow_gid'];
$str_contact_allow = $user['allow_cid']; $str_contact_allow = $user['allow_cid'];
$str_group_deny = $user['deny_gid']; $str_group_deny = $user['deny_gid'];
@ -331,12 +327,12 @@ function item_post(App $a) {
$private = $parent_item['private']; $private = $parent_item['private'];
} }
$pubmail_enable = ((x($_REQUEST, 'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && (! $private)) ? 1 : 0); $pubmail_enable = ((x($_REQUEST, 'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && !$private) ? 1 : 0);
// if using the API, we won't see pubmail_enable - figure out if it should be set // if using the API, we won't see pubmail_enable - figure out if it should be set
if ($api_source && $profile_uid && $profile_uid == local_user() && (! $private)) { if ($api_source && $profile_uid && $profile_uid == local_user() && !$private) {
$mail_disabled = ((function_exists('imap_open') && (! Config::get('system', 'imap_disabled'))) ? 0 : 1); $mail_disabled = ((function_exists('imap_open') && !Config::get('system', 'imap_disabled')) ? 0 : 1);
if (!$mail_disabled) { if (!$mail_disabled) {
/// @TODO Check if only pubmail is loaded, * loads all columns /// @TODO Check if only pubmail is loaded, * loads all columns
$r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
@ -496,7 +492,7 @@ function item_post(App $a) {
continue; continue;
} }
$success = handle_tag($a, $body, $inform, $str_tags, (local_user()) ? local_user() : $profile_uid , $tag, $network); $success = handle_tag($a, $body, $inform, $str_tags, local_user() ? local_user() : $profile_uid, $tag, $network);
if ($success['replaced']) { if ($success['replaced']) {
$tagged[] = $tag; $tagged[] = $tag;
} }
@ -595,7 +591,7 @@ function item_post(App $a) {
*/ */
$match = false; $match = false;
if ((! $preview) && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/", $body, $match)) { if (!$preview && preg_match_all("/\[attachment\](.*?)\[\/attachment\]/", $body, $match)) {
$attaches = $match[1]; $attaches = $match[1];
if (count($attaches)) { if (count($attaches)) {
foreach ($attaches as $attach) { foreach ($attaches as $attach) {
@ -684,16 +680,16 @@ function item_post(App $a) {
$network = NETWORK_DFRN; $network = NETWORK_DFRN;
} }
$gravity = (($parent) ? 6 : 0 ); $gravity = ($parent ? 6 : 0);
// even if the post arrived via API we are considering that it // even if the post arrived via API we are considering that it
// originated on this site by default for determining relayability. // originated on this site by default for determining relayability.
$origin = ((x($_REQUEST, 'origin')) ? intval($_REQUEST['origin']) : 1); $origin = (x($_REQUEST, 'origin') ? intval($_REQUEST['origin']) : 1);
$notify_type = (($parent) ? 'comment-new' : 'wall-new' ); $notify_type = ($parent ? 'comment-new' : 'wall-new');
$uri = (($message_id) ? $message_id : item_new_uri($a->get_hostname(),$profile_uid, $guid)); $uri = ($message_id ? $message_id : item_new_uri($a->get_hostname(),$profile_uid, $guid));
// Fallback so that we alway have a thr-parent // Fallback so that we alway have a thr-parent
if (!$thr_parent) { if (!$thr_parent) {
@ -756,7 +752,6 @@ function item_post(App $a) {
*/ */
$datarray['parent'] = $parent; $datarray['parent'] = $parent;
$datarray['self'] = $self; $datarray['self'] = $self;
// $datarray['prvnets'] = $user['prvnets'];
// This triggers posts via API and the mirror functions // This triggers posts via API and the mirror functions
$datarray['api_source'] = $api_source; $datarray['api_source'] = $api_source;
@ -814,35 +809,29 @@ function item_post(App $a) {
killme(); killme();
} }
if ($orig_post) {
// Fill the cache field // Fill the cache field
// This could be done in Item::update as well - but we have to check for the existance of some fields.
put_item_in_cache($datarray); put_item_in_cache($datarray);
$datarray = store_conversation($datarray); $fields = array(
'title' => $datarray['title'],
'body' => $datarray['body'],
'tag' => $datarray['tag'],
'attach' => $datarray['attach'],
'file' => $datarray['file'],
'rendered-html' => $datarray['rendered-html'],
'rendered-hash' => $datarray['rendered-hash'],
'edited' => datetime_convert(),
'changed' => datetime_convert());
if ($orig_post) { Item::update($fields, ['id' => $post_id]);
$r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `attach` = '%s', `file` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d",
dbesc($datarray['title']),
dbesc($datarray['body']),
dbesc($datarray['tag']),
dbesc($datarray['attach']),
dbesc($datarray['file']),
dbesc($datarray['rendered-html']),
dbesc($datarray['rendered-hash']),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
intval($post_id),
intval($profile_uid)
);
create_tags_from_item($post_id);
create_files_from_item($post_id);
update_thread($post_id);
// update filetags in pconfig // update filetags in pconfig
file_tag_update_pconfig($uid,$categories_old,$categories_new,'category'); file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $post_id); if (x($_REQUEST, 'return') && strlen($return_path)) {
if ((x($_REQUEST, 'return')) && strlen($return_path)) {
logger('return: ' . $return_path); logger('return: ' . $return_path);
goaway($return_path); goaway($return_path);
} }
@ -851,123 +840,19 @@ function item_post(App $a) {
$post_id = 0; $post_id = 0;
} }
dba::transaction(); unset($datarray['edit']);
unset($datarray['self']);
unset($datarray['api_source']);
$r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`, $post_id = item_store($datarray);
`owner-name`,`owner-link`,`owner-avatar`, `owner-id`,
`author-name`, `author-link`, `author-avatar`, `author-id`,
`created`, `edited`, `commented`, `received`, `changed`,
`uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`,
`tag`, `inform`, `verb`, `object-type`, `postopts`,
`allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`,
`pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file`,
`rendered-html`, `rendered-hash`, `gcontact-id`, `object`,
`parent`, `parent-uri`, `plink`, `last-child`, `visible`)
VALUES('%s', '%s', %d, '%s', %d, %d, '%s', %d,
'%s', '%s', '%s', %d,
'%s', '%s', '%s', %d,
'%s', '%s', '%s', '%s', '%s',
'%s', '%s', '%s', '%s', '%s', '%s', '%s',
'%s', '%s', '%s', '%s', '%s',
'%s', '%s', '%s', '%s', %d,
%d, '%s', %d, %d, %d, '%s',
'%s', '%s', %d, '%s',
%d, '%s', '%s', %d, %d)",
dbesc($datarray['guid']),
dbesc($datarray['extid']),
intval($datarray['uid']),
dbesc($datarray['type']),
intval($datarray['wall']),
intval($datarray['gravity']),
dbesc($datarray['network']),
intval($datarray['contact-id']),
dbesc($datarray['owner-name']),
dbesc($datarray['owner-link']),
dbesc($datarray['owner-avatar']),
intval($datarray['owner-id']),
dbesc($datarray['author-name']),
dbesc($datarray['author-link']),
dbesc($datarray['author-avatar']),
intval($datarray['author-id']),
dbesc($datarray['created']),
dbesc($datarray['edited']),
dbesc($datarray['commented']),
dbesc($datarray['received']),
dbesc($datarray['changed']),
dbesc($datarray['uri']),
dbesc($datarray['thr-parent']),
dbesc($datarray['title']),
dbesc($datarray['body']),
dbesc($datarray['app']),
dbesc($datarray['location']),
dbesc($datarray['coord']),
dbesc($datarray['tag']),
dbesc($datarray['inform']),
dbesc($datarray['verb']),
dbesc($datarray['object-type']),
dbesc($datarray['postopts']),
dbesc($datarray['allow_cid']),
dbesc($datarray['allow_gid']),
dbesc($datarray['deny_cid']),
dbesc($datarray['deny_gid']),
intval($datarray['private']),
intval($datarray['pubmail']),
dbesc($datarray['attach']),
intval($datarray['bookmark']),
intval($datarray['origin']),
intval($datarray['moderated']),
dbesc($datarray['file']),
dbesc($datarray['rendered-html']),
dbesc($datarray['rendered-hash']),
intval($datarray['gcontact-id']),
dbesc($datarray['object']),
intval($datarray['parent']),
dbesc($datarray['parent-uri']),
dbesc($datarray['plink']),
intval($datarray['last-child']),
intval($datarray['visible'])
);
if (DBM::is_result($r)) {
$post_id = dba::lastInsertId();
} else {
logger('mod_item: unable to create post.');
$post_id = 0;
}
if ($post_id == 0) {
dba::commit();
logger('mod_item: unable to retrieve post that was just stored.');
notice(t('System error. Post not saved.') . EOL);
goaway($return_path);
// NOTREACHED
}
logger('mod_item: saved item ' . $post_id);
$datarray["id"] = $post_id; $datarray["id"] = $post_id;
item_set_last_item($datarray);
// update filetags in pconfig // update filetags in pconfig
file_tag_update_pconfig($uid, $categories_old, $categories_new, 'category'); file_tag_update_pconfig($uid, $categories_old, $categories_new, 'category');
// These notifications are sent if someone else is commenting other your wall
if ($parent) { if ($parent) {
// This item is the last leaf and gets the comment box, clear any ancestors
$r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d AND `last-child` AND `id` != %d",
dbesc(datetime_convert()),
intval($parent),
intval($post_id)
);
// update the commented timestamp on the parent
q("UPDATE `item` SET `visible` = 1, `commented` = '%s', `changed` = '%s' WHERE `id` = %d",
dbesc(datetime_convert()),
dbesc(datetime_convert()),
intval($parent)
);
if ($contact_record != $author) { if ($contact_record != $author) {
notification(array( notification(array(
'type' => NOTIFY_COMMENT, 'type' => NOTIFY_COMMENT,
@ -986,20 +871,11 @@ function item_post(App $a) {
'parent' => $parent, 'parent' => $parent,
'parent_uri' => $parent_item['uri'] 'parent_uri' => $parent_item['uri']
)); ));
} }
// Store the comment signature information in case we need to relay to Diaspora // Store the comment signature information in case we need to relay to Diaspora
Diaspora::storeCommentSignature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id); Diaspora::storeCommentSignature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id);
} else { } else {
$parent = $post_id;
$r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d",
intval($parent),
intval($post_id));
if (($contact_record != $author) && !count($forum_contact)) { if (($contact_record != $author) && !count($forum_contact)) {
notification(array( notification(array(
'type' => NOTIFY_WALL, 'type' => NOTIFY_WALL,
@ -1056,19 +932,6 @@ function item_post(App $a) {
} }
} }
if ($post_id == $parent) {
add_thread($post_id);
} else {
update_thread($parent, true);
}
dba::commit();
create_tags_from_item($post_id);
create_files_from_item($post_id);
check_user_notification($post_id);
// Insert an item entry for UID=0 for global entries. // Insert an item entry for UID=0 for global entries.
// We now do it in the background to save some time. // We now do it in the background to save some time.
// This is important in interactive environments like the frontend or the API. // This is important in interactive environments like the frontend or the API.
@ -1110,7 +973,7 @@ function item_post_return($baseurl, $api_source, $return_path) {
function item_content(App $a) { function item_content(App $a) {
if ((! local_user()) && (! remote_user())) { if (!local_user() && !remote_user()) {
return; return;
} }

63
src/Model/Item.php Normal file
View file

@ -0,0 +1,63 @@
<?php
/**
* @file src/Model/Item.php
*/
namespace Friendica\Model;
use Friendica\Core\Worker;
use dba;
require_once 'include/tags.php';
require_once 'include/files.php';
require_once 'include/threads.php';
class Item
{
/**
* @brief Update existing item entries
*
* @param array $fields The fields that are to be changed
* @param array $condition The condition for finding the item entries
*
* In the future we may have to change permissions as well.
* Then we had to add the user id as third parameter.
*
* A return value of "0" doesn't mean an error - but that 0 rows had been changed.
*
* @return integer|boolean number of affected rows - or "false" if there was an error
*/
public static function update(array $fields, array $condition)
{
if (empty($condition) || empty($fields)) {
return false;
}
$success = dba::update('item', $fields, $condition);
if (!$success) {
return false;
}
$rows = dba::affected_rows();
// We cannot simply expand the condition to check for origin entries
// The condition needn't to be a simple array but could be a complex condition.
$items = dba::select('item', ['id', 'origin'], $condition);
while ($item = dba::fetch($items)) {
// We only need to notfiy others when it is an original entry from us
if (!$item['origin']) {
continue;
}
create_tags_from_item($item['id']);
create_files_from_item($item['id']);
update_thread($item['id']);
Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
}
return $rows;
}
}