-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
130 lines (103 loc) · 3.68 KB
/
utils.py
File metadata and controls
130 lines (103 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from typing import Union, Type
from asgiref.sync import sync_to_async
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from chats.exceptions import (
NonMatchingReplyChatIdException,
WrongChatIdException,
NonMatchingDirectChatIdException,
)
from chats.models import DirectChatMessage, ProjectChatMessage, FileToMessage
from files.models import UserFile
User = get_user_model()
def clean_message_text(text: str) -> str:
"""
Cleans message text. -
"""
return text.strip()
def validate_message_text(text: str) -> bool:
"""
Validates message text. -
"""
# TODO: add bad word filter
return 0 < len(text) <= 8192
def get_user_channel_cache_key(user: User) -> str:
return f"user_channel_{user.pk}"
async def create_message(
chat_id: Union[str, int],
chat_model: Union[Type[DirectChatMessage], Type[ProjectChatMessage]],
author: User,
text: str,
reply_to: int = None,
) -> Union[DirectChatMessage, ProjectChatMessage]:
"""
Creates message.
Args:
chat_id: An integer representing chat id.
chat_model: A chat model.
author: A user instance.
text: A string representing message text.
reply_to: An integer representing message id to reply to.
Returns:
A message instance.
Raises:
NonMatchingReplyChatIdException: If reply_to message is not in chat.
"""
try:
return await sync_to_async(chat_model.objects.create)(
chat_id=chat_id,
author=author,
text=text,
reply_to=reply_to,
)
except ValidationError:
raise NonMatchingReplyChatIdException(
f"Message {reply_to} is not in chat {chat_id}"
)
async def get_chat_and_user_ids_from_content(content, current_user) -> tuple[str, User]:
chat_id = content["chat_id"]
# check if chat_id is in the format of <user1_id>_<user2_id>
try:
user1_id, user2_id = map(int, chat_id.split("_"))
except ValueError:
raise WrongChatIdException(
f'Chat id "{chat_id}" is not in the format of'
f" <user1_id>_<user2_id>, where user1_id < user2_id"
)
# check if user is a member of this chat and get other user
if user1_id == current_user.id or user2_id == current_user.id:
other_user = await sync_to_async(User.objects.get)(
id=user1_id if user1_id != current_user.id else user2_id
)
else:
raise NonMatchingDirectChatIdException(
f"User {current_user.id} is not a member of chat {chat_id}"
)
return chat_id, other_user
async def create_file_to_message(
direct_message: Union[str, None, DirectChatMessage],
project_message: Union[str, None, ProjectChatMessage],
file: str,
) -> FileToMessage:
return await sync_to_async(FileToMessage.objects.create)(
direct_message=direct_message, project_message=project_message, file=file
)
async def match_files_and_messages(file_urls, messages):
for url in file_urls:
user_file = await sync_to_async(UserFile.objects.get)(pk=url)
# implicitly matches a file and a message
await create_file_to_message(
direct_message=messages["direct_message"],
project_message=messages["project_message"],
file=user_file,
)
def get_all_files(messages):
# looks like something bad -
files = []
for message in messages:
if hasattr(message, "file_to_message"):
files_in_message = [
file_to_message.file for file_to_message in message.file_to_message.all()
]
files.extend(files_in_message)
return files