-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from CPSSD/n/readfollows
Add reading of follows to follows microservice
- Loading branch information
Showing
7 changed files
with
227 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,36 @@ message FollowResponse { | |
string error = 2; | ||
} | ||
|
||
message FollowUser { | ||
string handle = 1; | ||
string host = 2; | ||
string display_name = 3; | ||
} | ||
|
||
message GetFollowsRequest { | ||
/* Eg. "admin", "[email protected]", "@jose" */ | ||
string username = 1; | ||
} | ||
|
||
message GetFollowsResponse { | ||
enum ResultType { | ||
OK = 0; | ||
ERROR = 1; | ||
// More types can be added here. | ||
} | ||
|
||
ResultType result_type = 1; | ||
|
||
/* Should only be set if result_type is not OK. */ | ||
string error = 2; | ||
|
||
/* Should only be set if result_type is OK. */ | ||
repeated FollowUser results = 3; | ||
} | ||
|
||
service Follows { | ||
rpc SendFollowRequest(LocalToAnyFollow) returns (FollowResponse); | ||
rpc ReceiveFollowRequest(ForeignToLocalFollow) returns (FollowResponse); | ||
rpc GetFollowers(GetFollowsRequest) returns (GetFollowsResponse); | ||
rpc GetFollowing(GetFollowsRequest) returns (GetFollowsResponse); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from enum import Enum | ||
|
||
import database_pb2 | ||
import follows_pb2 | ||
|
||
|
||
class GetFollowsReceiver: | ||
|
||
def __init__(self, logger, util, database_stub): | ||
self._logger = logger | ||
self._util = util | ||
self._database_stub = database_stub | ||
self.RequestType = Enum('RequestType', 'FOLLOWING FOLLOWERS') | ||
|
||
def _get_follows(self, request, context, request_type): | ||
if request_type == self.RequestType.FOLLOWERS: | ||
self._logger.debug('List of followers of %s requested', | ||
request.username) | ||
else: | ||
self._logger.debug('List of users %s is following requested', | ||
request.username) | ||
resp = follows_pb2.GetFollowsResponse() | ||
|
||
# Parse input username | ||
handle, host = self._util.parse_username( | ||
request.username) | ||
if handle is None and host is None: | ||
resp.result_type = follows_pb2.GetFollowsResponse.ERROR | ||
resp.error = 'Could not parse queried username' | ||
return resp | ||
|
||
# Get user obj associated with given user handle & host from database | ||
user_entry = self._util.get_user_from_db(handle, host) | ||
if user_entry is None: | ||
error = 'Could not find or create user {}@{}'.format(from_handle, | ||
from_instance) | ||
self._logger.error(error) | ||
resp.result_type = follows_pb2.GetFollowersResponse.ERROR | ||
resp.error = error | ||
return resp | ||
user_id = user_entry.global_id | ||
|
||
# Get followers/followings for this user. | ||
following_ids = None | ||
if request_type == self.RequestType.FOLLOWERS: | ||
following_ids = self._util.get_follows(followed_id=user_id).results | ||
else: | ||
following_ids = self._util.get_follows(follower_id=user_id).results | ||
|
||
# Convert other following users and add to output proto. | ||
for following_id in following_ids: | ||
_id = following_id.followed | ||
if request_type == self.RequestType.FOLLOWERS: | ||
_id = following_id.follower | ||
user = self._util.get_user_from_db(global_id=_id) | ||
if user is None: | ||
self._logger.warning('Could not find user for id %d', | ||
_id) | ||
continue | ||
|
||
ok = self._util.convert_db_user_to_follow_user(user, | ||
resp.results.add()) | ||
if not ok: | ||
self._logger.warning('Could not convert user %s@%s to ' + | ||
'FollowUser', user.handle, user.host) | ||
|
||
resp.result_type = follows_pb2.GetFollowsResponse.OK | ||
return resp | ||
|
||
def GetFollowing(self, request, context): | ||
self._logger.debug('GetFollowing, username = %s', request.username) | ||
return self._get_follows(request, context, self.RequestType.FOLLOWING) | ||
|
||
def GetFollowers(self, request, context): | ||
self._logger.debug('GetFollowers, username = %s', request.username) | ||
return self._get_follows(request, context, self.RequestType.FOLLOWERS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters