From 5d4c9f5617ec8a99132a59d080f66989eba918f9 Mon Sep 17 00:00:00 2001 From: friendica Date: Thu, 15 Nov 2012 21:52:05 -0800 Subject: more progress on notifier cleanup and encoding items for transit. --- include/conversation.php | 2 - include/items.php | 112 +++++++++++++++++++++++++++++++++++++++++------ include/notifier.php | 87 +++++++++++++++++++++++++++++++----- include/text.php | 3 +- mod/acl.php | 2 +- version.inc | 2 +- 6 files changed, 179 insertions(+), 29 deletions(-) diff --git a/include/conversation.php b/include/conversation.php index 1d068f424..07ecf7207 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -89,8 +89,6 @@ function localize_item(&$item){ if($extracted['images']) $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']); - logger('localize'); - if (activity_match($item['verb'],ACTIVITY_LIKE) || activity_match($item['verb'],ACTIVITY_DISLIKE)){ $obj= json_decode($item['object'],true); diff --git a/include/items.php b/include/items.php index 94554252c..355f48b91 100755 --- a/include/items.php +++ b/include/items.php @@ -6,6 +6,36 @@ require_once('include/crypto.php'); require_once('include/Photo.php'); + +function collect_recipients($item) { + + if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid']) { + $allow_people = expand_acl($item['allow_cid']); + $allow_groups = expand_groups(expand_acl($item['allow_gid'])); + $deny_people = expand_acl($item['deny_cid']); + $deny_groups = expand_groups(expand_acl($item['deny_gid'])); + + $recipients = array_unique(array_merge($allow_people,$allow_groups)); + $deny = array_unique(array_merge($deny_people,$deny_groups)); + $recipients = array_diff($recipients,$deny); + } + else { + $recipients = array(); + $r = q("select * from abook where abook_channel = %d and not (abook_flags & %d) ", + intval($item['uid']), + intval(ABOOK_FLAG_SELF) + ); + if($r) { + foreach($r as $rr) { + // FIXME check permissions of each + $recipients[] = $rr['abook_xchan']; + } + } + } + return $recipients; +} + + function get_public_feed($channel,$params) { $type = 'xml'; @@ -472,6 +502,8 @@ function get_item_elements($j) { function encode_item($item) { $x = array(); + logger('encode_item: ' . print_r($item,true)); + $x['message_id'] = $item['uri']; $x['message_top'] = $item['parent_uri']; $x['message_parent'] = $item['thr_parent']; @@ -487,21 +519,54 @@ function encode_item($item) { $x['location'] = $item['location']; $x['longlat'] = $item['coord']; - $x['owner'] = array(); - $x['author'] = array(); - $x['object'] = array(); - $x['target'] = array(); - $x['attach'] = array(); + $x['owner'] = encode_item_xchan($item['owner']); + $x['author'] = encode_item_xchan($item['author']); + if($item['object']) + $x['object'] = json_decode($item['object'],true); + if($item['target']) + $x['target'] = json_decode($item['target'],true); + if($item['attach']) + $x['attach'] = json_decode($item['attach'],true); + $x['restrictions'] = array(); $x['flags'] = array(); - $x['tags'] = array(); + if($item['term']) + $x['tags'] = encode_item_terms($item['term']); return $x; } +function encode_item_xchan($xchan) { + $ret = array(); + $ret['name'] = $xchan['xchan_name']; + $ret['address'] = $xchan['xchan_addr']; + $ret['url'] = $xchan['hubloc_url']; + $ret['photo'] = array('mimetype' => $xchan['xchan_photo_mimetype'], 'src' => $xchan['xchan_photo_m']); + $ret['guid'] = $xchan['xchan_guid']; + $ret['guid_sig'] = $xchan['xchan_guid_sig']; + return $ret; +} +function encode_item_terms($terms) { + $ret = array(); + + $allowed_export_terms = array( TERM_UNKNOWN, TERM_HASHTAG, TERM_MENTION, TERM_CATEGORY ); + + if($terms) { + foreach($terms as $term) { + if(in_array($term['type'],$allowed_export_terms)) + $ret = array('tag' => $term['term'], 'url' => $term['url'], 'type' => termtype($term['type'])); + } + } + return $ret; +} + +function termtype($t) { + $types = array('unknown','hashtag','mention','category','private_category','file','search'); + return(($types[$t]) ? $types[$t] : 'unknown'); +} function get_atom_elements($feed,$item) { @@ -3838,12 +3903,21 @@ function posted_date_widget($url,$uid,$wall) { function fetch_post_tags($items) { $tag_finder = array(); - if($items && count($items)) - foreach($items as $item) - if(! in_array($item['item_id'],$tag_finder)) - $tag_finder[] = $item['item_id']; + if($items) { + foreach($items as $item) { + if(array_key_exists('item_id',$item)) { + if(! in_array($item['item_id'],$tag_finder)) + $tag_finder[] = $item['item_id']; + } + else { + if(! in_array($item['id'],$tag_finder)) + $tag_finder[] = $item['id']; + } + } + } $tag_finder_str = implode(', ', $tag_finder); + if(strlen($tag_finder_str)) { $tags = q("select * from term where oid in ( %s ) and otype = %d", dbesc($tag_finder_str), @@ -3851,13 +3925,23 @@ function fetch_post_tags($items) { ); } + for($x = 0; $x < count($items); $x ++) { if(count($tags)) { foreach($tags as $t) { - if($t['oid'] == $items[$x]['item_id']) { - if(! is_array($items[$x]['term'])) - $items[$x]['term'] = array(); - $items[$x]['term'][] = $t; + if(array_key_exists('item_id',$items[$x])) { + if($t['oid'] == $items[$x]['item_id']) { + if(! is_array($items[$x]['term'])) + $items[$x]['term'] = array(); + $items[$x]['term'][] = $t; + } + } + else { + if($t['oid'] == $items[$x]['id']) { + if(! is_array($items[$x]['term'])) + $items[$x]['term'] = array(); + $items[$x]['term'][] = $t; + } } } } diff --git a/include/notifier.php b/include/notifier.php index 06e9d8299..efe417a0f 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -96,12 +96,19 @@ function notifier_run($argv, $argc){ ); if($h) { foreach($h as $hh) { - $result = zot_notify($s[0],$hh['hubloc_callback'],'refresh',array(array( + $data = zot_build_packet($s[0],'refresh',array(array( 'guid' => $hh['hubloc_guid'], 'guid_sig' => $hh['hubloc_guid_sig'], 'url' => $hh['hubloc_url']) - )); - // should probably queue these + )); + if($data) { + $result = zot_zot($hh['hubloc_callback'],$data); + +// if(! $result['success'] +// zot_queue_item(); + + } + } } } @@ -158,19 +165,77 @@ function notifier_run($argv, $argc){ } else { - // Normal items - find ancestors + // Normal items - $r = q("SELECT * FROM `item` WHERE `id` = %d and item_restrict = 0 LIMIT 1", + // Fetch the target item + + $r = q("SELECT * FROM item WHERE id = %d and parent != 0 LIMIT 1", intval($item_id) ); - if((! $r) || (! intval($r[0]['parent']))) { + if(! $r) return; - } xchan_query($r); - + $r = fetch_post_tags($r); + $target_item = $r[0]; + + if($target_item['id'] == $target_item['parent']) { + $parent_item = $target_item; + $top_level_post = true; + } + else { + // fetch the parent item + $r = q("SELECT * from item where id = %d order by id asc", + intval($parent_id) + ); + if(! $r) + return; + xchan_query($r); + $r = fetch_post_tags($r); + + $parent_item = $r[0]; + $top_level_post = false; + } + + $relay_to_owner = (((! $top_level_post) && ($target_item['item_flags'] & ITEM_ORIGIN)) ? true : false); + if($relay_to_owner) { + logger('notifier: followup relay', LOGGER_DEBUG); + $recipients = array($parent_item['owner_xchan']); + } + else { + logger('notifier: normal distribution', LOGGER_DEBUG); + $recipients = collect_recipients($parent_item); + + // FIXME add any additional recipients such as mentions, etc. + + } + + + $encoded_item = encode_item($target_item); + + logger('notifier: encoded item: ' . print_r($encoded_item,true)); + + stringify_array_elms($recipients); + + logger('notifier: recipients: ' . print_r($recipients,true)); + + + + // get all hubs we need to talk to. + + + +return; + + + + + + + + $parent_id = intval($r[0]['parent']); $uid = $r[0]['uid']; $updated = $r[0]['edited']; @@ -290,8 +355,6 @@ function notifier_run($argv, $argc){ $public_message = false; // private recipients, not public } -// FIXME - expand_acl now takes xchan_hashes - $allow_people = expand_acl($parent['allow_cid']); $allow_groups = expand_groups(expand_acl($parent['allow_gid'])); $deny_people = expand_acl($parent['deny_cid']); @@ -304,6 +367,10 @@ function notifier_run($argv, $argc){ // proc_run('php','include/notifier.php','uplink',$item_id); // } + + + + $conversants = array(); foreach($items as $item) { diff --git a/include/text.php b/include/text.php index 63bb320ff..ee95646b7 100644 --- a/include/text.php +++ b/include/text.php @@ -1710,7 +1710,8 @@ function xchan_query(&$items) { } } if(count($arr)) { - $chans = q("select * from xchan where xchan_hash in (" . implode(',', $arr) . ")"); + $chans = q("select xchan.*,hubloc.* from xchan left join hubloc on hubloc_hash = xchan_hash + where xchan_hash in (" . implode(',', $arr) . ") and ( hubloc_flags & " . intval(HUBLOC_FLAGS_PRIMARY) . " )"); } if($items && count($items) && $chans && count($chans)) { for($x = 0; $x < count($items); $x ++) { diff --git a/mod/acl.php b/mod/acl.php index 630f1e209..0db8af45e 100644 --- a/mod/acl.php +++ b/mod/acl.php @@ -91,7 +91,7 @@ function acl_init(&$a){ if ($type=='' || $type=='g'){ - $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids + $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`xchan` SEPARATOR ',') as uids FROM `group`,`group_member` WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d AND `group_member`.`gid`=`group`.`id` diff --git a/version.inc b/version.inc index 079a2a735..3ae96a107 100644 --- a/version.inc +++ b/version.inc @@ -1 +1 @@ -2012-11-14.138 +2012-11-15.139 -- cgit v1.2.3