1
1
Fork 0

More abstraction for the item access

This commit is contained in:
Michael 2018-06-12 09:05:36 +00:00
parent d602da024f
commit a4607f8d1c
5 changed files with 103 additions and 115 deletions

View file

@ -8,6 +8,7 @@ use Friendica\Core\ACL;
use Friendica\Core\Addon; use Friendica\Core\Addon;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item;
require_once 'include/dba.php'; require_once 'include/dba.php';
require_once 'mod/proxy.php'; require_once 'mod/proxy.php';
@ -250,25 +251,26 @@ function acl_content(App $a)
* but first get known contacts url to filter them out * but first get known contacts url to filter them out
*/ */
$known_contacts = array_map(function ($i) { $known_contacts = array_map(function ($i) {
return dbesc($i['link']); return $i['link'];
}, $contacts); }, $contacts);
$unknown_contacts = []; $unknown_contacts = [];
$r = q("SELECT `author-link`
FROM `item` WHERE `parent` = %d $condition = ["`parent` = ?", $conv_id];
AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%') $params = ['order' => ['author-name' => true]];
AND `author-link` NOT IN ('%s') $authors = Item::select(local_user(), ['author-link'], $condition, $params);
GROUP BY `author-link`, `author-avatar`, `author-name` $item_authors = [];
ORDER BY `author-name` ASC while ($author = dba::fetch($authors)) {
", $item_authors[$author['author-link']] = $author['author-link'];
intval($conv_id), }
dbesc($search), dba::close($authors);
dbesc($search),
implode("', '", $known_contacts) foreach ($item_authors as $author) {
); if (in_array($author, $known_contacts)) {
if (DBM::is_result($r)) { continue;
foreach ($r as $row) { }
$contact = Contact::getDetailsByURL($row['author-link']);
$contact = Contact::getDetailsByURL($author);
if (count($contact) > 0) { if (count($contact) > 0) {
$unknown_contacts[] = [ $unknown_contacts[] = [
@ -284,7 +286,6 @@ function acl_content(App $a)
]; ];
} }
} }
}
$items = array_merge($items, $unknown_contacts); $items = array_merge($items, $unknown_contacts);
$tot += count($unknown_contacts); $tot += count($unknown_contacts);

View file

