implement 'can_comment_on_post()' which doesn't require a separate DB lookup per item.

This commit is contained in:
friendica 2013-06-16 20:44:29 -07:00
parent 764ee785f5
commit 28cd8594a7
3 changed files with 43 additions and 3 deletions

View File

@ -44,7 +44,7 @@ class Item extends BaseObject {
$this->commentable = $this->writable;
if(($this->observer) && (! $this->writable)) {
$this->commentable = perm_is_allowed($this->data['uid'],$this->observer['xchan_hash'],'post_comments');
$this->commentable = can_comment_on_post($this->observer['xchan_hash'],$data);
}
// logger('writable: ' . $this->writable);

View File

@ -53,6 +53,37 @@ function collect_recipients($item,&$private) {
}
function can_comment_on_post($observer_xchan,$item) {
if(! $observer_xchan)
return false;
if($item['comment_policy'] === 'none')
return false;
switch($item['comment_policy']) {
case 'self':
if($observer_xchan === $item['author_xchan'] || $observer_xchan === $item['owner_xchan'])
return true;
break;
case 'public':
return false;
break;
case 'contacts':
case '':
if(($item['owner']['abook_xchan']) && ($item['owner']['abook_their_perms'] & PERMS_W_COMMENT))
return true;
break;
default:
break;
}
if(strstr('network:',$item['comment_policy']) && strstr('red',$item['comment_policy']))
return true;
if(strstr('site:', $item['comment_policy']) && strstr(get_app()->get_hostname(),$item['comment_policy']))
return true;
return false;
}
/**
* @function red_zrl_callback
* preg_match function when fixing 'naked' links in mod item.php
@ -142,6 +173,7 @@ function post_activity_item($arr) {
$arr['deny_cid'] = ((x($arr,'deny_cid')) ? $arr['deny_cid'] : $channel['channel_deny_cid']);
$arr['deny_gid'] = ((x($arr,'deny_gid')) ? $arr['deny_gid'] : $channel['channel_deny_gid']);
$arr['comment_policy'] = map_policy($channel['channel_w_comment']);
// for the benefit of plugins, we will behave as if this is an API call rather than a normal online post
@ -481,7 +513,8 @@ function get_item_elements($x) {
$arr['mimetype'] = (($x['mimetype']) ? htmlentities($x['mimetype'], ENT_COMPAT,'UTF-8',false) : '');
$arr['obj_type'] = (($x['object_type']) ? htmlentities($x['object_type'], ENT_COMPAT,'UTF-8',false) : '');
$arr['tgt_type'] = (($x['target_type']) ? htmlentities($x['target_type'], ENT_COMPAT,'UTF-8',false) : '');
$arr['comment_policy'] = (($x['comment_scope']) ? htmlentities($x['comment_scope'], ENT_COMPAT,'UTF-8',false) : 'contacts');
$arr['object'] = activity_sanitise($x['object']);
$arr['target'] = activity_sanitise($x['target']);
@ -1332,10 +1365,17 @@ function item_store($arr,$force_parent = false) {
$arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : '');
$arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : '');
$arr['item_restrict'] = ((x($arr,'item_restrict')) ? intval($arr['item_restrict']) : 0 );
$arr['comment_policy'] = ((x($arr,'comment_policy')) ? notags(trim($arr['comment_policy'])) : 'contacts' );
$arr['item_flags'] = ((x($arr,'item_flags')) ? intval($arr['item_flags']) : 0 );
$arr['item_flags'] = $arr['item_flags'] | ITEM_UNSEEN;
if($arr['comment_policy'] == 'none')
$arr['item_flags'] = $arr['item_flags'] | ITEM_NOCOMMENT;
// handle time travelers
// Allow a bit of fudge in case somebody just has a slightly slow/fast clock

View File

@ -1794,7 +1794,7 @@ function ids_to_querystr($arr,$idx = 'id') {
// author_xchan and owner_xchan. If $abook is true also include the abook info.
// This is needed in the API to save extra per item lookups there.
function xchan_query(&$items,$abook = false) {
function xchan_query(&$items,$abook = true) {
$arr = array();
if($items && count($items)) {
foreach($items as $item) {