Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST API Endpoints for Sending and Accepting Friend invitations between two Users; GET request endpoint for listview for Friend Invites #370

Open
wants to merge 37 commits into
base: dev
Choose a base branch
from

Conversation

Esterello2
Copy link

Creating two separate API endpoints that deal with POST requests for Friend Invitations. To send an invitation, a Friend Invitation object is posted at URL
friend-invitations/send/sender<pk>/receiver<pk>/
and to accept, the endpoint is run at:
friend-invitations/accept/invite<pk>/
Then a third endpoint that deals with a GET request for a ListView of all Friend Invitations (pending and accepted) is created at:
/friend-invitations/

@Esterello2 Esterello2 self-assigned this Dec 2, 2023
@Esterello2 Esterello2 linked an issue Dec 2, 2023 that may be closed by this pull request
@Esterello2
Copy link
Author

As of right now, the POST endpoint for "sending invitations" and the GET endpoint for the LISTview of invitations are working. There is an issue with accepting invitations that stems from the model itself (accept_invitation method is not working properly)

@Esterello2
Copy link
Author

The Accept Invite POST Endpoint works now, meaning that all three endpoints are successful. I also had to make endpoints for the UserProfile objects since it is a separate model from Users. I introduced three endpoints for this, each handling POST, PATCH, and GET (listview) requests. This had to be done to ensure that the Friend Invitation endpoint functioned properly.

@Esterello2 Esterello2 requested review from bsokolow10 and removed request for abrahmasandra December 2, 2023 04:33
Copy link

@bsokolow10 bsokolow10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@majorsylvie majorsylvie added this to the 2023/Sprint 3 milestone Dec 7, 2023
Copy link
Contributor

@majorsylvie majorsylvie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking better now! There are more places to polish up.

Immediate problem is that, with the current version of the code, upon trying to access the sending enpoint, I get an error message which asks me to add fields = "__all__" to the UserProfileSerializer`.

After doing so locally, I could get from http://localhost:8000/api/friend-invitations/ the list. In each entry seeing the id accepted status, timestamp, sender, and receiver:
image

I was then able to navigate to multiple different

http://localhost:8000/api/friend-invitations/send/<sender pk>/<receiver pk>

endpoints.

then upon sending an empty post request (via just clicking post on an empty message via the DRF gui:
image

image

I get feedback that my POST is successful

Then if I give user primary keys that are invalid, I am returned an unsuccessful response to my POST request:
image

image

I can then go to: http://localhost:8000/api/friend-invitations/ and see a lit of all the POST'ed friend invitation

 {
            "id": 14,
            "accepted": false,
            "timestamp": "2023-12-07T16:49:39.098944-06:00",
            "sender": 1,
            "receiver": 2
        },
        {
            "id": 15,
            "accepted": false,
            "timestamp": "2023-12-07T16:49:43.343774-06:00",
            "sender": 1,
            "receiver": 3
        },
        {
            "id": 16,
            "accepted": false,
            "timestamp": "2023-12-07T16:49:47.416511-06:00",
            "sender": 1,
            "receiver": 4
        },
        {
            "id": 17,
            "accepted": false,
            "timestamp": "2023-12-07T16:49:55.486581-06:00",
            "sender": 2,
            "receiver": 3
        },
        {
            "id": 18,
            "accepted": false,
            "timestamp": "2023-12-07T16:52:01.030614-06:00",
            "sender": 2,
            "receiver": 7
        }

However, there is a problem here
Once I try to accept an invitation by POST'ing at the friend-invitation/accept endpoint (such as at URL: http://localhost:8000/api/friend-invitations/accept/18/ ) I get the following error:

image

Then there also seems to be some JWT user authentication code still in this PR

Overall, please:
[] add the fields = "__all__" to the UserProfileSerializer`
[] update your code and test if accepting inivtations actually works
[] remove the extraneuous code as it's been commented upon
[] generally answer all comments

permission_classes = [IsAuthenticated]


class UserProfileUpdateView(APIView):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this update and user profile list view are relevant for JWT and are related to authentication.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I am removing the permission classes

serializer_class = FriendInvitationSerializer


class UserProfileCreateView(APIView):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this class have to do with friend invitations?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IT is because Friend Invitations use User Profile pks rather than User class, so I had to create a way to make these UserProfile objects

@majorsylvie majorsylvie self-requested a review December 8, 2023 18:31
Copy link
Contributor

@majorsylvie majorsylvie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even after creating user profiles through the administrator site, I am still not able to actually accept friend invitations.

I specifically created 4 user profiles via the admin page:
image

Then I made have a request with pk=1 from profile id=1 to profile id=2:
image

Thus to my understand of how your endpoints are supposed to work, I should then be able to go to:

http://localhost:8000/api/friend-invitations/accept/1/

which corresponds to friend invitation pk=1

Then by posting nothing to this endpoint, it should then cause this friend invitation to be accepted, in my example making profiles 1 and 2 friends.

However when I post with the following:
image

I get this error:
image

Which upon further inspection, happens in the .accept_invitation() call on line 134:

        invitation.accept_invitation()  # Error with this method in the users.models

Where in my case, it is failing when trying to run the line:

        receiver_profile = UserProfile.objects.get(user__pk=receiver.pk)

where the reciever.pk We can create a bugfix issue to ask the users team to look at this method, since we don't think it should be failing.is 2.

Also, unless I need to create UserProfiles through the API endpoint specifically instead of the admin site, I don't understand what the problem is.

We can create a bugfix issue to ask the users team to look at this method, since we don't think it should be failing.

For now, if I've properly understood how these endpoints are supposed to work, your coded functionality fails due to a function outside of your team.

Overall, this is the current state of trying to run this code, please do the following:

  • if you want, try to see if the reason I can't accept friend invitations is because of something in your code
  • make a bugfix issue for the users team which details AND LINKS TO what's going wrong in this PR with accept_invitation()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating API endpoint to send/accept friend requests
4 participants