@ -26,31 +26,31 @@ require_once 'include/items.php';
function poke_init(App $a) { function poke_init(App $a) {
if (! local_user()) { if (!local_user()) {
return; return;
} }
$uid = local_user(); $uid = local_user();
$verb = notags(trim($_GET['verb'])); $verb = notags(trim($_GET['verb']));
if (! $verb) { if (!$verb) {
return; return;
} }
$verbs = get_poke_verbs(); $verbs = get_poke_verbs();
if (! array_key_exists($verb,$verbs)) { if (!array_key_exists($verb, $verbs)) {
return; return;
} }
$activity = ACTIVITY_POKE . '#' . urlencode($verbs[$verb][0]); $activity = ACTIVITY_POKE . '#' . urlencode($verbs[$verb][0]);
$contact_id = intval($_GET['cid']); $contact_id = intval($_GET['cid']);
if (! $contact_id) { if (!$contact_id) {
return; return;
} }
$parent = ((x($_GET,'parent')) ? intval($_GET['parent']) : 0); $parent = (x($_GET,'parent') ? intval($_GET['parent']) : 0);
logger('poke: verb ' . $verb . ' contact ' . $contact_id, LOGGER_DEBUG); logger('poke: verb ' . $verb . ' contact ' . $contact_id, LOGGER_DEBUG);
@ -61,49 +61,45 @@ function poke_init(App $a) {
intval($uid) intval($uid)
); );
if (! DBM::is_result($r)) { if (!DBM::is_result($r)) {
logger('poke: no contact ' . $contact_id); logger('poke: no contact ' . $contact_id);
return; return;
} }
$target = $r[0]; $target = $r[0];
if($parent) { if ($parent) {
$r = q("SELECT `uri`, `private`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` $fields = ['uri', 'private', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
FROM `item` WHERE `id` = %d AND `parent` = %d AND `uid` = %d LIMIT 1", $condition = ['id' => $parent, 'parent' => $parent, 'uid' => $uid];
intval($parent), $item = Item::selectFirst(local_user(), $fields, $condition);
intval($parent),
intval($uid)
);
if (DBM::is_result($r)) {
$parent_uri = $r[0]['uri'];
$private = $r[0]['private'];
$allow_cid = $r[0]['allow_cid'];
$allow_gid = $r[0]['allow_gid'];
$deny_cid = $r[0]['deny_cid'];
$deny_gid = $r[0]['deny_gid'];
}
}
else {
$private = ((x($_GET,'private')) ? intval($_GET['private']) : 0); if (DBM::is_result($item)) {
$parent_uri = $item['uri'];
$private = $item['private'];
$allow_cid = $item['allow_cid'];
$allow_gid = $item['allow_gid'];
$deny_cid = $item['deny_cid'];
$deny_gid = $item['deny_gid'];
}
} else {
$private = (x($_GET,'private') ? intval($_GET['private']) : 0);
$allow_cid = (($private) ? '<' . $target['id']. '>' : $a->user['allow_cid']); $allow_cid = ($private ? '<' . $target['id']. '>' : $a->user['allow_cid']);
$allow_gid = (($private) ? '' : $a->user['allow_gid']); $allow_gid = ($private ? '' : $a->user['allow_gid']);
$deny_cid = (($private) ? '' : $a->user['deny_cid']); $deny_cid = ($private ? '' : $a->user['deny_cid']);
$deny_gid = (($private) ? '' : $a->user['deny_gid']); $deny_gid = ($private ? '' : $a->user['deny_gid']);
} }
$poster = $a->contact; $poster = $a->contact;
$uri = item_new_uri($a->get_hostname(),$uid); $uri = item_new_uri($a->get_hostname(), $uid);
$arr = []; $arr = [];
$arr['guid'] = get_guid(32); $arr['guid'] = get_guid(32);
$arr['uid'] = $uid; $arr['uid'] = $uid;
$arr['uri'] = $uri; $arr['uri'] = $uri;
$arr['parent-uri'] = (($parent_uri) ? $parent_uri : $uri); $arr['parent-uri'] = ($parent_uri ? $parent_uri : $uri);
$arr['type'] = 'activity'; $arr['type'] = 'activity';
$arr['wall'] = 1; $arr['wall'] = 1;
$arr['contact-id'] = $poster['id']; $arr['contact-id'] = $poster['id'];
@ -133,7 +129,7 @@ function poke_init(App $a) {
$arr['object'] .= '</link></object>' . "\n"; $arr['object'] .= '</link></object>' . "\n";
$item_id = Item::insert($arr); $item_id = Item::insert($arr);
if($item_id) { if ($item_id) {
Worker::add(PRIORITY_HIGH, "Notifier", "tag", $item_id); Worker::add(PRIORITY_HIGH, "Notifier", "tag", $item_id);
} }
@ -146,7 +142,7 @@ function poke_init(App $a) {
function poke_content(App $a) { function poke_content(App $a) {
if (! local_user()) { if (!local_user()) {
notice(L10n::t('Permission denied.') . EOL); notice(L10n::t('Permission denied.') . EOL);
return; return;
} }
@ -154,14 +150,14 @@ function poke_content(App $a) {
$name = ''; $name = '';
$id = ''; $id = '';
if(intval($_GET['c'])) { if (intval($_GET['c'])) {
$r = q("SELECT `id`,`name` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", $r = q("SELECT `id`,`name` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($_GET['c']), intval($_GET['c']),
intval(local_user()) intval(local_user())
); );
if (DBM::is_result($r)) { if (DBM::is_result($r)) {
$name = $r[0]['name']; $name = $item['name'];
$id = $r[0]['id']; $id = $item['id'];
} }
} }
@ -175,16 +171,17 @@ function poke_content(App $a) {
]); ]);
$parent = ((x($_GET,'parent')) ? intval($_GET['parent']) : '0'); $parent = (x($_GET,'parent') ? intval($_GET['parent']) : '0');
$verbs = get_poke_verbs(); $verbs = get_poke_verbs();
$shortlist = []; $shortlist = [];
foreach($verbs as $k => $v) foreach ($verbs as $k => $v) {
if($v[1] !== 'NOTRANSLATION') if ($v[1] !== 'NOTRANSLATION') {
$shortlist[] = [$k,$v[1]]; $shortlist[] = [$k, $v[1]];
}
}
$tpl = get_markup_template('poke_content.tpl'); $tpl = get_markup_template('poke_content.tpl');
@ -202,5 +199,4 @@ function poke_content(App $a) {
]); ]);
return $o; return $o;
} }

