Fix set followed method (#168)

* Fix set followed rpc method

* Fix get followed squeaks method
This commit is contained in:
Jonathan Zernik 2020-07-29 16:53:52 -07:00 committed by GitHub
parent b39bb94a92
commit 3543f498cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 103 additions and 30 deletions

View file

@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS profile (
profile_name VARCHAR(64) NOT NULL,
private_key bytea,
address VARCHAR(35) UNIQUE NOT NULL, -- Maximum length of a bitcoin address is 35.
sharing BOOLEAN NOT NULL,
following BOOLEAN NOT NULL,
shared BOOLEAN NOT NULL,
followed BOOLEAN NOT NULL,
whitelisted BOOLEAN NOT NULL
);

View file

@ -178,15 +178,6 @@ def test_make_squeak(server_stub, admin_stub, signing_profile_id):
== "Hello from the profile on the server!"
)
# # Get all followed squeak display items
# get_followed_squeak_display_response = admin_stub.GetFollowedSqueakDisplays(
# squeak_admin_pb2.GetFollowedSqueakDisplaysRequest()
# )
# print("Get followed squeak displays response: " + str(get_followed_squeak_display_response))
# assert (
# len(get_followed_squeak_display_response.squeak_display_entries) >= 1
# )
# Get the squeak profile
get_squeak_profile_response = admin_stub.GetSqueakProfile(
squeak_admin_pb2.GetSqueakProfileRequest(profile_id=signing_profile_id,)
@ -343,3 +334,45 @@ def test_set_profile_whitelisted(server_stub, admin_stub, contact_profile_id):
squeak_admin_pb2.GetSqueakProfileRequest(profile_id=contact_profile_id,)
)
assert get_squeak_profile_response.squeak_profile.whitelisted == True
def test_set_profile_followed(server_stub, admin_stub, contact_profile_id):
# Get the existing profile
get_squeak_profile_response = admin_stub.GetSqueakProfile(
squeak_admin_pb2.GetSqueakProfileRequest(profile_id=contact_profile_id,)
)
assert get_squeak_profile_response.squeak_profile.followed == False
# Set the profile to be followed
admin_stub.SetSqueakProfileFollowed(
squeak_admin_pb2.SetSqueakProfileFollowedRequest(
profile_id=contact_profile_id,
followed=True,
)
)
# Get the squeak profile again
get_squeak_profile_response = admin_stub.GetSqueakProfile(
squeak_admin_pb2.GetSqueakProfileRequest(profile_id=contact_profile_id,)
)
assert get_squeak_profile_response.squeak_profile.followed == True
def test_get_followed_squeaks(server_stub, admin_stub, saved_squeak_hash, signing_profile_id):
# Set the profile to be followed
admin_stub.SetSqueakProfileFollowed(
squeak_admin_pb2.SetSqueakProfileFollowedRequest(
profile_id=signing_profile_id,
followed=True,
)
)
# Get all squeak displays for the known address
get_followed_squeak_display_response = admin_stub.GetFollowedSqueakDisplays(
squeak_admin_pb2.GetFollowedSqueakDisplaysRequest()
)
assert (
len(get_followed_squeak_display_response.squeak_display_entries) == 1
)
for squeak_display_entry in get_followed_squeak_display_response.squeak_display_entries:
# TODO: check the profile id of the squeak display entry
# assert squeak_display_entry.profile_id == signing_profile_id
pass

View file

