react list takes comment_ids

This commit is contained in:
jessop 2020-09-26 15:21:26 -04:00
parent 7b666efcf8
commit d6ee6446dd
2 changed files with 67 additions and 40 deletions

View file

@ -5273,13 +5273,13 @@ class Daemon(metaclass=JSONRPCServerType):
@requires(WALLET_COMPONENT) @requires(WALLET_COMPONENT)
async def jsonrpc_comment_react( async def jsonrpc_comment_react(
self, comment_id, channel_name=None, channel_id=None, self, comment_ids, channel_name=None, channel_id=None,
channel_account_id=None, remove=False, clear_types=None, react_type=None, wallet_id=None): channel_account_id=None, remove=False, clear_types=None, react_type=None, wallet_id=None):
""" """
Create and associate a reaction emoji with a comment using your channel identity. Create and associate a reaction emoji with a comment using your channel identity.
Usage: Usage:
comment_react (<comment_id> | --comment_id=<comment_id>) comment_react (--comment_ids=<comment_ids>)
(--channel_id=<channel_id>) (--channel_id=<channel_id>)
(--channel_name=<channel_name>) (--channel_name=<channel_name>)
(--react_type=<react_type>) (--react_type=<react_type>)
@ -5287,7 +5287,7 @@ class Daemon(metaclass=JSONRPCServerType):
[--channel_account_id=<channel_account_id>...] [--wallet_id=<wallet_id>] [--channel_account_id=<channel_account_id>...] [--wallet_id=<wallet_id>]
Options: Options:
--comment_id=<comment_id> : (str) The comment id reacted to --comment_ids=<comment_ids> : (str) one or more comment id reacted to, comma delimited
--channel_id=<claim_id> : (str) The ID of channel reacting --channel_id=<claim_id> : (str) The ID of channel reacting
--channel_name=<claim_name> : (str) The name of the channel reacting --channel_name=<claim_name> : (str) The name of the channel reacting
--wallet_id=<wallet_id> : (str) restrict operation to specific wallet --wallet_id=<wallet_id> : (str) restrict operation to specific wallet
@ -5312,7 +5312,7 @@ class Daemon(metaclass=JSONRPCServerType):
) )
react_body = { react_body = {
'comment_ids': comment_id, 'comment_ids': comment_ids,
'channel_id': channel_id, 'channel_id': channel_id,
'channel_name': channel.claim_name, 'channel_name': channel.claim_name,
'type': react_type, 'type': react_type,
@ -5327,19 +5327,19 @@ class Daemon(metaclass=JSONRPCServerType):
@requires(WALLET_COMPONENT) @requires(WALLET_COMPONENT)
async def jsonrpc_comment_react_list( async def jsonrpc_comment_react_list(
self, comment_id, channel_name=None, channel_id=None, self, comment_ids, channel_name=None, channel_id=None,
channel_account_id=None, react_types=None, wallet_id=None): channel_account_id=None, react_types=None, wallet_id=None):
""" """
List reactions emoji with a claim using your channel identity. List reactions emoji with a claim using your channel identity.
Usage: Usage:
comment_react_list (<comment_id> | --comment_id=<comment_id>) comment_react_list (--comment_ids=<comment_ids>)
(--channel_id=<channel_id>) (--channel_id=<channel_id>)
(--channel_name=<channel_name>) (--channel_name=<channel_name>)
[--react_types=<react_types>] [--react_types=<react_types>]
Options: Options:
--comment_id=<comment_id> : (str) The comment id reacted to --comment_ids=<comment_ids> : (str) The comment ids reacted to, comma delimited
--channel_id=<claim_id> : (str) The ID of channel reacting --channel_id=<claim_id> : (str) The ID of channel reacting
--channel_name=<claim_name> : (str) The name of the channel reacting --channel_name=<claim_name> : (str) The name of the channel reacting
--wallet_id=<wallet_id> : (str) restrict operation to specific wallet --wallet_id=<wallet_id> : (str) restrict operation to specific wallet
@ -5368,7 +5368,7 @@ class Daemon(metaclass=JSONRPCServerType):
) )
react_list_body = { react_list_body = {
'comment_ids': comment_id, 'comment_ids': comment_ids,
'channel_id': channel_id, 'channel_id': channel_id,
'channel_name': channel.claim_name, 'channel_name': channel.claim_name,
} }

View file