View file

@ -2,6 +2,7 @@
use Friendica\App; use Friendica\App;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Item;
function share_init(App $a) { function share_init(App $a) {
$post_id = (($a->argc > 1) ? intval($a->argv[1]) : 0); $post_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
@ -10,27 +11,25 @@ function share_init(App $a) {
killme(); killme();
} }
$r = q("SELECT item.*, contact.network FROM `item` $fields = ['private', 'body', 'author-name', 'author-link', 'author-avatar',
INNER JOIN `contact` ON `item`.`contact-id` = `contact`.`id` 'guid', 'created', 'plink', 'title'];
WHERE `item`.`id` = %d LIMIT 1", $item = Item::selectFirst(local_user(), $fields, ['id' => $post_id]);
intval($post_id)
);
if (!DBM::is_result($r) || ($r[0]['private'] == 1)) { if (!DBM::is_result($item) || $item['private']) {
killme(); killme();
} }
if (strpos($r[0]['body'], "[/share]") !== false) { if (strpos($item['body'], "[/share]") !== false) {
$pos = strpos($r[0]['body'], "[share"); $pos = strpos($item['body'], "[share");
$o = substr($r[0]['body'], $pos); $o = substr($item['body'], $pos);
} else { } else {
$o = share_header($r[0]['author-name'], $r[0]['author-link'], $r[0]['author-avatar'], $r[0]['guid'], $r[0]['created'], $r[0]['plink']); $o = share_header($item['author-name'], $item['author-link'], $item['author-avatar'], $item['guid'], $item['created'], $item['plink']);
if ($r[0]['title']) { if ($item['title']) {
$o .= '[b]'.$r[0]['title'].'[/b]'."\n"; $o .= '[b]'.$item['title'].'[/b]'."\n";
} }
$o .= $r[0]['body']; $o .= $item['body'];
$o .= "[/share]"; $o .= "[/share]";
} }

View file

