From 1b0390af0659e0c5534acbee97db1ef3c1e65288 Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 13 Oct 2014 15:27:03 -0700 Subject: new function hubloc_change_primary() --- include/hubloc.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'include/hubloc.php') diff --git a/include/hubloc.php b/include/hubloc.php index fded434d2..89ec6231d 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -113,4 +113,46 @@ function remove_obsolete_hublocs() { } +function hubloc_change_primary($hubloc) { + + if(! is_array($hubloc)) + return false; + if(! $hubloc['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY) + return false; + + // See if there's a local channel + + $r = q("select channel_id, channel_primary from channel where channel_hash = '%s' limit 1", + dbesc($hubloc['hubloc_hash']) + ); + if(($r) && (! $r[0]['channel_primary'])) { + q("update channel set channel_primary = 1 where channel_id = %d limit 1", + intval($r[0]['channel_id']) + ); + } + + // do we even have an xchan for this hubloc and if so is it already set as primary? + + $r = q("select * from xchan where xchan_hash = '%s' limit 1", + dbesc($hubloc['hubloc_hash']) + ); + if(! $r) + return false; + if($r[0]['xchan_addr'] === $hubloc['hubloc_addr']) + return false; + + $url = $hubloc['hubloc_url']; + $lwebbie = substr($hubloc['hubloc_addr'],0,strpos($hubloc['hubloc_addr'],'@')); + + $r = q("update xchan set xchan_addr, xchan_url = '%s', xchan_follow = '%s', xchan_connurl = '%s', where xchan_hash = '%s' limit 1", + dbesc($hubloc['hubloc_addr']), + dbesc($url . '/channel/' . $lwebbie), + dbesc($url . '/follow?f=&url=%s'), + dbesc($url . '/poco/' . $lwebbie), + dbesc($hubloc['hubloc_hash']) + ); + + return true; + +} \ No newline at end of file -- cgit v1.2.3 From 6503b911316d9573ba06b90fe45d8757488d464e Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 13 Oct 2014 16:12:32 -0700 Subject: sql --- include/hubloc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/hubloc.php') diff --git a/include/hubloc.php b/include/hubloc.php index 89ec6231d..1906833c5 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -144,7 +144,7 @@ function hubloc_change_primary($hubloc) { $url = $hubloc['hubloc_url']; $lwebbie = substr($hubloc['hubloc_addr'],0,strpos($hubloc['hubloc_addr'],'@')); - $r = q("update xchan set xchan_addr, xchan_url = '%s', xchan_follow = '%s', xchan_connurl = '%s', where xchan_hash = '%s' limit 1", + $r = q("update xchan set xchan_addr = '%s', xchan_url = '%s', xchan_follow = '%s', xchan_connurl = '%s' where xchan_hash = '%s' limit 1", dbesc($hubloc['hubloc_addr']), dbesc($url . '/channel/' . $lwebbie), dbesc($url . '/follow?f=&url=%s'), -- cgit v1.2.3 From 8a907a789f080ccae45e5b44d0bc43ed65477e07 Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 13 Oct 2014 17:59:03 -0700 Subject: more diagnostic when changing primary --- include/hubloc.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'include/hubloc.php') diff --git a/include/hubloc.php b/include/hubloc.php index 1906833c5..735aad432 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -115,10 +115,16 @@ function remove_obsolete_hublocs() { function hubloc_change_primary($hubloc) { - if(! is_array($hubloc)) + if(! is_array($hubloc)) { + logger('no hubloc'); return false; - if(! $hubloc['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY) + } + if(! ($hubloc['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY)) { + logger('not primary: ' . $hubloc['hubloc_url']); return false; + } + + logger('setting primary: ' . $hubloc['hubloc_url']); // See if there's a local channel @@ -136,10 +142,14 @@ function hubloc_change_primary($hubloc) { $r = q("select * from xchan where xchan_hash = '%s' limit 1", dbesc($hubloc['hubloc_hash']) ); - if(! $r) + if(! $r) { + logger('xchan not found'); return false; - if($r[0]['xchan_addr'] === $hubloc['hubloc_addr']) + } + if($r[0]['xchan_addr'] === $hubloc['hubloc_addr']) { + logger('xchan already changed'); return false; + } $url = $hubloc['hubloc_url']; $lwebbie = substr($hubloc['hubloc_addr'],0,strpos($hubloc['hubloc_addr'],'@')); @@ -151,7 +161,10 @@ function hubloc_change_primary($hubloc) { dbesc($url . '/poco/' . $lwebbie), dbesc($hubloc['hubloc_hash']) ); + if(! $r) + logger('xchan_update failed.'); + logger('primary hubloc changed.' . print_r($hubloc,true),LOGGER_DEBUG); return true; } -- cgit v1.2.3 From 0057612a9f9ecb49c2bd69e550827e2304c85204 Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 13 Oct 2014 21:38:36 -0700 Subject: figuring out how to bootstrap the change_primary procedure when all you have is inconsistent data which you think you trust. --- include/hubloc.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include/hubloc.php') diff --git a/include/hubloc.php b/include/hubloc.php index 735aad432..04c29315a 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -113,6 +113,8 @@ function remove_obsolete_hublocs() { } +// This actually changes other structures to match the given (presumably current) hubloc primary selection + function hubloc_change_primary($hubloc) { if(! is_array($hubloc)) { -- cgit v1.2.3 From 241bb3b94059ac4edfcc3b66e907dbf493dd8b4a Mon Sep 17 00:00:00 2001 From: friendica Date: Thu, 23 Oct 2014 19:33:47 -0700 Subject: API: xchan get/create --- include/hubloc.php | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) (limited to 'include/hubloc.php') diff --git a/include/hubloc.php b/include/hubloc.php index 04c29315a..df817329f 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -170,4 +170,82 @@ function hubloc_change_primary($hubloc) { return true; } - \ No newline at end of file + + +function xchan_store($arr) { + + if(! $arr['hash']) + $arr['hash'] = $arr['guid']; + if(! $arr['hash']) + return false; + + $r = q("select * from xchan where xchan_hash = '%s' limit 1", + dbesc($arr['hash']) + ); + if($r) + return true; + + if(! $arr['network']) + $arr['network'] = 'unknown'; + if(! $arr['name']) + $arr['name'] = 'unknown'; + if(! $arr['url']) + $arr['url'] = z_root(); + if(! $arr['photo']) + $arr['photo'] = get_default_profile_photo(); + + $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_guid_sig, xchan_pubkey, xchan_addr, xchan_url, xchan_connurl, xchan_follow, xchan_connpage, xchan_name, xchan_network, xchan_instance_url, xchan_flags, xchan_name_date ) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s',%d,'%s') ", + dbesc($arr['hash']), + dbesc($arr['guid']), + dbesc($arr['guid_sig']), + dbesc($arr['pubkey']), + dbesc($arr['address']), + dbesc($arr['url']), + dbesc($arr['connurl']), + dbesc($arr['follow']), + dbesc($arr['connpage']), + dbesc($arr['name']), + dbesc($arr['network']), + dbesc($arr['instance_url']), + intval($arr['flags']), + dbesc(datetime_convert()) + ); + if(! $r) + return $r; + + $photos = import_profile_photo($arr['photo'],$arr['hash']); + $r = q("update xchan set xchan_photo_date = '%s', xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s' where xchan_hash = '%s' limit 1", + dbesc(datetime_convert()), + dbesc($photos[0]), + dbesc($photos[1]), + dbesc($photos[2]), + dbesc($photos[3]), + dbesc($arr['hash']) + ); + return $r; + +} + + +function xchan_fetch($arr) { + + $key = ''; + if($arr['hash']) { + $key = 'xchan_hash'; + $v = $arr['hash']; + } + elseif($arr['guid']) { + $key = 'xchan_guid'; + $v = $arr['guid']; + } + elseif($arr['address']) { + $key = 'xchan_addr'; + $v = $arr['address']; + } + + if(! $key) + return false; + + $r = q("select * from xchan where $key = '$v'"); + return $r; +} \ No newline at end of file -- cgit v1.2.3 From f524fb1f20ec3edb0cf2c486d41f6bb65a60db6a Mon Sep 17 00:00:00 2001 From: friendica Date: Thu, 23 Oct 2014 19:41:42 -0700 Subject: generalise the output format of xchan_fetch so it matches the input format --- include/hubloc.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'include/hubloc.php') diff --git a/include/hubloc.php b/include/hubloc.php index df817329f..0a1b51331 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -247,5 +247,15 @@ function xchan_fetch($arr) { return false; $r = q("select * from xchan where $key = '$v'"); - return $r; + if(! $r) + return false; + + $ret = array(); + foreach($r as $k => $v) { + if($k === 'xchan_addr') + $ret['address'] = $v; + else + $ret[str_replace('xchan_','',$k)] = $v; + } + return $ret; } \ No newline at end of file -- cgit v1.2.3