def ReceiveUnfollow(self, request, context): resp = general_pb2.GeneralResponse() local_user, foreign_user = self._util.validate_and_get_users( resp, request) if foreign_user is None or local_user is None: self._logger.info('Error receiving unfollow: %s', resp.error) return resp self._logger.info('User ID %d is unfollowing User ID %d', foreign_user.global_id, local_user.global_id) follow_resp = self._util.delete_follow_in_db(foreign_user.global_id, local_user.global_id) if follow_resp.result_type == general_pb2.ResultType.ERROR: self._logger.error('Error deleting follow: %s', follow_resp.error) resp.result_type = general_pb2.ResultType.ERROR resp.error = 'Could not delete requested follow from database' return resp if self._recommender_stub is not None: req = recommend_follows_pb2.UpdateFollowRecommendationsRequest( follower=foreign_user.global_id, followed=local_user.global_id, following=False) self._recommender_stub.UpdateFollowRecommendations(req) resp.result_type = general_pb2.ResultType.OK return resp
def ReceiveFollowRequest(self, request, context): resp = general_pb2.GeneralResponse() local_user, foreign_user = self._util.validate_and_get_users(resp, request) if foreign_user is None or local_user is None: return resp self._logger.info('User ID %d has requested to follow User ID %d', foreign_user.global_id, local_user.global_id) if not local_user.private.value: self._logger.info('Accepting follow request') self._util.attempt_to_accept( local_user, foreign_user, self._host_name, True) state = database_pb2.Follow.ACTIVE if local_user.private.value: self._logger.info('Follow private user: waiting for approval') state = database_pb2.Follow.PENDING follow_resp = self._util.create_follow_in_db(foreign_user.global_id, local_user.global_id, state=state) if follow_resp.result_type == general_pb2.ResultType.ERROR: self._logger.error('Error creating follow: %s', follow_resp.error) resp.result_type = general_pb2.ResultType.ERROR resp.error = 'Could not add requested follow to database' return resp if self._recommender_stub is not None: req = recommend_follows_pb2.UpdateFollowRecommendationsRequest( follower=foreign_user.global_id, followed=local_user.global_id, following=True) self._recommender_stub.UpdateFollowRecommendations(req) resp.result_type = general_pb2.ResultType.OK return resp
def SendFollowRequest(self, request, context): resp = follows_pb2.FollowResponse() self._logger.info('Sending follow request.') 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 follow %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 followed username' return resp # Get user IDs for follow. 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 is_local = to_instance is None created_user = False followed_entry = self._users_util.get_user_from_db( handle=to_handle, host=to_instance, host_is_null=is_local) if not is_local: created_user = True fu_details = self._users_util.get_actor_details( to_handle, to_instance) followed_entry = self._users_util.get_or_create_user_from_db( handle=to_handle, host=to_instance, bio=fu_details[2]) 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 follow User ID %d', follower_entry.global_id, followed_entry.global_id) err = self._add_follow(resp, follower_entry.global_id, followed_entry.global_id, followed_entry.private.value, not is_local) if err is not None: return resp if not is_local: err = self._send_s2s(from_handle, to_handle, to_instance) if err is not None: self._logger.error("Error from s2sFollow: %s", err) self._roll_back_follow(follower_entry.global_id, followed_entry.global_id, created_user) resp.result_type = follows_pb2.FollowResponse.ERROR resp.error = err return resp if self._recommender_stub is not None: req = recommend_follows_pb2.UpdateFollowRecommendationsRequest( follower=follower_entry.global_id, followed=followed_entry.global_id, following=True) self._recommender_stub.UpdateFollowRecommendations(req) resp.result_type = follows_pb2.FollowResponse.OK return resp
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