def CanViewRemotePost(post, user, serializer):
    post = serializer.data

    if post['visibility'] in ['PRIVATE', 'SERVERONLY']:
        return False
    if post['visibility'] == 'PUBLIC':
        return True

    if 'author' not in post:
        return False
    if post['visibility'] == 'FRIENDS':
        myFriends = set([str(id).strip() for id in allFriend(user.id)])
        if str(post['author']['id']).strip() in myFriends:
            return True
        else:
            return False

    if post['visibility'] == 'FOAF':
        myFriends = set([str(id).strip() for id in allFriend(user.id)])
        if str(post['author']['id']).strip() in myFriends:
            return True
    if post['visibility'] == 'FOAF':
        myFriends = list(set([str(id).strip() for id in allFriend(user.id)]))
        data = {'query': 'friends', 'author': str(post['author']['id']).strip(), 'authors': myFriends}
        success, result = PostWithListOfFriends(data, post, post['author'])
        if not success:
            print('Failed to get FOAF info for author {0} with url {1}, reason: {2}'.format(str(post['author']['id']), post['author']['url'], result))
            return False
        if len(result['authors']) > 0:
            return True
        else:
            return False

    return True # should never reach here
def AreFoaf(user, other_user):
    """
    This function is for LOCAL foaf check
    """
    if AreFriends(user, other_user):
        return True

    friends1 = set(allFriend(user.id))
    friends2 = set(allFriend(other_user.id))
    intersect = friends1.intersection(friends2)
    if len(intersect) > 0:
        return True

    return False
Exemplo n.º 3
0
    def post(self, request):
        if not IsRemoteAuthUser(request.user):
            return Response({
                'is_foaf': False,
                'error': 'need to be a remote user to call this API'
            })

        post_id = request.data['id']
        viewer = request.data['author']
        friends = request.data['friends']

        post = Post.objects.get(pk=post_id)
        if post is None:
            return Response({
                'is_foaf': False,
                'message': 'post not exist'
            })
        author = post.author
        author_friends = allFriend(author.id)

        if viewer['id'] in author_friends:
            return Response({
                'is_foaf': False,
                'error': 'they are friend'
            })

        comm = list(set(friends) & set(author_friends))
        # TODO: remote check
        if len(comm) is not 0:
            serializer = PostReadSerializer(post, context={'request': request})
            return Response({
                'is_foaf': True,
                'post': serializer.data
            })
        else:
            return Response({
                'is_foaf': False,
                'post': None
            })