def _send_s2s(self, from_handle, to_handle, to_host): local_user = s2s_follow_pb2.FollowActivityUser() local_user.handle = from_handle local_user.host = self._host_name foreign_user = s2s_follow_pb2.FollowActivityUser() foreign_user.handle = to_handle foreign_user.host = to_host s2s_follow = s2s_follow_pb2.FollowDetails() s2s_follow.follower.handle = from_handle s2s_follow.follower.host = self._host_name s2s_follow.followed.handle = to_handle s2s_follow.followed.host = to_host resp = self._follow_activity_stub.SendFollowActivity(s2s_follow) if resp.result_type == s2s_follow_pb2.FollowActivityResponse.ERROR: return resp.error return None
def attempt_to_accept(self, local_user, foreign_user, host_name, is_accepted): s2s_follow = s2s_follow_pb2.FollowDetails( follower=s2s_follow_pb2.FollowActivityUser( handle=foreign_user.handle, host=foreign_user.host, ), followed=s2s_follow_pb2.FollowActivityUser( handle=local_user.handle, host=host_name, ), ) req = approver_pb2.Approval( accept=is_accepted, follow=s2s_follow, ) # TODO(devoxel): Add response logic print(self._approver_stub.SendApproval(req))
import unittest from unittest.mock import Mock from activities.approver.send import SendApprovalServicer from services.proto import approver_pb2 from services.proto import s2s_follow_pb2 from utils.activities import ActivitiesUtil example_req = approver_pb2.Approval( accept=True, follow=s2s_follow_pb2.FollowDetails( follower=s2s_follow_pb2.FollowActivityUser( handle="rtgame", host="youtube.com", ), followed=s2s_follow_pb2.FollowActivityUser( handle="callmekevin", host="example.com", ), )) class SendServicerTest(unittest.TestCase): def setUp(self): activ_util = ActivitiesUtil(Mock(), Mock()) activ_util.send_activity = Mock(return_value=(None, None)) self.servicer = SendApprovalServicer(Mock(), activ_util) def test_build_accept_activity(self): req = example_req req.accept = True
def SendUnfollow(self, request, context): resp = follows_pb2.FollowResponse() self._logger.info('Setting unfollow.') from_handle, from_instance = self._users_util.parse_username( request.follower) to_handle, to_instance = \ self._users_util.parse_username(request.followed) self._logger.info('%s@%s has requested to unfollow %s@%s.', from_handle, from_instance, to_handle, to_instance) if to_instance is None and to_handle is None: resp.result_type = follows_pb2.FollowResponse.ERROR resp.error = 'Could not parse unfollowed username' return resp # Get user IDs for unfollow. follower_entry = self._users_util.get_or_create_user_from_db( handle=from_handle, host=from_instance, host_is_null=(from_instance is None)) if follower_entry is None: error = 'Could not find or create user {}@{}'.format(from_handle, from_instance) self._logger.error(error) resp.result_type = follows_pb2.FollowResponse.ERROR resp.error = error return resp followed_entry = self._users_util.get_or_create_user_from_db( handle=to_handle, host=to_instance, host_is_null=(to_instance is None)) if followed_entry is None: error = 'Could not find or create user {}@{}'.format(to_handle, to_instance) self._logger.error(error) resp.result_type = follows_pb2.FollowResponse.ERROR resp.error = error return resp self._logger.info('User ID %d has requested to unfollow User ID %d', follower_entry.global_id, followed_entry.global_id) is_foreign = to_instance is not None err = self._remove_follow(resp, follower_entry.global_id, followed_entry.global_id) if err is not None: # If there was an error during unfollowing, return it. return resp if is_foreign: # Local user won't have a from_instance, set it. from_instance = self._host_name s2s_follower = s2s_follow_pb2.FollowActivityUser(handle=from_handle, host=from_instance) s2s_followed = s2s_follow_pb2.FollowActivityUser(handle=to_handle, host=to_instance) s2s_req = s2s_follow_pb2.FollowDetails(follower=s2s_follower, followed=s2s_followed) self._s2s_stub.SendUnfollowActivity(s2s_req) if self._recommender_stub is not None: req = recommend_follows_pb2.UpdateFollowRecommendationsRequest( follower=follower_entry.global_id, followed=followed_entry.global_id, following=False) self._recommender_stub.UpdateFollowRecommendations(req) resp.result_type = follows_pb2.FollowResponse.OK return resp