Subterfuge-Revived / Remake-Backend

Server side validation and API
Creative Commons Zero v1.0 Universal
10 stars 1 forks source link

Message groups sending same group twice #26

Open QuinnBast opened 4 years ago

QuinnBast commented 4 years ago

Expected Behavior

When getting a list of message groups, I should get a list of all of the unique message groups that I am a member of. This is because when creating a message group is automatically adds yourself to the group and then you can go and join the group a second time. You shouldn't be able to join a group you are already a member of.

Current Behavior

When getting the list of message groups, some message groups appear twice because you can join the same group multiple times.

Failure Information

For some reason api/rooms/#/groups is returning the same message groups twice. I've determined that this is because I didn't realize that when a message group is created it automatically assumes that the player sending the request should be in the group. Because of this, I have been sending the current player's id in the participants array, thus the user was getting added to the group twice. This made it so that when getting the list of the groups, the same group got returned multiple times because the player is in it twice.

[
    {
        "id": 1,
        "room_id": 4,
        "message_group_members": [
            {
                "id": 1,
                "created_at": "2020-04-25 20:49:58",
                "updated_at": "2020-04-25 20:49:58",
                "message_group_id": 1,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            },
            {
                "id": 2,
                "created_at": "2020-04-25 20:49:58",
                "updated_at": "2020-04-25 20:49:58",
                "message_group_id": 1,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            }
        ]
    },
    {
        "id": 1,
        "room_id": 4,
        "message_group_members": [
            {
                "id": 1,
                "created_at": "2020-04-25 20:49:58",
                "updated_at": "2020-04-25 20:49:58",
                "message_group_id": 1,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            },
            {
                "id": 2,
                "created_at": "2020-04-25 20:49:58",
                "updated_at": "2020-04-25 20:49:58",
                "message_group_id": 1,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            }
        ]
    },
    {
        "id": 2,
        "room_id": 4,
        "message_group_members": [
            {
                "id": 3,
                "created_at": "2020-04-26 01:26:04",
                "updated_at": "2020-04-26 01:26:04",
                "message_group_id": 2,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            },
            {
                "id": 4,
                "created_at": "2020-04-26 01:26:04",
                "updated_at": "2020-04-26 01:26:04",
                "message_group_id": 2,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            }
        ]
    },
    {
        "id": 2,
        "room_id": 4,
        "message_group_members": [
            {
                "id": 3,
                "created_at": "2020-04-26 01:26:04",
                "updated_at": "2020-04-26 01:26:04",
                "message_group_id": 2,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            },
            {
                "id": 4,
                "created_at": "2020-04-26 01:26:04",
                "updated_at": "2020-04-26 01:26:04",
                "message_group_id": 2,
                "player_id": 1,
                "player": {
                    "id": 1,
                    "created_at": "2020-04-14 02:44:32",
                    "updated_at": "2020-04-25 20:40:14",
                    "name": "asdfg1",
                    "rating": 1200,
                    "wins": 0,
                    "resignations": 1,
                    "last_online_at": "2020-04-14 02:44:32"
                }
            }
        ]
    }
]

Possible Solution

Should add a check to players who are joining a message group to ensure that you cannot join a group you are already in.

Steps to Reproduce

  1. Login
  2. Create a game
  3. Have someone join your game
  4. Start the game early
  5. Create a new message group
  6. Join the group you just created
  7. List all message groups