Skip to content

Commit

Permalink
Merge pull request #182 from gcivil-nyu-org/feature-private-chat-1-copy
Browse files Browse the repository at this point in the history
Private chat + chat deployment in aws
  • Loading branch information
kravisankaran committed Dec 5, 2023
2 parents db4dc7a + a8d54f4 commit 2dbb0b0
Show file tree
Hide file tree
Showing 30 changed files with 713 additions and 161 deletions.
90 changes: 90 additions & 0 deletions .ebextensions/00_supervisord.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
files:
"/etc/supervisord.conf":
mode: "000755"
owner: root
group: root
content: |
[unix_http_server]
file = /tmp/supervisor.sock

[supervisord]
logfile = /tmp/supervisord.log
logfile_maxbytes = 50MB
logfile_backups = 10
loglevel = info
pidfile = /tmp/supervisord.pid
nodaemon = false
minfds = 1024
minprocs = 200

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl = unix:///tmp/supervisor.sock

[include]
files = /usr/local/etc/*.conf

[inet_http_server]
port = 127.0.0.1:9001

"/etc/supervisor/conf.d/web.conf":
mode: "000755"
owner: root
group: root
content: |
[program:web]
command = gunicorn --bind :8000 --workers 2 --threads 2 vibecheck.wsgi:application
numprocs = 1
autostart = true
autorestart = unexpected
startsecs = 10
startretries = 3
exitcodes = 0
stopsignal = TERM
stopwaitsecs = 10
stopasgroup = false
killasgroup = false
user = root
redirect_stderr = false
stdout_logfile = /var/log/web.log
stdout_logfile_maxbytes = 1MB
stdout_logfile_backups = 10
stdout_capture_maxbytes = 1MB
stdout_events_enabled = false
stderr_logfile = /var/log/web.err
stderr_logfile_maxbytes = 1MB
stderr_logfile_backups = 10
stderr_capture_maxbytes = 1MB
stderr_events_enabled = false

"/etc/supervisor/conf.d/websocket.conf":
mode: "000755"
owner: root
group: root
content: |
[program:websocket]
command = daphne -b 0.0.0.0 -p 5000 vibecheck.asgi:application
numprocs = 1
autostart = true
autorestart = unexpected
startsecs = 10
startretries = 3
exitcodes = 0
stopsignal = TERM
stopwaitsecs = 10
stopasgroup = false
killasgroup = false
user = root
redirect_stderr = false
stdout_logfile = /var/log/websocket.log
stdout_logfile_maxbytes = 1MB
stdout_logfile_backups = 10
stdout_capture_maxbytes = 1MB
stdout_events_enabled = false
stderr_logfile = /var/log/websocket.err
stderr_logfile_maxbytes = 1MB
stderr_logfile_backups = 10
stderr_capture_maxbytes = 1MB
stderr_events_enabled = false
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: soloconnect.wsgi:application
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
/static: static
aws:elbv2:listener:80:
ListenerEnabled: 'true'
Protocol: HTTP
aws:elbv2:listener:5000:
ListenerEnabled: 'true'
Protocol: HTTP
10 changes: 10 additions & 0 deletions .ebextensions/04_logs.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
files:
"/opt/elasticbeanstalk/tasks/taillogs.d/daphne.conf":
mode: "000755"
owner: root
group: root
content: |
/var/log/stdout_daphne.log
/var/log/stderr_daphne.log
/var/log/stdout_worker.log
/var/log/stderr_worker.log
2 changes: 1 addition & 1 deletion .elasticbeanstalk/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ global:
instance_profile: null
platform_name: null
platform_version: null
profile: eb-cli
profile: null
repository: null
sc: git
workspace_type: Application
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ python:
script:
- black --check .
- flake8 --exclude=migrations --max-line-length=99 .
- python -m coverage run --source=soloconnect manage.py test --keepdb
- python -m coverage run manage.py test --keepdb
# - coverage run --source=soloconnect manage.py test
install:
- pip install -r requirements.txt
Expand Down Expand Up @@ -39,6 +39,6 @@ deploy:
region: $REGION_UE1
app: $APP_NAME
bucket: $BUCKET_NAME_UE1
env: soloconnect-integration
env: soloconnect-chat-integration
on:
branch: feature-trip-model
branch: feature-private-chat-1-copy
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn --bind :8000 --workers 1 --threads 2 soloconnect.wsgi:application
Empty file added chat/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions chat/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin # noqa

# Register your models here.
6 changes: 6 additions & 0 deletions chat/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ChatConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "chat"
150 changes: 150 additions & 0 deletions chat/consumers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import json

from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from django.contrib.auth.models import User
from common import db_retrieve_or_none
from .models import Thread, ChatMessage

# class ChatConsumer(WebsocketConsumer):
# def __init__(self, *args, **kwargs):
# super().__init__(args, kwargs)
# self.room_group_name = None
# self.room_name = None
# self.user = None
#
# def connect(self):
# self.user = self.scope["user"]
# self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
# self.room_group_name = f"chat_room_{self.user.username}"
#
# # Join room group
# async_to_sync(self.channel_layer.group_add)(
# self.room_group_name, self.channel_name
# )
#
# self.accept()
#
# def receive(self, text_data):
# text_data_json = json.loads(text_data)
# message = text_data_json["message"]
# username = self.user.username
#
# # Send message to room group
# async_to_sync(self.channel_layer.group_send)(
# self.room_group_name,
# {"type": "chat.message", "message": message, "username": username},
# )
#
# # Receive message from room group
# def chat_message(self, event):
# message = event["message"]
# username = event["username"]
# # Send message to WebSocket
# self.send(
# text_data=json.dumps(
# {
# "type": "chat",
# "message": message,
# "username": username,
# }
# )
# )
#
# def disconnect(self, close_code):
# # Leave room group
# async_to_sync(self.channel_layer.group_discard)(
# self.room_group_name, self.channel_name
# )


class ChatConsumer(WebsocketConsumer):
def __init__(self, *args, **kwargs):
super().__init__(args, kwargs)
self.user = None
self.chat_room = None

def connect(self):
self.user = self.scope["user"]
self.chat_room = f"user_chatroom_{self.user.id}"
async_to_sync(self.channel_layer.group_add)(self.chat_room, self.channel_name)
self.accept()

def receive(self, text_data):
received_json = json.loads(text_data)
message = received_json["message"]
sending_user_id = received_json["sent_by"]
receiving_user_id = received_json["send_to"]
thread_id = received_json["thread_id"]

if not message:
print("Error: empty message")
return False

sending_user_instance = self.get_user_instance(sending_user_id)
receiving_user_instance = self.get_user_instance(receiving_user_id)

thread_instance = self.get_thread(thread_id)

if not sending_user_instance:
print("Error: SUI is incorrect")
if not receiving_user_instance:
print("Error: RUI is incorrect")
if not thread_instance:
print("Error: thread instance is incorrect")

target_chat_room = f"user_chatroom_{receiving_user_id}"

self.create_chatmessage_object(thread_instance, sending_user_instance, message)

async_to_sync(self.channel_layer.group_send)(
self.chat_room,
{
"type": "chat.message",
"message": message,
"sent_by": self.user.id,
"thread_id": thread_id,
"send_to": receiving_user_id,
"user_name": sending_user_instance.username,
},
)
print("user name: " + sending_user_instance.username)
async_to_sync(self.channel_layer.group_send)(
target_chat_room,
{
"type": "chat.message",
"message": message,
"sent_by": self.user.id,
"thread_id": thread_id,
"send_to": receiving_user_id,
"user_name": sending_user_instance.username,
},
)

def chat_message(self, event):
self.send(
text_data=json.dumps(
{
"type": "chat",
"message": event["message"],
"sent_by": event["sent_by"],
"thread_id": event["thread_id"],
"send_to": event["send_to"],
"user_name": event["user_name"],
}
)
)

def disconnect(self, close_code):
pass

def get_user_instance(self, user_id):
return db_retrieve_or_none(User, user_id)

def get_thread(self, thread_id):
return db_retrieve_or_none(Thread, thread_id)

def create_chatmessage_object(self, thread, sending_user, message):
ChatMessage.objects.create(
thread=thread, sending_user=sending_user, message=message
)
88 changes: 88 additions & 0 deletions chat/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Generated by Django 4.2.7 on 2023-11-30 18:14

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Thread",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("updated", models.DateTimeField(auto_now=True)),
("timestamp", models.DateTimeField(auto_now_add=True)),
(
"first_user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="chatroom_first_user",
to=settings.AUTH_USER_MODEL,
),
),
(
"second_user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="chatroom_second_user",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"unique_together": {("first_user", "second_user")},
},
),
migrations.CreateModel(
name="ChatMessage",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("message", models.TextField()),
("timestamp", models.DateTimeField(auto_now_add=True)),
(
"sending_user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
(
"thread",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="chat_message",
to="chat.thread",
),
),
],
),
]
Empty file added chat/migrations/__init__.py
Empty file.

0 comments on commit 2dbb0b0

Please sign in to comment.