From 3543f498cc896b0264dbd0c80fe61fd055f306cf Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Wed, 29 Jul 2020 16:53:52 -0700 Subject: [PATCH] Fix set followed method (#168) * Fix set followed rpc method * Fix get followed squeaks method --- init.sql | 4 +- itests/tests/test_squeak_node.py | 51 +++++++++++++++---- proto/squeak_admin.proto | 25 +++++++-- .../admin/squeak_admin_server_handler.py | 7 +++ .../admin/squeak_admin_server_servicer.py | 10 +++- squeakserver/node/squeak_node.py | 11 ++-- squeakserver/server/postgres_db.py | 23 ++++++--- squeakserver/server/squeak_profile.py | 2 +- 8 files changed, 103 insertions(+), 30 deletions(-) diff --git a/init.sql b/init.sql index f5287cca..22898206 100644 --- a/init.sql +++ b/init.sql @@ -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 ); diff --git a/itests/tests/test_squeak_node.py b/itests/tests/test_squeak_node.py index a7f64f35..474b5cca 100644 --- a/itests/tests/test_squeak_node.py +++ b/itests/tests/test_squeak_node.py @@ -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 diff --git a/proto/squeak_admin.proto b/proto/squeak_admin.proto index f5c59163..2eda23a8 100644 --- a/proto/squeak_admin.proto +++ b/proto/squeak_admin.proto @@ -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; diff --git a/squeakserver/admin/squeak_admin_server_handler.py b/squeakserver/admin/squeak_admin_server_handler.py index b2329ab8..d6c6db0e 100644 --- a/squeakserver/admin/squeak_admin_server_handler.py +++ b/squeakserver/admin/squeak_admin_server_handler.py @@ -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( diff --git a/squeakserver/admin/squeak_admin_server_servicer.py b/squeakserver/admin/squeak_admin_server_servicer.py index 72a639bf..93cfe2c9 100644 --- a/squeakserver/admin/squeak_admin_server_servicer.py +++ b/squeakserver/admin/squeak_admin_server_servicer.py @@ -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, ) diff --git a/squeakserver/node/squeak_node.py b/squeakserver/node/squeak_node.py index a17af99b..7db361e7 100644 --- a/squeakserver/node/squeak_node.py +++ b/squeakserver/node/squeak_node.py @@ -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) diff --git a/squeakserver/server/postgres_db.py b/squeakserver/server/postgres_db.py index 3ca2041d..29e86ef4 100644 --- a/squeakserver/server/postgres_db.py +++ b/squeakserver/server/postgres_db.py @@ -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"], ) diff --git a/squeakserver/server/squeak_profile.py b/squeakserver/server/squeak_profile.py index ed9b597b..dbeb30d4 100644 --- a/squeakserver/server/squeak_profile.py +++ b/squeakserver/server/squeak_profile.py @@ -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", )