Skip to content

Commit

Permalink
- Added fixes for contact service functions
Browse files Browse the repository at this point in the history
  • Loading branch information
onstabb committed Dec 12, 2023
1 parent 12d7155 commit 31dd930
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/contacts/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def create_contact(data_in: ContactCreateDataIn, current_user: CurrentActiveComp
)

contact = service.create_contact_by_initiator(current_user, target_user, data_in)

if data_in.action == ContactState.ESTABLISHED:
notification_manager.put_notification(
UserPublicOut.model_validate(current_user, from_attributes=True),
Expand All @@ -60,7 +59,7 @@ def update_contact_state(
current_user: CurrentActiveCompletedUser,
state_data: ContactStateIn,
):
contact = service.get_contact_by_users_pair(current_user.id, target_user.id)
contact = service.get_contact_by_users_pair(current_user, target_user, use_id_for_current_user=False)
if not contact:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Contact doesn't exists")

Expand All @@ -84,7 +83,7 @@ def send_message(
current_user: CurrentActiveCompletedUser,
target_user: TargetActiveCompletedUser,
):
contact = service.get_contact_by_users_pair(current_user, target_user)
contact = service.get_contact_by_users_pair(current_user, target_user, use_id_for_current_user=False)
if not contact or not contact.established:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Contact must be established")

Expand Down
30 changes: 17 additions & 13 deletions src/contacts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,38 @@ def get_contacts_for_user(user: User, *, limit: int = 0, **filters) -> list[dict
return result


def get_contact_by_users_pair(user: User, target_user: User) -> Contact | None:
def get_contact_by_users_pair(
current_user: User, target_user: User, use_id_for_current_user: bool = True
) -> Contact | None:
"""
Retrieve a Contact instance for the given pair of users.
:param user: The first user in the pair.
:param current_user: The first user in the pair.
:param target_user: The second user in the pair.
:param use_id_for_current_user: if True then current_user object in found contact will be changed to the ObjectId
:return: A Contact instance if it exists.
Note: if a contact is found, this function updates the Contact instance by setting
the appropriate user ID based on the role of the provided 'user' in the contact. If 'user'
is the respondent, the 'respondent' attribute in the Contact instance is set to the user's ID.
If 'user' is the initiator, the 'initiator' attribute is set to the user's ID.
Note: if a contact is found and parameter `use_id_for_current_user` is True,
this function updates the Contact instance by setting the appropriate user ID based on the role of the provided
'user' in the contact. If 'user' is the respondent, the 'respondent' attribute in the Contact instance is set to the
user's ID. If 'user' is the initiator, the 'initiator' attribute is set to the user's ID.
"""

query = (
(Query(initiator=user) & Query(respondent=target_user)) |
(Query(respondent=user) & Query(initiator=target_user))
(Query(initiator=current_user) & Query(respondent=target_user)) |
(Query(respondent=current_user) & Query(initiator=target_user))
)
try:
contact: Contact = Contact.objects.get(query)
except DoesNotExist:
return None

# From this we cand understand who is target user
if user == contact.respondent:
contact.respondent = user.id
else:
contact.initiator = user.id
if use_id_for_current_user:
if current_user == contact.respondent:
contact.respondent = current_user.id
else:
contact.initiator = current_user.id

return contact

Expand Down Expand Up @@ -109,7 +113,7 @@ def create_message(contact: Contact, sender: User, message_in: MessageIn) -> Mes


def get_messages_count_from_sender(contact: Contact, sender: User) -> int:
if sender.id not in (contact.initiator, contact.respondent):
if sender not in (contact.initiator, contact.respondent):
raise ValueError(f"Contact does not contain user with {sender.id}")

return len(contact.messages.filter(sender=sender))
Expand Down

0 comments on commit 31dd930

Please sign in to comment.