Merge pull request #5238 from annando/more-abstraction

Even more replaced item calls ...
This commit is contained in:
Hypolite Petovan 2018-06-19 14:02:43 -04:00 committed by GitHub
commit 82d55f120f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 316 additions and 447 deletions

View file

@ -688,59 +688,8 @@ function api_get_user(App $a, $contact_id = null)
$usr = dba::selectFirst('user', ['default-location'], ['uid' => api_user()]); $usr = dba::selectFirst('user', ['default-location'], ['uid' => api_user()]);
$profile = dba::selectFirst('profile', ['about'], ['uid' => api_user(), 'is-default' => true]); $profile = dba::selectFirst('profile', ['about'], ['uid' => api_user(), 'is-default' => true]);
/// @TODO old-lost code? (twice)
// Counting is deactivated by now, due to performance issues
// count public wall messages
//$r = q("SELECT COUNT(*) as `count` FROM `item` WHERE `uid` = %d AND `wall`",
// intval($uinfo[0]['uid'])
//);
//$countitms = $r[0]['count'];
$countitms = 0;
} else {
// Counting is deactivated by now, due to performance issues
//$r = q("SELECT count(*) as `count` FROM `item`
// WHERE `contact-id` = %d",
// intval($uinfo[0]['id'])
//);
//$countitms = $r[0]['count'];
$countitms = 0;
} }
$countitems = 0;
/// @TODO old-lost code? (twice)
/*
// Counting is deactivated by now, due to performance issues
// count friends
$r = q("SELECT count(*) as `count` FROM `contact`
WHERE `uid` = %d AND `rel` IN ( %d, %d )
AND `self`=0 AND NOT `blocked` AND NOT `pending` AND `hidden`=0",
intval($uinfo[0]['uid']),
intval(CONTACT_IS_SHARING),
intval(CONTACT_IS_FRIEND)
);
$countfriends = $r[0]['count'];
$r = q("SELECT count(*) as `count` FROM `contact`
WHERE `uid` = %d AND `rel` IN ( %d, %d )
AND `self`=0 AND NOT `blocked` AND NOT `pending` AND `hidden`=0",
intval($uinfo[0]['uid']),
intval(CONTACT_IS_FOLLOWER),
intval(CONTACT_IS_FRIEND)
);
$countfollowers = $r[0]['count'];
$r = q("SELECT count(*) as `count` FROM item where starred = 1 and uid = %d and deleted = 0",
intval($uinfo[0]['uid'])
);
$starred = $r[0]['count'];
if (! $uinfo[0]['self']) {
$countfriends = 0;
$countfollowers = 0;
$starred = 0;
}
*/
$countfriends = 0; $countfriends = 0;
$countfollowers = 0; $countfollowers = 0;
$starred = 0; $starred = 0;
@ -790,7 +739,7 @@ function api_get_user(App $a, $contact_id = null)
'time_zone' => 'UTC', 'time_zone' => 'UTC',
'geo_enabled' => false, 'geo_enabled' => false,
'verified' => true, 'verified' => true,
'statuses_count' => intval($countitms), 'statuses_count' => intval($countitems),
'lang' => '', 'lang' => '',
'contributors_enabled' => false, 'contributors_enabled' => false,
'is_translator' => false, 'is_translator' => false,
@ -1173,18 +1122,8 @@ function api_statuses_update($type)
if ($throttle_day > 0) { if ($throttle_day > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60); $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60);
$r = q( $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom];
"SELECT COUNT(*) AS `posts_day` FROM `item` WHERE `uid`=%d AND `wall` $posts_day = dba::count('item', $condition);
AND `created` > '%s' AND `id` = `parent`",
intval(api_user()),
dbesc($datefrom)
);
if (DBM::is_result($r)) {
$posts_day = $r[0]["posts_day"];
} else {
$posts_day = 0;
}
if ($posts_day > $throttle_day) { if ($posts_day > $throttle_day) {
logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG);
@ -1197,18 +1136,8 @@ function api_statuses_update($type)
if ($throttle_week > 0) { if ($throttle_week > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7); $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7);
$r = q( $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom];
"SELECT COUNT(*) AS `posts_week` FROM `item` WHERE `uid`=%d AND `wall` $posts_week = dba::count('item', $condition);
AND `created` > '%s' AND `id` = `parent`",
intval(api_user()),
dbesc($datefrom)
);
if (DBM::is_result($r)) {
$posts_week = $r[0]["posts_week"];
} else {
$posts_week = 0;
}
if ($posts_week > $throttle_week) { if ($posts_week > $throttle_week) {
logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG);
@ -1221,18 +1150,8 @@ function api_statuses_update($type)
if ($throttle_month > 0) { if ($throttle_month > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30); $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30);
$r = q( $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom];
"SELECT COUNT(*) AS `posts_month` FROM `item` WHERE `uid`=%d AND `wall` $posts_month = dba::count('item', $condition);
AND `created` > '%s' AND `id` = `parent`",
intval(api_user()),
dbesc($datefrom)
);
if (DBM::is_result($r)) {
$posts_month = $r[0]["posts_month"];
} else {
$posts_month = 0;
}
if ($posts_month > $throttle_month) { if ($posts_month > $throttle_month) {
logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG);
@ -2806,20 +2725,16 @@ function api_format_items_activities(&$item, $type = "json")
'attendmaybe' => [], 'attendmaybe' => [],
]; ];
$items = q( $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri']];
'SELECT * FROM `item` $ret = Item::selectForUser($item['uid'], ['author-id', 'verb'], $condition);
WHERE `uid` = %d AND `thr-parent` = "%s" AND `visible` AND NOT `deleted`',
intval($item['uid']),
dbesc($item['uri'])
);
foreach ($items as $i) { while ($item = dba::fetch($ret)) {
// not used as result should be structured like other user data // not used as result should be structured like other user data
//builtin_activity_puller($i, $activities); //builtin_activity_puller($i, $activities);
// get user data and add it to the array of the activity // get user data and add it to the array of the activity
$user = api_get_user($a, $i['author-id']); $user = api_get_user($a, $item['author-id']);
switch ($i['verb']) { switch ($item['verb']) {
case ACTIVITY_LIKE: case ACTIVITY_LIKE:
$activities['like'][] = $user; $activities['like'][] = $user;
break; break;
@ -2840,6 +2755,8 @@ function api_format_items_activities(&$item, $type = "json")
} }
} }
dba::close($ret);
if ($type == "xml") { if ($type == "xml") {
$xml_activities = []; $xml_activities = [];
foreach ($activities as $k => $v) { foreach ($activities as $k => $v) {
@ -3924,16 +3841,13 @@ function api_fr_photoalbum_delete($type)
// function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
// to the user and the contacts of the users (drop_items() performs the federation of the deletion to other networks // to the user and the contacts of the users (drop_items() performs the federation of the deletion to other networks
foreach ($r as $rr) { foreach ($r as $rr) {
$photo_item = q( $condition = ['uid' => local_user(), 'resource-id' => $rr['resource-id'], 'type' => 'photo'];
"SELECT `id` FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'", $photo_item = Item::selectFirstForUser(local_user(), ['id'], $condition);
intval(local_user()),
dbesc($rr['resource-id'])
);
if (!DBM::is_result($photo_item)) { if (!DBM::is_result($photo_item)) {
throw new InternalServerErrorException("problem with deleting items occured"); throw new InternalServerErrorException("problem with deleting items occured");
} }
Item::deleteForUser(['id' => $photo_item[0]['id']], api_user()); Item::deleteForUser(['id' => $photo_item['id']], api_user());
} }
// now let's delete all photos from the album // now let's delete all photos from the album
@ -4214,18 +4128,15 @@ function api_fr_photo_delete($type)
// return success of deletion or error message // return success of deletion or error message
if ($result) { if ($result) {
// retrieve the id of the parent element (the photo element) // retrieve the id of the parent element (the photo element)
$photo_item = q( $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
"SELECT `id` FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'", $photo_item = Item::selectFirstForUser(local_user(), ['id'], $condition);
intval(local_user()),
dbesc($photo_id)
);
if (!DBM::is_result($photo_item)) { if (!DBM::is_result($photo_item)) {
throw new InternalServerErrorException("problem with deleting items occured"); throw new InternalServerErrorException("problem with deleting items occured");
} }
// function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
// to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion) // to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion)
Item::deleteForUser(['id' => $photo_item[0]['id']], api_user()); Item::deleteForUser(['id' => $photo_item['id']], api_user());
$answer = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.']; $answer = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.'];
return api_format_data("photo_delete", $type, ['$result' => $answer]); return api_format_data("photo_delete", $type, ['$result' => $answer]);
@ -4713,12 +4624,10 @@ function prepare_photo_data($type, $scale, $photo_id)
} }
// retrieve item element for getting activities (like, dislike etc.) related to photo // retrieve item element for getting activities (like, dislike etc.) related to photo
$item = q( $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
"SELECT * FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'", $item = Item::selectFirstForUser(local_user(), ['id'], $condition);
intval(local_user()),
dbesc($photo_id) $data['photo']['friendica_activities'] = api_format_items_activities($item, $type);
);
$data['photo']['friendica_activities'] = api_format_items_activities($item[0], $type);
// retrieve comments on photo // retrieve comments on photo
$condition = ["`parent` = ? AND `uid` = ? AND (`verb` = ? OR `type`='photo')", $condition = ["`parent` = ? AND `uid` = ? AND (`verb` = ? OR `type`='photo')",
@ -5013,35 +4922,26 @@ function api_in_reply_to($item)
$in_reply_to['screen_name'] = null; $in_reply_to['screen_name'] = null;
if (($item['thr-parent'] != $item['uri']) && (intval($item['parent']) != intval($item['id']))) { if (($item['thr-parent'] != $item['uri']) && (intval($item['parent']) != intval($item['id']))) {
$r = q( $parent = Item::selectFirst(['id'], ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
"SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", if (DBM::is_result($parent)) {
intval($item['uid']), $in_reply_to['status_id'] = intval($parent['id']);
dbesc($item['thr-parent'])
);
if (DBM::is_result($r)) {
$in_reply_to['status_id'] = intval($r[0]['id']);
} else { } else {
$in_reply_to['status_id'] = intval($item['parent']); $in_reply_to['status_id'] = intval($item['parent']);
} }
$in_reply_to['status_id_str'] = (string) intval($in_reply_to['status_id']); $in_reply_to['status_id_str'] = (string) intval($in_reply_to['status_id']);
$r = q( $fields = ['author-nick', 'author-name', 'author-id', 'author-link'];
"SELECT `contact`.`nick`, `contact`.`name`, `contact`.`id`, `contact`.`url` FROM `item` $parent = Item::selectFirst($fields, ['id' => $in_reply_to['status_id']]);
STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`author-id`
WHERE `item`.`id` = %d LIMIT 1",
intval($in_reply_to['status_id'])
);
if (DBM::is_result($r)) { if (DBM::is_result($parent)) {
if ($r[0]['nick'] == "") { if ($parent['author-nick'] == "") {
$r[0]['nick'] = api_get_nick($r[0]["url"]); $parent['author-nick'] = api_get_nick($parent['author-link']);
} }
$in_reply_to['screen_name'] = (($r[0]['nick']) ? $r[0]['nick'] : $r[0]['name']); $in_reply_to['screen_name'] = (($parent['author-nick']) ? $parent['author-nick'] : $parent['author-name']);
$in_reply_to['user_id'] = intval($r[0]['id']); $in_reply_to['user_id'] = intval($parent['author-id']);
$in_reply_to['user_id_str'] = (string) intval($r[0]['id']); $in_reply_to['user_id_str'] = (string) intval($parent['author-id']);
} }
// There seems to be situation, where both fields are identical: // There seems to be situation, where both fields are identical:

View file

@ -633,16 +633,12 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order =
$location_e = $location; $location_e = $location;
$owner_name_e = $owner_name; $owner_name_e = $owner_name;
if ($item['item_network'] == "") {
$item['item_network'] = $item['network'];
}
$tmp_item = [ $tmp_item = [
'template' => $tpl, 'template' => $tpl,
'id' => (($preview) ? 'P0' : $item['item_id']), 'id' => (($preview) ? 'P0' : $item['item_id']),
'guid' => (($preview) ? 'Q0' : $item['guid']), 'guid' => (($preview) ? 'Q0' : $item['guid']),
'network' => $item['item_network'], 'network' => $item['network'],
'network_name' => ContactSelector::networkToName($item['item_network'], $profile_link), 'network_name' => ContactSelector::networkToName($item['network'], $profile_link),
'linktitle' => L10n::t('View %s\'s profile @ %s', $profile_name, $item['author-link']), 'linktitle' => L10n::t('View %s\'s profile @ %s', $profile_name, $item['author-link']),
'profile_url' => $profile_link, 'profile_url' => $profile_link,
'item_photo_menu' => item_photo_menu($item), 'item_photo_menu' => item_photo_menu($item),
@ -688,7 +684,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order =
Addon::callHooks('display_item', $arr); Addon::callHooks('display_item', $arr);
$threads[$threadsid]['id'] = $item['item_id']; $threads[$threadsid]['id'] = $item['item_id'];
$threads[$threadsid]['network'] = $item['item_network']; $threads[$threadsid]['network'] = $item['network'];
$threads[$threadsid]['items'] = [$arr['output']]; $threads[$threadsid]['items'] = [$arr['output']];
} }

View file

@ -335,17 +335,14 @@ function drop_item($id) {
// locate item to be deleted // locate item to be deleted
$r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", $fields = ['id', 'uid', 'contact-id', 'deleted'];
intval($id) $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $id]);
);
if (!DBM::is_result($r)) { if (!DBM::is_result($item)) {
notice(L10n::t('Item not found.') . EOL); notice(L10n::t('Item not found.') . EOL);
goaway(System::baseUrl() . '/' . $_SESSION['return_url']); goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
} }
$item = $r[0];
if ($item['deleted']) { if ($item['deleted']) {
return 0; return 0;
} }
@ -364,7 +361,6 @@ function drop_item($id) {
} }
if ((local_user() == $item['uid']) || $contact_id) { if ((local_user() == $item['uid']) || $contact_id) {
// Check if we should do HTML-based delete confirmation // Check if we should do HTML-based delete confirmation
if ($_REQUEST['confirm']) { if ($_REQUEST['confirm']) {
// <form> can't take arguments in its "action" parameter // <form> can't take arguments in its "action" parameter

View file

@ -1177,8 +1177,8 @@ function put_item_in_cache(&$item, $update = false)
} }
if ($update && ($item["id"] > 0)) { if ($update && ($item["id"] > 0)) {
dba::update('item', ['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]], Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]],
['id' => $item["id"]], false); ['id' => $item["id"]]);
} }
} }

View file

@ -41,80 +41,70 @@ function display_init(App $a)
} }
} }
$r = false; $item = null;
$fields = ['id', 'parent', 'author-id', 'body', 'uid'];
// If there is only one parameter, then check if this parameter could be a guid // If there is only one parameter, then check if this parameter could be a guid
if ($a->argc == 2) { if ($a->argc == 2) {
$nick = ""; $nick = "";
$r = false;
// Does the local user have this item? // Does the local user have this item?
if (local_user()) { if (local_user()) {
$r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid` $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated` if (DBM::is_result($item)) {
AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
if (DBM::is_result($r)) {
$nick = $a->user["nickname"]; $nick = $a->user["nickname"];
} }
} }
// Is it an item with uid=0? // Is it an item with uid=0?
if (!DBM::is_result($r)) { if (!DBM::is_result($item)) {
$r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid` $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => false, 'uid' => 0]);
FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated` }
AND NOT `private` AND `uid` = 0 } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
AND `guid` = ? LIMIT 1", $a->argv[1]); $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => false, 'uid' => 0]);
} }
if (!DBM::is_result($r)) { if (!DBM::is_result($item) || $item['deleted']) {
$a->error = 404; $a->error = 404;
notice(L10n::t('Item not found.') . EOL); notice(L10n::t('Item not found.') . EOL);
return; return;
} }
} elseif (($a->argc == 3) && ($nick == 'feed-item')) {
$r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid`
FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated`
AND NOT `private` AND `uid` = 0
AND `id` = ? LIMIT 1", $a->argv[2]);
}
if (DBM::is_result($r)) {
if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) { if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG); logger('Directly serving XML for id '.$item["id"], LOGGER_DEBUG);
displayShowFeed($r["id"], false); displayShowFeed($item["id"], false);
} }
if ($r["id"] != $r["parent"]) { if ($item["id"] != $item["parent"]) {
$r = dba::fetch_first("SELECT `id`, `author-id`, `body`, `uid` FROM `item` $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND `id` = ?", $r["parent"]);
} }
$profiledata = display_fetchauthor($a, $r); $profiledata = display_fetchauthor($a, $item);
if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) { if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) {
$nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"])); $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"]));
if (($nickname != $a->user["nickname"])) { if (($nickname != $a->user["nickname"])) {
$r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile` $profile = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1", WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
$nickname $nickname
); );
if (DBM::is_result($r)) { if (DBM::is_result($profile)) {
$profiledata = $r; $profiledata = $profile;
} }
$profiledata["network"] = NETWORK_DFRN; $profiledata["network"] = NETWORK_DFRN;
} else { } else {
$profiledata = []; $profiledata = [];
} }
} }
}
Profile::load($a, $nick, 0, $profiledata); Profile::load($a, $nick, 0, $profiledata);
} }
function display_fetchauthor($a, $item) { function display_fetchauthor($a, $item)
{
$author = dba::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]); $author = dba::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
$profiledata = []; $profiledata = [];
@ -198,7 +188,8 @@ function display_fetchauthor($a, $item) {
return($profiledata); return($profiledata);
} }
function display_content(App $a, $update = false, $update_uid = 0) { function display_content(App $a, $update = false, $update_uid = 0)
{
if (Config::get('system','block_public') && !local_user() && !remote_user()) { if (Config::get('system','block_public') && !local_user() && !remote_user()) {
notice(L10n::t('Public access denied.') . EOL); notice(L10n::t('Public access denied.') . EOL);
return; return;
@ -224,27 +215,25 @@ function display_content(App $a, $update = false, $update_uid = 0) {
if ($a->argc == 2) { if ($a->argc == 2) {
$item_parent = 0; $item_parent = 0;
$fields = ['id', 'parent', 'parent-uri'];
if (local_user()) { if (local_user()) {
$r = dba::fetch_first("SELECT `id`, `parent`, `parent-uri` FROM `item` $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` $item = Item::selectFirstForUser(local_user(), $fields, $condition);
AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user()); if (DBM::is_result($item)) {
if (DBM::is_result($r)) { $item_id = $item["id"];
$item_id = $r["id"]; $item_parent = $item["parent"];
$item_parent = $r["parent"]; $item_parent_uri = $item['parent-uri'];
$item_parent_uri = $r['parent-uri'];
} }
} }
if ($item_parent == 0) { if ($item_parent == 0) {
$r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`parent-uri` FROM `item` $condition = ['private' => false, 'guid' => $a->argv[1], 'uid' => 0];
WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` $item = Item::selectFirstForUser(local_user(), $fields, $condition);
AND NOT `item`.`private` AND `item`.`uid` = 0 if (DBM::is_result($item)) {
AND `item`.`guid` = ?", $a->argv[1]); $item_id = $item["id"];
if (DBM::is_result($r)) { $item_parent = $item["parent"];
$item_id = $r["id"]; $item_parent_uri = $item['parent-uri'];
$item_parent = $r["parent"];
$item_parent_uri = $r['parent-uri'];
} }
} }
} }
@ -274,7 +263,7 @@ function display_content(App $a, $update = false, $update_uid = 0) {
$groups = []; $groups = [];
$contact = null; $contact = null;
$remote_contact = false; $is_remote_contact = false;
$contact_id = 0; $contact_id = 0;
@ -289,30 +278,27 @@ function display_content(App $a, $update = false, $update_uid = 0) {
if ($contact_id) { if ($contact_id) {
$groups = Group::getIdsByContactId($contact_id); $groups = Group::getIdsByContactId($contact_id);
$r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1", $remote_contact = dba::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]);
$contact_id, if (DBM::is_result($remote_contact)) {
$a->profile['uid'] $contact = $remote_contact;
); $is_remote_contact = true;
if (DBM::is_result($r)) {
$contact = $r;
$remote_contact = true;
} }
} }
if (!$remote_contact) { if (!$is_remote_contact) {
if (local_user()) { if (local_user()) {
$contact_id = $_SESSION['cid']; $contact_id = $_SESSION['cid'];
$contact = $a->contact; $contact = $a->contact;
} }
} }
$r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']); $page_contact = dba::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
if (DBM::is_result($r)) { if (DBM::is_result($page_contact)) {
$a->page_contact = $r; $a->page_contact = $page_contact;
} }
$is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false); $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) { if (x($a->profile, 'hidewall') && !$is_owner && !$is_remote_contact) {
notice(L10n::t('Access to this profile has been restricted.') . EOL); notice(L10n::t('Access to this profile has been restricted.') . EOL);
return; return;
} }
@ -333,7 +319,7 @@ function display_content(App $a, $update = false, $update_uid = 0) {
$o .= status_editor($a, $x, 0, true); $o .= status_editor($a, $x, 0, true);
} }
$sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups); $sql_extra = item_permissions_sql($a->profile['uid'], $is_remote_contact, $groups);
if (local_user() && (local_user() == $a->profile['uid'])) { if (local_user() && (local_user() == $a->profile['uid'])) {
$condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true]; $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
@ -349,21 +335,19 @@ function display_content(App $a, $update = false, $update_uid = 0) {
$condition = ["`item`.`parent-uri` = (SELECT `parent-uri` FROM `item` WHERE `id` = ?) $condition = ["`item`.`parent-uri` = (SELECT `parent-uri` FROM `item` WHERE `id` = ?)
AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()]; AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()];
$params = ['order' => ['uid', 'parent' => true, 'gravity', 'id']]; $params = ['order' => ['uid', 'parent' => true, 'gravity', 'id']];
$r = Item::selectForUser(local_user(), [], $condition, $params); $items_obj = Item::selectForUser(local_user(), [], $condition, $params);
if (!DBM::is_result($r)) { if (!DBM::is_result($items_obj)) {
notice(L10n::t('Item not found.') . EOL); notice(L10n::t('Item not found.') . EOL);
return $o; return $o;
} }
$s = dba::inArray($r);
if ($unseen) { if ($unseen) {
$condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true]; $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
dba::update('item', ['unseen' => false], $condition); Item::update(['unseen' => false], $condition);
} }
$items = conv_sort($s, "`commented`"); $items = conv_sort(dba::inArray($items_obj), "`commented`");
if (!$update) { if (!$update) {
$o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>"; $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
@ -425,7 +409,8 @@ function display_content(App $a, $update = false, $update_uid = 0) {
return $o; return $o;
} }
function displayShowFeed($item_id, $conversation) { function displayShowFeed($item_id, $conversation)
{
$xml = DFRN::itemFeed($item_id, $conversation); $xml = DFRN::itemFeed($item_id, $conversation);
if ($xml == '') { if ($xml == '') {
System::httpExit(500); System::httpExit(500);

View file

@ -8,6 +8,7 @@ use Friendica\Core\Addon;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Model\Item;
use Friendica\Database\DBM; use Friendica\Database\DBM;
function editpost_content(App $a) { function editpost_content(App $a) {
@ -26,12 +27,10 @@ function editpost_content(App $a) {
return; return;
} }
$itm = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
intval($post_id), 'type', 'body', 'title', 'file'];
intval(local_user()) $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $post_id, 'uid' => local_user()]);
); if (!DBM::is_result($item)) {
if (! DBM::is_result($itm)) {
notice(L10n::t('Item not found') . EOL); notice(L10n::t('Item not found') . EOL);
return; return;
} }
@ -61,7 +60,7 @@ function editpost_content(App $a) {
$tpl = get_markup_template("jot.tpl"); $tpl = get_markup_template("jot.tpl");
if (strlen($itm['allow_cid']) || strlen($itm['allow_gid']) || strlen($itm['deny_cid']) || strlen($itm['deny_gid'])) { if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
$lockstate = 'lock'; $lockstate = 'lock';
} else { } else {
$lockstate = 'unlock'; $lockstate = 'unlock';
@ -70,7 +69,7 @@ function editpost_content(App $a) {
$jotplugins = ''; $jotplugins = '';
$jotnets = ''; $jotnets = '';
$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);
$mail_enabled = false; $mail_enabled = false;
$pubmail_enabled = false; $pubmail_enabled = false;
@ -81,28 +80,15 @@ function editpost_content(App $a) {
); );
if (DBM::is_result($r)) { if (DBM::is_result($r)) {
$mail_enabled = true; $mail_enabled = true;
if(intval($r[0]['pubmail'])) if (intval($r[0]['pubmail'])) {
$pubmail_enabled = true; $pubmail_enabled = true;
} }
} }
}
// I don't think there's any need for the $jotnets when editing the post,
// and including them makes it difficult for the JS-free theme, so let's
// disable them
/* if($mail_enabled) {
$selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
$jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> '
. L10n::t("Post to Email") . '</div>';
}*/
Addon::callHooks('jot_tool', $jotplugins); Addon::callHooks('jot_tool', $jotplugins);
//Addon::callHooks('jot_networks', $jotnets); //Addon::callHooks('jot_networks', $jotnets);
//$tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
$o .= replace_macros($tpl, [ $o .= replace_macros($tpl, [
'$is_edit' => true, '$is_edit' => true,
'$return_path' => $_SESSION['return_url'], '$return_path' => $_SESSION['return_url'],
@ -124,8 +110,8 @@ function editpost_content(App $a) {
'$shortnoloc' => L10n::t('clear location'), '$shortnoloc' => L10n::t('clear location'),
'$wait' => L10n::t('Please wait'), '$wait' => L10n::t('Please wait'),
'$permset' => L10n::t('Permission settings'), '$permset' => L10n::t('Permission settings'),
'$ptyp' => $itm[0]['type'], '$ptyp' => $item['type'],
'$content' => undo_post_tagging($itm[0]['body']), '$content' => undo_post_tagging($item['body']),
'$post_id' => $post_id, '$post_id' => $post_id,
'$baseurl' => System::baseUrl(), '$baseurl' => System::baseUrl(),
'$defloc' => $a->user['default-location'], '$defloc' => $a->user['default-location'],
@ -134,9 +120,9 @@ function editpost_content(App $a) {
'$emailcc' => L10n::t('CC: email addresses'), '$emailcc' => L10n::t('CC: email addresses'),
'$public' => L10n::t('Public post'), '$public' => L10n::t('Public post'),
'$jotnets' => $jotnets, '$jotnets' => $jotnets,
'$title' => htmlspecialchars($itm[0]['title']), '$title' => htmlspecialchars($item['title']),
'$placeholdertitle' => L10n::t('Set title'), '$placeholdertitle' => L10n::t('Set title'),
'$category' => file_tag_file_to_list($itm[0]['file'], 'category'), '$category' => file_tag_file_to_list($item['file'], 'category'),
'$placeholdercategory' => (Feature::isEnabled(local_user(),'categories') ? L10n::t("Categories \x28comma-separated list\x29") : ''), '$placeholdercategory' => (Feature::isEnabled(local_user(),'categories') ? L10n::t("Categories \x28comma-separated list\x29") : ''),
'$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'), '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
'$lockstate' => $lockstate, '$lockstate' => $lockstate,

View file

@ -7,7 +7,10 @@ use Friendica\App;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Protocol\Diaspora; use Friendica\Protocol\Diaspora;
use Friendica\Model\Item;
use Friendica\Model\User;
use Friendica\Util\XML; use Friendica\Util\XML;
use Friendica\Database\DBM;
function fetch_init(App $a) function fetch_init(App $a)
{ {
@ -20,24 +23,14 @@ function fetch_init(App $a)
$guid = $a->argv[2]; $guid = $a->argv[2];
// Fetch the item // Fetch the item
$item = q( $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network'];
"SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`, `coord` $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1", $item = Item::selectFirst($fields, $condition);
dbesc($guid), if (!DBM::is_result($item)) {
NETWORK_DFRN, $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
NETWORK_DIASPORA $item = Item::selectFirst(['author-link'], $condition);
); if (DBM::is_result($item)) {
if (!$item) { $parts = parse_url($item["author-link"]);
$r = q(
"SELECT `author-link`
FROM `item` WHERE `uid` = 0 AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
dbesc($guid),
NETWORK_DFRN,
NETWORK_DIASPORA
);
if ($r) {
$parts = parse_url($r[0]["author-link"]);
$host = $parts["scheme"]."://".$parts["host"]; $host = $parts["scheme"]."://".$parts["host"];
if (normalise_link($host) != normalise_link(System::baseUrl())) { if (normalise_link($host) != normalise_link(System::baseUrl())) {
@ -54,20 +47,13 @@ function fetch_init(App $a)
} }
// Fetch some data from the author (We could combine both queries - but I think this is more readable) // Fetch some data from the author (We could combine both queries - but I think this is more readable)
$r = q( $user = User::getOwnerDataById($item["uid"]);
"SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user` if (!$user) {
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
WHERE `user`.`uid` = %d",
intval($item[0]["uid"])
);
if (!$r) {
header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found')); header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
killme(); killme();
} }
$user = $r[0];
$status = Diaspora::buildStatus($item[0], $user); $status = Diaspora::buildStatus($item, $user);
$xml = Diaspora::buildPostXml($status["type"], $status["message"]); $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
// Send the envelope // Send the envelope

View file

@ -19,6 +19,7 @@ use Friendica\Model\Group;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\User;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Protocol\DFRN; use Friendica\Protocol\DFRN;
@ -175,19 +176,14 @@ function photos_post(App $a)
killme(); killme();
} }
$r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` $owner_record = User::getOwnerDataById($page_owner_uid);
WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
intval($page_owner_uid)
);
if (!DBM::is_result($r)) { if (!$owner_record) {
notice(L10n::t('Contact information unavailable') . EOL); notice(L10n::t('Contact information unavailable') . EOL);
logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid); logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
killme(); killme();
} }
$owner_record = $r[0];
if ($a->argc > 3 && $a->argv[2] === 'album') { if ($a->argc > 3 && $a->argv[2] === 'album') {
$album = hex2bin($a->argv[3]); $album = hex2bin($a->argv[3]);
@ -487,14 +483,11 @@ function photos_post(App $a)
} }
if ($item_id) { if ($item_id) {
$r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $item = Item::selectFirst(['tag', 'inform'], ['id' => $item_id, 'uid' => $page_owner_uid]);
intval($item_id),
intval($page_owner_uid)
);
} }
if (DBM::is_result($r)) { if (DBM::is_result($item)) {
$old_tag = $r[0]['tag']; $old_tag = $item['tag'];
$old_inform = $r[0]['inform']; $old_inform = $item['inform'];
} }
if (strlen($rawtags)) { if (strlen($rawtags)) {

View file

@ -10,29 +10,25 @@ use Friendica\Core\System;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Item; use Friendica\Model\Item;
function tagrm_post(App $a) { function tagrm_post(App $a)
{
if (!local_user()) { if (!local_user()) {
goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
} }
if ((x($_POST,'submit')) && ($_POST['submit'] === L10n::t('Cancel'))) { if (x($_POST,'submit') && ($_POST['submit'] === L10n::t('Cancel'))) {
goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
} }
$tag = ((x($_POST,'tag')) ? hex2bin(notags(trim($_POST['tag']))) : ''); $tag = (x($_POST,'tag') ? hex2bin(notags(trim($_POST['tag']))) : '');
$item = ((x($_POST,'item')) ? intval($_POST['item']) : 0 ); $item_id = (x($_POST,'item') ? intval($_POST['item']) : 0);
$r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
intval($item), if (!DBM::is_result($item)) {
intval(local_user())
);
if (!DBM::is_result($r)) {
goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
} }
$arr = explode(',', $r[0]['tag']); $arr = explode(',', $item['tag']);
for ($x = 0; $x < count($arr); $x ++) { for ($x = 0; $x < count($arr); $x ++) {
if ($arr[$x] === $tag) { if ($arr[$x] === $tag) {
unset($arr[$x]); unset($arr[$x]);
@ -42,7 +38,7 @@ function tagrm_post(App $a) {
$tag_str = implode(',',$arr); $tag_str = implode(',',$arr);
Item::update(['tag' => $tag_str], ['id' => $item]); Item::update(['tag' => $tag_str], ['id' => $item_id]);
info(L10n::t('Tag removed') . EOL ); info(L10n::t('Tag removed') . EOL );
goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
@ -52,8 +48,8 @@ function tagrm_post(App $a) {
function tagrm_content(App $a) { function tagrm_content(App $a)
{
$o = ''; $o = '';
if (!local_user()) { if (!local_user()) {
@ -61,22 +57,18 @@ function tagrm_content(App $a) {
// NOTREACHED // NOTREACHED
} }
$item = (($a->argc > 1) ? intval($a->argv[1]) : 0); $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
if (!$item) { if (!$item_id) {
goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
// NOTREACHED // NOTREACHED
} }
$r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
intval($item), if (!DBM::is_result($item)) {
intval(local_user())
);
if (!DBM::is_result($r)) {
goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
} }
$arr = explode(',', $r[0]['tag']); $arr = explode(',', $item['tag']);
if (!count($arr)) { if (!count($arr)) {
goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']);
@ -87,7 +79,7 @@ function tagrm_content(App $a) {
$o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>'; $o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>';
$o .= '<form id="tagrm" action="tagrm" method="post" >'; $o .= '<form id="tagrm" action="tagrm" method="post" >';
$o .= '<input type="hidden" name="item" value="' . $item . '" />'; $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
$o .= '<ul>'; $o .= '<ul>';
foreach ($arr as $x) { foreach ($arr as $x) {
@ -100,5 +92,4 @@ function tagrm_content(App $a) {
$o .= '</form>'; $o .= '</form>';
return $o; return $o;
} }

View file

@ -5,9 +5,10 @@
use Friendica\App; use Friendica\App;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Item;
function viewsrc_content(App $a) { function viewsrc_content(App $a)
{
if (!local_user()) { if (!local_user()) {
notice(L10n::t('Access denied.') . EOL); notice(L10n::t('Access denied.') . EOL);
return; return;
@ -22,21 +23,15 @@ function viewsrc_content(App $a) {
return; return;
} }
$r = q("SELECT `item`.`body` FROM `item` $item = Item::selectFirst(['body'], ['uid' => local_user(), 'id' => $item_id]);
WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
and `item`.`moderated` = 0
AND `item`.`id` = '%s' LIMIT 1",
intval(local_user()),
dbesc($item_id)
);
if (DBM::is_result($r)) if (DBM::is_result($item)) {
if (is_ajax()) { if (is_ajax()) {
echo str_replace("\n",'<br />',$r[0]['body']); echo str_replace("\n", '<br />', $item['body']);
killme(); killme();
} else { } else {
$o .= str_replace("\n",'<br />',$r[0]['body']); $o .= str_replace("\n", '<br />', $item['body']);
}
} }
return $o; return $o;
} }

View file

@ -38,8 +38,8 @@ class Item extends BaseObject
'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink', 'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'attach', 'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'attach',
'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object', 'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id', 'item_network', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id',
'author-id', 'author-link', 'author-name', 'author-avatar', 'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-id', 'owner-link', 'owner-name', 'owner-avatar',
'contact-id', 'contact-link', 'contact-name', 'contact-avatar', 'contact-id', 'contact-link', 'contact-name', 'contact-avatar',
'network', 'url', 'name', 'writable', 'self', 'cid', 'alias', 'network', 'url', 'name', 'writable', 'self', 'cid', 'alias',
@ -164,21 +164,63 @@ class Item extends BaseObject
return dba::p($sql, $condition); return dba::p($sql, $condition);
} }
/**
* @brief Select rows from the starting post in the item table
*
* @param integer $uid User ID
* @param array $fields 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
*/
public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
{
$params['uid'] = $uid;
if (empty($selected)) {
$selected = Item::DISPLAY_FIELDLIST;
}
return self::selectThread($selected, $condition, $params);
}
/** /**
* Retrieve a single record from the starting post in the item table and returns it in an associative array * Retrieve a single record from the starting post in the item table and returns it in an associative array
* *
* @brief Retrieve a single record from a table * @brief Retrieve a single record from a table
* @param integer $uid User ID * @param integer $uid User ID
* @param array $selected
* @param array $condition
* @param array $params
* @return bool|array
* @see dba::select
*/
public static function selectFirstThreadForUser($uid, array $selected = [], array $condition = [], $params = [])
{
$params['uid'] = $uid;
if (empty($selected)) {
$selected = Item::DISPLAY_FIELDLIST;
}
return self::selectFirstThread($selected, $condition, $params);
}
/**
* Retrieve a single record from the starting post in the item table and returns it in an associative array
*
* @brief Retrieve a single record from a table
* @param array $fields * @param array $fields
* @param array $condition * @param array $condition
* @param array $params * @param array $params
* @return bool|array * @return bool|array
* @see dba::select * @see dba::select
*/ */
public static function selectFirstThreadForUser($uid, array $fields = [], array $condition = [], $params = []) public static function selectFirstThread(array $fields = [], array $condition = [], $params = [])
{ {
$params['limit'] = 1; $params['limit'] = 1;
$result = self::selectThreadForUser($uid, $fields, $condition, $params); $result = self::selectThread($fields, $condition, $params);
if (is_bool($result)) { if (is_bool($result)) {
return $result; return $result;
@ -192,17 +234,20 @@ class Item extends BaseObject
/** /**
* @brief Select rows from the starting post in the item table * @brief Select rows from the starting post in the item table
* *
* @param integer $uid User ID * @param array $selected Array of selected fields, empty for all
* @param array $fields Array of selected fields, empty for all
* @param array $condition Array of fields for condition * @param array $condition Array of fields for condition
* @param array $params Array of several parameters * @param array $params Array of several parameters
* *
* @return boolean|object * @return boolean|object
*/ */
public static function selectThreadForUser($uid, array $selected = [], array $condition = [], $params = []) public static function selectThread(array $selected = [], array $condition = [], $params = [])
{ {
if (empty($selected)) { $uid = 0;
$selected = Item::DISPLAY_FIELDLIST; $usermode = false;
if (isset($params['uid'])) {
$uid = $params['uid'];
$usermode = true;
} }
$fields = self::fieldlist($selected); $fields = self::fieldlist($selected);
@ -219,7 +264,9 @@ class Item extends BaseObject
$condition_string = self::addTablesToFields($condition_string, $threadfields); $condition_string = self::addTablesToFields($condition_string, $threadfields);
$condition_string = self::addTablesToFields($condition_string, $fields); $condition_string = self::addTablesToFields($condition_string, $fields);
if ($usermode) {
$condition_string = $condition_string . ' AND ' . self::condition(true); $condition_string = $condition_string . ' AND ' . self::condition(true);
}
$param_string = dba::buildParameter($params); $param_string = dba::buildParameter($params);
$param_string = self::addTablesToFields($param_string, $threadfields); $param_string = self::addTablesToFields($param_string, $threadfields);
@ -250,17 +297,19 @@ class Item extends BaseObject
'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'unseen', 'deleted', 'origin', 'forum_mode', 'mention',
'rendered-hash', 'rendered-html', 'global', 'shadow', 'content-warning', 'rendered-hash', 'rendered-html', 'global', 'shadow', 'content-warning',
'id' => 'item_id', 'network' => 'item_network']; 'id' => 'item_id', 'network'];
$fields['author'] = ['url' => 'author-link', 'name' => 'author-name', 'thumb' => 'author-avatar']; $fields['author'] = ['url' => 'author-link', 'name' => 'author-name',
'thumb' => 'author-avatar', 'nick' => 'author-nick', 'network' => 'author-network'];
$fields['owner'] = ['url' => 'owner-link', 'name' => 'owner-name', 'thumb' => 'owner-avatar']; $fields['owner'] = ['url' => 'owner-link', 'name' => 'owner-name',
'thumb' => 'owner-avatar', 'nick' => 'owner-nick', 'network' => 'owner-network'];
$fields['contact'] = ['url' => 'contact-link', 'name' => 'contact-name', 'thumb' => 'contact-avatar', $fields['contact'] = ['url' => 'contact-link', 'name' => 'contact-name', 'thumb' => 'contact-avatar',
'network', 'url', 'name', 'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid', 'network', 'url', 'name', 'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid',
'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id']; 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id', 'network' => 'contact-network'];
$fields['parent-item'] = ['guid' => 'parent-guid']; $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network'];
$fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name']; $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name'];

View file

@ -6,6 +6,7 @@ namespace Friendica\Model;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Item;
use dba; use dba;
require_once 'boot.php'; require_once 'boot.php';
@ -114,7 +115,7 @@ class Term
$users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link); $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
foreach ($users AS $user) { foreach ($users AS $user) {
if ($user['uid'] == $message['uid']) { if ($user['uid'] == $message['uid']) {
dba::update('item', ['mention' => true], ['id' => $itemid]); Item::update(['mention' => true], ['id' => $itemid]);
dba::update('thread', ['mention' => true], ['iid' => $message['parent']]); dba::update('thread', ['mention' => true], ['iid' => $message['parent']]);
} }
} }

View file

@ -65,6 +65,21 @@ class User
return $r; return $r;
} }
/**
* @brief Get owner data by nick name
*
* @param int $nick
* @return boolean|array
*/
public static function getOwnerDataByNick($nick)
{
$user = dba::selectFirst('user', ['uid'], ['nickname' => $nick]);
if (!DBM::is_result($user)) {
return false;
}
return self::getOwnerDataById($user['uid']);
}
/** /**
* @brief Returns the default group for a given user and network * @brief Returns the default group for a given user and network
* *

View file

@ -326,17 +326,17 @@ class Post extends BaseObject
$owner_name_e = $this->getOwnerName(); $owner_name_e = $this->getOwnerName();
// Disable features that aren't available in several networks // Disable features that aren't available in several networks
if (!in_array($item["item_network"], [NETWORK_DFRN, NETWORK_DIASPORA]) && isset($buttons["dislike"])) { if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA]) && isset($buttons["dislike"])) {
unset($buttons["dislike"]); unset($buttons["dislike"]);
$isevent = false; $isevent = false;
$tagger = ''; $tagger = '';
} }
if (($item["item_network"] == NETWORK_FEED) && isset($buttons["like"])) { if (($item["network"] == NETWORK_FEED) && isset($buttons["like"])) {
unset($buttons["like"]); unset($buttons["like"]);
} }
if (($item["item_network"] == NETWORK_MAIL) && isset($buttons["like"])) { if (($item["network"] == NETWORK_MAIL) && isset($buttons["like"])) {
unset($buttons["like"]); unset($buttons["like"]);
} }
@ -401,8 +401,8 @@ class Post extends BaseObject
'wait' => L10n::t('Please wait'), 'wait' => L10n::t('Please wait'),
'thread_level' => $thread_level, 'thread_level' => $thread_level,
'edited' => $edited, 'edited' => $edited,
'network' => $item["item_network"], 'network' => $item["network"],
'network_name' => ContactSelector::networkToName($item['item_network'], $profile_link), 'network_name' => ContactSelector::networkToName($item['network'], $profile_link),
'received' => $item['received'], 'received' => $item['received'],
'commented' => $item['commented'], 'commented' => $item['commented'],
'created_date' => $item['created'], 'created_date' => $item['created'],

View file

@ -15,6 +15,7 @@ use Friendica\Model\Contact;
use Friendica\Model\Conversation; use Friendica\Model\Conversation;
use Friendica\Model\GContact; use Friendica\Model\GContact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\User;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -1619,22 +1620,14 @@ class OStatus
$title = self::entryHeader($doc, $entry, $owner, $item, $toplevel); $title = self::entryHeader($doc, $entry, $owner, $item, $toplevel);
$r = q( $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => false,
"SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' AND NOT `private` AND `network` IN ('%s', '%s', '%s') LIMIT 1", 'network' => [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]];
intval($owner["uid"]), $repeated_item = Item::selectFirst([], $condition);
dbesc($repeated_guid), if (!DBM::is_result($repeated_item)) {
dbesc(NETWORK_DFRN),
dbesc(NETWORK_DIASPORA),
dbesc(NETWORK_OSTATUS)
);
if (DBM::is_result($r)) {
$repeated_item = $r[0];
} else {
return false; return false;
} }
$contact = self::contactEntry($repeated_item['author-link'], $owner);
$parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']); $contact = self::contactEntry($repeated_item['author-link'], $owner);
$title = $owner["nick"]." repeated a notice by ".$contact["nick"]; $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
@ -1695,16 +1688,11 @@ class OStatus
$as_object = $doc->createElement("activity:object"); $as_object = $doc->createElement("activity:object");
$parent = q( $parent = Item::selectFirst([], ['uri' => $item["thr-parent"], 'uid' => $item["uid"]]);
"SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
dbesc($item["thr-parent"]),
intval($item["uid"])
);
$parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
XML::addElement($doc, $as_object, "activity:object-type", self::constructObjecttype($parent[0])); XML::addElement($doc, $as_object, "activity:object-type", self::constructObjecttype($parent));
self::entryContent($doc, $as_object, $parent[0], $owner, "New entry"); self::entryContent($doc, $as_object, $parent, $owner, "New entry");
$entry->appendChild($as_object); $entry->appendChild($as_object);
@ -1952,22 +1940,19 @@ class OStatus
$mentioned = []; $mentioned = [];
if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) { if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
$parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"])); $parent = Item::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item["parent"]]);
$parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']); $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
$thrparent = q( $thrparent = Item::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $parent_item]);
"SELECT `guid`, `author-link`, `owner-link`, `plink` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
intval($owner["uid"]), if (DBM::is_result($thrparent)) {
dbesc($parent_item) $mentioned[$thrparent["author-link"]] = $thrparent["author-link"];
); $mentioned[$thrparent["owner-link"]] = $thrparent["owner-link"];
if ($thrparent) { $parent_plink = $thrparent["plink"];
$mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"];
$mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"];
$parent_plink = $thrparent[0]["plink"];
} else { } else {
$mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"]; $mentioned[$parent["author-link"]] = $parent["author-link"];
$mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"]; $mentioned[$parent["owner-link"]] = $parent["owner-link"];
$parent_plink = System::baseUrl()."/display/".$parent[0]["guid"]; $parent_plink = System::baseUrl()."/display/".$parent["guid"];
} }
$attributes = [ $attributes = [
@ -2119,25 +2104,23 @@ class OStatus
{ {
$stamp = microtime(true); $stamp = microtime(true);
$owner = User::getOwnerDataByNick($owner_nick);
if (!$owner) {
return;
}
$cachekey = "ostatus:feed:" . $owner_nick . ":" . $filter . ":" . $last_update; $cachekey = "ostatus:feed:" . $owner_nick . ":" . $filter . ":" . $last_update;
$previous_created = $last_update; $previous_created = $last_update;
// Don't cache when the last item was posted less then 15 minutes ago (Cache duration)
if ((time() - strtotime($owner['last-item'])) < 15*60) {
$result = Cache::get($cachekey); $result = Cache::get($cachekey);
if (!$nocache && !is_null($result)) { if (!$nocache && !is_null($result)) {
logger('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created . ' (cached)', LOGGER_DEBUG); logger('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created . ' (cached)', LOGGER_DEBUG);
$last_update = $result['last_update']; $last_update = $result['last_update'];
return $result['feed']; return $result['feed'];
} }
$owner = dba::fetch_first(
"SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`, `user`.`account-type`
FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
WHERE `contact`.`self` AND `user`.`nickname` = ? LIMIT 1",
$owner_nick
);
if (!DBM::is_result($owner)) {
return;
} }
if (!strlen($last_update)) { if (!strlen($last_update)) {
@ -2147,37 +2130,30 @@ class OStatus
$check_date = DateTimeFormat::utc($last_update); $check_date = DateTimeFormat::utc($last_update);
$authorid = Contact::getIdForURL($owner["url"], 0, true); $authorid = Contact::getIdForURL($owner["url"], 0, true);
$sql_extra = ''; $condition = ["`uid` = ? AND `created` > ? AND NOT `deleted`
if ($filter === 'posts') { AND NOT `private` AND `visible` AND `wall` AND `parent-network` IN (?, ?)",
$sql_extra .= ' AND `item`.`id` = `item`.`parent` '; $owner["uid"], $check_date, NETWORK_OSTATUS, NETWORK_DFRN];
}
if ($filter === 'comments') { if ($filter === 'comments') {
$sql_extra .= sprintf(" AND `item`.`object-type` = '%s' ", dbesc(ACTIVITY_OBJ_COMMENT)); $condition[0] .= " AND `object-type` = ? ";
$condition[] = ACTIVITY_OBJ_COMMENT;
} }
if ($owner['account-type'] != ACCOUNT_TYPE_COMMUNITY) { if ($owner['account-type'] != ACCOUNT_TYPE_COMMUNITY) {
$sql_extra .= sprintf(" AND `item`.`contact-id` = %d AND `item`.`author-id` = %d ", intval($owner["id"]), intval($authorid)); $condition[0] .= " AND `contact-id` = ? AND `author-id` = ?";
$condition[] = $owner["id"];
$condition[] = $authorid;
} }
$items = q( $params = ['order' => ['created' => true], 'limit' => $max_items];
"SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent` if ($filter === 'posts') {
WHERE `item`.`uid` = %d $ret = Item::selectThread([], $condition, $params);
AND `item`.`created` > '%s' } else {
AND NOT `item`.`deleted` $ret = Item::select([], $condition, $params);
AND NOT `item`.`private` }
AND `item`.`visible`
AND `item`.`wall` $items = dba::inArray($ret);
AND `thread`.`network` IN ('%s', '%s')
$sql_extra
ORDER BY `item`.`created` DESC LIMIT %d",
intval($owner["uid"]),
dbesc($check_date),
dbesc(NETWORK_OSTATUS),
dbesc(NETWORK_DFRN),
intval($max_items)
);
$doc = new DOMDocument('1.0', 'utf-8'); $doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true; $doc->formatOutput = true;