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

Feat/replies #28

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Some cleanup
  • Loading branch information
delneg committed Jan 2, 2022
commit ae882a9edfeee202d057c1886a152b156bfd4d1b
7 changes: 4 additions & 3 deletions django_private_chat2/consumers/chat_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

logger = logging.getLogger('django_private_chat2.chat_consumer')
TEXT_MAX_LENGTH = getattr(settings, 'TEXT_MAX_LENGTH', 65535)

UNAUTH_REJECT_CODE: int = 4001

class ChatConsumer(AsyncWebsocketConsumer):
async def _after_message_save(self, msg: MessageModel, rid: int, user_pk: str):
Expand Down Expand Up @@ -48,14 +48,15 @@ async def connect(self):
if str(d) != self.group_name:
await self.channel_layer.group_send(str(d), OutgoingEventWentOnline(user_pk=str(self.user.pk))._asdict())
else:
await self.close(code=4001)
logger.info(f"Rejecting unauthenticated user with code {UNAUTH_REJECT_CODE}")
await self.close(code=UNAUTH_REJECT_CODE)

async def disconnect(self, close_code):
# TODO:
# Set user offline
# Save user was_online
# Notify other users that the user went offline
if close_code != 4001 and getattr(self, 'user', None) is not None:
if close_code != UNAUTH_REJECT_CODE and getattr(self, 'user', None) is not None:
logger.info(
f"User {self.user.pk} disconnected, removing channel {self.channel_name} from group {self.group_name}")
await self.channel_layer.group_discard(self.group_name, self.channel_name)
Expand Down
2 changes: 1 addition & 1 deletion django_private_chat2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def serialize_file_model(m: UploadedFile) -> dict[str | str]:
'size': m.file.size, 'name': os.path.basename(m.file.name)}


def serialize_message_model(m: MessageModel, user_id) -> dict[str, bool | dict[str, str | bytes] | None | int | str]:
def serialize_message_model(m: MessageModel, user_id) -> dict[str, bool | dict[str, str | bytes] | None | int | str]:
sender_pk = m.sender.pk
is_out = sender_pk == user_id
# TODO: add forwards
Expand Down