diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 162ea10..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 - 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 e63b0d1..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,17 +36,17 @@ class LBRY_Daemon public function wallet_balance() { $result = $this->request('wallet_balance'); - return json_decode($result)->result; + return $result->result; } /** * 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 = $this->request('channel_list')->result; + return empty($result) ? null : $result; } /** @@ -55,7 +55,29 @@ class LBRY_Daemon */ public function channel_new($channel_name, $bid_amount) { - return null; + // 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( + 'channel_name' => $channel_name, + 'amount' => floatval($bid_amount) + ) + ); + $this->check_for_errors($result); + return $result->result; } /** @@ -86,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 70aaf15..5a2932f 100644 --- a/templates/options_page.php +++ b/templates/options_page.php @@ -20,7 +20,11 @@ $channel_list = $LBRY->daemon->channel_list();
Looks like you haven't added any channels yet, feel free to do so below:
@@ -34,7 +38,8 @@ $channel_list = $LBRY->daemon->channel_list();