Ejemplo n.º 1
0
    def processRequest(self, request, pk=None):
        body = request.data
        idOfFriendToAddOrDeny = body["IdOfFriendToAddOrDeny"]
        idOfLoggedInUser = body["IdOfLoggedInUser"]
        action = body["action"]

        friendToAddOrDeny = CustomUser.objects.get(pk=idOfFriendToAddOrDeny)
        loggedInUser = CustomUser.objects.get(pk=idOfLoggedInUser)

        if action == "ACCEPT":
            requesterIsRemote = False
            if Services.isNotBlank(friendToAddOrDeny.host):
                requesterIsRemote = True

            if requesterIsRemote:
                result = ServerMethods.befriend_remote_author_by_id(
                    idOfFriendToAddOrDeny, idOfLoggedInUser)
            else:
                Services.handle_friend_request(friendToAddOrDeny, loggedInUser)

        else:
            follow = Follow.objects.get(follower=idOfFriendToAddOrDeny,
                                        receiver=idOfLoggedInUser)
            follow.delete()
            Services.updateNotificationsById(friendToAddOrDeny.id)
            Services.updateNotificationsById(loggedInUser.id)

        return Response(status=200)
Ejemplo n.º 2
0
    def create(self, request):
        if request.method == "POST":
            # extract the author and receiver IDs
            body = json.loads(request.body.decode('utf-8'))
            author = body["author"]
            friend = body["friend"]["id"].split("/")[-1]

            # This should be done better, but right now
            # we are gonna get the username using the id
            # and then handle the friendrequest

            # If the sender ID or the receiver ID do not exist still just 200 them
            # If the author doesn't exist create a foregin account for them on connectify
            Services.addAuthor(author)

            # If the receiver doesn't exist do nothing
            try:
                friend = CustomUser.objects.get(pk=friend)
                author = CustomUser.objects.get(pk=author["id"].split("/")[-1])
            except:
                return Response(status=200)

            Services.handle_friend_request(friend, author)

        return Response(status=200)
Ejemplo n.º 3
0
def friendRequestView(request):
    # When the user posts here, they will send a follow/friend request
    # This will add an element to follow or something maybe?
    # So I think this should all be done through ajax too to be honest
    # So on the profile page when you click the button it just sends the
    # http request using AJAX instead of the browser

    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = friendRequestForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # Send the friend request

            # Uncomment this code to add 10 friends to the currently logged in user
            # You cant login to these accounts though lol
            #
            # for i in range(10):
            #     newUser = CustomUser(username=uniform(1,10),password=uniform(1,10))
            #     newUser.save()
            #     friend = Friendship(friend_a=request.user, friend_b=newUser)
            #     friend.save()

            # receiver_data is either a username or an id
            receiver_data = form.cleaned_data["friendToAdd"]
            follower = request.user

            # if receiver_data is a username, check its not the same as the follower's username
            if receiver_data == follower.username:
                # just return a redirect for now
                return HttpResponseRedirect('/')

            # TODO: functionality to prevent multiple requests in a row

            is_remote_author = form.cleaned_data["isRemoteAuthor"]
            if is_remote_author:
                result = befriend_remote_author_by_id(receiver_data,
                                                      follower.id)
            else:
                receiver = CustomUser.objects.get(username=receiver_data)
                Services.handle_friend_request(receiver, follower)

            # redirect to a new URL: homepage
            return HttpResponseRedirect('/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = friendRequestForm()

    return render(request, 'friendrequest/friendrequest.html', {'form': form})
Ejemplo n.º 4
0
    def test_test_cannot_add_yourself_as_friend(self):
        """Check that you cannot add yourself as a friend"""
        Services.handle_friend_request(self.userA, self.userA)

        if Follow.objects.filter(follower=self.userB,
                                 receiver=self.userA).exists():
            self.assertTrue(False)
        elif Follow.objects.filter(follower=self.userA,
                                   receiver=self.userB).exists():
            self.assertTrue(False)
        elif Friendship.objects.filter(friend_a=self.userA,
                                       friend_b=self.userB).exists():
            self.assertTrue(False)
        elif Friendship.objects.filter(friend_a=self.userB,
                                       friend_b=self.userA).exists():
            self.assertTrue(False)
Ejemplo n.º 5
0
    def test_adding_new_friend(self):
        """Check that after adding a friend, the user is following that friend"""
        Services.handle_friend_request(self.userA, self.userB)

        if not Follow.objects.filter(follower=self.userB,
                                     receiver=self.userA).exists():
            self.assertTrue(False)
        elif Follow.objects.filter(follower=self.userA,
                                   receiver=self.userB).exists():
            self.assertTrue(False)
        elif Friendship.objects.filter(friend_a=self.userA,
                                       friend_b=self.userB).exists():
            self.assertTrue(False)
        elif Friendship.objects.filter(friend_a=self.userB,
                                       friend_b=self.userA).exists():
            self.assertTrue(False)
def befriend_remote_author_by_id(remote_author_id, local_author_id):

    local_author, _ = get_user(local_author_id)
    if local_author == None:
        return False
    author = {}
    local_host = get_our_server().host
    author["id"] = local_host + "/author/" + str(local_author.id)
    author["host"] = local_host
    author["displayName"] = local_author.displayname
    author["url"] = local_host + "/author/" + str(local_author.id)

    remote_author, author_exists_on_local = get_user(remote_author_id)
    if remote_author == None:
        return False

    # by now, either the remote author is already saved locally, or
    # they didn't exist locally but have been newly saved locally.
    # either way, we want to send the local friend a friend request
    Services.handle_friend_request(remote_author, local_author)

    friend = {}
    friend["id"] = remote_author.url
    friend["host"] = remote_author.host
    friend["displayName"] = remote_author.displayname
    friend["url"] = remote_author.url

    data = {}
    data["query"] = "friendrequest"
    data["author"] = author
    data["friend"] = friend

    request_url = str(
        remote_author.url).split('/author/')[0] + "/friendrequest"
    remote_server = get_server_info(
        str(remote_author.url).split('/author/')[0])
    r = requests.post(request_url,
                      auth=(remote_server.username, remote_server.password),
                      data=json.dumps(data))
    response = r.text

    if r.status_code != 200:
        print(response)
        return False

    return True