def create_conversation(self, convo): """Create conversation or marks them for update based on last message time""" convo_id = convo['id'] # id looks like: u'77871924:6924356' # our id is first from_user_id = convo_id.split(':')[1] # username from conversation list of users try: from_user_name = (user['username'] for user in convo['users'] if str(user['id']) == from_user_id).next() except KeyError as e: if from_user_id == 'system': from_user_name = 'system' else: raise e last_message_time = parser.parse(convo['last_message']['sent_at']) try: convo = Conversation.objects.get(convo_id=convo_id) convo.needs_update = convo.last_message_time != last_message_time convo.save() except Conversation.DoesNotExist: convo = Conversation( convo_id=convo_id, user_name=from_user_name, user_id=from_user_id, last_message_time=last_message_time ) convo.save() logger.info('created conversation: %s', convo)
def webapp_conversation_start(request, uuid): template = loader.get_template('annonce.html') context = {} ad = Ad.objects.get(pk=uuid) try: conversation = Conversation.objects.get(ad=ad) try: messages = Message.objects.get(conversation=conversation) except Message.DoesNotExist: messages = None context.update({ 'ad': ad, 'conversation': conversation }) except Conversation.DoesNotExist: Conversation(ad=ad).save() conversation = Conversation.objects.get(ad=ad) if conversation is not None: context.update({ 'ad': ad, 'conversation': conversation }) return HttpResponse(template.render(context, request))
def test_register_response(self, mock_tasks): self.mock_wallet.return_value.get_new_address.return_value = '1' convo = Conversation( convo_id='test', last_message_time=datetime.now() ) convo.save() Message( user_name='bopeep2', user_id='whatev', conversation=convo, message='register', sent_at=datetime.now() ).save() self.processor.process_messages() self.assertEqual(User.objects.get(user_name='bopeep2').deposit_address, '1')
def create_pm(sender, text, user_channel_id): """ Create a new message """ try: lc = UserChannel.objects.get(id=user_channel_id) uc = UserChannel.objects.get(channel_id=lc.channel_id, nickname=sender) except UserChannel.DoesNotExist: uc = UserChannel(channel_id=lc.channel_id, nickname=sender) uc.save() try: conv = Conversation.objects.get( Q(user_channel_1__in=[lc, uc]) & Q(user_channel_2__in=[lc, uc])) except Exception: conv = Conversation(user_channel_1=lc, user_channel_2=uc) conv.save() pm = PrivateMessage(conversation=conv, user_channel=uc, text=text) pm.save() return pm