adds comment abandon function

This commit is contained in:
Oleg Silkin 2019-07-19 00:29:21 -04:00 committed by Lex Berezhny
parent 8138675808
commit 89c0b5dbdf
2 changed files with 54 additions and 6 deletions

View file

@ -33,7 +33,8 @@ from lbry.extras.daemon.Components import EXCHANGE_RATE_MANAGER_COMPONENT, UPNP_
from lbry.extras.daemon.ComponentManager import RequiredCondition
from lbry.extras.daemon.ComponentManager import ComponentManager
from lbry.extras.daemon.json_response_encoder import JSONResponseEncoder
from lbry.extras.daemon.comment_client import jsonrpc_post, sign_comment, is_comment_signed_by_channel
from lbry.extras.daemon.comment_client import jsonrpc_post, sign_comment, sign_abandon_comment
from lbry.extras.daemon.comment_client import is_comment_signed_by_channel
from lbry.extras.daemon.undecorated import undecorated
from lbry.wallet.transaction import Transaction, Output, Input
from lbry.wallet.account import Account as LBCAccount
@ -3491,11 +3492,45 @@ class Daemon(metaclass=JSONRPCServerType):
'channel_name': channel.claim_name,
})
sign_comment(comment_body, channel)
response = await jsonrpc_post(self.conf.comment_server, 'create_comment', **comment_body)
response = await jsonrpc_post(self.conf.comment_server, 'create_comment', comment_body)
if 'signature' in response:
response['is_claim_signature_valid'] = is_comment_signed_by_channel(response, channel)
return response
async def jsonrpc_comment_abandon(self, comment_id, channel_name=None, channel_id=None, channel_account_id=None):
""""
Delete a comment published under your channel identity
Usage:
comment_delete (<comment_id> | --comment_id=<comment_id>)
(--channel_id=<channel_id> | --channel_name=<channel_name>)
[--channel_account_id=<channel_account_id>...]
Options:
--comment_id=<comment_id> : (str) The ID of the comment to be deleted.
--channel_id=<channel_id> : (str) The ID of the channel that posted the comment.
--channel_name=<channel_name> : (str) The Name of the channel that posted the comment..
--channel_account_id=<channel_account_id> : (str) one or more account ids for accounts to look in
for channel certificates, defaults to all accounts.
Returns:
"""
abandon_comment_body = {'comment_id': comment_id}
if not channel_name and not channel_id:
chan = await jsonrpc_post(
self.conf.comment_server, 'get_channel_from_comment_id', comment_id=comment_id
)
channel_id = chan.get('channel_id')
channel_name = chan.get('channel_name')
channel = await self.get_channel_or_none(channel_account_id, channel_id, channel_name, for_signing=True)
if not channel:
raise Exception('You must own the channel to delete the comment')
abandon_comment_body.update({
'channel_id': channel.claim_id,
'channel_name': channel.claim_name,
})
sign_abandon_comment(abandon_comment_body, channel)
return await jsonrpc_post(self.conf.comment_server, 'delete_comment', abandon_comment_body)
async def broadcast_or_release(self, account, tx, blocking=False):
try:

View file

@ -41,11 +41,24 @@ def sign_comment(comment: dict, channel: Output):
pieces = [timestamp, channel.claim_hash, comment['comment'].encode()]
digest = sha256(b''.join(pieces))
signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
comment['signature'] = binascii.hexlify(signature).decode()
comment['signing_ts'] = timestamp.decode()
comment.update({
'signature': binascii.hexlify(signature).decode(),
'signing_ts': timestamp.decode()
})
async def jsonrpc_post(url: str, method: str, **params) -> any:
def sign_abandon_comment(body: dict, channel: Output):
pieces = [body['comment_id'].encode(), channel.claim_hash]
digest = sha256(b''.join(pieces))
signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
body.update({
'signature': binascii.hexlify(signature).decode()
})
async def jsonrpc_post(url: str, method: str, params: dict = None, **kwargs) -> any:
params = dict() if not params else params
params.update(kwargs)
json_body = {'jsonrpc': '2.0', 'id': None, 'method': method, 'params': params}
headers = {'Content-Type': 'application/json'}
async with utils.aiohttp_request('POST', url, json=json_body, headers=headers) as response:
@ -53,5 +66,5 @@ async def jsonrpc_post(url: str, method: str, **params) -> any:
result = await response.json()
return result['result'] if 'result' in result else result
except Exception as cte:
log.exception('Unable to decode respose from server: %s', cte)
log.exception('Unable to decode response from server: %s', cte)
return await response.text()