def get(self):
    thread_id = self.request.get('thread')
    thread = gql_limit1(Thread, thread_id = thread_id)
    messages = Message.all().filter('thread_id = ', thread_id)

    formatted_messages = []
    for message in messages:
      if message.content_type == 'text/plain':
        message.body = strip_tags(message.body).strip().\
                                                replace(' ', ' ').\
                                                replace('\n\n', '</p><p>').\
                                                replace('\n', '<br>')
        message.body = '<p>' + message.body + '</p>'
      formatted_messages.append(message)
    
    if len(formatted_messages) == 0:
      render(self, 'info.html', error_message = "No messages in this thread.")
      return

    user = users.get_current_user()
    already_seen = None
    if user:
      # If user is logged in, mark these messages as viewed.
      max_viewed = max(msg.date for msg in formatted_messages)
      userthread = gql_limit1(UserThread, thread_id = thread_id, user = user)
      if not userthread:
        userthread = UserThread(user = user, thread_id = thread_id,
                                last_viewed = max_viewed)
      else:
        already_seen = userthread.last_viewed
        for message in formatted_messages:
          if message.date <= already_seen:
            message.read = True
      userthread.last_viewed = max_viewed
      userthread.put()


    render(self, 'thread.html', thread=thread, already_seen=already_seen,
           messages=formatted_messages)
 def lookup():
   if len(lookup_pool) == 0:
     return
   for ut in UserThread.all().filter('thread_id IN', lookup_pool):
     last_viewed[ut.thread_id] = ut.last_viewed
   del lookup_pool[:]