Skip to content

Commit

Permalink
Merge pull request #32 from dibs-devs/ahmed
Browse files Browse the repository at this point in the history
Features and UI Improvements
  • Loading branch information
dibs-devs authored May 26, 2019
2 parents 74c5780 + 87695d2 commit eec348f
Show file tree
Hide file tree
Showing 22 changed files with 1,000 additions and 375 deletions.
2 changes: 1 addition & 1 deletion chatter/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
TIME_ZONE = "America/New_York"

USE_I18N = True

Expand Down
106 changes: 48 additions & 58 deletions django_chatter/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
--------------------------------------------------------------------------AI'''
from django.contrib.auth import get_user_model
from django.db import connection
from django.utils.timezone import now


'''AI--------------------------------------------------------------------------
Third-party Imports
--------------------------------------------------------------------------AI'''
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from channels.db import database_sync_to_async
import bleach

Expand Down Expand Up @@ -72,16 +71,18 @@ def save_message(room, sender, text, multitenant=False, schema_name=None):
new_message.save()
room.date_modified = new_message.date_modified
room.save()
return new_message.date_created
else:
new_message = Message(room=room, sender=sender, text=text)
new_message.save()
new_message.recipients.add(sender)
new_message.save()
room.date_modified = new_message.date_modified
room.save()
return new_message.date_created


class ChatConsumer(AsyncWebsocketConsumer):
class ChatConsumer(AsyncJsonWebsocketConsumer):

'''
AI-------------------------------------------------------------------
Expand Down Expand Up @@ -134,64 +135,53 @@ async def disconnect(self, close_code):
self.channel_name
)

async def receive(self, text_data):
async def receive_json(self, data):
username = self.user.username

text_data_json = json.loads(text_data)
message = text_data_json['message']
room_id = text_data_json['room_id']
message_type = data['type']
if message_type == "text":
message = data['message']
room_id = data['room_id']

# Clean code off message if message contains code
self.message_safe = bleach.clean(message)
message_harmful = (self.message_safe != message)
# Clean code off message if message contains code
self.message_safe = bleach.clean(message)
message_harmful = (self.message_safe != message)

try:
# room = await self.get_room(room_id)
room_group_name = 'chat_%s' % room_id
except Exception as ex:
raise ex
await self.disconnect(500)

await save_message(self.room,
self.user,
self.message_safe,
self.multitenant,
self.schema_name
)

if message_harmful:
warning = "Your message has been escaped due to security reasons.\
For more information, see \
https://en.wikipedia.org/wiki/Cross-site_scripting"
else:
warning = ''

await self.channel_layer.group_send(
room_group_name,
{
'type': 'send_to_websocket',
'message': self.message_safe,
'warning': warning,
'sender': username,
'room_id': room_id,
}
)
try:
# room = await self.get_room(room_id)
room_group_name = 'chat_%s' % room_id
except Exception as ex:
raise ex
await self.disconnect(500)

time = await save_message(self.room,
self.user,
self.message_safe,
self.multitenant,
self.schema_name
)
time = time.strftime("%d %b %Y %H:%M:%S %Z")


if message_harmful:
warning = "Your message has been escaped due to security reasons.\
For more information, see \
https://en.wikipedia.org/wiki/Cross-site_scripting"
else:
warning = ''

await self.channel_layer.group_send(
room_group_name,
{
'type': 'send_to_websocket',
'message_type': 'text',
'message': self.message_safe,
'date_created': time,
'warning': warning,
'sender': username,
'room_id': room_id,
}
)

async def send_to_websocket(self, event):
message = event['message']
warning = event['warning']
sender = event['sender']
room_id = event['room_id']
if warning == '':
await self.send(text_data=(json.dumps({
'message': message,
'sender': sender,
'room_id': room_id,
})))
else:
await self.send(text_data=(json.dumps({
'message': message,
'sender': sender,
'warning': warning,
'room_id': room_id
})))
await self.send_json(event)
17 changes: 17 additions & 0 deletions django_chatter/migrations/0002_auto_20190524_2308.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.0.9 on 2019-05-25 03:08

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('django_chatter', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='message',
options={'ordering': ['-id']},
),
]
17 changes: 17 additions & 0 deletions django_chatter/migrations/0003_auto_20190524_2315.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.0.9 on 2019-05-25 03:15

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('django_chatter', '0002_auto_20190524_2308'),
]

operations = [
migrations.AlterModelOptions(
name='message',
options={},
),
]
6 changes: 6 additions & 0 deletions django_chatter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class UserProfile(models.Model):
on_delete=models.CASCADE, related_name='profile')
last_visit = models.DateTimeField()


# This model is used to give date and time when a message was created/modified.
class DateTimeModel(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
Expand All @@ -17,6 +18,7 @@ class DateTimeModel(models.Model):
class Meta:
abstract = True


class Room(DateTimeModel):
id = models.UUIDField(primary_key=True,
default=uuid.uuid4,
Expand All @@ -31,6 +33,7 @@ def __str__(self):

return ", ".join(members_list)


class Message(DateTimeModel):
sender = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, related_name='sender')
Expand All @@ -41,3 +44,6 @@ class Message(DateTimeModel):

def __str__(self):
return f'{self.text} sent by "{self.sender}" in Room "{self.room}"'

class Meta:
ordering = ['-id']
Loading

0 comments on commit eec348f

Please sign in to comment.