-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: merge chat history with chat notifications (#1127)
* feat: add chat_id to upload and crawl payload * feat(chat): return chat_history_with_notifications * feat: explicit notification status on create * feat: handle notifications in frontend * feat: delete chat notifications on chat delete request
- Loading branch information
1 parent
575d988
commit 9464707
Showing
17 changed files
with
213 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
backend/repository/chat/get_chat_history_with_notifications.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from enum import Enum | ||
from typing import List, Union | ||
from uuid import UUID | ||
|
||
from models.notifications import Notification | ||
from pydantic import BaseModel | ||
from utils.parse_message_time import ( | ||
parse_message_time, | ||
) | ||
|
||
from repository.chat.get_chat_history import GetChatHistoryOutput, get_chat_history | ||
from repository.notification.get_chat_notifications import ( | ||
get_chat_notifications, | ||
) | ||
|
||
|
||
class ChatItemType(Enum): | ||
MESSAGE = "MESSAGE" | ||
NOTIFICATION = "NOTIFICATION" | ||
|
||
|
||
class ChatItem(BaseModel): | ||
item_type: ChatItemType | ||
body: Union[GetChatHistoryOutput, Notification] | ||
|
||
|
||
def merge_chat_history_and_notifications( | ||
chat_history: List[GetChatHistoryOutput], notifications: List[Notification] | ||
) -> List[ChatItem]: | ||
chat_history_and_notifications = chat_history + notifications | ||
|
||
chat_history_and_notifications.sort( | ||
key=lambda x: parse_message_time(x.message_time) | ||
if isinstance(x, GetChatHistoryOutput) | ||
else parse_message_time(x.datetime) | ||
) | ||
|
||
transformed_data = [] | ||
for item in chat_history_and_notifications: | ||
if isinstance(item, GetChatHistoryOutput): | ||
item_type = ChatItemType.MESSAGE | ||
body = item | ||
else: | ||
item_type = ChatItemType.NOTIFICATION | ||
body = item | ||
transformed_item = ChatItem(item_type=item_type, body=body) | ||
transformed_data.append(transformed_item) | ||
|
||
return transformed_data | ||
|
||
|
||
def get_chat_history_with_notifications( | ||
chat_id: UUID, | ||
) -> List[ChatItem]: | ||
chat_history = get_chat_history(str(chat_id)) | ||
chat_notifications = get_chat_notifications(chat_id) | ||
return merge_chat_history_and_notifications(chat_history, chat_notifications) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import List | ||
from uuid import UUID | ||
|
||
from models.notifications import Notification | ||
from models.settings import get_supabase_db | ||
|
||
|
||
def get_chat_notifications(chat_id: UUID) -> List[Notification]: | ||
""" | ||
Get notifications by chat_id | ||
""" | ||
supabase_db = get_supabase_db() | ||
|
||
return supabase_db.get_notifications_by_chat_id(chat_id) |
11 changes: 11 additions & 0 deletions
11
backend/repository/notification/remove_chat_notifications.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from uuid import UUID | ||
from models.settings import get_supabase_db | ||
|
||
|
||
def remove_chat_notifications(chat_id: UUID) -> None: | ||
""" | ||
Remove all notifications for a chat | ||
""" | ||
supabase_db = get_supabase_db() | ||
|
||
supabase_db.remove_notifications_by_chat_id(chat_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from datetime import datetime | ||
|
||
|
||
def parse_message_time(message_time_str): | ||
return datetime.strptime(message_time_str, "%Y-%m-%dT%H:%M:%S.%f") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
frontend/app/chat/[chatId]/utils/getMessagesFromChatHistory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ChatHistory, ChatItem } from "../types"; | ||
|
||
export const getMessagesFromChatHistory = ( | ||
chatHistory: ChatItem[] | ||
): ChatHistory[] => { | ||
const messages = chatHistory | ||
.filter((item) => item.item_type === "MESSAGE") | ||
.map((item) => item.body as ChatHistory); | ||
|
||
return messages; | ||
}; |
Oops, something went wrong.