adds get_channel_from_id & simplifies to all call comment_list

This commit is contained in:
Oleg Silkin 2020-03-27 15:59:57 -04:00
parent cc20088b06
commit 644e5e8477

View file

@ -130,25 +130,9 @@ def clean(thing: dict) -> dict:
def get_comment(comment_id: str) -> dict: def get_comment(comment_id: str) -> dict:
try: return (comment_list(expressions=(Comment.comment_id == comment_id), page_size=1)
comment: Comment = Comment.get_by_id(comment_id) .get('items')
except DoesNotExist as e: .pop())
raise ValueError from e
else:
as_dict = model_to_dict(comment)
if comment.channel:
as_dict.update({
'channel_id': comment.channel_id,
'channel_name': comment.channel.name,
'signature': comment.signature,
'signing_ts': comment.signing_ts,
'channel_url': comment.channel.channel_url
})
if comment.parent:
as_dict.update({
'parent_id': comment.parent_id
})
return clean(as_dict)
def get_comment_ids(claim_id: str = None, parent_id: str = None, def get_comment_ids(claim_id: str = None, parent_id: str = None,
@ -173,19 +157,12 @@ def get_comments_by_id(comment_ids: typing.Union[list, tuple]) -> dict:
def get_channel_from_comment_id(comment_id: str) -> dict: def get_channel_from_comment_id(comment_id: str) -> dict:
try: results = comment_list(
comment = Comment.get_by_id(comment_id) expressions=(Comment.comment_id == comment_id),
except DoesNotExist as e: select_fields=['channel_name', 'channel_id', 'channel_url'],
raise ValueError from e page_size=1
else: )
channel = comment.channel return results['items'].pop()
if not channel:
raise ValueError('The provided comment does not belong to a channel.')
return {
'channel_name': channel.name,
'channel_id': channel.claim_id,
'channel_url': 'lbry://' + channel.name + '#' + channel.claim_id
}
if __name__ == '__main__': if __name__ == '__main__':