From 41b3c58d4174072ede9881d2861d5919e32e3280 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Thu, 4 Oct 2018 19:19:06 -0500 Subject: [PATCH 1/3] Passing calls from options page to channel new --- classes/LBRY_Admin.php | 2 +- classes/LBRY_Daemon.php | 14 +++++++++++--- templates/options_page.php | 4 +++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 162ea10..d714ae9 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -192,7 +192,7 @@ class LBRY_Admin $bid_amount = $_POST['bid_amount']; // TODO: Wrap in a try catch - LBRY()->daemon->channel_new($new_channel, $bid_amount); + $result = LBRY()->daemon->channel_new($new_channel, $bid_amount); LBRY()->notice->set_notice('success', 'Successfully added a new channel!', true); } diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index e63b0d1..4d2beb5 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -41,12 +41,12 @@ class LBRY_Daemon /** * https://lbryio.github.io/lbry/#channel_list - * @return array claim dictionary + * @return array claim dictionary or null if empty */ public function channel_list() { - $result = $this->request('channel_list'); - return null; + $result = json_decode($this->request('channel_list'))->result; + return empty($result) ? null : $result; } /** @@ -55,6 +55,14 @@ class LBRY_Daemon */ public function channel_new($channel_name, $bid_amount) { + $result = $this->request( + 'channel_new', + array( + 'channel_name' => $channel_name, + 'amount' => $bid_amount + ) + ); + error_log($result); return null; } diff --git a/templates/options_page.php b/templates/options_page.php index 70aaf15..dfaaedd 100644 --- a/templates/options_page.php +++ b/templates/options_page.php @@ -20,7 +20,9 @@ $channel_list = $LBRY->daemon->channel_list();

Your Publishable Channels

- + $value): ?> + +

Looks like you haven't added any channels yet, feel free to do so below:

From 8839003fca799fd571c3c053332436ab1bb87cfe Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Thu, 4 Oct 2018 19:43:19 -0500 Subject: [PATCH 2/3] Make new_channel call, populate channel list --- classes/LBRY_Daemon.php | 4 ++-- templates/options_page.php | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 4d2beb5..2c3e42d 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -59,11 +59,11 @@ class LBRY_Daemon 'channel_new', array( 'channel_name' => $channel_name, - 'amount' => $bid_amount + 'amount' => floatval($bid_amount) ) ); error_log($result); - return null; + return json_decode($result)->result; } /** diff --git a/templates/options_page.php b/templates/options_page.php index dfaaedd..28d947f 100644 --- a/templates/options_page.php +++ b/templates/options_page.php @@ -20,9 +20,11 @@ $channel_list = $LBRY->daemon->channel_list();

Your Publishable Channels

- $value): ?> - - +

Looks like you haven't added any channels yet, feel free to do so below:

From eebe712f2cd2261afa97833e5c85ed99d2b02ff8 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Fri, 5 Oct 2018 12:22:47 -0500 Subject: [PATCH 3/3] New channel and options page functionality --- classes/LBRY_Admin.php | 11 ++++++++--- classes/LBRY_Daemon.php | 37 +++++++++++++++++++++++++++++++------ templates/options_page.php | 3 ++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index d714ae9..8ca99b0 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -191,9 +191,14 @@ class LBRY_Admin $new_channel = $_POST['new_channel']; $bid_amount = $_POST['bid_amount']; - // TODO: Wrap in a try catch - $result = LBRY()->daemon->channel_new($new_channel, $bid_amount); - LBRY()->notice->set_notice('success', 'Successfully added a new channel!', true); + // Try to add the new channel + try { + $result = LBRY()->daemon->channel_new($new_channel, $bid_amount); + // Tell the user it takes some time to go through + LBRY()->notice->set_notice('success', 'Successfully added a new channel! Please wait a few minutes for the bid to process.', true); + } catch (\Exception $e) { + LBRY()->notice->set_notice('error', $e->getMessage(), false); + } } wp_safe_redirect($redirect_url); diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 2c3e42d..13171ca 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -24,7 +24,7 @@ class LBRY_Daemon public function wallet_unused_address() { $result = $this->request('wallet_unused_address'); - return json_decode($result)->result; + return $result->result; } /** @@ -36,7 +36,7 @@ class LBRY_Daemon public function wallet_balance() { $result = $this->request('wallet_balance'); - return json_decode($result)->result; + return $result->result; } /** @@ -45,7 +45,7 @@ class LBRY_Daemon */ public function channel_list() { - $result = json_decode($this->request('channel_list'))->result; + $result = $this->request('channel_list')->result; return empty($result) ? null : $result; } @@ -55,6 +55,20 @@ class LBRY_Daemon */ public function channel_new($channel_name, $bid_amount) { + // TODO: Sanitize channel name and bid + + // Make sure no @ sign, as we will add that + if (strpos($channel_name, '@')) { + throw new \Exception('Illegal character "@" in channel name', 1); + } + + // No white space allowed + if (strpos($channel_name, ' ')) { + throw new \Exception("No spaces allowed in channel name", 1); + } + + $channel_name = '@' . $channel_name; + $result = $this->request( 'channel_new', array( @@ -62,8 +76,8 @@ class LBRY_Daemon 'amount' => floatval($bid_amount) ) ); - error_log($result); - return json_decode($result)->result; + $this->check_for_errors($result); + return $result->result; } /** @@ -94,7 +108,18 @@ class LBRY_Daemon $result = curl_exec($ch); curl_close($ch); - return $result; + return json_decode($result); + } + + /** + * Checks for erros in decoded daemon response and throws an exception if it finds one + * @param $response + */ + private function check_for_errors($response) + { + if (property_exists($response, 'error')) { + throw new \Exception($response->error->message, $response->error->code); + } } /** diff --git a/templates/options_page.php b/templates/options_page.php index 28d947f..5a2932f 100644 --- a/templates/options_page.php +++ b/templates/options_page.php @@ -38,7 +38,8 @@ $channel_list = $LBRY->daemon->channel_list(); New Channel Name - + @ +