refactor include/follow

This commit is contained in:
zotlabs 2018-09-15 16:13:44 -07:00
parent d67d26564d
commit f58c244ba0
2 changed files with 41 additions and 130 deletions

View file

@ -22,7 +22,6 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
$is_zot = false; $is_zot = false;
$protocol = ''; $protocol = '';
if(substr($url,0,1) === '[') { if(substr($url,0,1) === '[') {
$x = strpos($url,']'); $x = strpos($url,']');
if($x) { if($x) {
@ -31,8 +30,6 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
} }
} }
$is_http = ((strpos($url,'://') !== false) ? true : false);
$url = rtrim($url,'/'); $url = rtrim($url,'/');
if(! allowed_url($url)) { if(! allowed_url($url)) {
@ -45,7 +42,6 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
return $result; return $result;
} }
// check service class limits // check service class limits
$r = q("select count(*) as total from abook where abook_channel = %d and abook_self = 0 ", $r = q("select count(*) as total from abook where abook_channel = %d and abook_self = 0 ",
@ -59,86 +55,28 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
return $result; return $result;
} }
$arr = array('url' => $url, 'protocol' => $protocol, 'channel' => array());
call_hooks('follow_init', $arr);
if($arr['channel']['success'])
$ret = $arr['channel'];
else {
$href = \Zotlabs\Lib\Webfinger::zot_url(punify($url));
if($href) {
$zf = \Zotlabs\Lib\Zotfinger::exec($href,$channel);
}
if(is_array($zf) && array_path_exists('signature/signer',$zf) && $zf['signature']['signer'] === $href
&& intval($zf['signature']['header_valid']) && array_path_exists('data/permissions',$zf)) {
$x = Libzot::import_xchan($zf['data']);
$j = $zf['data'];
$is_zot = true;
}
}
$p = \Zotlabs\Access\Permissions::connect_perms($uid);
$my_perms = \Zotlabs\Access\Permissions::serialise($p['perms']);
if($x) {
// Premium channel, set confirm before callback to avoid recursion
// if(array_key_exists('connect_url',$j) && (! $confirm)) {
// if($interactive) {
// goaway(zid($j['connect_url']));
// }
// else {
// $result['message'] = t('Premium channel - please visit:') . ' ' . zid($j['connect_url']);
// logger('mod_follow: ' . $result['message']);
// return $result;
// }
// }
if(array_key_exists('deleted',$j) && intval($j['deleted'])) {
$result['message'] = t('Channel was deleted and no longer exists.');
return $result;
}
if(! $x['success'])
return $x;
$xchan_hash = $x['hash'];
$permissions = $j['permissions'];
set_abconfig($channel['channel_uid'],$xchan_hash,'system','their_perms',$j['permissions']);
}
else {
$xchan_hash = ''; $xchan_hash = '';
$sql_options = (($protocol) ? " and xchan_network = '" . dbesc($protocol) . "' " : ''); $sql_options = (($protocol) ? " and xchan_network = '" . dbesc($protocol) . "' " : '');
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s' $sql_options limit 1",
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' $sql_options limit 1", dbesc($url),
dbesc($url), dbesc($url),
dbesc($url) dbesc($url)
); );
$singleton = false;
$d = false;
if(! $r) { if(! $r) {
// attempt network auto-discovery // try RSS discovery
$wf = discover_by_webbie($url,$protocol); $wf = discover_by_webbie($url,$protocol);
if((! $wf) && ($is_http)) {
// try RSS discovery
$feeds = get_config('system','feed_contacts'); $feeds = get_config('system','feed_contacts');
if(($feeds) && ($protocol === '' || $protocol === 'feed' || $protocol === 'rss')) { if(($feeds) && ($protocol === '' || $protocol === 'feed' || $protocol === 'rss')) {
$d = discover_by_url($url); $d = discover_feed($url);
} }
else { else {
$result['message'] = t('Remote channel or protocol unavailable.'); $result['message'] = t('Remote channel or protocol unavailable.');
@ -147,12 +85,12 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
} }
if($wf || $d) { if($wf || $d) {
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' limit 1", $r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s' limit 1",
dbesc(($wf) ? $wf : $url), dbesc(($wf) ? $wf : $url),
dbesc($url),
dbesc($url) dbesc($url)
); );
} }
}
// if discovery was a success we should have an xchan record in $r // if discovery was a success we should have an xchan record in $r
@ -161,7 +99,6 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
$xchan_hash = $r[0]['xchan_hash']; $xchan_hash = $r[0]['xchan_hash'];
$their_perms = EMPTY_STR; $their_perms = EMPTY_STR;
} }
}
if(! $xchan_hash) { if(! $xchan_hash) {
$result['message'] = t('Channel discovery failed.'); $result['message'] = t('Channel discovery failed.');
@ -169,19 +106,6 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
return $result; return $result;
} }
$allowed = (($is_zot || $r[0]['xchan_network'] === 'rss') ? 1 : 0);
$x = array('channel_id' => $uid, 'follow_address' => $url, 'xchan' => $r[0], 'allowed' => $allowed, 'singleton' => 0);
call_hooks('follow_allow',$x);
if(! $x['allowed']) {
$result['message'] = t('Protocol disabled.');
return $result;
}
$singleton = intval($x['singleton']);
$aid = $channel['channel_account_id']; $aid = $channel['channel_account_id'];
$hash = $channel['channel_hash']; $hash = $channel['channel_hash'];
$default_group = $channel['channel_default_group']; $default_group = $channel['channel_default_group'];
@ -217,6 +141,9 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
} }
$p = \Zotlabs\Access\Permissions::connect_perms($uid);
$my_perms = \Zotlabs\Access\Permissions::serialise($p['perms']);
$profile_assign = get_pconfig($uid,'system','profile_assign',''); $profile_assign = get_pconfig($uid,'system','profile_assign','');
@ -268,7 +195,7 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
} }
if(! $r) if(! $r)
logger('mod_follow: abook creation failed'); logger('abook creation failed');
if($my_perms) { if($my_perms) {
set_abconfig($uid,$xchan_hash,'system','my_perms',$my_perms); set_abconfig($uid,$xchan_hash,'system','my_perms',$my_perms);
@ -282,20 +209,20 @@ function new_contact($uid,$url,$channel,$interactive = false, $confirm = false)
if($r) { if($r) {
$result['abook'] = $r[0]; $result['abook'] = $r[0];
Zotlabs\Daemon\Master::Summon(array('Notifier', 'permissions_create', $result['abook']['abook_id'])); Zotlabs\Daemon\Master::Summon([ 'Notifier', 'permissions_create', $result['abook']['abook_id'] ]);
} }
$arr = array('channel_id' => $uid, 'channel' => $channel, 'abook' => $result['abook']); $arr = [ 'channel_id' => $uid, 'channel' => $channel, 'abook' => $result['abook'] ];
call_hooks('follow', $arr); call_hooks('follow', $arr);
/** If there is a default group for this channel, add this connection to it */ /** If there is a default group for this channel, add this connection to it */
if($default_group) { if($default_group) {
$g = Group::rec_byhash($uid,$default_group); $g = Group::rec_byhash($uid,$default_group);
if($g) if($g) {
Group_member_add($uid,'',$xchan_hash,$g['id']); Group::member_add($uid,'',$xchan_hash,$g['id']);
}
} }
$result['success'] = true; $result['success'] = true;

View file

@ -1005,7 +1005,7 @@ function email_header_encode($in_str, $charset = 'UTF-8') {
* *
* @return boolean * @return boolean
*/ */
function discover_by_url($url, $arr = null) { function discover_feed($url, $arr = null) {
$x = scrape_feed($url); $x = scrape_feed($url);
if(! $x) { if(! $x) {
@ -1192,23 +1192,7 @@ function discover_by_webbie($webbie, $protocol = '') {
$x = Libzot::import_xchan($record['data']); $x = Libzot::import_xchan($record['data']);
if($x['success']) { if($x['success']) {
return true; return $x['hash'];
}
}
if($link['rel'] === PROTOCOL_ZOT && ((! $protocol) || (strtolower($protocol) === 'zot'))) {
logger('zot found for ' . $webbie, LOGGER_DEBUG);
if(array_key_exists('zot',$x) && $x['zot']['success']) {
$i = import_xchan($x['zot']);
return true;
}
else {
$z = z_fetch_url($link['href']);
if($z['success']) {
$j = json_decode($z['body'],true);
$i = import_xchan($j);
return true;
}
} }
} }
} }