def create_new_chat(request): chat_creator = SocialUser.objects.get(id= request.POST['creator_id']) chat_title = request.POST['title'] chat_users = request.POST['users_id'] date_created = datetime.datetime.now() last_modified = date_created chat = Chats(creator=chat_creator, date_created = date_created, title = chat_title, last_modified = last_modified) chat.save() client = Client("http://127.0.0.1:8080/publish") for chat_user in chat_users: social_user = SocialUser.objects.get(id=chat_user) chats_users = ChatsUsers(chat = chat, user = social_user) chats_users.save() result = client.publish(topic="SocialUserId."+social_user.id, data={"event":"new_chat","chat_data":{"chat_creator_id":chat_creator.id, "chat_title": chat_title, "last_modified": last_modified}})
def send_message(request): chat_id = request.POST['chat_id'] chat = Chats.objects.get(id=chat_id) message = request.POST['message'] sender = SocialUser.objects.get(id=request.POST['sender_id']) chats_users = ChatsUsers.objects.filter(chat = chat) if chats_users.filter(user=sender) is not None: chats_users_model = ChatsUsers(chat=chat, user= sender) chats_users_model.save() chats_users = chats_users.exclude(user= sender) client = Client("http://127.0.0.1:8080/publish") for chat_user in chats_users: result = client.publish(topic="SocialUserId."+chat_user.user.id, data={"event":"new_msg","msg_data":{"msg_sender_id":sender.id, "chat_id": chat_user.chat.id, "msg_content": message}}) chat_message = ChatMessages(text = message, date_pub = datetime.now(), chat = chat, user = sender) chat_message.save()