@ -1,5 +1,4 @@
import re import re
import time import time
import typing import typing
from math import ceil from math import ceil
@ -183,7 +182,6 @@ class MockedCommentServer:
c_id = item.strip() c_id = item.strip()
reacts_for_comment_id = [r for r in list(self.reacts.values()) if r['comment_id'] == c_id] reacts_for_comment_id = [r for r in list(self.reacts.values()) if r['comment_id'] == c_id]
channels_reacts_for_comment_id = [r for r in reacts_for_comment_id if r['channel_id'] == channel_id] channels_reacts_for_comment_id = [r for r in reacts_for_comment_id if r['channel_id'] == channel_id]
print('channels_reacts', channels_reacts_for_comment_id)
if remove: if remove:
matching_react = None matching_react = None
for reaction in channels_reacts_for_comment_id: for reaction in channels_reacts_for_comment_id:
@ -209,32 +207,44 @@ class MockedCommentServer:
) )
self.reacts[self.react_id] = react self.reacts[self.react_id] = react
self.react_id += 1 self.react_id += 1
return self.clean(react) self.clean(react)
return True
def list_reacts(self, comment_ids, channel_id, channel_name, types=None, **kwargs): def list_reacts(self, comment_ids, channel_id, channel_name, types=None, **kwargs):
all_types = list(set([r['reaction_type'] for r in list(self.reacts.values())])) all_types = list(set([r['reaction_type'] for r in list(self.reacts.values())]))
# better test would support multiple comment_ids comment_id_list = comment_ids.split(',')
reacts_for_comment = list(filter(lambda c: c['comment_id'] == comment_ids, list(self.reacts.values()))) # _reacts: {'a1': {'like': 0, 'dislike': 0}, 'a2': {'like': 0, 'dislike': 0}}
if types: own_reacts = {}
reacts_for_comment = list(filter(lambda c: c['reaction_type'] in types.split(','), reacts_for_comment)) other_reacts = {}
own_reacts_for_comment = list(filter(lambda c: c['channel_id'] == channel_id, reacts_for_comment))
other_reacts_for_comment = list(filter(lambda c: c['channel_id'] != channel_id, reacts_for_comment)) # for each comment_id
own_counts = dict.fromkeys(all_types, 0) # add comment_id: {} to own_reacts and other_reacts
other_counts = dict.copy(own_counts) # for each react in own_reacts
if own_reacts_for_comment: # for each react in other_reacts
for react in own_reacts_for_comment: for cid in comment_id_list:
own_counts[react['reaction_type']] += 1 own_reacts[cid] = {}
if other_reacts_for_comment: other_reacts[cid] = {}
for react in other_reacts_for_comment:
other_counts[react['reaction_type']] += 1 for r_type in all_types:
own_reacts[cid][r_type] = 0
other_reacts[cid][r_type] = 0
# process that comment ids reactions for own and other categories
reacts_for_comment = list(filter(lambda c: c['comment_id'] == cid, list(self.reacts.values())))
if types:
reacts_for_comment = list(filter(lambda c: c['reaction_type'] in types.split(','), reacts_for_comment))
own_reacts_for_comment = list(filter(lambda c: c['channel_id'] == channel_id, reacts_for_comment))
other_reacts_for_comment = list(filter(lambda c: c['channel_id'] != channel_id, reacts_for_comment))
if own_reacts_for_comment:
for react in own_reacts_for_comment:
own_reacts[cid][react['reaction_type']] += 1
if other_reacts_for_comment:
for react in other_reacts_for_comment:
other_reacts[cid][react['reaction_type']] += 1
return { return {
'my_reactions': { 'my_reactions': own_reacts,
comment_ids: own_counts, 'others_reactions': other_reacts,
},
'others_reactions': {
comment_ids: other_counts,
}
} }
methods = { methods = {
@ -644,20 +654,20 @@ class CommentCommands(CommandTestCase):
self.assertEqual(comment_list['total_items'], 2) self.assertEqual(comment_list['total_items'], 2)
bee_like_reaction = await self.daemon.jsonrpc_comment_react( bee_like_reaction = await self.daemon.jsonrpc_comment_react(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=bee['claim_id'], channel_id=bee['claim_id'],
channel_name=bee['name'], channel_name=bee['name'],
react_type='like', react_type='like',
) )
moth_like_reaction = await self.daemon.jsonrpc_comment_react( moth_like_reaction = await self.daemon.jsonrpc_comment_react(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=moth['claim_id'], channel_id=moth['claim_id'],
channel_name=moth['name'], channel_name=moth['name'],
react_type='like', react_type='like',
) )
reactions = await self.daemon.jsonrpc_comment_react_list( reactions = await self.daemon.jsonrpc_comment_react_list(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=moth['claim_id'], channel_id=moth['claim_id'],
channel_name=moth['name'], channel_name=moth['name'],
) )
@ -666,7 +676,7 @@ class CommentCommands(CommandTestCase):
self.assertEqual(reactions['others_reactions']['0']['like'], 1) self.assertEqual(reactions['others_reactions']['0']['like'], 1)
bee_dislike_reaction = await self.daemon.jsonrpc_comment_react( bee_dislike_reaction = await self.daemon.jsonrpc_comment_react(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=bee['claim_id'], channel_id=bee['claim_id'],
channel_name=bee['name'], channel_name=bee['name'],
react_type='dislike', react_type='dislike',
@ -674,7 +684,7 @@ class CommentCommands(CommandTestCase):
) )
reactions_after_bee_dislikes = await self.daemon.jsonrpc_comment_react_list( reactions_after_bee_dislikes = await self.daemon.jsonrpc_comment_react_list(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=moth['claim_id'], channel_id=moth['claim_id'],
channel_name=moth['name'], channel_name=moth['name'],
) )
@ -685,20 +695,19 @@ class CommentCommands(CommandTestCase):
self.assertEqual(reactions_after_bee_dislikes['others_reactions']['0']['like'], 0) self.assertEqual(reactions_after_bee_dislikes['others_reactions']['0']['like'], 0)
only_likes_after_bee_dislikes = await self.daemon.jsonrpc_comment_react_list( only_likes_after_bee_dislikes = await self.daemon.jsonrpc_comment_react_list(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=moth['claim_id'], channel_id=moth['claim_id'],
channel_name=moth['name'], channel_name=moth['name'],
react_types='like', react_types='like',
) )
print('only', only_likes_after_bee_dislikes)
self.assertEqual(only_likes_after_bee_dislikes['my_reactions']['0']['like'], 1) self.assertEqual(only_likes_after_bee_dislikes['my_reactions']['0']['like'], 1)
self.assertEqual(only_likes_after_bee_dislikes['my_reactions']['0']['dislike'], 0) self.assertEqual(only_likes_after_bee_dislikes['my_reactions']['0']['dislike'], 0)
self.assertEqual(only_likes_after_bee_dislikes['others_reactions']['0']['dislike'], 0) self.assertEqual(only_likes_after_bee_dislikes['others_reactions']['0']['dislike'], 0)
self.assertEqual(only_likes_after_bee_dislikes['others_reactions']['0']['like'], 0) self.assertEqual(only_likes_after_bee_dislikes['others_reactions']['0']['like'], 0)
bee_un_dislike_reaction = await self.daemon.jsonrpc_comment_react( bee_un_dislike_reaction = await self.daemon.jsonrpc_comment_react(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=bee['claim_id'], channel_id=bee['claim_id'],
channel_name=bee['name'], channel_name=bee['name'],
remove=True, remove=True,
@ -706,7 +715,7 @@ class CommentCommands(CommandTestCase):
) )
reactions_after_bee_absconds = await self.daemon.jsonrpc_comment_react_list( reactions_after_bee_absconds = await self.daemon.jsonrpc_comment_react_list(
comment_id=first_comment['comment_id'], comment_ids=first_comment['comment_id'],
channel_id=moth['claim_id'], channel_id=moth['claim_id'],
channel_name=moth['name'], channel_name=moth['name'],
) )
@ -715,3 +724,21 @@ class CommentCommands(CommandTestCase):
self.assertNotIn('dislike', reactions_after_bee_absconds['my_reactions']['0']) self.assertNotIn('dislike', reactions_after_bee_absconds['my_reactions']['0'])
self.assertEqual(reactions_after_bee_absconds['others_reactions']['0']['like'], 0) self.assertEqual(reactions_after_bee_absconds['others_reactions']['0']['like'], 0)
self.assertNotIn('dislike', reactions_after_bee_absconds['others_reactions']['0']) self.assertNotIn('dislike', reactions_after_bee_absconds['others_reactions']['0'])
bee_reacts_to_both_comments = await self.daemon.jsonrpc_comment_react(
comment_ids=first_comment['comment_id'] + ',' + second_comment['comment_id'],
channel_id=bee['claim_id'],
channel_name=bee['name'],
react_type='frozen_tom',
)
reactions_after_double_frozen_tom = await self.daemon.jsonrpc_comment_react_list(
comment_ids=first_comment['comment_id'] + ',' + second_comment['comment_id'],
channel_id=moth['claim_id'],
channel_name=moth['name'],
)
self.assertEqual(reactions_after_double_frozen_tom['my_reactions']['0']['like'], 1)
self.assertNotIn('dislike', reactions_after_double_frozen_tom['my_reactions']['0'])
self.assertEqual(reactions_after_double_frozen_tom['others_reactions']['0']['frozen_tom'], 1)
self.assertEqual(reactions_after_double_frozen_tom['others_reactions']['1']['frozen_tom'], 1)