NDOJ/chat_box/models.py

155 lines
4.7 KiB
Python
Raw Normal View History

2020-01-27 20:37:52 +00:00
from django.db import models
2023-08-28 19:20:35 +00:00
from django.db.models import CASCADE, Q
2020-01-28 20:12:17 +00:00
from django.utils.translation import gettext_lazy as _
2023-11-02 01:54:09 +00:00
from django.utils.functional import cached_property
2020-01-28 20:12:17 +00:00
from judge.models.profile import Profile
2023-08-28 19:20:35 +00:00
from judge.caching import cache_wrapper
2020-01-28 20:12:17 +00:00
2022-06-18 07:58:59 +00:00
__all__ = ["Message", "Room", "UserRoom", "Ignore"]
2022-05-14 17:57:27 +00:00
2020-01-28 20:12:17 +00:00
2021-11-21 04:23:03 +00:00
class Room(models.Model):
2022-05-14 17:57:27 +00:00
user_one = models.ForeignKey(
Profile, related_name="user_one", verbose_name="user 1", on_delete=CASCADE
)
user_two = models.ForeignKey(
Profile, related_name="user_two", verbose_name="user 2", on_delete=CASCADE
)
2023-11-02 01:54:09 +00:00
last_msg_time = models.DateTimeField(
verbose_name=_("last seen"), null=True, db_index=True
)
2021-11-21 04:23:03 +00:00
2023-10-10 22:38:48 +00:00
class Meta:
app_label = "chat_box"
unique_together = ("user_one", "user_two")
2023-10-10 22:38:48 +00:00
2023-11-02 01:54:09 +00:00
@cached_property
def _cached_info(self):
2024-02-23 23:07:34 +00:00
return get_room_info(self.id)
2023-11-02 01:54:09 +00:00
2021-11-21 04:23:03 +00:00
def contain(self, profile):
2024-02-23 23:07:34 +00:00
return profile.id in [self.user_one_id, self.user_two_id]
2022-05-14 17:57:27 +00:00
2021-11-21 04:23:03 +00:00
def other_user(self, profile):
return self.user_one if profile == self.user_two else self.user_two
2022-05-14 17:57:27 +00:00
2023-11-02 01:54:09 +00:00
def other_user_id(self, profile):
2024-02-23 23:07:34 +00:00
user_ids = [self.user_one_id, self.user_two_id]
2023-11-02 01:54:09 +00:00
return sum(user_ids) - profile.id
2021-11-21 04:23:03 +00:00
def users(self):
return [self.user_one, self.user_two]
2020-01-28 20:12:17 +00:00
2023-08-29 23:36:01 +00:00
def last_message_body(self):
2023-11-02 01:54:09 +00:00
return self._cached_info["last_message"]
2023-08-29 23:36:01 +00:00
2024-02-23 23:07:34 +00:00
@classmethod
def prefetch_room_cache(self, room_ids):
get_room_info.prefetch_multi([(i,) for i in room_ids])
2022-05-14 17:57:27 +00:00
2020-01-28 20:12:17 +00:00
class Message(models.Model):
2022-05-14 17:57:27 +00:00
author = models.ForeignKey(Profile, verbose_name=_("user"), on_delete=CASCADE)
2023-08-28 19:20:35 +00:00
time = models.DateTimeField(
verbose_name=_("posted time"), auto_now_add=True, db_index=True
)
2022-05-14 17:57:27 +00:00
body = models.TextField(verbose_name=_("body of comment"), max_length=8192)
2023-03-08 07:25:54 +00:00
hidden = models.BooleanField(verbose_name="is hidden", default=False)
2022-05-14 17:57:27 +00:00
room = models.ForeignKey(
Room, verbose_name="room id", on_delete=CASCADE, default=None, null=True
)
2020-01-28 20:12:17 +00:00
def save(self, *args, **kwargs):
self.body = self.body.strip()
super(Message, self).save(*args, **kwargs)
2020-01-27 20:37:52 +00:00
2020-01-28 20:12:17 +00:00
class Meta:
2022-05-14 17:57:27 +00:00
verbose_name = "message"
verbose_name_plural = "messages"
2023-03-08 07:25:54 +00:00
ordering = ("-id",)
indexes = [
models.Index(fields=["hidden", "room", "-id"]),
]
2023-10-10 22:38:48 +00:00
app_label = "chat_box"
2022-05-14 17:57:27 +00:00
2021-11-21 04:23:03 +00:00
class UserRoom(models.Model):
2022-05-14 17:57:27 +00:00
user = models.ForeignKey(Profile, verbose_name=_("user"), on_delete=CASCADE)
room = models.ForeignKey(
Room, verbose_name="room id", on_delete=CASCADE, default=None, null=True
)
last_seen = models.DateTimeField(verbose_name=_("last seen"), auto_now_add=True)
2023-08-28 19:20:35 +00:00
unread_count = models.IntegerField(default=0, db_index=True)
2021-11-21 04:23:03 +00:00
2022-10-27 20:02:54 +00:00
class Meta:
unique_together = ("user", "room")
2023-10-10 22:38:48 +00:00
app_label = "chat_box"
2021-11-21 04:23:03 +00:00
2022-10-29 02:29:48 +00:00
2021-11-21 04:23:03 +00:00
class Ignore(models.Model):
2022-06-18 07:58:59 +00:00
user = models.OneToOneField(
2022-05-14 17:57:27 +00:00
Profile,
related_name="ignored_chat_users",
verbose_name=_("user"),
on_delete=CASCADE,
2022-06-18 07:58:59 +00:00
db_index=True,
2022-05-14 17:57:27 +00:00
)
2021-11-21 04:23:03 +00:00
ignored_users = models.ManyToManyField(Profile)
2023-10-10 22:38:48 +00:00
class Meta:
app_label = "chat_box"
2021-11-21 04:23:03 +00:00
@classmethod
def is_ignored(self, current_user, new_friend):
try:
2023-08-28 19:20:35 +00:00
return current_user.ignored_chat_users.ignored_users.filter(
id=new_friend.id
).exists()
2021-11-21 04:23:03 +00:00
except:
return False
@classmethod
def get_ignored_users(self, user):
2021-11-23 03:54:48 +00:00
try:
2022-06-18 07:58:59 +00:00
return self.objects.get(user=user).ignored_users.all()
2022-04-29 20:27:25 +00:00
except:
2021-11-23 03:54:48 +00:00
return Profile.objects.none()
2021-11-21 04:23:03 +00:00
2023-08-28 19:20:35 +00:00
@classmethod
def get_ignored_rooms(self, user):
try:
ignored_users = self.objects.get(user=user).ignored_users.all()
return Room.objects.filter(Q(user_one=user) | Q(user_two=user)).filter(
Q(user_one__in=ignored_users) | Q(user_two__in=ignored_users)
)
except:
return Room.objects.none()
2021-11-21 04:23:03 +00:00
@classmethod
def add_ignore(self, current_user, friend):
2022-05-14 17:57:27 +00:00
ignore, created = self.objects.get_or_create(user=current_user)
2021-11-21 04:23:03 +00:00
ignore.ignored_users.add(friend)
@classmethod
def remove_ignore(self, current_user, friend):
2022-05-14 17:57:27 +00:00
ignore, created = self.objects.get_or_create(user=current_user)
2021-11-21 04:23:03 +00:00
ignore.ignored_users.remove(friend)
@classmethod
def toggle_ignore(self, current_user, friend):
2022-05-14 17:57:27 +00:00
if self.is_ignored(current_user, friend):
2021-11-21 04:23:03 +00:00
self.remove_ignore(current_user, friend)
else:
2022-05-14 17:57:27 +00:00
self.add_ignore(current_user, friend)
2024-02-23 23:07:34 +00:00
@cache_wrapper(prefix="Rinfo")
def get_room_info(room_id):
last_msg = Message.objects.filter(room_id=room_id).first()
return {
"last_message": last_msg.body if last_msg else None,
}