def add_friend(self, account): """ Add a new friend. """ if not account in self.friends.all(): self.friends.add(account) self.save() content_type = ContentType.objects.get_for_model(self) # Create notification # Can create this way if you want. Doesn't matter. # Notification( # target=self.user, # from_user=account, # redirect_url=f"{settings.BASE_URL}/account/{account.pk}/", # verb=f"You are now friends with {account.username}.", # content_type=content_type, # object_id=self.id, # ).save() self.notifications.create( target=self.user, from_user=account, redirect_url=f"{settings.BASE_URL}/account/{account.pk}/", verb=f"You are now friends with {account.username}.", content_type=content_type, ) self.save() # Create a private chat (or activate an old one) chat = find_or_create_private_chat(self.user, account) if not chat.is_active: chat.is_active = True chat.save()
def remove_friend(self, account): if account in self.friends.all(): self.friends.remove(account) # Deactivate the private chat between these two users chat = find_or_create_private_chat(self.user, account) if chat.is_active: chat.is_active = False chat.save()
def remove_friend(self, account): """ Remove a friend """ if account in self.friends.all(): self.friends.remove(account) chat = find_or_create_private_chat(self.user, account) if chat.is_active: chat.is_active = False chat.save()
def get_recent_chatroom_messages(user): """ sort in terms of most recent chats (users that you most recently had conversations with) """ # 1. Find all the rooms this user is a part of rooms1 = PrivateChatRoom.objects.filter(user1=user, is_active=True) rooms2 = PrivateChatRoom.objects.filter(user2=user, is_active=True) # 2. merge the lists rooms = list(chain(rooms1, rooms2)) # 3. find the newest msg in each room m_and_f = [] for room in rooms: # Figure out which user is the "other user" (aka friend) if room.user1 == user: friend = room.user2 else: friend = room.user1 # confirm you are even friends (in case chat is left active somehow) friend_list = FriendList.objects.get(user=user) if not friend_list.is_mutual_friend(friend): chat = find_or_create_private_chat(user, friend) chat.is_active = False chat.save() else: # find newest msg from that friend in the chat room try: message = RoomChatMessage.objects.filter( room=room, user=friend).latest("timestamp") except RoomChatMessage.DoesNotExist: # create a dummy message with dummy timestamp today = datetime(year=1950, month=1, day=1, hour=1, minute=1, second=1, tzinfo=pytz.UTC) message = RoomChatMessage( user=friend, room=room, timestamp=today, content="", ) m_and_f.append({'message': message, 'friend': friend}) return sorted(m_and_f, key=lambda x: x['message'].timestamp, reverse=True)
def add_friend(self, account): """ add a new friend """ if not account in self.friends.all(): self.friends.add(account) self.save() #Create a private chat (or activate an old one) chat = find_or_create_private_chat(self.user, account) if not chat.is_active: chat.is_active = True chat.save()
def create_or_return_private_chat(request, *args, **kwargs): user1 = request.user payload = {} if user1.is_authenticated: if request.method == "POST": user2_id = request.POST.get("user2_id") try: user2 = Account.objects.get(pk=user2_id) chat = find_or_create_private_chat(user1, user2) payload['response'] = "Successfully got the chat." payload['chatroom_id'] = chat.id except Account.DoesNotExist: payload['response'] = "Unable to start a chat with that user." else: payload[ 'response'] = "You can't start a chat if you are not authenticated." return HttpResponse(json.dumps(payload), content_type="application/json")