Skip to content

Commit

Permalink
add last chat in list chat head response
Browse files Browse the repository at this point in the history
  • Loading branch information
wuttinanhi committed Nov 1, 2022
1 parent a6af62e commit b903bfc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
10 changes: 9 additions & 1 deletion chat/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from auth.function import get_user
from flask import Blueprint, request
from pagination.pagination import create_pagination_options_from_request
from user.service import UserService

from chat.service import ChatService

Expand All @@ -20,6 +21,13 @@ def list_chat():
user = get_user()
pagination_options = create_pagination_options_from_request(request)
result = ChatService.list_chat_head(user, pagination_options)

for chat_head in result:
response.append(chat_head.json_populated())
new_json = chat_head.json_populated()
from_user = UserService.find_by_id(chat_head.user_id)
to_user = UserService.find_by_id(chat_head.target_user_id)
last_chat = ChatService.get_last_chat_message(from_user, to_user)
new_json["last_chat"] = last_chat.json()
response.append(new_json)

return response
26 changes: 25 additions & 1 deletion chat/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from database.database import db_session
from marshmallow import fields, validate
from pagination.pagination import Pagination, PaginationOptions, PaginationSortOptions
from pagination.pagination import (Pagination, PaginationOptions,
PaginationSortOptions)
from sqlalchemy import desc
from sqlalchemy.orm.query import Query
from sqlalchemy.sql import and_, or_
from user.model import User
Expand Down Expand Up @@ -98,6 +100,28 @@ def update_chat_head(user: User, from_user: User):
db_session.rollback()
raise InternalServerError("Failed to update chat head!")

@staticmethod
def get_last_chat_message(from_user: User, to_user: User) -> Chat:
query: Query = db_session.query(Chat)
query = query.where(
or_(
(
and_(
Chat.from_user_id == from_user.id,
Chat.to_user_id == to_user.id,
)
),
(
and_(
Chat.from_user_id == to_user.id,
Chat.to_user_id == from_user.id,
)
),
)
)
query = query.order_by(desc(Chat.id)).limit(1)
return query.one()

@staticmethod
def list_chat_head(
user: User, pagination_options: PaginationOptions
Expand Down
9 changes: 9 additions & 0 deletions example/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@

for chat in chats:
print(chat)

print("=" * 10)

# get last chat message
from_user = UserService.find_by_id(2)
to_user = UserService.find_by_id(3)

last_chat_message = ChatService.get_last_chat_message(from_user, to_user)
print(last_chat_message)
15 changes: 9 additions & 6 deletions mock/seed_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ def seed_chat():
charlie = UserService.find_by_email("[email protected]")

# mock dummy chat
ChatService.send_chat(annie, bobbie, "Hello Bobbie!")
ChatService.send_chat(bobbie, annie, "Hi Annie!")
ChatService.send_chat(annie, bobbie, "How are you?")
ChatService.send_chat(bobbie, annie, "I'm good!")
ChatService.send_chat(annie, bobbie, "1 start Hello Bobbie!")
ChatService.send_chat(bobbie, annie, "2 Hi Annie!")
ChatService.send_chat(annie, bobbie, "3 How are you?")
ChatService.send_chat(bobbie, annie, "4 end I'm good!")

ChatService.send_chat(bobbie, charlie, "Hello Charlie!")
ChatService.send_chat(charlie, bobbie, "Hi Bobbie!")
ChatService.send_chat(bobbie, charlie, "1 start Hello Charlie!")
ChatService.send_chat(charlie, bobbie, "2 end Hi Bobbie!")

ChatService.send_chat(annie, charlie, "1 start Hello Charlie!")
ChatService.send_chat(charlie, annie, "2 end Hi Annie!")


if __name__ == "__main__":
Expand Down

0 comments on commit b903bfc

Please sign in to comment.