def context(self): """See soc.views.template.Template.context for full specification.""" conv_model = self.conversation.get() context = { 'subject': conv_model.subject, 'num_messages': gcimessage_logic.numMessagesInConversation(conv_model.key), 'num_new_messages': gciconversation_logic.numUnreadMessagesForConversationAndUser( conv_model.key, ndb.Key.from_old_key(self.data.user.key())), } last_message = gcimessage_logic.getLastMessageForConversation( conv_model.key) if last_message is not None: last_message_author = db.get( ndb.Key.to_old_key(last_message.author)) context['last_message_author'] = last_message_author.name context['last_message_content'] = (html.strip_tags( last_message.content).replace('\r', '').replace('\n', ' ').strip()) context['last_message_time'] = timeformat_helper.relativeTime( last_message.sent_on) context['last_message_ctime'] = last_message.sent_on.ctime() return context
def testGetLastMessageForConversation(self): """Tests that getLastMessageForConversation returns the last GCIMessage in a conversation. """ last_msg = self.msg_keys[-1].get() # Make sure we get the last message expected = last_msg actual = gcimessage_logic.getLastMessageForConversation( conversation=self.conv.key) self.assertEqual(expected, actual) # Add a new message, one minute after the last one new_msg = self.conv_utils.addMessage(conversation=self.conv.key, time=(last_msg.sent_on + timedelta(minutes=1))) # Make sure we get the new last message expected = new_msg actual = gcimessage_logic.getLastMessageForConversation( conversation=self.conv.key) self.assertEqual(expected, actual)
def testGetLastMessageForConversation(self): """Tests that getLastMessageForConversation returns the last GCIMessage in a conversation. """ last_msg = self.msg_keys[-1].get() # Make sure we get the last message expected = last_msg actual = gcimessage_logic.getLastMessageForConversation( conversation=self.conv.key) self.assertEqual(expected, actual) # Add a new message, one minute after the last one new_msg = self.conv_utils.addMessage( conversation=self.conv.key, time=(last_msg.sent_on + timedelta(minutes=1))) # Make sure we get the new last message expected = new_msg actual = gcimessage_logic.getLastMessageForConversation( conversation=self.conv.key) self.assertEqual(expected, actual)
def markAllReadForConversationAndUser(conversation, user): """Marks all messages in a conversation as read for the user. Sets the GCIConversationUser's last_message_seen_on to the last message's sent_on. Args: conversation: Key (ndb) of GCIConversation. user: Key (ndb) of User. """ conv_user_results = queryConversationUserForConversationAndUser( conversation, user).fetch(1) if not conv_user_results: raise Exception('No GCIConversationUser could be found.') conv_user = conv_user_results[0] last_message = gcimessage_logic.getLastMessageForConversation(conversation) conv_user.last_message_seen_on = last_message.sent_on conv_user.put()