@ -46,10 +46,14 @@ service SqueakAdmin {
*/
rpc GetSqueakProfileByAddress (GetSqueakProfileByAddressRequest) returns (GetSqueakProfileByAddressReply) {}
/** sqkadmin: `getsqueakprofilebyaddress`
/** sqkadmin: `setsqueakprofilewhitelisted`
*/
rpc SetSqueakProfileWhitelisted (SetSqueakProfileWhitelistedRequest) returns (SetSqueakProfileWhitelistedReply) {}
/** sqkadmin: `setsqueakprofilefollowed`
*/
rpc SetSqueakProfileFollowed (SetSqueakProfileFollowedRequest) returns (SetSqueakProfileFollowedReply) {}
/** sqkadmin: `makesqueak`
*/
rpc MakeSqueak (MakeSqueakRequest) returns (MakeSqueakReply) {}
@ -150,6 +154,17 @@ message SetSqueakProfileWhitelistedRequest {
message SetSqueakProfileWhitelistedReply {
}
message SetSqueakProfileFollowedRequest {
/// The profile id
int32 profile_id = 1;
/// Followed
bool followed = 2;
}
message SetSqueakProfileFollowedReply {
}
message SqueakProfile {
/// The profile id
int32 profile_id = 1;
@ -163,11 +178,11 @@ message SqueakProfile {
/// The address
string address = 4;
/// Sharing
bool sharing = 5;
/// Shared
bool shared = 5;
/// Following
bool following = 6;
/// Followed
bool followed = 6;
/// Whitelisted
bool whitelisted = 7;

View file

@ -66,6 +66,13 @@ class SqueakAdminServerHandler(object):
))
self.squeak_node.set_squeak_profile_whitelisted(profile_id, whitelisted)
def handle_set_squeak_profile_followed(self, profile_id, followed):
logger.info("Handle set squeak profile followed with profile id: {}, followed: {}".format(
profile_id,
followed,
))
self.squeak_node.set_squeak_profile_followed(profile_id, followed)
def handle_make_squeak(self, profile_id, content_str, replyto_hash):
logger.info("Handle make squeak profile with id: {}".format(profile_id))
inserted_squeak_hash = self.squeak_node.make_squeak(

View file

@ -87,6 +87,12 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
self.handler.handle_set_squeak_profile_whitelisted(profile_id, whitelisted)
return squeak_admin_pb2.SetSqueakProfileWhitelistedReply()
def SetSqueakProfileFollowed(self, request, context):
profile_id = request.profile_id
followed = request.followed
self.handler.handle_set_squeak_profile_followed(profile_id, followed)
return squeak_admin_pb2.SetSqueakProfileFollowedReply()
def MakeSqueak(self, request, context):
profile_id = request.profile_id
content_str = request.content
@ -188,8 +194,8 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
profile_name=squeak_profile.profile_name,
has_private_key=has_private_key,
address=squeak_profile.address,
sharing=squeak_profile.sharing,
following=squeak_profile.following,
shared=squeak_profile.shared,
followed=squeak_profile.followed,
whitelisted=squeak_profile.whitelisted,
)

View file

@ -133,8 +133,8 @@ class SqueakNode:
profile_name=profile_name,
private_key=signing_key_bytes,
address=str(address),
sharing=False,
following=False,
shared=False,
followed=False,
whitelisted=False,
)
return self.postgres_db.insert_profile(squeak_profile)
@ -148,8 +148,8 @@ class SqueakNode:
profile_name=profile_name,
private_key=None,
address=squeak_address,
sharing=False,
following=False,
shared=False,
followed=False,
whitelisted=False,
)
return self.postgres_db.insert_profile(squeak_profile)
@ -170,6 +170,9 @@ class SqueakNode:
self.postgres_db.set_profile_whitelisted(profile_id, whitelisted)
self.squeak_whitelist.refresh()
def set_squeak_profile_followed(self, profile_id, followed):
self.postgres_db.set_profile_followed(profile_id, followed)
def make_squeak(self, profile_id, content_str, replyto_hash):
squeak_profile = self.postgres_db.get_profile(profile_id)
squeak_maker = SqueakMaker(self.lightning_client)

View file

@ -111,13 +111,12 @@ class PostgresDb:
def get_followed_squeak_entries_with_profile(self):
""" Get a squeak. """
# TODO: use profile.following=True
sql = """
SELECT * FROM squeak
JOIN profile
ON squeak.author_address=profile.address
WHERE squeak.block_header IS NOT NULL
AND profile.following=False
AND profile.followed
ORDER BY n_block_height DESC, n_time DESC;
"""
with self.get_cursor() as curs:
@ -190,7 +189,7 @@ class PostgresDb:
def insert_profile(self, squeak_profile):
""" Insert a new squeak profile. """
sql = """
INSERT INTO profile(profile_name, private_key, address, sharing, following, whitelisted)
INSERT INTO profile(profile_name, private_key, address, shared, followed, whitelisted)
VALUES(%s, %s, %s, %s, %s, %s)
RETURNING profile_id;
"""
@ -202,8 +201,8 @@ class PostgresDb:
squeak_profile.profile_name,
squeak_profile.private_key,
squeak_profile.address,
squeak_profile.sharing,
squeak_profile.following,
squeak_profile.shared,
squeak_profile.followed,
squeak_profile.whitelisted,
),
)
@ -278,6 +277,16 @@ class PostgresDb:
with self.get_cursor() as curs:
curs.execute(sql, (whitelisted, profile_id,))
def set_profile_followed(self, profile_id, followed):
""" Set a profile is followed. """
sql = """
UPDATE profile
SET followed=%s
WHERE profile_id=%s;
"""
with self.get_cursor() as curs:
curs.execute(sql, (followed, profile_id,))
def get_unverified_block_squeaks(self):
""" Get all squeaks without block header. """
sql = """
@ -347,8 +356,8 @@ class PostgresDb:
profile_name=row["profile_name"],
private_key=private_key,
address=row["address"],
sharing=row["sharing"],
following=row["following"],
shared=row["shared"],
followed=row["followed"],
whitelisted=row["whitelisted"],
)

View file

@ -2,5 +2,5 @@ from collections import namedtuple
SqueakProfile = namedtuple(
"SqueakProfile",
"profile_id, profile_name, private_key, address, sharing, following, whitelisted",
"profile_id, profile_name, private_key, address, shared, followed, whitelisted",
)