Merged in daemon-channel-calls (pull request #2)

Daemon channel calls
This commit is contained in:
Paul Kirby 2018-10-05 17:57:31 +00:00
commit 8afabfc7e8
3 changed files with 55 additions and 12 deletions

View file

@ -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);

View file

@ -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);
}
}
/**

View file

@ -20,7 +20,11 @@ $channel_list = $LBRY->daemon->channel_list();
<h2>Your Publishable Channels</h2>
<?php if ($channel_list): ?>
<ul class="lbry-channel-list">
<?php foreach ($channel_list as $channel): ?>
<li><?= $channel->name ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>Looks like you haven't added any channels yet, feel free to do so below:</p>
<?php endif; ?>
@ -34,7 +38,8 @@ $channel_list = $LBRY->daemon->channel_list();
<tr>
<th scope="row">New Channel Name</th>
<td>
<input type="text" name="new_channel" value="" placeholder="Your New Channel" required>
<span>@</span>
<input type="text" name="new_channel" value="" placeholder="your-new-channel" required>
</td>
</tr>
<tr>