@ -14,7 +14,7 @@ require_once 'include/items.php';
function subthread_content(App $a) { function subthread_content(App $a) {
if(! local_user() && ! remote_user()) { if (!local_user() && !remote_user()) {
return; return;
} }
@ -22,36 +22,32 @@ function subthread_content(App $a) {
$item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0); $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
$r = q("SELECT * FROM `item` WHERE `parent` = '%s' OR `parent-uri` = '%s' and parent = id LIMIT 1", $condition = ["`parent` = ? OR `parent-uri` = ? AND `parent` = `id`", $item_id, $item_id];
dbesc($item_id), $item = Item::selectFirst(local_user(), [], $condition);
dbesc($item_id)
);
if(! $item_id || (! DBM::is_result($r))) { if (empty($item_id) || !DBM::is_result($item)) {
logger('subthread: no item ' . $item_id); logger('subthread: no item ' . $item_id);
return; return;
} }
$item = $r[0];
$owner_uid = $item['uid']; $owner_uid = $item['uid'];
if(! can_write_wall($owner_uid)) { if (!can_write_wall($owner_uid)) {
return; return;
} }
$remote_owner = null; $remote_owner = null;
if(! $item['wall']) { if (!$item['wall']) {
// The top level post may have been written by somebody on another system // The top level post may have been written by somebody on another system
$r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($item['contact-id']), intval($item['contact-id']),
intval($item['uid']) intval($item['uid'])
); );
if (! DBM::is_result($r)) { if (!DBM::is_result($r)) {
return; return;
} }
if (! $r[0]['self']) { if (!$r[0]['self']) {
$remote_owner = $r[0]; $remote_owner = $r[0];
} }
} }
@ -68,19 +64,19 @@ function subthread_content(App $a) {
$owner = $r[0]; $owner = $r[0];
} }
if (! $owner) { if (!$owner) {
logger('like: no owner'); logger('like: no owner');
return; return;
} }
if (! $remote_owner) { if (!$remote_owner) {
$remote_owner = $owner; $remote_owner = $owner;
} }
$contact = null; $contact = null;
// This represents the person posting // This represents the person posting
if ((local_user()) && (local_user() == $owner_uid)) { if (local_user() && (local_user() == $owner_uid)) {
$contact = $owner; $contact = $owner;
} else { } else {
$r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
@ -92,7 +88,7 @@ function subthread_content(App $a) {
$contact = $r[0]; $contact = $r[0];
} }
} }
if (! $contact) { if (!$contact) {
return; return;
} }
@ -116,7 +112,7 @@ function subthread_content(App $a) {
EOT; EOT;
$bodyverb = L10n::t('%1$s is following %2$s\'s %3$s'); $bodyverb = L10n::t('%1$s is following %2$s\'s %3$s');
if (! isset($bodyverb)) { if (!isset($bodyverb)) {
return; return;
} }
@ -168,5 +164,3 @@ EOT;
killme(); killme();
} }

View file

@ -15,7 +15,7 @@ require_once 'include/items.php';
function tagger_content(App $a) { function tagger_content(App $a) {
if(! local_user() && ! remote_user()) { if (!local_user() && !remote_user()) {
return; return;
} }
@ -23,25 +23,22 @@ function tagger_content(App $a) {
// no commas allowed // no commas allowed
$term = str_replace([',',' '],['','_'],$term); $term = str_replace([',',' '],['','_'],$term);
if(! $term) if (!$term) {
return; return;
}
$item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0); $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
logger('tagger: tag ' . $term . ' item ' . $item_id); logger('tagger: tag ' . $term . ' item ' . $item_id);
$r = q("SELECT * FROM `item` WHERE `id` = '%s' LIMIT 1", $item = Item::selectFirst(local_user(), [], ['id' => $item_id]);
dbesc($item_id)
);
if(! $item_id || (! DBM::is_result($r))) { if (!$item_id || !DBM::is_result($item)) {
logger('tagger: no item ' . $item_id); logger('tagger: no item ' . $item_id);
return; return;
} }
$item = $r[0];
$owner_uid = $item['uid']; $owner_uid = $item['uid'];
$owner_nick = ''; $owner_nick = '';
$blocktags = 0; $blocktags = 0;
@ -54,15 +51,16 @@ function tagger_content(App $a) {
$blocktags = $r[0]['blocktags']; $blocktags = $r[0]['blocktags'];
} }
if(local_user() != $owner_uid) if (local_user() != $owner_uid) {
return; return;
}
$r = q("select * from contact where self = 1 and uid = %d limit 1", $r = q("select * from contact where self = 1 and uid = %d limit 1",
intval(local_user()) intval(local_user())
); );
if (DBM::is_result($r)) if (DBM::is_result($r)) {
$contact = $r[0]; $contact = $r[0];
else { } else {
logger('tagger: no contact_id'); logger('tagger: no contact_id');
return; return;
} }
@ -109,7 +107,7 @@ EOT;
$bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s'); $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
if (! isset($bodyverb)) { if (!isset($bodyverb)) {
return; return;
} }
@ -165,7 +163,7 @@ EOT;
dbesc($term) dbesc($term)
); );
if ((!$blocktags) && $t[0]['tcount'] == 0 ) { if (!$blocktags && $t[0]['tcount'] == 0) {
q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)", q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
intval($item['id']), intval($item['id']),
$term_objtype, $term_objtype,