refactor include/follow (Connect::connect()) and update strings

This commit is contained in:
zotlabs 2019-01-21 17:31:24 -08:00
parent 1719986ba0
commit 1d3540ac6c
5 changed files with 2891 additions and 3180 deletions

View file

@ -1,16 +1,17 @@
<?php
namespace Zotlabs\Module;
use App;
use Zotlabs\Web\Controller;
use Zotlabs\Lib\Libsync;
use Zotlabs\Lib\ActivityStreams;
use Zotlabs\Lib\Activity;
use Zotlabs\Web\HTTPSignatures;
use Zotlabs\Lib\LDSignatures;
use Zotlabs\Lib\Connect;
use Zotlabs\Daemon\Master;
require_once('include/follow.php');
class Follow extends \Zotlabs\Web\Controller {
class Follow extends Controller {
function init() {
@ -68,9 +69,9 @@ class Follow extends \Zotlabs\Web\Controller {
$return_url = $_SESSION['return_url'];
$confirm = intval($_REQUEST['confirm']);
$interactive = (($_REQUEST['interactive']) ? intval($_REQUEST['interactive']) : 1);
$channel = \App::get_channel();
$channel = App::get_channel();
$result = new_contact($uid,$url,$channel,$interactive,$confirm);
$result = Connect::connect($channel,$url);
if($result['success'] == false) {
if($result['message'])
@ -99,17 +100,17 @@ class Follow extends \Zotlabs\Web\Controller {
if($abconfig)
$clone['abconfig'] = $abconfig;
Libsync::build_sync_packet(0 /* use the current local_channel */, array('abook' => array($clone)), true);
Libsync::build_sync_packet(0, [ 'abook' => [ $clone ] ], true);
$can_view_stream = their_perms_contains($channel['channel_id'],$clone['abook_xchan'],'view_stream');
// If we can view their stream, pull in some posts
if(($can_view_stream) || ($result['abook']['xchan_network'] === 'rss'))
\Zotlabs\Daemon\Master::Summon(array('Onepoll',$result['abook']['abook_id']));
Master::Summon([ 'Onepoll', $result['abook']['abook_id'] ]);
if($interactive) {
goaway(z_root() . '/connedit/' . $result['abook']['abook_id'] . '?f=&follow=1');
goaway(z_root() . '/connedit/' . $result['abook']['abook_id'] . '?follow=1');
}
else {
json_return_and_die([ 'success' => true ]);

View file

@ -8,6 +8,7 @@ use Zotlabs\Lib\Libzot;
use Zotlabs\Lib\Libsync;
use Zotlabs\Lib\Group;
use Zotlabs\Lib\Crypto;
use Zotlabs\Lib\Connect;
use Zotlabs\Access\PermissionRoles;
use Zotlabs\Access\PermissionLimits;
use Zotlabs\Access\Permissions;
@ -472,13 +473,39 @@ function create_identity($arr) {
$accts = get_config('system','auto_follow');
if(($accts) && (! $total_identities)) {
require_once('include/follow.php');
if(! is_array($accts))
$accts = array($accts);
foreach($accts as $acct) {
if(trim($acct))
new_contact($newuid,trim($acct),$ret['channel'],false);
if(trim($acct)) {
$f = Connect::connect($ret['channel'],trim($acct));
if($f['success']) {
$clone = [];
foreach($f['abook'] as $k => $v) {
if(strpos($k,'abook_') === 0) {
$clone[$k] = $v;
}
}
unset($clone['abook_id']);
unset($clone['abook_account']);
unset($clone['abook_channel']);
$abconfig = load_abconfig($ret['channel']['channel_id'],$clone['abook_xchan']);
if($abconfig) {
$clone['abconfig'] = $abconfig;
}
Libsync::build_sync_packet(0, [ 'abook' => [ $clone ] ], true);
$can_view_stream = their_perms_contains($ret['channel']['channel_id'],$clone['abook_xchan'],'view_stream');
// If we can view their stream, pull in some posts
if(($can_view_stream) || ($f['abook']['xchan_network'] === 'rss')) {
Master::Summon([ 'Onepoll',$f['abook']['abook_id'] ]);
}
}
}
}
}

View file

@ -1,260 +0,0 @@
<?php /** @file */
use Zotlabs\Lib\Libzot;
use Zotlabs\Lib\Group;
use Zotlabs\Access\Permissions;
use Zotlabs\Daemon\Master;
//
// Takes a $uid and the channel associated with the uid, and a url/handle and adds a new channel
// Returns an array
// $return['success'] boolean true if successful
// $return['abook'] Address book entry joined with xchan if successful
// $return['message'] error text if success is false.
function new_contact($uid,$url,$channel,$interactive = false, $confirm = false) {
$result = [ 'success' => false, 'message' => '' ];
$my_perms = false;
$is_zot = false;
$protocol = '';
if(substr($url,0,1) === '[') {
$x = strpos($url,']');
if($x) {
$protocol = substr($url,1,$x-1);
$url = substr($url,$x+1);
}
}
// This turned out to have issues on an activitypub project (forgot which at the moment) that requires a trailing slash.
// $url = rtrim($url,'/');
if(! allowed_url($url)) {
$result['message'] = t('Channel is blocked on this site.');
return $result;
}
if(! $url) {
$result['message'] = t('Channel location missing.');
return $result;
}
// check service class limits
$r = q("select count(*) as total from abook where abook_channel = %d and abook_self = 0 ",
intval($uid)
);
if($r)
$total_channels = $r[0]['total'];
if(! service_class_allows($uid,'total_channels',$total_channels)) {
$result['message'] = upgrade_message();
return $result;
}
$xchan_hash = '';
$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 ",
dbesc($url),
dbesc($url),
dbesc($url)
);
if($r) {
// reset results to the best record or the first if we don't have the best
// note: this is a single record and not an array of results
$r = Libzot::zot_record_preferred($r,'xchan_network');
}
$singleton = false;
$d = false;
if(! $r) {
// try RSS discovery
$wf = discover_by_webbie($url,$protocol);
if(! $wf) {
$feeds = get_config('system','feed_contacts');
if(($feeds) && (in_array($protocol, [ '', 'feed', 'rss' ]))) {
$d = discover_feed($url);
}
else {
$result['message'] = t('Remote channel or protocol unavailable.');
return $result;
}
}
}
if($wf || $d) {
// find the record which was just created.
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s' $sql_options",
dbesc(($wf) ? $wf : $url),
dbesc($url),
dbesc($url)
);
// convert to a single record (once again preferring a zot solution in the case of multiples)
if($r) {
$r = Libzot::zot_record_preferred($r,'xchan_network');
}
}
// if discovery was a success we should have an xchan record in $r
if($r) {
$xchan = $r;
$xchan_hash = $r['xchan_hash'];
$their_perms = EMPTY_STR;
}
if(! $xchan_hash) {
$result['message'] = t('Channel discovery failed.');
logger('follow: ' . $result['message']);
return $result;
}
if($r['xchan_network'] === 'activitypub') {
$singleton = 1;
if(defined('NOMADIC')) {
$result['message'] = t('Protocol not supported');
return $result;
}
}
$aid = $channel['channel_account_id'];
$hash = $channel['channel_hash'];
$default_group = $channel['channel_default_group'];
if($hash == $xchan_hash) {
$result['message'] = t('Cannot connect to yourself.');
return $result;
}
if($xchan['xchan_network'] === 'rss') {
// check service class feed limits
$t = q("select count(*) as total from abook where abook_account = %d and abook_feed = 1 ",
intval($aid)
);
if($t)
$total_feeds = $t[0]['total'];
if(! service_class_allows($uid,'total_feeds',$total_feeds)) {
$result['message'] = upgrade_message();
return $result;
}
// Always set these "remote" permissions for feeds since we cannot interact with them
// to negotiate a suitable permission response
$p = get_abconfig($uid,$xchan_hash,'system','their_perms',EMPTY_STR);
if($p)
$p .= ',';
$p .= 'view_stream,republish';
set_abconfig($uid,$xchan_hash,'system','their_perms',$p);
}
$p = Permissions::connect_perms($uid);
$my_perms = Permissions::serialise($p['perms']);
$profile_assign = get_pconfig($uid,'system','profile_assign','');
$r = q("select abook_id, abook_xchan, abook_pending, abook_instance from abook
where abook_xchan = '%s' and abook_channel = %d limit 1",
dbesc($xchan_hash),
intval($uid)
);
if($r) {
$abook_instance = $r[0]['abook_instance'];
if(($singleton) && strpos($abook_instance,z_root()) === false) {
if($abook_instance)
$abook_instance .= ',';
$abook_instance .= z_root();
$x = q("update abook set abook_instance = '%s', abook_not_here = 0 where abook_id = %d",
dbesc($abook_instance),
intval($r[0]['abook_id'])
);
}
if(intval($r[0]['abook_pending'])) {
$x = q("update abook set abook_pending = 0 where abook_id = %d",
intval($r[0]['abook_id'])
);
}
}
else {
$closeness = get_pconfig($uid,'system','new_abook_closeness',80);
$r = abook_store_lowlevel(
[
'abook_account' => intval($aid),
'abook_channel' => intval($uid),
'abook_closeness' => intval($closeness),
'abook_xchan' => $xchan_hash,
'abook_profile' => $profile_assign,
'abook_feed' => intval(($xchan['xchan_network'] === 'rss') ? 1 : 0),
'abook_created' => datetime_convert(),
'abook_updated' => datetime_convert(),
'abook_instance' => (($singleton) ? z_root() : '')
]
);
}
if(! $r)
logger('abook creation failed');
if($my_perms) {
set_abconfig($uid,$xchan_hash,'system','my_perms',$my_perms);
}
$r = q("select abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash
where abook_xchan = '%s' and abook_channel = %d limit 1",
dbesc($xchan_hash),
intval($uid)
);
if($r) {
$result['abook'] = $r[0];
Master::Summon([ 'Notifier', 'permissions_create', $result['abook']['abook_id'] ]);
}
$arr = [ 'channel_id' => $uid, 'channel' => $channel, 'abook' => $result['abook'] ];
call_hooks('follow', $arr);
/** If there is a default group for this channel, add this connection to it */
if($default_group) {
$g = Group::rec_byhash($uid,$default_group);
if($g) {
Group::member_add($uid,'',$xchan_hash,$g['id']);
}
}
$result['success'] = true;
return $result;
}

View file

@ -4,6 +4,8 @@
// connect utility
use Zotlabs\Lib\Libsync;
use Zotlabs\Lib\Connect;
use Zotlabs\Daemon\Master;
if(! file_exists('include/cli_startup.php')) {
echo t('Run from the top level $Projectname web directory, as util/connect <args>') . PHP_EOL;
@ -11,7 +13,6 @@ if(! file_exists('include/cli_startup.php')) {
}
require_once('include/cli_startup.php');
require_once('include/follow.php');
cli_startup();
@ -33,7 +34,7 @@ cli_startup();
exit(1);
}
$result = new_contact($c['channel_id'],$argv[2],$c,false,false);
$result = Connect::connect($c,$argv[2]);
if($result['success'] == false) {
echo $result['message'];
@ -59,8 +60,8 @@ cli_startup();
// If we can view their stream, pull in some posts
if(($can_view_stream) || ($result['abook']['xchan_network'] === 'rss'))
\Zotlabs\Daemon\Master::Summon(array('Onepoll',$result['abook']['abook_id']));
if(($can_view_stream) || ($result['abook']['xchan_network'] === 'rss')) {
Master::Summon([ 'Onepoll',$result['abook']['abook_id'] ]);
}
exit(0);

File diff suppressed because it is too large Load diff