def server_add_friend(self, data): username = data['username'] friend_username = data['friend_username'] print(f"server_add_friend, name:{username}, friend: {friend_username}") user = User.objects.get(username=username) friend = User.objects.get(username=friend_username) user.znajomi.add(friend) N = 20 res = ''.join( random.choices(string.ascii_uppercase + string.digits, k=N)) print(f'tworze nowa konwersacje: nazwa to: {res}') conversation = Conversation(name=res) conversation.save() conversation.related_users.add(user, friend) self.send( json.dumps({ 'command': 'js_add_friend_to_dom', 'username': friend_username }))
def _create_match(self, user_id1, user_id2): print(f'{user_id1} with {user_id2}') attendees_user_ids = [user_id1, user_id2] # calculating attendees dict chat_users = ChatUser.objects.filter(id__in=attendees_user_ids) attendees = {chat_user.id: chat_user.name for chat_user in chat_users} conversation = Conversation.create_conversation(attendees_user_ids) if self._matchcreated_callback is not None: self._matchcreated_callback(self._pool[user_id1], self._pool[user_id2], conversation.id, attendees) del self._pool[user_id1] del self._pool[user_id2]
def execute(self, message, message_data): print(message_data) to_return = {'intent:': self.name, 'success': False, 'error': None} if 'conversation_id' in message_data: conversation_id = message_data['conversation_id'] result = Conversation.objects.filter(id=conversation_id) if result.count() != 0: conv = result.first() if message.user in conv.participants.all(): to_return['success'] = True new_message = Message() new_message.content = message_data['content'] new_message.conversation = conv new_message.from_user = message.user new_message.save() unixtime = time.mktime(new_message.date_sent.timetuple()) for p in conv.participants.all(): Group("chat-%s" % p.username).send({ "text": json.dumps({"intent": "receive_message", "conversation_id": conversation_id, "content": message_data['content'], "date_sent": unixtime, "username": new_message.from_user.username}) }) else: to_return['error'] = "You do not have access to this conversation." elif 'username' in message_data: recipient_username = message_data['username'] if recipient_username in get_friends(message.user) and recipient_username != message.user.username: to_return['success'] = True recipient = User.objects.get(username=recipient_username) conv = Conversation() conv.save() conv.participants.add(recipient) conv.participants.add(message.user) new_message = Message() new_message.content = message_data['content'] new_message.conversation = conv new_message.from_user = message.user new_message.save() conv.save() print(new_message.date_sent) unixtime = time.mktime(new_message.date_sent.timetuple()) for p in conv.participants.all(): print(("chat-%s" % p.username)) Group("chat-%s" % p.username).send({ "text": json.dumps({"intent": "receive_message", "conversation_id": conv.id, "content": message_data['content'], "date_sent": unixtime, "username": message.from_user.username}) }) else: to_return['error'] = "You cannot speak with this user as you are not friends with the user." else: to_return['error'] = "You appear to have sent a malformed request." Group("chat-%s" % message.user.username).send({ "text": json.dumps(to_return) })
def send_message(self, request): self.log_action = "send" self.log_resource = "message" self.log_context = {} if "file" in request.data: file = request.FILES["file"] data = json.loads(request.data["data"]) username = data["email"] user_two = data["user_two"] subject = data["subject"] msg_text = data["text"] create_date = data["create_date"] else: file = None data = ( request.data if request.data else json.loads(request.body.decode("utf-8")) ) username = data["email"] user_two = data["user_two"] subject = data["subject"] msg_text = data["text"] create_date = data["create_date"] info = {} if not user_two == "" and not username == "": user = User.objects.get(email=username) user_to = User.objects.get(email=user_two) talks = Conversation.objects.filter( (Q(user_one__email=username) & Q(user_two__email=user_two)) | (Q(user_two__email=username) & Q(user_one__email=user_two)) ) if talks.count() > 0: talk = talks[0] else: talk = Conversation() talk.user_one = user talk.user_two = user_to talk.save() if subject != "": subject = Subject.objects.get(slug=subject) space = subject.slug space_type = "subject" self.log_context["subject_id"] = subject.id self.log_context["subject_slug"] = space self.log_context["subject_name"] = subject.name else: subject = None space = 0 space_type = "general" message = TalkMessages() message.text = "<p>" + msg_text + "</p>" message.user = user message.talk = talk message.subject = subject if not file is None: message.image = file message.save() self.log_context["talk_id"] = talk.id self.log_context["user_id"] = user_to.id self.log_context["user_name"] = str(user_to) self.log_context["user_email"] = user_two if not message.pk is None: simple_notify = textwrap.shorten( strip_tags(message.text), width=30, placeholder="..." ) notification = { "type": "chat", "subtype": space_type, "space": space, "user_icon": message.user.image_url, "notify_title": str(message.user), "simple_notify": simple_notify, "view_url": reverse( "chat:view_message", args=(message.id,), kwargs={} ), "complete": render_to_string( "chat/_message.html", {"talk_msg": message}, request ), "container": "chat-" + str(message.user.id), "last_date": _("Last message in %s") % ( formats.date_format( message.create_date, "SHORT_DATETIME_FORMAT" ) ), } notification = json.dumps(notification) Group("user-%s" % user_to.id).send({"text": notification}) ChatVisualizations.objects.create( viewed=False, message=message, user=user_to ) serializer = ChatSerializer(message) json_r = json.dumps(serializer.data) json_r = json.loads(json_r) info["data"] = {} info["data"]["message_sent"] = json_r info["message"] = _("Message sent successfully!") info["success"] = True info["number"] = 1 sendChatPushNotification(user_to, message) super(ChatViewset, self).createLog( user, self.log_component, self.log_action, self.log_resource, self.log_context, ) else: info["message"] = _("Error while sending message!") info["success"] = False info["number"] = 0 else: info["data"] = {} info["data"]["message_sent"] = {} info["message"] = _("No information received!") info["success"] = False info["number"] = 0 info["data"]["messages"] = [] info["type"] = "" info["title"] = _("Amadeus") info["extra"] = 0 response = json.dumps(info) return HttpResponse(response)
def home_view(request): if request.user.is_authenticated(): if request.method == 'POST': doctor_p = Conversation() man = Conversation() man.massage = request.POST.get('massage') man.type_id = 1 doctor_p.massage = doctor.get_response(request.POST.get('massage')) doctor_p.type_id = 2 man.save() doctor_p.save() context = Conversation.objects.all() return render(request, 'design/chater.html', {'conversations': context}) else: return redirect('login_views')
def send_message(self, request): self.log_action = 'send' self.log_resource = 'message' self.log_context = {} if 'file' in request.data: file = request.FILES['file'] data = json.loads(request.data['data']) username = data['email'] user_two = data['user_two'] subject = data['subject'] msg_text = data['text'] create_date = data['create_date'] else: file = None data = request.data if request.data else json.loads(request.body.decode('utf-8')) username = data['email'] user_two = data['user_two'] subject = data['subject'] msg_text = data['text'] create_date = data['create_date'] info = {} if not user_two == "" and not username == "": user = User.objects.get(email = username) user_to = User.objects.get(email = user_two) talks = Conversation.objects.filter((Q(user_one__email = username) & Q(user_two__email = user_two)) | (Q(user_two__email = username) & Q(user_one__email = user_two))) if talks.count() > 0: talk = talks[0] else: talk = Conversation() talk.user_one = user talk.user_two = user_to talk.save() if subject != "": subject = Subject.objects.get(slug = subject) space = subject.slug space_type = "subject" self.log_context['subject_id'] = subject.id self.log_context['subject_slug'] = space self.log_context['subject_name'] = subject.name else: subject = None space = 0 space_type = "general" message = TalkMessages() message.text = "<p>" + msg_text + "</p>" message.user = user message.talk = talk message.subject = subject if not file is None: message.image = file message.save() self.log_context['talk_id'] = talk.id self.log_context['user_id'] = user_to.id self.log_context['user_name'] = str(user_to) self.log_context['user_email'] = user_two if not message.pk is None: simple_notify = textwrap.shorten(strip_tags(message.text), width = 30, placeholder = "...") notification = { "type": "chat", "subtype": space_type, "space": space, "user_icon": message.user.image_url, "notify_title": str(message.user), "simple_notify": simple_notify, "view_url": reverse("chat:view_message", args = (message.id, ), kwargs = {}), "complete": render_to_string("chat/_message.html", {"talk_msg": message}, request), "container": "chat-" + str(message.user.id), "last_date": _("Last message in %s")%(formats.date_format(message.create_date, "SHORT_DATETIME_FORMAT")) } notification = json.dumps(notification) Group("user-%s" % user_to.id).send({'text': notification}) ChatVisualizations.objects.create(viewed = False, message = message, user = user_to) serializer = ChatSerializer(message) json_r = json.dumps(serializer.data) json_r = json.loads(json_r) info["data"] = {} info["data"]["message_sent"] = json_r info["message"] = _("Message sent successfully!") info["success"] = True info["number"] = 1 sendChatPushNotification(user_to, message) super(ChatViewset, self).createLog(user, self.log_component, self.log_action, self.log_resource, self.log_context) else: info["message"] = _("Error while sending message!") info["success"] = False info["number"] = 0 else: info["data"] = {} info["data"]["message_sent"] = {} info["message"] = _("No information received!") info["success"] = False info["number"] = 0 info["data"]["messages"] = [] info["type"] = "" info["title"] = _("Amadeus") info['extra'] = 0 response = json.dumps(info) return HttpResponse(response)