Make chat faster
This commit is contained in:
parent
f11d9b4b53
commit
2854ac97e9
11 changed files with 903 additions and 801 deletions
20
chat_box/migrations/0013_alter_message_time.py
Normal file
20
chat_box/migrations/0013_alter_message_time.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Generated by Django 3.2.18 on 2023-08-28 01:24
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("chat_box", "0012_auto_20230308_1417"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="message",
|
||||
name="time",
|
||||
field=models.DateTimeField(
|
||||
auto_now_add=True, db_index=True, verbose_name="posted time"
|
||||
),
|
||||
),
|
||||
]
|
38
chat_box/migrations/0014_userroom_unread_count.py
Normal file
38
chat_box/migrations/0014_userroom_unread_count.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
# Generated by Django 3.2.18 on 2023-08-28 06:02
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def migrate(apps, schema_editor):
|
||||
UserRoom = apps.get_model("chat_box", "UserRoom")
|
||||
Message = apps.get_model("chat_box", "Message")
|
||||
|
||||
for ur in UserRoom.objects.all():
|
||||
if not ur.room:
|
||||
continue
|
||||
messages = ur.room.message_set
|
||||
last_msg = messages.first()
|
||||
try:
|
||||
if last_msg and last_msg.author != ur.user:
|
||||
ur.unread_count = messages.filter(time__gte=ur.last_seen).count()
|
||||
else:
|
||||
ur.unread_count = 0
|
||||
ur.save()
|
||||
except:
|
||||
continue
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("chat_box", "0013_alter_message_time"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="userroom",
|
||||
name="unread_count",
|
||||
field=models.IntegerField(db_index=True, default=0),
|
||||
),
|
||||
migrations.RunPython(migrate, migrations.RunPython.noop, atomic=True),
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue