コード例 #1
0
def insert_room_chat_messages(num_messages):
    mitch = Account.objects.get(pk=1)
    jessica = Account.objects.get(pk=5)
    room = Room.objects.get(pk=room_id)
    for x in range(0, num_messages):
        content = str(x) + " " + get_random_string(25)
        if x % 2 == 0:
            usr = mitch
        else:
            usr = jessica
        message = RoomChatMessage(user=usr, room=room, content=content)
        message.save()
コード例 #2
0
ファイル: views.py プロジェクト: katkoffff/web
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)
コード例 #3
0
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)
    rooms2 = PrivateChatRoom.objects.filter(user2=user)

    # 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
        # 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})

    date_format = '%m/%d/%Y %I:%M %p'
    return sorted(m_and_f, key=lambda x: x['message'].timestamp, reverse=True)