-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using web hooks the chat now pulls messages in real time. yet to add send message function.
- Loading branch information
Showing
3,464 changed files
with
614,599 additions
and
9 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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 @@ | ||
default_app_config = 'chat.apps.ChatConfig' |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -1,3 +1,11 @@ | ||
from django.contrib import admin | ||
from .models import Message, Chat | ||
|
||
# Register your models here. | ||
@admin.register(Message) | ||
class MessageAdmin(admin.ModelAdmin): | ||
list_display = ('content', 'sender', 'receiver', 'sent_at', 'chat_id') | ||
search_fields = ('content', 'sender__username') | ||
|
||
@admin.register(Chat) | ||
class ChatAdmin(admin.ModelAdmin): | ||
list_display = ('chat_id', 'last_message_at', 'employee_id', 'mhp_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
import json | ||
from .models import Message | ||
from asgiref.sync import sync_to_async | ||
|
||
class ChatConsumer(AsyncWebsocketConsumer): | ||
async def connect(self): | ||
self.room_name = self.scope['url_route']['kwargs']['room_name'] | ||
self.room_group_name = f'chat_{self.room_name}' | ||
|
||
# Join room group | ||
await self.channel_layer.group_add( | ||
self.room_group_name, | ||
self.channel_name | ||
) | ||
|
||
|
||
await self.accept() | ||
print("webhook accepted") | ||
|
||
recent_messages = await fetch_recent_messages(self.room_name) | ||
for message in recent_messages: | ||
await self.send_chat_message(message) | ||
|
||
|
||
async def disconnect(self, close_code): | ||
# Leave room group | ||
await self.channel_layer.group_discard( | ||
self.room_group_name, | ||
self.channel_name | ||
) | ||
|
||
# Receive message from WebSocket (not used here but included for completeness) | ||
async def receive(self, text_data): | ||
pass | ||
|
||
async def chat_message(self, event): | ||
await self.send(text_data=json.dumps({ | ||
'message': event['message'] | ||
})) | ||
|
||
# Method to send chat messages to WebSocket | ||
async def send_chat_message(self, message): | ||
# Directly send message to WebSocket | ||
await self.send(text_data=json.dumps({ | ||
'message': message | ||
})) | ||
|
||
@sync_to_async | ||
def fetch_recent_messages(room_name, limit=10): | ||
print("Connecting to room:", room_name) | ||
messages = Message.objects.filter(chat_id=room_name).order_by('sent_at')[:limit] | ||
recent_messages = [{ | ||
"content": message.content, | ||
"sender": message.sender.username, | ||
"receiver": message.receiver.username, | ||
"sent_at": message.sent_at.strftime("%Y-%m-%d %H:%M:%S") | ||
} for message in messages] | ||
print("Fetched messages:", recent_messages) # Debug print | ||
return recent_messages | ||
|
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,30 @@ | ||
# Generated by Django 5.0.2 on 2024-03-30 12:44 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Message', | ||
fields=[ | ||
('message_id', models.AutoField(primary_key=True, serialize=False)), | ||
('content', models.TextField()), | ||
('sent_at', models.DateTimeField(auto_now_add=True)), | ||
('receiver', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='received_messages', to=settings.AUTH_USER_MODEL)), | ||
('sender', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sent_messages', to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'ordering': ['sent_at'], | ||
}, | ||
), | ||
] |
26 changes: 26 additions & 0 deletions
26
backend/chat/migrations/0002_alter_message_receiver_alter_message_sender.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,26 @@ | ||
# Generated by Django 5.0.2 on 2024-03-30 12:47 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('chat', '0001_initial'), | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='message', | ||
name='receiver', | ||
field=models.ForeignKey(db_column='receiver_username', on_delete=django.db.models.deletion.CASCADE, related_name='received_messages', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
), | ||
migrations.AlterField( | ||
model_name='message', | ||
name='sender', | ||
field=models.ForeignKey(db_column='sender_username', on_delete=django.db.models.deletion.CASCADE, related_name='sent_messages', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
), | ||
] |
35 changes: 35 additions & 0 deletions
35
backend/chat/migrations/0003_alter_message_receiver_alter_message_sender_chat.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,35 @@ | ||
# Generated by Django 5.0.2 on 2024-03-30 13:05 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('chat', '0002_alter_message_receiver_alter_message_sender'), | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='message', | ||
name='receiver', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='received_messages', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
), | ||
migrations.AlterField( | ||
model_name='message', | ||
name='sender', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sent_messages', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
), | ||
migrations.CreateModel( | ||
name='Chat', | ||
fields=[ | ||
('chat_id', models.AutoField(primary_key=True, serialize=False)), | ||
('last_message_at', models.DateTimeField(auto_now=True)), | ||
('employee_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='employee', to=settings.AUTH_USER_MODEL, to_field='username')), | ||
('mhp_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='mhp', to=settings.AUTH_USER_MODEL, to_field='username')), | ||
], | ||
), | ||
] |
52 changes: 52 additions & 0 deletions
52
backend/chat/migrations/0004_remove_chat_employee_id_remove_chat_mhp_id_and_more.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,52 @@ | ||
# Generated by Django 5.0.2 on 2024-03-30 13:14 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('chat', '0003_alter_message_receiver_alter_message_sender_chat'), | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='chat', | ||
name='employee_id', | ||
), | ||
migrations.RemoveField( | ||
model_name='chat', | ||
name='mhp_id', | ||
), | ||
migrations.AddField( | ||
model_name='chat', | ||
name='employee', | ||
field=models.ForeignKey(default=55, on_delete=django.db.models.deletion.CASCADE, related_name='employee_id', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name='chat', | ||
name='mhp', | ||
field=models.ForeignKey(default=56, on_delete=django.db.models.deletion.CASCADE, related_name='mhp_id', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name='message', | ||
name='chat', | ||
field=models.ForeignKey(default=57, on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='chat.chat'), | ||
preserve_default=False, | ||
), | ||
migrations.AlterField( | ||
model_name='message', | ||
name='receiver', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='receiver_id', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
), | ||
migrations.AlterField( | ||
model_name='message', | ||
name='sender', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sender_id', to=settings.AUTH_USER_MODEL, to_field='username'), | ||
), | ||
] |
Binary file not shown.
Binary file added
BIN
+1.58 KB
...t/migrations/__pycache__/0002_alter_message_receiver_alter_message_sender.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+2.21 KB
...rations/__pycache__/0003_alter_message_receiver_alter_message_sender_chat.cpython-311.pyc
Binary file not shown.
Binary file added
BIN
+2.5 KB
...ions/__pycache__/0004_remove_chat_employee_id_remove_chat_mhp_id_and_more.cpython-311.pyc
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -1,3 +1,19 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
# Create your models here. | ||
class Message(models.Model): | ||
message_id = models.AutoField(primary_key=True) | ||
chat = models.ForeignKey('Chat', related_name='messages', on_delete=models.CASCADE) | ||
content = models.TextField() | ||
sender = models.ForeignKey(User, related_name='sender_id', on_delete=models.CASCADE, to_field='username') | ||
receiver = models.ForeignKey(User, related_name='receiver_id', on_delete=models.CASCADE, to_field='username') | ||
sent_at = models.DateTimeField(auto_now_add=True) | ||
|
||
class Meta: | ||
ordering = ['sent_at'] | ||
|
||
class Chat(models.Model): | ||
chat_id = models.AutoField(primary_key=True) | ||
employee = models.ForeignKey(User, related_name='employee_id', on_delete=models.CASCADE, to_field='username') | ||
mhp = models.ForeignKey(User, related_name='mhp_id', on_delete=models.CASCADE, to_field='username') | ||
last_message_at = models.DateTimeField(auto_now=True) |
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,6 @@ | ||
from django.urls import path | ||
from . import consumers | ||
|
||
websocket_urlpatterns = [ | ||
path('ws/chat/<str:room_name>/', consumers.ChatConsumer.as_asgi()), | ||
] |
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,29 @@ | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from .models import Message | ||
from channels.layers import get_channel_layer | ||
from asgiref.sync import async_to_sync | ||
|
||
@receiver(post_save, sender=Message) | ||
def send_message_update(sender, instance, created, **kwargs): | ||
if created: | ||
channel_layer = get_channel_layer() | ||
room_group_name = f'chat_{instance.chat_id}' | ||
|
||
# Prepare the message content | ||
message_content = { | ||
'type': 'chat_message', # This should match the method name in your consumer | ||
'message': { | ||
'content': instance.content, | ||
'sender': instance.sender.username, | ||
"receiver": instance.receiver.username, | ||
'sent_at': instance.sent_at.strftime("%Y-%m-%d %H:%M:%S") # Format datetime as string | ||
} | ||
} | ||
|
||
# Send message to room group | ||
async_to_sync(channel_layer.group_send)( | ||
room_group_name, | ||
message_content | ||
) | ||
|
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
Binary file not shown.
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,10 @@ | ||
#!/bin/sh | ||
'''exec' "/Users/taseen/Library/Mobile Documents/com~apple~CloudDocs/QMUL/Year 2/Semester 2/SE Project/Prototype/Serenity/venv/bin/python" "$0" "$@" | ||
' ''' | ||
# -*- coding: utf-8 -*- | ||
import re | ||
import sys | ||
from automat._visualize import tool | ||
if __name__ == '__main__': | ||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) | ||
sys.exit(tool()) |
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,10 @@ | ||
#!/bin/sh | ||
'''exec' "/Users/taseen/Library/Mobile Documents/com~apple~CloudDocs/QMUL/Year 2/Semester 2/SE Project/Prototype/Serenity/venv/bin/python" "$0" "$@" | ||
' ''' | ||
# -*- coding: utf-8 -*- | ||
import re | ||
import sys | ||
from twisted.conch.scripts.cftp import run | ||
if __name__ == '__main__': | ||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) | ||
sys.exit(run()) |
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,10 @@ | ||
#!/bin/sh | ||
'''exec' "/Users/taseen/Library/Mobile Documents/com~apple~CloudDocs/QMUL/Year 2/Semester 2/SE Project/Prototype/Serenity/venv/bin/python" "$0" "$@" | ||
' ''' | ||
# -*- coding: utf-8 -*- | ||
import re | ||
import sys | ||
from twisted.conch.scripts.ckeygen import run | ||
if __name__ == '__main__': | ||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) | ||
sys.exit(run()) |
Oops, something went wrong.