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

room: return 409 if room exists #107

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## XX.XX

* `POST /users/me/rooms` now returns 409 if room with same participants exists

## 22.16

* New query parameter `user_uuid` has been added to the `GET /1.0/users/me/rooms` endpoint.
Expand Down
17 changes: 9 additions & 8 deletions integration_tests/suite/test_user_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,21 @@ def test_create_minimal_parameters(self):
)
self._delete_room(room)

@fixtures.http.room(name='old')
@fixtures.http.room()
def test_create_when_already_exists(self, existing_room):
room_args = {
'name': 'ignored',
'name': 'my-name',
'users': existing_room['users'],
}
room = self.chatd.rooms.create_from_user(room_args)

assert_that(
room,
has_entries(
uuid=existing_room['uuid'],
name=existing_room['name'],
users=existing_room['users'],
calling(self.chatd.rooms.create_from_user).with_args(room_args),
raises(
ChatdError,
has_properties(
status_code=409,
details=has_entries(uuid=existing_room['uuid']),
),
),
)

Expand Down
18 changes: 9 additions & 9 deletions wazo_chatd/plugins/rooms/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ paths:
post:
operationId: create_room
summary: Create room
description: |
**Required ACL:** `chatd.users.me.rooms.create`

**Warning**: **>=22.16**: If a room with the same participants exists,
it will be returned instead of creating new one. In this case, no other
parameter will be taken into account and the return code will be 201.
This behaviour will disappear in the future and a 409 error will be
raised.
description: '**Required ACL:** `chatd.users.me.rooms.create`'
tags:
- rooms
parameters:
Expand All @@ -27,6 +20,8 @@ paths:
$ref: '#/definitions/Room'
'400':
$ref: '#/responses/InvalidRequest'
'409':
$ref: '#/responses/RoomAlreadyExists'
get:
operationId: get_room
summary: Get room
Expand Down Expand Up @@ -129,8 +124,13 @@ parameters:
description: 'The date and time from which to retrieve messages.
Example: 2019-06-12T10:00:00.000+00:00'

definitions:
responses:
RoomAlreadyExists:
description: Room already exists for the given users
schema:
$ref: '#/definitions/Error'

definitions:
Room:
title: Room
allOf:
Expand Down
9 changes: 8 additions & 1 deletion wazo_chatd/plugins/rooms/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019 The Wazo Authors (see the AUTHORS file)
# Copyright 2019-2022 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

from xivo.rest_api_helpers import APIException
Expand All @@ -8,3 +8,10 @@ class DuplicateUserException(APIException):
def __init__(self):
msg = 'Duplicate user detected'
super().__init__(400, msg, 'duplicate-user', {}, 'rooms')


class RoomAlreadyExists(APIException):
def __init__(self, uuid, users):
msg = f'Room "{uuid}" already exists for users: "{users}"'
details = {'uuid': str(uuid), 'users': users}
super().__init__(409, msg, 'conflict', details, 'rooms')
12 changes: 6 additions & 6 deletions wazo_chatd/plugins/rooms/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from wazo_chatd.http import AuthResource
from wazo_chatd.database.models import Room, RoomUser, RoomMessage

from .exceptions import DuplicateUserException
from .exceptions import DuplicateUserException, RoomAlreadyExists
from .schemas import (
ListRequestSchema,
MessageListRequestSchema,
Expand Down Expand Up @@ -45,11 +45,11 @@ def post(self):
user_uuids = [str(user['uuid']) for user in room_args['users']]
rooms = self._service.list_([room_args['tenant_uuid']], user_uuids=user_uuids)
if rooms:
room = rooms[0]
else:
room_args['users'] = [RoomUser(**user) for user in room_args['users']]
room = Room(**room_args)
room = self._service.create(room)
raise RoomAlreadyExists(uuid=rooms[0].uuid, users=user_uuids)

room_args['users'] = [RoomUser(**user) for user in room_args['users']]
room = Room(**room_args)
room = self._service.create(room)

return RoomSchema().dump(room), 201

Expand Down